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