]> git.proxmox.com Git - mirror_frr.git/blob - lib/sigevent.c
Merge pull request #2483 from pacovn/clang_scan_bgpd_updgrp_deref
[mirror_frr.git] / lib / sigevent.c
1 /* Quagga signal handling functions.
2 * Copyright (C) 2004 Paul Jakma,
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22 #include <sigevent.h>
23 #include <log.h>
24 #include <memory.h>
25
26 #ifdef SA_SIGINFO
27 #ifdef HAVE_UCONTEXT_H
28 #ifdef GNU_LINUX
29 /* get REG_EIP from ucontext.h */
30 #ifndef __USE_GNU
31 #define __USE_GNU
32 #endif /* __USE_GNU */
33 #endif /* GNU_LINUX */
34 #include <ucontext.h>
35 #endif /* HAVE_UCONTEXT_H */
36 #endif /* SA_SIGINFO */
37
38
39 /* master signals descriptor struct */
40 struct quagga_sigevent_master_t {
41 struct thread *t;
42
43 struct quagga_signal_t *signals;
44 int sigc;
45
46 volatile sig_atomic_t caught;
47 } sigmaster;
48
49 /* Generic signal handler
50 * Schedules signal event thread
51 */
52 static void quagga_signal_handler(int signo)
53 {
54 int i;
55 struct quagga_signal_t *sig;
56
57 for (i = 0; i < sigmaster.sigc; i++) {
58 sig = &(sigmaster.signals[i]);
59
60 if (sig->signal == signo)
61 sig->caught = 1;
62 }
63
64 sigmaster.caught = 1;
65 }
66
67 /* check if signals have been caught and run appropriate handlers */
68 int quagga_sigevent_process(void)
69 {
70 struct quagga_signal_t *sig;
71 int i;
72 #ifdef SIGEVENT_BLOCK_SIGNALS
73 /* shouldnt need to block signals, but potentially may be needed */
74 sigset_t newmask, oldmask;
75
76 /*
77 * Block most signals, but be careful not to defer SIGTRAP because
78 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
79 * block SIGKILL, just because we shouldn't be able to do so.
80 */
81 sigfillset(&newmask);
82 sigdelset(&newmask, SIGTRAP);
83 sigdelset(&newmask, SIGKILL);
84
85 if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) {
86 zlog_err("quagga_signal_timer: couldnt block signals!");
87 return -1;
88 }
89 #endif /* SIGEVENT_BLOCK_SIGNALS */
90
91 if (sigmaster.caught > 0) {
92 sigmaster.caught = 0;
93 /* must not read or set sigmaster.caught after here,
94 * race condition with per-sig caught flags if one does
95 */
96
97 for (i = 0; i < sigmaster.sigc; i++) {
98 sig = &(sigmaster.signals[i]);
99
100 if (sig->caught > 0) {
101 sig->caught = 0;
102 if (sig->handler)
103 sig->handler();
104 }
105 }
106 }
107
108 #ifdef SIGEVENT_BLOCK_SIGNALS
109 if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0)
110 ;
111 return -1;
112 #endif /* SIGEVENT_BLOCK_SIGNALS */
113
114 return 0;
115 }
116
117 #ifdef SIGEVENT_SCHEDULE_THREAD
118 /* timer thread to check signals. Shouldnt be needed */
119 int quagga_signal_timer(struct thread *t)
120 {
121 struct quagga_sigevent_master_t *sigm;
122
123 sigm = THREAD_ARG(t);
124 sigm->t = NULL;
125 thread_add_timer(sigm->t->master, quagga_signal_timer, &sigmaster,
126 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigm->t);
127 return quagga_sigevent_process();
128 }
129 #endif /* SIGEVENT_SCHEDULE_THREAD */
130
131 /* Initialization of signal handles. */
132 /* Signal wrapper. */
133 static int signal_set(int signo)
134 {
135 int ret;
136 struct sigaction sig;
137 struct sigaction osig;
138
139 sig.sa_handler = &quagga_signal_handler;
140 sigfillset(&sig.sa_mask);
141 sig.sa_flags = 0;
142 if (signo == SIGALRM) {
143 #ifdef SA_INTERRUPT
144 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
145 #endif
146 } else {
147 #ifdef SA_RESTART
148 sig.sa_flags |= SA_RESTART;
149 #endif /* SA_RESTART */
150 }
151
152 ret = sigaction(signo, &sig, &osig);
153 if (ret < 0)
154 return ret;
155 else
156 return 0;
157 }
158
159 #ifdef SA_SIGINFO
160
161 /* XXX This function should be enhanced to support more platforms
162 (it currently works only on Linux/x86). */
163 static void *program_counter(void *context)
164 {
165 #ifdef HAVE_UCONTEXT_H
166 #ifdef GNU_LINUX
167 /* these are from GNU libc, rather than Linux, strictly speaking */
168 #if defined(REG_EIP)
169 # define REG_INDEX REG_EIP
170 #elif defined(REG_RIP)
171 # define REG_INDEX REG_RIP
172 #elif defined(__powerpc__)
173 # define REG_INDEX 32
174 #endif
175 #elif defined(SUNOS_5) /* !GNU_LINUX */
176 # define REG_INDEX REG_PC
177 #endif /* GNU_LINUX */
178
179 #ifdef REG_INDEX
180 #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
181 # define REGS gregs[REG_INDEX]
182 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
183 # define REGS uc_regs->gregs[REG_INDEX]
184 #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
185 #endif /* REG_INDEX */
186
187 #ifdef REGS
188 if (context)
189 return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
190 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
191 /* older Linux / struct pt_regs ? */
192 if (context)
193 return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
194 #endif /* REGS */
195
196 #endif /* HAVE_UCONTEXT_H */
197 return NULL;
198 }
199
200 #endif /* SA_SIGINFO */
201
202 static void __attribute__((noreturn))
203 exit_handler(int signo
204 #ifdef SA_SIGINFO
205 ,
206 siginfo_t *siginfo, void *context
207 #endif
208 )
209 {
210 zlog_signal(signo, "exiting..."
211 #ifdef SA_SIGINFO
212 ,
213 siginfo, program_counter(context)
214 #endif
215 );
216 _exit(128 + signo);
217 }
218
219 static void __attribute__((noreturn))
220 core_handler(int signo
221 #ifdef SA_SIGINFO
222 ,
223 siginfo_t *siginfo, void *context
224 #endif
225 )
226 {
227 /* make sure we don't hang in here. default for SIGALRM is terminate.
228 * - if we're in backtrace for more than a second, abort. */
229 struct sigaction sa_default = {.sa_handler = SIG_DFL};
230 sigaction(SIGALRM, &sa_default, NULL);
231
232 sigset_t sigset;
233 sigemptyset(&sigset);
234 sigaddset(&sigset, SIGALRM);
235 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
236
237 alarm(1);
238
239 zlog_signal(signo, "aborting..."
240 #ifdef SA_SIGINFO
241 ,
242 siginfo, program_counter(context)
243 #endif
244 );
245 /* dump memory stats on core */
246 log_memstats(stderr, "core_handler");
247 abort();
248 }
249
250 static void trap_default_signals(void)
251 {
252 static const int core_signals[] = {
253 SIGQUIT, SIGILL,
254 #ifdef SIGEMT
255 SIGEMT,
256 #endif
257 SIGFPE, SIGBUS, SIGSEGV,
258 #ifdef SIGSYS
259 SIGSYS,
260 #endif
261 #ifdef SIGXCPU
262 SIGXCPU,
263 #endif
264 #ifdef SIGXFSZ
265 SIGXFSZ,
266 #endif
267 };
268 static const int exit_signals[] = {
269 SIGHUP, SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2,
270 #ifdef SIGPOLL
271 SIGPOLL,
272 #endif
273 #ifdef SIGVTALRM
274 SIGVTALRM,
275 #endif
276 #ifdef SIGSTKFLT
277 SIGSTKFLT,
278 #endif
279 };
280 static const int ignore_signals[] = {
281 SIGPIPE,
282 };
283 static const struct {
284 const int *sigs;
285 unsigned int nsigs;
286 void (*handler)(int signo
287 #ifdef SA_SIGINFO
288 ,
289 siginfo_t *info, void *context
290 #endif
291 );
292 } sigmap[] = {
293 {core_signals, array_size(core_signals), core_handler},
294 {exit_signals, array_size(exit_signals), exit_handler},
295 {ignore_signals, array_size(ignore_signals), NULL},
296 };
297 unsigned int i;
298
299 for (i = 0; i < array_size(sigmap); i++) {
300 unsigned int j;
301
302 for (j = 0; j < sigmap[i].nsigs; j++) {
303 struct sigaction oact;
304 if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0)
305 && (oact.sa_handler == SIG_DFL)) {
306 struct sigaction act;
307 sigfillset(&act.sa_mask);
308 if (sigmap[i].handler == NULL) {
309 act.sa_handler = SIG_IGN;
310 act.sa_flags = 0;
311 } else {
312 #ifdef SA_SIGINFO
313 /* Request extra arguments to signal
314 * handler. */
315 act.sa_sigaction = sigmap[i].handler;
316 act.sa_flags = SA_SIGINFO;
317 #else
318 act.sa_handler = sigmap[i].handler;
319 act.sa_flags = 0;
320 #endif
321 #ifdef SA_RESETHAND
322 /* don't try to print backtraces
323 * recursively */
324 if (sigmap[i].handler == core_handler)
325 act.sa_flags |= SA_RESETHAND;
326 #endif
327 }
328 if (sigaction(sigmap[i].sigs[j], &act, NULL)
329 < 0)
330 zlog_warn(
331 "Unable to set signal handler for signal %d: %s",
332 sigmap[i].sigs[j],
333 safe_strerror(errno));
334 }
335 }
336 }
337 }
338
339 void signal_init(struct thread_master *m, int sigc,
340 struct quagga_signal_t signals[])
341 {
342
343 int i = 0;
344 struct quagga_signal_t *sig;
345
346 /* First establish some default handlers that can be overridden by
347 the application. */
348 trap_default_signals();
349
350 while (i < sigc) {
351 sig = &signals[i];
352 if (signal_set(sig->signal) < 0)
353 exit(-1);
354 i++;
355 }
356
357 sigmaster.sigc = sigc;
358 sigmaster.signals = signals;
359
360 #ifdef SIGEVENT_SCHEDULE_THREAD
361 sigmaster.t = NULL;
362 thread_add_timer(m, quagga_signal_timer, &sigmaster,
363 QUAGGA_SIGNAL_TIMER_INTERVAL, &sigmaster.t);
364 #endif /* SIGEVENT_SCHEDULE_THREAD */
365 }