1 /* Quagga signal handling functions.
2 * Copyright (C) 2004 Paul Jakma,
4 * This file is part of Quagga.
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
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.
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
27 #ifdef HAVE_UCONTEXT_H
29 /* get REG_EIP from ucontext.h */
32 #endif /* __USE_GNU */
33 #endif /* GNU_LINUX */
35 #endif /* HAVE_UCONTEXT_H */
36 #endif /* SA_SIGINFO */
39 /* master signals descriptor struct */
40 struct quagga_sigevent_master_t
44 struct quagga_signal_t
*signals
;
47 volatile sig_atomic_t caught
;
50 /* Generic signal handler
51 * Schedules signal event thread
54 quagga_signal_handler (int signo
)
57 struct quagga_signal_t
*sig
;
59 for (i
= 0; i
< sigmaster
.sigc
; i
++)
61 sig
= &(sigmaster
.signals
[i
]);
63 if (sig
->signal
== signo
)
70 /* check if signals have been caught and run appropriate handlers */
72 quagga_sigevent_process (void)
74 struct quagga_signal_t
*sig
;
76 #ifdef SIGEVENT_BLOCK_SIGNALS
77 /* shouldnt need to block signals, but potentially may be needed */
78 sigset_t newmask
, oldmask
;
81 * Block most signals, but be careful not to defer SIGTRAP because
82 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
83 * block SIGKILL, just because we shouldn't be able to do so.
85 sigfillset (&newmask
);
86 sigdelset (&newmask
, SIGTRAP
);
87 sigdelset (&newmask
, SIGKILL
);
89 if ( (sigprocmask (SIG_BLOCK
, &newmask
, &oldmask
)) < 0)
91 zlog_err ("quagga_signal_timer: couldnt block signals!");
94 #endif /* SIGEVENT_BLOCK_SIGNALS */
96 if (sigmaster
.caught
> 0)
99 /* must not read or set sigmaster.caught after here,
100 * race condition with per-sig caught flags if one does
103 for (i
= 0; i
< sigmaster
.sigc
; i
++)
105 sig
= &(sigmaster
.signals
[i
]);
115 #ifdef SIGEVENT_BLOCK_SIGNALS
116 if ( sigprocmask (SIG_UNBLOCK
, &oldmask
, NULL
) < 0 );
118 #endif /* SIGEVENT_BLOCK_SIGNALS */
123 #ifdef SIGEVENT_SCHEDULE_THREAD
124 /* timer thread to check signals. Shouldnt be needed */
126 quagga_signal_timer (struct thread
*t
)
128 struct quagga_sigevent_master_t
*sigm
;
129 struct quagga_signal_t
*sig
;
132 sigm
= THREAD_ARG (t
);
133 sigm
->t
= thread_add_timer (sigm
->t
->master
, quagga_signal_timer
, &sigmaster
,
134 QUAGGA_SIGNAL_TIMER_INTERVAL
);
135 return quagga_sigevent_process ();
137 #endif /* SIGEVENT_SCHEDULE_THREAD */
139 /* Initialization of signal handles. */
140 /* Signal wrapper. */
142 signal_set (int signo
)
145 struct sigaction sig
;
146 struct sigaction osig
;
148 sig
.sa_handler
= &quagga_signal_handler
;
149 sigfillset (&sig
.sa_mask
);
151 if (signo
== SIGALRM
) {
153 sig
.sa_flags
|= SA_INTERRUPT
; /* SunOS */
157 sig
.sa_flags
|= SA_RESTART
;
158 #endif /* SA_RESTART */
161 ret
= sigaction (signo
, &sig
, &osig
);
170 /* XXX This function should be enhanced to support more platforms
171 (it currently works only on Linux/x86). */
173 program_counter(void *context
)
175 #ifdef HAVE_UCONTEXT_H
179 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.gregs
[REG_EIP
]);
181 #endif /* GNU_LINUX */
182 #endif /* HAVE_UCONTEXT_H */
186 #endif /* SA_SIGINFO */
188 static void __attribute__ ((noreturn
))
189 exit_handler(int signo
191 , siginfo_t
*siginfo
, void *context
195 zlog_signal(signo
, "exiting..."
197 , siginfo
, program_counter(context
)
203 static void __attribute__ ((noreturn
))
204 core_handler(int signo
206 , siginfo_t
*siginfo
, void *context
210 zlog_signal(signo
, "aborting..."
212 , siginfo
, program_counter(context
)
219 trap_default_signals(void)
221 static const int core_signals
[] = {
240 static const int exit_signals
[] = {
257 static const int ignore_signals
[] = {
260 static const struct {
263 void (*handler
)(int signo
265 , siginfo_t
*info
, void *context
269 { core_signals
, sizeof(core_signals
)/sizeof(core_signals
[0]), core_handler
},
270 { exit_signals
, sizeof(exit_signals
)/sizeof(exit_signals
[0]), exit_handler
},
271 { ignore_signals
, sizeof(ignore_signals
)/sizeof(ignore_signals
[0]), NULL
},
275 for (i
= 0; i
< sizeof(sigmap
)/sizeof(sigmap
[0]); i
++)
279 for (j
= 0; j
< sigmap
[i
].nsigs
; j
++)
281 struct sigaction oact
;
282 if ((sigaction(sigmap
[i
].sigs
[j
],NULL
,&oact
) == 0) &&
283 (oact
.sa_handler
== SIG_DFL
))
285 struct sigaction act
;
286 sigfillset (&act
.sa_mask
);
287 if (sigmap
[i
].handler
== NULL
)
289 act
.sa_handler
= SIG_IGN
;
295 /* Request extra arguments to signal handler. */
296 act
.sa_sigaction
= sigmap
[i
].handler
;
297 act
.sa_flags
= SA_SIGINFO
;
299 act
.sa_handler
= sigmap
[i
].handler
;
303 if (sigaction(sigmap
[i
].sigs
[j
],&act
,NULL
) < 0)
304 zlog_warn("Unable to set signal handler for signal %d: %s",
305 sigmap
[i
].sigs
[j
],safe_strerror(errno
));
313 signal_init (struct thread_master
*m
, int sigc
,
314 struct quagga_signal_t signals
[])
318 struct quagga_signal_t
*sig
;
320 /* First establish some default handlers that can be overridden by
322 trap_default_signals();
327 if ( signal_set (sig
->signal
) < 0 )
332 sigmaster
.sigc
= sigc
;
333 sigmaster
.signals
= signals
;
335 #ifdef SIGEVENT_SCHEDULE_THREAD
337 thread_add_timer (m
, quagga_signal_timer
, &sigmaster
,
338 QUAGGA_SIGNAL_TIMER_INTERVAL
);
339 #endif /* SIGEVENT_SCHEDULE_THREAD */