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