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
28 #ifdef HAVE_UCONTEXT_H
30 /* get REG_EIP from ucontext.h */
33 #endif /* __USE_GNU */
34 #endif /* GNU_LINUX */
36 #endif /* HAVE_UCONTEXT_H */
37 #endif /* SA_SIGINFO */
40 /* master signals descriptor struct */
41 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
53 static void quagga_signal_handler(int signo
)
56 struct quagga_signal_t
*sig
;
58 for (i
= 0; i
< sigmaster
.sigc
; i
++) {
59 sig
= &(sigmaster
.signals
[i
]);
61 if (sig
->signal
== signo
)
68 /* check if signals have been caught and run appropriate handlers */
69 int quagga_sigevent_process(void)
71 struct quagga_signal_t
*sig
;
73 #ifdef SIGEVENT_BLOCK_SIGNALS
74 /* shouldnt need to block signals, but potentially may be needed */
75 sigset_t newmask
, oldmask
;
78 * Block most signals, but be careful not to defer SIGTRAP because
79 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
80 * block SIGKILL, just because we shouldn't be able to do so.
83 sigdelset(&newmask
, SIGTRAP
);
84 sigdelset(&newmask
, SIGKILL
);
86 if ((sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
)) < 0) {
87 zlog_err("quagga_signal_timer: couldnt block signals!");
90 #endif /* SIGEVENT_BLOCK_SIGNALS */
92 if (sigmaster
.caught
> 0) {
94 /* must not read or set sigmaster.caught after here,
95 * race condition with per-sig caught flags if one does
98 for (i
= 0; i
< sigmaster
.sigc
; i
++) {
99 sig
= &(sigmaster
.signals
[i
]);
101 if (sig
->caught
> 0) {
109 #ifdef SIGEVENT_BLOCK_SIGNALS
110 if (sigprocmask(SIG_UNBLOCK
, &oldmask
, NULL
) < 0)
113 #endif /* SIGEVENT_BLOCK_SIGNALS */
118 #ifdef SIGEVENT_SCHEDULE_THREAD
119 /* timer thread to check signals. Shouldnt be needed */
120 int quagga_signal_timer(struct thread
*t
)
122 struct quagga_sigevent_master_t
*sigm
;
123 struct quagga_signal_t
*sig
;
126 sigm
= THREAD_ARG(t
);
127 sigm
->t
= thread_add_timer(sigm
->t
->master
, quagga_signal_timer
,
128 &sigmaster
, QUAGGA_SIGNAL_TIMER_INTERVAL
);
129 return quagga_sigevent_process();
131 #endif /* SIGEVENT_SCHEDULE_THREAD */
133 /* Initialization of signal handles. */
134 /* Signal wrapper. */
135 static int signal_set(int signo
)
138 struct sigaction sig
;
139 struct sigaction osig
;
141 sig
.sa_handler
= &quagga_signal_handler
;
142 sigfillset(&sig
.sa_mask
);
144 if (signo
== SIGALRM
) {
146 sig
.sa_flags
|= SA_INTERRUPT
; /* SunOS */
150 sig
.sa_flags
|= SA_RESTART
;
151 #endif /* SA_RESTART */
154 ret
= sigaction(signo
, &sig
, &osig
);
163 /* XXX This function should be enhanced to support more platforms
164 (it currently works only on Linux/x86). */
165 static void *program_counter(void *context
)
167 #ifdef HAVE_UCONTEXT_H
169 /* these are from GNU libc, rather than Linux, strictly speaking */
171 # define REG_INDEX REG_EIP
172 #elif defined(REG_RIP)
173 # define REG_INDEX REG_RIP
174 #elif defined(__powerpc__)
175 # define REG_INDEX 32
177 #elif defined(SUNOS_5) /* !GNU_LINUX */
178 # define REG_INDEX REG_PC
179 #endif /* GNU_LINUX */
182 #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
183 # define REGS gregs[REG_INDEX]
184 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
185 # define REGS uc_regs->gregs[REG_INDEX]
186 #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
187 #endif /* REG_INDEX */
191 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.REGS
);
192 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
193 /* older Linux / struct pt_regs ? */
195 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.regs
->nip
);
198 #endif /* HAVE_UCONTEXT_H */
202 #endif /* SA_SIGINFO */
204 static void __attribute__((noreturn
))
205 exit_handler(int signo
208 siginfo_t
*siginfo
, void *context
212 zlog_signal(signo
, "exiting..."
215 siginfo
, program_counter(context
)
221 static void __attribute__((noreturn
))
222 core_handler(int signo
225 siginfo_t
*siginfo
, void *context
229 zlog_signal(signo
, "aborting..."
232 siginfo
, program_counter(context
)
235 /* dump memory stats on core */
236 log_memstats_stderr("core_handler");
240 static void trap_default_signals(void)
242 static const int core_signals
[] = {
247 SIGFPE
, SIGBUS
, SIGSEGV
,
258 static const int exit_signals
[] = {
259 SIGHUP
, SIGINT
, SIGALRM
, SIGTERM
, SIGUSR1
, SIGUSR2
,
270 static const int ignore_signals
[] = {
273 static const struct {
276 void (*handler
)(int signo
279 siginfo_t
*info
, void *context
283 {core_signals
, array_size(core_signals
), core_handler
},
284 {exit_signals
, array_size(exit_signals
), exit_handler
},
285 {ignore_signals
, array_size(ignore_signals
), NULL
},
289 for (i
= 0; i
< array_size(sigmap
); i
++) {
292 for (j
= 0; j
< sigmap
[i
].nsigs
; j
++) {
293 struct sigaction oact
;
294 if ((sigaction(sigmap
[i
].sigs
[j
], NULL
, &oact
) == 0)
295 && (oact
.sa_handler
== SIG_DFL
)) {
296 struct sigaction act
;
297 sigfillset(&act
.sa_mask
);
298 if (sigmap
[i
].handler
== NULL
) {
299 act
.sa_handler
= SIG_IGN
;
303 /* Request extra arguments to signal
305 act
.sa_sigaction
= sigmap
[i
].handler
;
306 act
.sa_flags
= SA_SIGINFO
;
308 act
.sa_handler
= sigmap
[i
].handler
;
312 if (sigaction(sigmap
[i
].sigs
[j
], &act
, NULL
)
315 "Unable to set signal handler for signal %d: %s",
317 safe_strerror(errno
));
323 void signal_init(struct thread_master
*m
, int sigc
,
324 struct quagga_signal_t signals
[])
328 struct quagga_signal_t
*sig
;
330 /* First establish some default handlers that can be overridden by
332 trap_default_signals();
336 if (signal_set(sig
->signal
) < 0)
341 sigmaster
.sigc
= sigc
;
342 sigmaster
.signals
= signals
;
344 #ifdef SIGEVENT_SCHEDULE_THREAD
345 sigmaster
.t
= thread_add_timer(m
, quagga_signal_timer
, &sigmaster
,
346 QUAGGA_SIGNAL_TIMER_INTERVAL
);
347 #endif /* SIGEVENT_SCHEDULE_THREAD */