blob: a120028d810580f5d1d73ff69326c445062dfee2 [file] [log] [blame]
paulc49b3062004-01-19 21:23:37 +00001/* 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>
Balaji.G837d16c2012-09-26 14:09:10 +053025#include <memory.h>
paulc49b3062004-01-19 21:23:37 +000026
ajs31364272005-01-18 22:18:59 +000027#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +000028#ifdef HAVE_UCONTEXT_H
29#ifdef GNU_LINUX
30/* get REG_EIP from ucontext.h */
ajs67bf16c2005-04-08 19:02:04 +000031#ifndef __USE_GNU
ajs40abf232005-01-12 17:27:27 +000032#define __USE_GNU
ajs67bf16c2005-04-08 19:02:04 +000033#endif /* __USE_GNU */
ajs40abf232005-01-12 17:27:27 +000034#endif /* GNU_LINUX */
35#include <ucontext.h>
36#endif /* HAVE_UCONTEXT_H */
ajs31364272005-01-18 22:18:59 +000037#endif /* SA_SIGINFO */
ajs40abf232005-01-12 17:27:27 +000038
39
paul05c447d2004-07-22 19:14:27 +000040/* master signals descriptor struct */
paulc49b3062004-01-19 21:23:37 +000041struct quagga_sigevent_master_t
42{
paulc49b3062004-01-19 21:23:37 +000043 struct thread *t;
44
paul05c447d2004-07-22 19:14:27 +000045 struct quagga_signal_t *signals;
paulc49b3062004-01-19 21:23:37 +000046 int sigc;
paul05c447d2004-07-22 19:14:27 +000047
48 volatile sig_atomic_t caught;
49} sigmaster;
paulc49b3062004-01-19 21:23:37 +000050
51/* Generic signal handler
52 * Schedules signal event thread
53 */
ajs59a06a92004-11-23 18:19:14 +000054static void
paulc49b3062004-01-19 21:23:37 +000055quagga_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)
paul05c447d2004-07-22 19:14:27 +000065 sig->caught = 1;
paulc49b3062004-01-19 21:23:37 +000066 }
paul05c447d2004-07-22 19:14:27 +000067
68 sigmaster.caught = 1;
paulc49b3062004-01-19 21:23:37 +000069}
70
paul05c447d2004-07-22 19:14:27 +000071/* check if signals have been caught and run appropriate handlers */
paulc49b3062004-01-19 21:23:37 +000072int
paul05c447d2004-07-22 19:14:27 +000073quagga_sigevent_process (void)
paulc49b3062004-01-19 21:23:37 +000074{
paulc49b3062004-01-19 21:23:37 +000075 struct quagga_signal_t *sig;
76 int i;
paul05c447d2004-07-22 19:14:27 +000077#ifdef SIGEVENT_BLOCK_SIGNALS
78 /* shouldnt need to block signals, but potentially may be needed */
79 sigset_t newmask, oldmask;
paulc49b3062004-01-19 21:23:37 +000080
gdtb7797132004-07-13 13:47:25 +000081 /*
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 */
paulc49b3062004-01-19 21:23:37 +000086 sigfillset (&newmask);
gdtb7797132004-07-13 13:47:25 +000087 sigdelset (&newmask, SIGTRAP);
88 sigdelset (&newmask, SIGKILL);
paul05c447d2004-07-22 19:14:27 +000089
paulc49b3062004-01-19 21:23:37 +000090 if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0)
91 {
92 zlog_err ("quagga_signal_timer: couldnt block signals!");
paulc49b3062004-01-19 21:23:37 +000093 return -1;
94 }
paul05c447d2004-07-22 19:14:27 +000095#endif /* SIGEVENT_BLOCK_SIGNALS */
96
97 if (sigmaster.caught > 0)
paulc49b3062004-01-19 21:23:37 +000098 {
paul05c447d2004-07-22 19:14:27 +000099 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++)
paulc49b3062004-01-19 21:23:37 +0000105 {
paul05c447d2004-07-22 19:14:27 +0000106 sig = &(sigmaster.signals[i]);
107
108 if (sig->caught > 0)
109 {
110 sig->caught = 0;
111 sig->handler ();
112 }
paulc49b3062004-01-19 21:23:37 +0000113 }
114 }
paulc49b3062004-01-19 21:23:37 +0000115
paul05c447d2004-07-22 19:14:27 +0000116#ifdef SIGEVENT_BLOCK_SIGNALS
paulc49b3062004-01-19 21:23:37 +0000117 if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 );
118 return -1;
paul05c447d2004-07-22 19:14:27 +0000119#endif /* SIGEVENT_BLOCK_SIGNALS */
120
paulc49b3062004-01-19 21:23:37 +0000121 return 0;
122}
123
paul05c447d2004-07-22 19:14:27 +0000124#ifdef SIGEVENT_SCHEDULE_THREAD
125/* timer thread to check signals. Shouldnt be needed */
126int
127quagga_signal_timer (struct thread *t)
128{
129 struct quagga_sigevent_master_t *sigm;
130 struct quagga_signal_t *sig;
131 int i;
132
133 sigm = THREAD_ARG (t);
134 sigm->t = thread_add_timer (sigm->t->master, quagga_signal_timer, &sigmaster,
135 QUAGGA_SIGNAL_TIMER_INTERVAL);
136 return quagga_sigevent_process ();
137}
138#endif /* SIGEVENT_SCHEDULE_THREAD */
139
paulc49b3062004-01-19 21:23:37 +0000140/* Initialization of signal handles. */
ajs40abf232005-01-12 17:27:27 +0000141/* Signal wrapper. */
ajs59a06a92004-11-23 18:19:14 +0000142static int
paulc49b3062004-01-19 21:23:37 +0000143signal_set (int signo)
144{
145 int ret;
146 struct sigaction sig;
147 struct sigaction osig;
148
149 sig.sa_handler = &quagga_signal_handler;
150 sigfillset (&sig.sa_mask);
151 sig.sa_flags = 0;
152 if (signo == SIGALRM) {
153#ifdef SA_INTERRUPT
154 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
155#endif
156 } else {
157#ifdef SA_RESTART
158 sig.sa_flags |= SA_RESTART;
159#endif /* SA_RESTART */
160 }
161
162 ret = sigaction (signo, &sig, &osig);
163 if (ret < 0)
164 return ret;
165 else
166 return 0;
167}
168
ajs31364272005-01-18 22:18:59 +0000169#ifdef SA_SIGINFO
170
ajs40abf232005-01-12 17:27:27 +0000171/* XXX This function should be enhanced to support more platforms
172 (it currently works only on Linux/x86). */
173static void *
174program_counter(void *context)
ajs59a06a92004-11-23 18:19:14 +0000175{
ajs40abf232005-01-12 17:27:27 +0000176#ifdef HAVE_UCONTEXT_H
177#ifdef GNU_LINUX
Joakim Tjernlundb166ea22009-06-25 16:40:06 +0100178 /* these are from GNU libc, rather than Linux, strictly speaking */
179# if defined(REG_EIP)
180# define REG_INDEX REG_EIP
181# elif defined(REG_RIP)
182# define REG_INDEX REG_RIP
183# elif defined(__powerpc__)
184# define REG_INDEX 32
185# endif
186#elif defined(SUNOS_5) /* !GNU_LINUX */
187# define REG_INDEX REG_PC
ajs40abf232005-01-12 17:27:27 +0000188#endif /* GNU_LINUX */
Joakim Tjernlundb166ea22009-06-25 16:40:06 +0100189
190#ifdef REG_INDEX
191# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
192# define REGS gregs[REG_INDEX]
193# elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
194# define REGS uc_regs->gregs[REG_INDEX]
195# endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
196#endif /* REG_INDEX */
197
198#ifdef REGS
199 if (context)
200 return (void *)(((ucontext_t *)context)->uc_mcontext.REGS);
201#elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
202 /* older Linux / struct pt_regs ? */
203 if (context)
204 return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip);
205#endif /* REGS */
206
ajs40abf232005-01-12 17:27:27 +0000207#endif /* HAVE_UCONTEXT_H */
208 return NULL;
209}
210
ajs31364272005-01-18 22:18:59 +0000211#endif /* SA_SIGINFO */
212
paul8cc41982005-05-06 21:25:49 +0000213static void __attribute__ ((noreturn))
ajs31364272005-01-18 22:18:59 +0000214exit_handler(int signo
215#ifdef SA_SIGINFO
216 , siginfo_t *siginfo, void *context
217#endif
218 )
ajs40abf232005-01-12 17:27:27 +0000219{
ajs31364272005-01-18 22:18:59 +0000220 zlog_signal(signo, "exiting..."
221#ifdef SA_SIGINFO
222 , siginfo, program_counter(context)
223#endif
224 );
ajs59a06a92004-11-23 18:19:14 +0000225 _exit(128+signo);
226}
227
paul8cc41982005-05-06 21:25:49 +0000228static void __attribute__ ((noreturn))
ajs31364272005-01-18 22:18:59 +0000229core_handler(int signo
230#ifdef SA_SIGINFO
231 , siginfo_t *siginfo, void *context
232#endif
233 )
ajs59a06a92004-11-23 18:19:14 +0000234{
ajs31364272005-01-18 22:18:59 +0000235 zlog_signal(signo, "aborting..."
236#ifdef SA_SIGINFO
237 , siginfo, program_counter(context)
238#endif
239 );
Lou Bergeree5fbe22016-05-17 07:10:38 -0400240 /* dump memory stats on core */
241 log_memstats_stderr ("core_handler");
ajs59a06a92004-11-23 18:19:14 +0000242 abort();
243}
244
245static void
246trap_default_signals(void)
247{
248 static const int core_signals[] = {
249 SIGQUIT,
250 SIGILL,
251#ifdef SIGEMT
252 SIGEMT,
253#endif
254 SIGFPE,
255 SIGBUS,
256 SIGSEGV,
257#ifdef SIGSYS
258 SIGSYS,
259#endif
260#ifdef SIGXCPU
261 SIGXCPU,
262#endif
263#ifdef SIGXFSZ
264 SIGXFSZ,
265#endif
266 };
267 static const int exit_signals[] = {
268 SIGHUP,
269 SIGINT,
ajs59a06a92004-11-23 18:19:14 +0000270 SIGALRM,
271 SIGTERM,
272 SIGUSR1,
273 SIGUSR2,
274#ifdef SIGPOLL
275 SIGPOLL,
276#endif
277#ifdef SIGVTALRM
278 SIGVTALRM,
279#endif
280#ifdef SIGSTKFLT
281 SIGSTKFLT,
282#endif
283 };
ajs81fc57c2004-12-15 17:41:14 +0000284 static const int ignore_signals[] = {
285 SIGPIPE,
286 };
ajs59a06a92004-11-23 18:19:14 +0000287 static const struct {
288 const int *sigs;
ajs81fc57c2004-12-15 17:41:14 +0000289 u_int nsigs;
ajs31364272005-01-18 22:18:59 +0000290 void (*handler)(int signo
291#ifdef SA_SIGINFO
292 , siginfo_t *info, void *context
293#endif
294 );
ajs81fc57c2004-12-15 17:41:14 +0000295 } sigmap[] = {
Balaji.G837d16c2012-09-26 14:09:10 +0530296 { core_signals, array_size(core_signals), core_handler},
297 { exit_signals, array_size(exit_signals), exit_handler},
298 { ignore_signals, array_size(ignore_signals), NULL},
ajs59a06a92004-11-23 18:19:14 +0000299 };
ajs81fc57c2004-12-15 17:41:14 +0000300 u_int i;
ajs59a06a92004-11-23 18:19:14 +0000301
Balaji.G837d16c2012-09-26 14:09:10 +0530302 for (i = 0; i < array_size(sigmap); i++)
ajs59a06a92004-11-23 18:19:14 +0000303 {
ajs81fc57c2004-12-15 17:41:14 +0000304 u_int j;
ajs59a06a92004-11-23 18:19:14 +0000305
306 for (j = 0; j < sigmap[i].nsigs; j++)
307 {
308 struct sigaction oact;
309 if ((sigaction(sigmap[i].sigs[j],NULL,&oact) == 0) &&
310 (oact.sa_handler == SIG_DFL))
311 {
312 struct sigaction act;
ajs59a06a92004-11-23 18:19:14 +0000313 sigfillset (&act.sa_mask);
ajs40abf232005-01-12 17:27:27 +0000314 if (sigmap[i].handler == NULL)
315 {
316 act.sa_handler = SIG_IGN;
317 act.sa_flags = 0;
318 }
319 else
320 {
ajs31364272005-01-18 22:18:59 +0000321#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000322 /* Request extra arguments to signal handler. */
323 act.sa_sigaction = sigmap[i].handler;
324 act.sa_flags = SA_SIGINFO;
ajs31364272005-01-18 22:18:59 +0000325#else
326 act.sa_handler = sigmap[i].handler;
327 act.sa_flags = 0;
328#endif
ajs40abf232005-01-12 17:27:27 +0000329 }
ajs59a06a92004-11-23 18:19:14 +0000330 if (sigaction(sigmap[i].sigs[j],&act,NULL) < 0)
331 zlog_warn("Unable to set signal handler for signal %d: %s",
332 sigmap[i].sigs[j],safe_strerror(errno));
333
334 }
335 }
336 }
337}
338
paulc49b3062004-01-19 21:23:37 +0000339void
paul05c447d2004-07-22 19:14:27 +0000340signal_init (struct thread_master *m, int sigc,
341 struct quagga_signal_t signals[])
paulc49b3062004-01-19 21:23:37 +0000342{
343
344 int i = 0;
345 struct quagga_signal_t *sig;
ajs59a06a92004-11-23 18:19:14 +0000346
347 /* First establish some default handlers that can be overridden by
348 the application. */
349 trap_default_signals();
paulc49b3062004-01-19 21:23:37 +0000350
351 while (i < sigc)
352 {
353 sig = &signals[i];
354 if ( signal_set (sig->signal) < 0 )
355 exit (-1);
356 i++;
357 }
358
359 sigmaster.sigc = sigc;
360 sigmaster.signals = signals;
paul05c447d2004-07-22 19:14:27 +0000361
362#ifdef SIGEVENT_SCHEDULE_THREAD
paulc49b3062004-01-19 21:23:37 +0000363 sigmaster.t =
364 thread_add_timer (m, quagga_signal_timer, &sigmaster,
365 QUAGGA_SIGNAL_TIMER_INTERVAL);
paul05c447d2004-07-22 19:14:27 +0000366#endif /* SIGEVENT_SCHEDULE_THREAD */
paulc49b3062004-01-19 21:23:37 +0000367}