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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <lib_errors.h>
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 */
38 /* master signals descriptor struct */
39 static struct quagga_sigevent_master_t
{
42 struct quagga_signal_t
*signals
;
45 volatile sig_atomic_t caught
;
48 /* Generic signal handler
49 * Schedules signal event thread
51 static void quagga_signal_handler(int signo
)
54 struct quagga_signal_t
*sig
;
56 for (i
= 0; i
< sigmaster
.sigc
; i
++) {
57 sig
= &(sigmaster
.signals
[i
]);
59 if (sig
->signal
== signo
)
67 * Check whether any signals have been received and are pending. This is done
68 * with the application's key signals blocked. The complete set of signals
69 * is returned in 'setp', so the caller can restore them when appropriate.
70 * If there are pending signals, returns 'true', 'false' otherwise.
72 bool frr_sigevent_check(sigset_t
*setp
)
79 sigemptyset(&blocked
);
81 /* Set up mask of application's signals */
82 for (i
= 0; i
< sigmaster
.sigc
; i
++)
83 sigaddset(&blocked
, sigmaster
.signals
[i
].signal
);
85 pthread_sigmask(SIG_BLOCK
, &blocked
, setp
);
87 /* Now that the application's signals are blocked, test. */
88 ret
= (sigmaster
.caught
!= 0);
93 /* check if signals have been caught and run appropriate handlers */
94 int quagga_sigevent_process(void)
96 struct quagga_signal_t
*sig
;
98 #ifdef SIGEVENT_BLOCK_SIGNALS
99 /* shouldn't need to block signals, but potentially may be needed */
100 sigset_t newmask
, oldmask
;
103 * Block most signals, but be careful not to defer SIGTRAP because
104 * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to
105 * block SIGKILL, just because we shouldn't be able to do so.
107 sigfillset(&newmask
);
108 sigdelset(&newmask
, SIGTRAP
);
109 sigdelset(&newmask
, SIGKILL
);
111 if ((sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
)) < 0) {
112 flog_err_sys(EC_LIB_SYSTEM_CALL
,
113 "quagga_signal_timer: couldnt block signals!");
116 #endif /* SIGEVENT_BLOCK_SIGNALS */
118 if (sigmaster
.caught
> 0) {
119 sigmaster
.caught
= 0;
120 /* must not read or set sigmaster.caught after here,
121 * race condition with per-sig caught flags if one does
124 for (i
= 0; i
< sigmaster
.sigc
; i
++) {
125 sig
= &(sigmaster
.signals
[i
]);
127 if (sig
->caught
> 0) {
135 #ifdef SIGEVENT_BLOCK_SIGNALS
136 if (sigprocmask(SIG_UNBLOCK
, &oldmask
, NULL
) < 0)
139 #endif /* SIGEVENT_BLOCK_SIGNALS */
144 #ifdef SIGEVENT_SCHEDULE_THREAD
145 /* timer thread to check signals. shouldn't be needed */
146 int quagga_signal_timer(struct thread
*t
)
148 struct quagga_sigevent_master_t
*sigm
;
150 sigm
= THREAD_ARG(t
);
152 thread_add_timer(sigm
->t
->master
, quagga_signal_timer
, &sigmaster
,
153 QUAGGA_SIGNAL_TIMER_INTERVAL
, &sigm
->t
);
154 return quagga_sigevent_process();
156 #endif /* SIGEVENT_SCHEDULE_THREAD */
158 /* Initialization of signal handles. */
159 /* Signal wrapper. */
160 static int signal_set(int signo
)
163 struct sigaction sig
;
164 struct sigaction osig
;
166 sig
.sa_handler
= &quagga_signal_handler
;
167 sigfillset(&sig
.sa_mask
);
169 if (signo
== SIGALRM
) {
171 sig
.sa_flags
|= SA_INTERRUPT
; /* SunOS */
175 sig
.sa_flags
|= SA_RESTART
;
176 #endif /* SA_RESTART */
179 ret
= sigaction(signo
, &sig
, &osig
);
186 /* XXX This function should be enhanced to support more platforms
187 (it currently works only on Linux/x86). */
188 static void *program_counter(void *context
)
190 #ifdef HAVE_UCONTEXT_H
192 /* these are from GNU libc, rather than Linux, strictly speaking */
194 # define REG_INDEX REG_EIP
195 #elif defined(REG_RIP)
196 # define REG_INDEX REG_RIP
197 #elif defined(__powerpc__)
198 # define REG_INDEX 32
200 #endif /* GNU_LINUX */
203 #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS
204 # define REGS gregs[REG_INDEX]
205 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS)
206 # define REGS uc_regs->gregs[REG_INDEX]
207 #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */
208 #endif /* REG_INDEX */
212 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.REGS
);
213 #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP)
214 /* older Linux / struct pt_regs ? */
216 return (void *)(((ucontext_t
*)context
)->uc_mcontext
.regs
->nip
);
219 #endif /* HAVE_UCONTEXT_H */
223 static void __attribute__((noreturn
))
224 exit_handler(int signo
, siginfo_t
*siginfo
, void *context
)
226 void *pc
= program_counter(context
);
228 zlog_signal(signo
, "exiting...", siginfo
, pc
);
232 static void __attribute__((noreturn
))
233 core_handler(int signo
, siginfo_t
*siginfo
, void *context
)
235 void *pc
= program_counter(context
);
237 /* make sure we don't hang in here. default for SIGALRM is terminate.
238 * - if we're in backtrace for more than a second, abort. */
239 struct sigaction sa_default
= {.sa_handler
= SIG_DFL
};
241 sigaction(SIGALRM
, &sa_default
, NULL
);
242 sigaction(signo
, &sa_default
, NULL
);
246 sigemptyset(&sigset
);
247 sigaddset(&sigset
, SIGALRM
);
248 sigprocmask(SIG_UNBLOCK
, &sigset
, NULL
);
252 zlog_signal(signo
, "aborting...", siginfo
, pc
);
254 /* dump memory stats on core */
255 log_memstats(stderr
, "core_handler");
257 zlog_tls_buffer_fini();
259 /* give the kernel a chance to generate a coredump */
260 sigaddset(&sigset
, signo
);
261 sigprocmask(SIG_UNBLOCK
, &sigset
, NULL
);
264 /* only chance to end up here is if the default action for signo is
265 * something other than kill or coredump the process
270 static void trap_default_signals(void)
272 static const int core_signals
[] = {
273 SIGQUIT
, SIGILL
, SIGABRT
,
277 SIGFPE
, SIGBUS
, SIGSEGV
,
288 static const int exit_signals
[] = {
289 SIGHUP
, SIGINT
, SIGALRM
, SIGTERM
, SIGUSR1
, SIGUSR2
,
300 static const int ignore_signals
[] = {
303 static const struct {
306 void (*handler
)(int signo
, siginfo_t
*info
, void *context
);
308 {core_signals
, array_size(core_signals
), core_handler
},
309 {exit_signals
, array_size(exit_signals
), exit_handler
},
310 {ignore_signals
, array_size(ignore_signals
), NULL
},
314 for (i
= 0; i
< array_size(sigmap
); i
++) {
317 for (j
= 0; j
< sigmap
[i
].nsigs
; j
++) {
318 struct sigaction oact
;
319 if ((sigaction(sigmap
[i
].sigs
[j
], NULL
, &oact
) == 0)
320 && (oact
.sa_handler
== SIG_DFL
)) {
321 struct sigaction act
;
322 sigfillset(&act
.sa_mask
);
323 if (sigmap
[i
].handler
== NULL
) {
324 act
.sa_handler
= SIG_IGN
;
327 /* Request extra arguments to signal
329 act
.sa_sigaction
= sigmap
[i
].handler
;
330 act
.sa_flags
= SA_SIGINFO
;
332 /* don't try to print backtraces
334 if (sigmap
[i
].handler
== core_handler
)
335 act
.sa_flags
|= SA_RESETHAND
;
338 if (sigaction(sigmap
[i
].sigs
[j
], &act
, NULL
)
342 "Unable to set signal handler for signal %d: %s",
344 safe_strerror(errno
));
350 void signal_init(struct thread_master
*m
, int sigc
,
351 struct quagga_signal_t signals
[])
355 struct quagga_signal_t
*sig
;
357 /* First establish some default handlers that can be overridden by
359 trap_default_signals();
363 if (signal_set(sig
->signal
) < 0)
368 sigmaster
.sigc
= sigc
;
369 sigmaster
.signals
= signals
;
371 #ifdef SIGEVENT_SCHEDULE_THREAD
373 thread_add_timer(m
, quagga_signal_timer
, &sigmaster
,
374 QUAGGA_SIGNAL_TIMER_INTERVAL
, &sigmaster
.t
);
375 #endif /* SIGEVENT_SCHEDULE_THREAD */