blob: 7acdad290a239d827a795f94073ec745be731551 [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
ajs40abf232005-01-12 17:27:27 +000026#ifdef HAVE_UCONTEXT_H
27#ifdef GNU_LINUX
28/* get REG_EIP from ucontext.h */
29#define __USE_GNU
30#endif /* GNU_LINUX */
31#include <ucontext.h>
32#endif /* HAVE_UCONTEXT_H */
33
34
paul05c447d2004-07-22 19:14:27 +000035/* master signals descriptor struct */
paulc49b3062004-01-19 21:23:37 +000036struct quagga_sigevent_master_t
37{
paulc49b3062004-01-19 21:23:37 +000038 struct thread *t;
39
paul05c447d2004-07-22 19:14:27 +000040 struct quagga_signal_t *signals;
paulc49b3062004-01-19 21:23:37 +000041 int sigc;
paul05c447d2004-07-22 19:14:27 +000042
43 volatile sig_atomic_t caught;
44} sigmaster;
paulc49b3062004-01-19 21:23:37 +000045
46/* Generic signal handler
47 * Schedules signal event thread
48 */
ajs59a06a92004-11-23 18:19:14 +000049static void
paulc49b3062004-01-19 21:23:37 +000050quagga_signal_handler (int signo)
51{
52 int i;
53 struct quagga_signal_t *sig;
54
55 for (i = 0; i < sigmaster.sigc; i++)
56 {
57 sig = &(sigmaster.signals[i]);
58
59 if (sig->signal == signo)
paul05c447d2004-07-22 19:14:27 +000060 sig->caught = 1;
paulc49b3062004-01-19 21:23:37 +000061 }
paul05c447d2004-07-22 19:14:27 +000062
63 sigmaster.caught = 1;
paulc49b3062004-01-19 21:23:37 +000064}
65
paul05c447d2004-07-22 19:14:27 +000066/* check if signals have been caught and run appropriate handlers */
paulc49b3062004-01-19 21:23:37 +000067int
paul05c447d2004-07-22 19:14:27 +000068quagga_sigevent_process (void)
paulc49b3062004-01-19 21:23:37 +000069{
paulc49b3062004-01-19 21:23:37 +000070 struct quagga_signal_t *sig;
71 int i;
paul05c447d2004-07-22 19:14:27 +000072#ifdef SIGEVENT_BLOCK_SIGNALS
73 /* shouldnt need to block signals, but potentially may be needed */
74 sigset_t newmask, oldmask;
paulc49b3062004-01-19 21:23:37 +000075
gdtb7797132004-07-13 13:47:25 +000076 /*
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 */
paulc49b3062004-01-19 21:23:37 +000081 sigfillset (&newmask);
gdtb7797132004-07-13 13:47:25 +000082 sigdelset (&newmask, SIGTRAP);
83 sigdelset (&newmask, SIGKILL);
paul05c447d2004-07-22 19:14:27 +000084
paulc49b3062004-01-19 21:23:37 +000085 if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0)
86 {
87 zlog_err ("quagga_signal_timer: couldnt block signals!");
paulc49b3062004-01-19 21:23:37 +000088 return -1;
89 }
paul05c447d2004-07-22 19:14:27 +000090#endif /* SIGEVENT_BLOCK_SIGNALS */
91
92 if (sigmaster.caught > 0)
paulc49b3062004-01-19 21:23:37 +000093 {
paul05c447d2004-07-22 19:14:27 +000094 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++)
paulc49b3062004-01-19 21:23:37 +0000100 {
paul05c447d2004-07-22 19:14:27 +0000101 sig = &(sigmaster.signals[i]);
102
103 if (sig->caught > 0)
104 {
105 sig->caught = 0;
106 sig->handler ();
107 }
paulc49b3062004-01-19 21:23:37 +0000108 }
109 }
paulc49b3062004-01-19 21:23:37 +0000110
paul05c447d2004-07-22 19:14:27 +0000111#ifdef SIGEVENT_BLOCK_SIGNALS
paulc49b3062004-01-19 21:23:37 +0000112 if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 );
113 return -1;
paul05c447d2004-07-22 19:14:27 +0000114#endif /* SIGEVENT_BLOCK_SIGNALS */
115
paulc49b3062004-01-19 21:23:37 +0000116 return 0;
117}
118
paul05c447d2004-07-22 19:14:27 +0000119#ifdef SIGEVENT_SCHEDULE_THREAD
120/* timer thread to check signals. Shouldnt be needed */
121int
122quagga_signal_timer (struct thread *t)
123{
124 struct quagga_sigevent_master_t *sigm;
125 struct quagga_signal_t *sig;
126 int i;
127
128 sigm = THREAD_ARG (t);
129 sigm->t = thread_add_timer (sigm->t->master, quagga_signal_timer, &sigmaster,
130 QUAGGA_SIGNAL_TIMER_INTERVAL);
131 return quagga_sigevent_process ();
132}
133#endif /* SIGEVENT_SCHEDULE_THREAD */
134
paulc49b3062004-01-19 21:23:37 +0000135/* Initialization of signal handles. */
ajs40abf232005-01-12 17:27:27 +0000136/* Signal wrapper. */
ajs59a06a92004-11-23 18:19:14 +0000137static int
paulc49b3062004-01-19 21:23:37 +0000138signal_set (int signo)
139{
140 int ret;
141 struct sigaction sig;
142 struct sigaction osig;
143
144 sig.sa_handler = &quagga_signal_handler;
145 sigfillset (&sig.sa_mask);
146 sig.sa_flags = 0;
147 if (signo == SIGALRM) {
148#ifdef SA_INTERRUPT
149 sig.sa_flags |= SA_INTERRUPT; /* SunOS */
150#endif
151 } else {
152#ifdef SA_RESTART
153 sig.sa_flags |= SA_RESTART;
154#endif /* SA_RESTART */
155 }
156
157 ret = sigaction (signo, &sig, &osig);
158 if (ret < 0)
159 return ret;
160 else
161 return 0;
162}
163
ajs40abf232005-01-12 17:27:27 +0000164/* XXX This function should be enhanced to support more platforms
165 (it currently works only on Linux/x86). */
166static void *
167program_counter(void *context)
ajs59a06a92004-11-23 18:19:14 +0000168{
ajs40abf232005-01-12 17:27:27 +0000169#ifdef HAVE_UCONTEXT_H
170#ifdef GNU_LINUX
171#ifdef REG_EIP
172 if (context)
173 return (void *)(((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP]);
174#endif /* REG_EIP */
175#endif /* GNU_LINUX */
176#endif /* HAVE_UCONTEXT_H */
177 return NULL;
178}
179
180static void
181exit_handler(int signo, siginfo_t *siginfo, void *context)
182{
183 zlog_signal(signo, "exiting...", siginfo, program_counter(context));
ajs59a06a92004-11-23 18:19:14 +0000184 _exit(128+signo);
185}
186
187static void
ajs40abf232005-01-12 17:27:27 +0000188core_handler(int signo, siginfo_t *siginfo, void *context)
ajs59a06a92004-11-23 18:19:14 +0000189{
ajs40abf232005-01-12 17:27:27 +0000190 zlog_signal(signo, "aborting...", siginfo, program_counter(context));
ajs59a06a92004-11-23 18:19:14 +0000191 abort();
192}
193
194static void
195trap_default_signals(void)
196{
197 static const int core_signals[] = {
198 SIGQUIT,
199 SIGILL,
200#ifdef SIGEMT
201 SIGEMT,
202#endif
203 SIGFPE,
204 SIGBUS,
205 SIGSEGV,
206#ifdef SIGSYS
207 SIGSYS,
208#endif
209#ifdef SIGXCPU
210 SIGXCPU,
211#endif
212#ifdef SIGXFSZ
213 SIGXFSZ,
214#endif
215 };
216 static const int exit_signals[] = {
217 SIGHUP,
218 SIGINT,
ajs59a06a92004-11-23 18:19:14 +0000219 SIGALRM,
220 SIGTERM,
221 SIGUSR1,
222 SIGUSR2,
223#ifdef SIGPOLL
224 SIGPOLL,
225#endif
226#ifdef SIGVTALRM
227 SIGVTALRM,
228#endif
229#ifdef SIGSTKFLT
230 SIGSTKFLT,
231#endif
232 };
ajs81fc57c2004-12-15 17:41:14 +0000233 static const int ignore_signals[] = {
234 SIGPIPE,
235 };
ajs59a06a92004-11-23 18:19:14 +0000236 static const struct {
237 const int *sigs;
ajs81fc57c2004-12-15 17:41:14 +0000238 u_int nsigs;
ajs40abf232005-01-12 17:27:27 +0000239 void (*handler)(int signo, siginfo_t *info, void *context);
ajs81fc57c2004-12-15 17:41:14 +0000240 } sigmap[] = {
ajs40abf232005-01-12 17:27:27 +0000241 { core_signals, sizeof(core_signals)/sizeof(core_signals[0]), core_handler},
242 { exit_signals, sizeof(exit_signals)/sizeof(exit_signals[0]), exit_handler},
243 { ignore_signals, sizeof(ignore_signals)/sizeof(ignore_signals[0]), NULL},
ajs59a06a92004-11-23 18:19:14 +0000244 };
ajs81fc57c2004-12-15 17:41:14 +0000245 u_int i;
ajs59a06a92004-11-23 18:19:14 +0000246
ajs81fc57c2004-12-15 17:41:14 +0000247 for (i = 0; i < sizeof(sigmap)/sizeof(sigmap[0]); i++)
ajs59a06a92004-11-23 18:19:14 +0000248 {
ajs81fc57c2004-12-15 17:41:14 +0000249 u_int j;
ajs59a06a92004-11-23 18:19:14 +0000250
251 for (j = 0; j < sigmap[i].nsigs; j++)
252 {
253 struct sigaction oact;
254 if ((sigaction(sigmap[i].sigs[j],NULL,&oact) == 0) &&
255 (oact.sa_handler == SIG_DFL))
256 {
257 struct sigaction act;
ajs59a06a92004-11-23 18:19:14 +0000258 sigfillset (&act.sa_mask);
ajs40abf232005-01-12 17:27:27 +0000259 if (sigmap[i].handler == NULL)
260 {
261 act.sa_handler = SIG_IGN;
262 act.sa_flags = 0;
263 }
264 else
265 {
266 /* Request extra arguments to signal handler. */
267 act.sa_sigaction = sigmap[i].handler;
268 act.sa_flags = SA_SIGINFO;
269 }
ajs59a06a92004-11-23 18:19:14 +0000270 if (sigaction(sigmap[i].sigs[j],&act,NULL) < 0)
271 zlog_warn("Unable to set signal handler for signal %d: %s",
272 sigmap[i].sigs[j],safe_strerror(errno));
273
274 }
275 }
276 }
277}
278
paulc49b3062004-01-19 21:23:37 +0000279void
paul05c447d2004-07-22 19:14:27 +0000280signal_init (struct thread_master *m, int sigc,
281 struct quagga_signal_t signals[])
paulc49b3062004-01-19 21:23:37 +0000282{
283
284 int i = 0;
285 struct quagga_signal_t *sig;
ajs59a06a92004-11-23 18:19:14 +0000286
287 /* First establish some default handlers that can be overridden by
288 the application. */
289 trap_default_signals();
paulc49b3062004-01-19 21:23:37 +0000290
291 while (i < sigc)
292 {
293 sig = &signals[i];
294 if ( signal_set (sig->signal) < 0 )
295 exit (-1);
296 i++;
297 }
298
299 sigmaster.sigc = sigc;
300 sigmaster.signals = signals;
paul05c447d2004-07-22 19:14:27 +0000301
302#ifdef SIGEVENT_SCHEDULE_THREAD
paulc49b3062004-01-19 21:23:37 +0000303 sigmaster.t =
304 thread_add_timer (m, quagga_signal_timer, &sigmaster,
305 QUAGGA_SIGNAL_TIMER_INTERVAL);
paul05c447d2004-07-22 19:14:27 +0000306#endif /* SIGEVENT_SCHEDULE_THREAD */
paulc49b3062004-01-19 21:23:37 +0000307}