blob: 9c25d7649b3d8b6c19673e1426e4543e5de2e754 [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>
25
ajs31364272005-01-18 22:18:59 +000026#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +000027#ifdef HAVE_UCONTEXT_H
28#ifdef GNU_LINUX
29/* get REG_EIP from ucontext.h */
30#define __USE_GNU
31#endif /* GNU_LINUX */
32#include <ucontext.h>
33#endif /* HAVE_UCONTEXT_H */
ajs31364272005-01-18 22:18:59 +000034#endif /* SA_SIGINFO */
ajs40abf232005-01-12 17:27:27 +000035
36
paul05c447d2004-07-22 19:14:27 +000037/* master signals descriptor struct */
paulc49b3062004-01-19 21:23:37 +000038struct quagga_sigevent_master_t
39{
paulc49b3062004-01-19 21:23:37 +000040 struct thread *t;
41
paul05c447d2004-07-22 19:14:27 +000042 struct quagga_signal_t *signals;
paulc49b3062004-01-19 21:23:37 +000043 int sigc;
paul05c447d2004-07-22 19:14:27 +000044
45 volatile sig_atomic_t caught;
46} sigmaster;
paulc49b3062004-01-19 21:23:37 +000047
48/* Generic signal handler
49 * Schedules signal event thread
50 */
ajs59a06a92004-11-23 18:19:14 +000051static void
paulc49b3062004-01-19 21:23:37 +000052quagga_signal_handler (int signo)
53{
54 int i;
55 struct quagga_signal_t *sig;
56
57 for (i = 0; i < sigmaster.sigc; i++)
58 {
59 sig = &(sigmaster.signals[i]);
60
61 if (sig->signal == signo)
paul05c447d2004-07-22 19:14:27 +000062 sig->caught = 1;
paulc49b3062004-01-19 21:23:37 +000063 }
paul05c447d2004-07-22 19:14:27 +000064
65 sigmaster.caught = 1;
paulc49b3062004-01-19 21:23:37 +000066}
67
paul05c447d2004-07-22 19:14:27 +000068/* check if signals have been caught and run appropriate handlers */
paulc49b3062004-01-19 21:23:37 +000069int
paul05c447d2004-07-22 19:14:27 +000070quagga_sigevent_process (void)
paulc49b3062004-01-19 21:23:37 +000071{
paulc49b3062004-01-19 21:23:37 +000072 struct quagga_signal_t *sig;
73 int i;
paul05c447d2004-07-22 19:14:27 +000074#ifdef SIGEVENT_BLOCK_SIGNALS
75 /* shouldnt need to block signals, but potentially may be needed */
76 sigset_t newmask, oldmask;
paulc49b3062004-01-19 21:23:37 +000077
gdtb7797132004-07-13 13:47:25 +000078 /*
79 * Block most signals, but be careful not to defer SIGTRAP because
80 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
81 * block SIGKILL, just because we shouldn't be able to do so.
82 */
paulc49b3062004-01-19 21:23:37 +000083 sigfillset (&newmask);
gdtb7797132004-07-13 13:47:25 +000084 sigdelset (&newmask, SIGTRAP);
85 sigdelset (&newmask, SIGKILL);
paul05c447d2004-07-22 19:14:27 +000086
paulc49b3062004-01-19 21:23:37 +000087 if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0)
88 {
89 zlog_err ("quagga_signal_timer: couldnt block signals!");
paulc49b3062004-01-19 21:23:37 +000090 return -1;
91 }
paul05c447d2004-07-22 19:14:27 +000092#endif /* SIGEVENT_BLOCK_SIGNALS */
93
94 if (sigmaster.caught > 0)
paulc49b3062004-01-19 21:23:37 +000095 {
paul05c447d2004-07-22 19:14:27 +000096 sigmaster.caught = 0;
97 /* must not read or set sigmaster.caught after here,
98 * race condition with per-sig caught flags if one does
99 */
100
101 for (i = 0; i < sigmaster.sigc; i++)
paulc49b3062004-01-19 21:23:37 +0000102 {
paul05c447d2004-07-22 19:14:27 +0000103 sig = &(sigmaster.signals[i]);
104
105 if (sig->caught > 0)
106 {
107 sig->caught = 0;
108 sig->handler ();
109 }
paulc49b3062004-01-19 21:23:37 +0000110 }
111 }
paulc49b3062004-01-19 21:23:37 +0000112
paul05c447d2004-07-22 19:14:27 +0000113#ifdef SIGEVENT_BLOCK_SIGNALS
paulc49b3062004-01-19 21:23:37 +0000114 if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 );
115 return -1;
paul05c447d2004-07-22 19:14:27 +0000116#endif /* SIGEVENT_BLOCK_SIGNALS */
117
paulc49b3062004-01-19 21:23:37 +0000118 return 0;
119}
120
paul05c447d2004-07-22 19:14:27 +0000121#ifdef SIGEVENT_SCHEDULE_THREAD
122/* timer thread to check signals. Shouldnt be needed */
123int
124quagga_signal_timer (struct thread *t)
125{
126 struct quagga_sigevent_master_t *sigm;
127 struct quagga_signal_t *sig;
128 int i;
129
130 sigm = THREAD_ARG (t);
131 sigm->t = thread_add_timer (sigm->t->master, quagga_signal_timer, &sigmaster,
132 QUAGGA_SIGNAL_TIMER_INTERVAL);
133 return quagga_sigevent_process ();
134}
135#endif /* SIGEVENT_SCHEDULE_THREAD */
136
paulc49b3062004-01-19 21:23:37 +0000137/* Initialization of signal handles. */
ajs40abf232005-01-12 17:27:27 +0000138/* Signal wrapper. */
ajs59a06a92004-11-23 18:19:14 +0000139static int
paulc49b3062004-01-19 21:23:37 +0000140signal_set (int signo)
141{
142 int ret;
143 struct sigaction sig;
144 struct sigaction osig;
145
146 sig.sa_handler = &quagga_signal_handler;
147 sigfillset (&sig.sa_mask);
148 sig.sa_flags = 0;
149 if (signo == SIGALRM) {
150#ifdef SA_INTERRUPT
151 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
152#endif
153 } else {
154#ifdef SA_RESTART
155 sig.sa_flags |= SA_RESTART;
156#endif /* SA_RESTART */
157 }
158
159 ret = sigaction (signo, &sig, &osig);
160 if (ret < 0)
161 return ret;
162 else
163 return 0;
164}
165
ajs31364272005-01-18 22:18:59 +0000166#ifdef SA_SIGINFO
167
ajs40abf232005-01-12 17:27:27 +0000168/* XXX This function should be enhanced to support more platforms
169 (it currently works only on Linux/x86). */
170static void *
171program_counter(void *context)
ajs59a06a92004-11-23 18:19:14 +0000172{
ajs40abf232005-01-12 17:27:27 +0000173#ifdef HAVE_UCONTEXT_H
174#ifdef GNU_LINUX
175#ifdef REG_EIP
176 if (context)
177 return (void *)(((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP]);
178#endif /* REG_EIP */
179#endif /* GNU_LINUX */
180#endif /* HAVE_UCONTEXT_H */
181 return NULL;
182}
183
ajs31364272005-01-18 22:18:59 +0000184#endif /* SA_SIGINFO */
185
ajs40abf232005-01-12 17:27:27 +0000186static void
ajs31364272005-01-18 22:18:59 +0000187exit_handler(int signo
188#ifdef SA_SIGINFO
189 , siginfo_t *siginfo, void *context
190#endif
191 )
ajs40abf232005-01-12 17:27:27 +0000192{
ajs31364272005-01-18 22:18:59 +0000193 zlog_signal(signo, "exiting..."
194#ifdef SA_SIGINFO
195 , siginfo, program_counter(context)
196#endif
197 );
ajs59a06a92004-11-23 18:19:14 +0000198 _exit(128+signo);
199}
200
201static void
ajs31364272005-01-18 22:18:59 +0000202core_handler(int signo
203#ifdef SA_SIGINFO
204 , siginfo_t *siginfo, void *context
205#endif
206 )
ajs59a06a92004-11-23 18:19:14 +0000207{
ajs31364272005-01-18 22:18:59 +0000208 zlog_signal(signo, "aborting..."
209#ifdef SA_SIGINFO
210 , siginfo, program_counter(context)
211#endif
212 );
ajs59a06a92004-11-23 18:19:14 +0000213 abort();
214}
215
216static void
217trap_default_signals(void)
218{
219 static const int core_signals[] = {
220 SIGQUIT,
221 SIGILL,
222#ifdef SIGEMT
223 SIGEMT,
224#endif
225 SIGFPE,
226 SIGBUS,
227 SIGSEGV,
228#ifdef SIGSYS
229 SIGSYS,
230#endif
231#ifdef SIGXCPU
232 SIGXCPU,
233#endif
234#ifdef SIGXFSZ
235 SIGXFSZ,
236#endif
237 };
238 static const int exit_signals[] = {
239 SIGHUP,
240 SIGINT,
ajs59a06a92004-11-23 18:19:14 +0000241 SIGALRM,
242 SIGTERM,
243 SIGUSR1,
244 SIGUSR2,
245#ifdef SIGPOLL
246 SIGPOLL,
247#endif
248#ifdef SIGVTALRM
249 SIGVTALRM,
250#endif
251#ifdef SIGSTKFLT
252 SIGSTKFLT,
253#endif
254 };
ajs81fc57c2004-12-15 17:41:14 +0000255 static const int ignore_signals[] = {
256 SIGPIPE,
257 };
ajs59a06a92004-11-23 18:19:14 +0000258 static const struct {
259 const int *sigs;
ajs81fc57c2004-12-15 17:41:14 +0000260 u_int nsigs;
ajs31364272005-01-18 22:18:59 +0000261 void (*handler)(int signo
262#ifdef SA_SIGINFO
263 , siginfo_t *info, void *context
264#endif
265 );
ajs81fc57c2004-12-15 17:41:14 +0000266 } sigmap[] = {
ajs40abf232005-01-12 17:27:27 +0000267 { core_signals, sizeof(core_signals)/sizeof(core_signals[0]), core_handler},
268 { exit_signals, sizeof(exit_signals)/sizeof(exit_signals[0]), exit_handler},
269 { ignore_signals, sizeof(ignore_signals)/sizeof(ignore_signals[0]), NULL},
ajs59a06a92004-11-23 18:19:14 +0000270 };
ajs81fc57c2004-12-15 17:41:14 +0000271 u_int i;
ajs59a06a92004-11-23 18:19:14 +0000272
ajs81fc57c2004-12-15 17:41:14 +0000273 for (i = 0; i < sizeof(sigmap)/sizeof(sigmap[0]); i++)
ajs59a06a92004-11-23 18:19:14 +0000274 {
ajs81fc57c2004-12-15 17:41:14 +0000275 u_int j;
ajs59a06a92004-11-23 18:19:14 +0000276
277 for (j = 0; j < sigmap[i].nsigs; j++)
278 {
279 struct sigaction oact;
280 if ((sigaction(sigmap[i].sigs[j],NULL,&oact) == 0) &&
281 (oact.sa_handler == SIG_DFL))
282 {
283 struct sigaction act;
ajs59a06a92004-11-23 18:19:14 +0000284 sigfillset (&act.sa_mask);
ajs40abf232005-01-12 17:27:27 +0000285 if (sigmap[i].handler == NULL)
286 {
287 act.sa_handler = SIG_IGN;
288 act.sa_flags = 0;
289 }
290 else
291 {
ajs31364272005-01-18 22:18:59 +0000292#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000293 /* Request extra arguments to signal handler. */
294 act.sa_sigaction = sigmap[i].handler;
295 act.sa_flags = SA_SIGINFO;
ajs31364272005-01-18 22:18:59 +0000296#else
297 act.sa_handler = sigmap[i].handler;
298 act.sa_flags = 0;
299#endif
ajs40abf232005-01-12 17:27:27 +0000300 }
ajs59a06a92004-11-23 18:19:14 +0000301 if (sigaction(sigmap[i].sigs[j],&act,NULL) < 0)
302 zlog_warn("Unable to set signal handler for signal %d: %s",
303 sigmap[i].sigs[j],safe_strerror(errno));
304
305 }
306 }
307 }
308}
309
paulc49b3062004-01-19 21:23:37 +0000310void
paul05c447d2004-07-22 19:14:27 +0000311signal_init (struct thread_master *m, int sigc,
312 struct quagga_signal_t signals[])
paulc49b3062004-01-19 21:23:37 +0000313{
314
315 int i = 0;
316 struct quagga_signal_t *sig;
ajs59a06a92004-11-23 18:19:14 +0000317
318 /* First establish some default handlers that can be overridden by
319 the application. */
320 trap_default_signals();
paulc49b3062004-01-19 21:23:37 +0000321
322 while (i < sigc)
323 {
324 sig = &signals[i];
325 if ( signal_set (sig->signal) < 0 )
326 exit (-1);
327 i++;
328 }
329
330 sigmaster.sigc = sigc;
331 sigmaster.signals = signals;
paul05c447d2004-07-22 19:14:27 +0000332
333#ifdef SIGEVENT_SCHEDULE_THREAD
paulc49b3062004-01-19 21:23:37 +0000334 sigmaster.t =
335 thread_add_timer (m, quagga_signal_timer, &sigmaster,
336 QUAGGA_SIGNAL_TIMER_INTERVAL);
paul05c447d2004-07-22 19:14:27 +0000337#endif /* SIGEVENT_SCHEDULE_THREAD */
paulc49b3062004-01-19 21:23:37 +0000338}