]>
Commit | Line | Data |
---|---|---|
c49b3069 | 1 | /* 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 | * | |
896014f4 DL |
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 | |
c49b3069 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | #include <sigevent.h> | |
23 | #include <log.h> | |
837d16cc | 24 | #include <memory.h> |
481bc15f | 25 | #include <lib_errors.h> |
c49b3069 | 26 | |
40abf239 | 27 | #ifdef HAVE_UCONTEXT_H |
28 | #ifdef GNU_LINUX | |
29 | /* get REG_EIP from ucontext.h */ | |
67bf16c0 | 30 | #ifndef __USE_GNU |
40abf239 | 31 | #define __USE_GNU |
67bf16c0 | 32 | #endif /* __USE_GNU */ |
40abf239 | 33 | #endif /* GNU_LINUX */ |
34 | #include <ucontext.h> | |
35 | #endif /* HAVE_UCONTEXT_H */ | |
36 | ||
37 | ||
05c447dd | 38 | /* master signals descriptor struct */ |
c17faa4b | 39 | static struct quagga_sigevent_master_t { |
d62a17ae | 40 | struct thread *t; |
41 | ||
42 | struct quagga_signal_t *signals; | |
43 | int sigc; | |
c49b3069 | 44 | |
d62a17ae | 45 | volatile sig_atomic_t caught; |
05c447dd | 46 | } sigmaster; |
c49b3069 | 47 | |
d62a17ae | 48 | /* Generic signal handler |
c49b3069 | 49 | * Schedules signal event thread |
50 | */ | |
d62a17ae | 51 | static void quagga_signal_handler(int signo) |
c49b3069 | 52 | { |
d62a17ae | 53 | int i; |
54 | struct quagga_signal_t *sig; | |
55 | ||
56 | for (i = 0; i < sigmaster.sigc; i++) { | |
57 | sig = &(sigmaster.signals[i]); | |
58 | ||
59 | if (sig->signal == signo) | |
60 | sig->caught = 1; | |
61 | } | |
62 | ||
63 | sigmaster.caught = 1; | |
64 | } | |
c49b3069 | 65 | |
976c5cc1 MS |
66 | /* |
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. | |
71 | */ | |
72 | bool frr_sigevent_check(sigset_t *setp) | |
73 | { | |
74 | sigset_t blocked; | |
75 | int i; | |
76 | bool ret; | |
77 | ||
78 | sigemptyset(setp); | |
79 | sigemptyset(&blocked); | |
80 | ||
81 | /* Set up mask of application's signals */ | |
82 | for (i = 0; i < sigmaster.sigc; i++) | |
83 | sigaddset(&blocked, sigmaster.signals[i].signal); | |
84 | ||
85 | pthread_sigmask(SIG_BLOCK, &blocked, setp); | |
86 | ||
87 | /* Now that the application's signals are blocked, test. */ | |
88 | ret = (sigmaster.caught != 0); | |
89 | ||
90 | return ret; | |
91 | } | |
92 | ||
05c447dd | 93 | /* check if signals have been caught and run appropriate handlers */ |
d62a17ae | 94 | int quagga_sigevent_process(void) |
c49b3069 | 95 | { |
d62a17ae | 96 | struct quagga_signal_t *sig; |
97 | int i; | |
05c447dd | 98 | #ifdef SIGEVENT_BLOCK_SIGNALS |
d62a17ae | 99 | /* shouldnt need to block signals, but potentially may be needed */ |
100 | sigset_t newmask, oldmask; | |
101 | ||
102 | /* | |
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. | |
106 | */ | |
107 | sigfillset(&newmask); | |
108 | sigdelset(&newmask, SIGTRAP); | |
109 | sigdelset(&newmask, SIGKILL); | |
110 | ||
111 | if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) { | |
450971aa | 112 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 | 113 | "quagga_signal_timer: couldnt block signals!"); |
d62a17ae | 114 | return -1; |
115 | } | |
05c447dd | 116 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
117 | ||
d62a17ae | 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 | |
122 | */ | |
123 | ||
124 | for (i = 0; i < sigmaster.sigc; i++) { | |
125 | sig = &(sigmaster.signals[i]); | |
126 | ||
127 | if (sig->caught > 0) { | |
128 | sig->caught = 0; | |
129 | if (sig->handler) | |
130 | sig->handler(); | |
131 | } | |
132 | } | |
133 | } | |
c49b3069 | 134 | |
05c447dd | 135 | #ifdef SIGEVENT_BLOCK_SIGNALS |
d62a17ae | 136 | if (sigprocmask(SIG_UNBLOCK, &oldmask, NULL) < 0) |
137 | ; | |
138 | return -1; | |
05c447dd | 139 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
140 | ||
d62a17ae | 141 | return 0; |
c49b3069 | 142 | } |
143 | ||
05c447dd | 144 | #ifdef SIGEVENT_SCHEDULE_THREAD |
145 | /* timer thread to check signals. Shouldnt be needed */ | |
d62a17ae | 146 | int quagga_signal_timer(struct thread *t) |
05c447dd | 147 | { |
d62a17ae | 148 | struct quagga_sigevent_master_t *sigm; |
d62a17ae | 149 | |
150 | sigm = THREAD_ARG(t); | |
151 | sigm->t = NULL; | |
152 | thread_add_timer(sigm->t->master, quagga_signal_timer, &sigmaster, | |
153 | QUAGGA_SIGNAL_TIMER_INTERVAL, &sigm->t); | |
154 | return quagga_sigevent_process(); | |
05c447dd | 155 | } |
156 | #endif /* SIGEVENT_SCHEDULE_THREAD */ | |
157 | ||
c49b3069 | 158 | /* Initialization of signal handles. */ |
40abf239 | 159 | /* Signal wrapper. */ |
d62a17ae | 160 | static int signal_set(int signo) |
c49b3069 | 161 | { |
d62a17ae | 162 | int ret; |
163 | struct sigaction sig; | |
164 | struct sigaction osig; | |
165 | ||
166 | sig.sa_handler = &quagga_signal_handler; | |
167 | sigfillset(&sig.sa_mask); | |
168 | sig.sa_flags = 0; | |
169 | if (signo == SIGALRM) { | |
c49b3069 | 170 | #ifdef SA_INTERRUPT |
d62a17ae | 171 | sig.sa_flags |= SA_INTERRUPT; /* SunOS */ |
c49b3069 | 172 | #endif |
d62a17ae | 173 | } else { |
c49b3069 | 174 | #ifdef SA_RESTART |
d62a17ae | 175 | sig.sa_flags |= SA_RESTART; |
c49b3069 | 176 | #endif /* SA_RESTART */ |
d62a17ae | 177 | } |
c49b3069 | 178 | |
d62a17ae | 179 | ret = sigaction(signo, &sig, &osig); |
180 | if (ret < 0) | |
181 | return ret; | |
182 | else | |
183 | return 0; | |
c49b3069 | 184 | } |
185 | ||
40abf239 | 186 | /* XXX This function should be enhanced to support more platforms |
187 | (it currently works only on Linux/x86). */ | |
d62a17ae | 188 | static void *program_counter(void *context) |
40abf239 | 189 | { |
190 | #ifdef HAVE_UCONTEXT_H | |
191 | #ifdef GNU_LINUX | |
d62a17ae | 192 | /* these are from GNU libc, rather than Linux, strictly speaking */ |
193 | #if defined(REG_EIP) | |
bccbd141 | 194 | # define REG_INDEX REG_EIP |
d62a17ae | 195 | #elif defined(REG_RIP) |
bccbd141 | 196 | # define REG_INDEX REG_RIP |
d62a17ae | 197 | #elif defined(__powerpc__) |
bccbd141 | 198 | # define REG_INDEX 32 |
d62a17ae | 199 | #endif |
d62a17ae | 200 | #endif /* GNU_LINUX */ |
bccbd141 JT |
201 | |
202 | #ifdef REG_INDEX | |
d62a17ae | 203 | #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS |
bccbd141 | 204 | # define REGS gregs[REG_INDEX] |
d62a17ae | 205 | #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS) |
bccbd141 | 206 | # define REGS uc_regs->gregs[REG_INDEX] |
d62a17ae | 207 | #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */ |
bccbd141 JT |
208 | #endif /* REG_INDEX */ |
209 | ||
210 | #ifdef REGS | |
d62a17ae | 211 | if (context) |
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 ? */ | |
215 | if (context) | |
216 | return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip); | |
bccbd141 JT |
217 | #endif /* REGS */ |
218 | ||
40abf239 | 219 | #endif /* HAVE_UCONTEXT_H */ |
d62a17ae | 220 | return NULL; |
40abf239 | 221 | } |
222 | ||
d62a17ae | 223 | static void __attribute__((noreturn)) |
5e4f10b1 | 224 | exit_handler(int signo, siginfo_t *siginfo, void *context) |
59a06a91 | 225 | { |
c22f6d8c | 226 | void *pc = program_counter(context); |
c22f6d8c DL |
227 | |
228 | zlog_signal(signo, "exiting...", siginfo, pc); | |
d62a17ae | 229 | _exit(128 + signo); |
59a06a91 | 230 | } |
231 | ||
d62a17ae | 232 | static void __attribute__((noreturn)) |
5e4f10b1 | 233 | core_handler(int signo, siginfo_t *siginfo, void *context) |
59a06a91 | 234 | { |
c22f6d8c | 235 | void *pc = program_counter(context); |
c22f6d8c | 236 | |
d62a17ae | 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}; | |
240 | sigaction(SIGALRM, &sa_default, NULL); | |
3f11a103 | 241 | |
d62a17ae | 242 | sigset_t sigset; |
243 | sigemptyset(&sigset); | |
244 | sigaddset(&sigset, SIGALRM); | |
245 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); | |
3f11a103 | 246 | |
d62a17ae | 247 | alarm(1); |
3f11a103 | 248 | |
c22f6d8c DL |
249 | zlog_signal(signo, "aborting...", siginfo, pc); |
250 | ||
d62a17ae | 251 | /* dump memory stats on core */ |
9eed278b | 252 | log_memstats(stderr, "core_handler"); |
23961e75 DS |
253 | |
254 | zlog_tls_buffer_fini(); | |
d62a17ae | 255 | abort(); |
59a06a91 | 256 | } |
257 | ||
d62a17ae | 258 | static void trap_default_signals(void) |
59a06a91 | 259 | { |
d62a17ae | 260 | static const int core_signals[] = { |
3fb4be22 | 261 | SIGQUIT, SIGILL, SIGABRT, |
59a06a91 | 262 | #ifdef SIGEMT |
d62a17ae | 263 | SIGEMT, |
59a06a91 | 264 | #endif |
d62a17ae | 265 | SIGFPE, SIGBUS, SIGSEGV, |
59a06a91 | 266 | #ifdef SIGSYS |
d62a17ae | 267 | SIGSYS, |
59a06a91 | 268 | #endif |
269 | #ifdef SIGXCPU | |
d62a17ae | 270 | SIGXCPU, |
59a06a91 | 271 | #endif |
272 | #ifdef SIGXFSZ | |
d62a17ae | 273 | SIGXFSZ, |
59a06a91 | 274 | #endif |
d62a17ae | 275 | }; |
276 | static const int exit_signals[] = { | |
277 | SIGHUP, SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2, | |
59a06a91 | 278 | #ifdef SIGPOLL |
d62a17ae | 279 | SIGPOLL, |
59a06a91 | 280 | #endif |
281 | #ifdef SIGVTALRM | |
d62a17ae | 282 | SIGVTALRM, |
59a06a91 | 283 | #endif |
284 | #ifdef SIGSTKFLT | |
d62a17ae | 285 | SIGSTKFLT, |
59a06a91 | 286 | #endif |
d62a17ae | 287 | }; |
288 | static const int ignore_signals[] = { | |
289 | SIGPIPE, | |
290 | }; | |
291 | static const struct { | |
292 | const int *sigs; | |
d7c0a89a | 293 | unsigned int nsigs; |
5e4f10b1 | 294 | void (*handler)(int signo, siginfo_t *info, void *context); |
d62a17ae | 295 | } sigmap[] = { |
296 | {core_signals, array_size(core_signals), core_handler}, | |
297 | {exit_signals, array_size(exit_signals), exit_handler}, | |
298 | {ignore_signals, array_size(ignore_signals), NULL}, | |
299 | }; | |
d7c0a89a | 300 | unsigned int i; |
d62a17ae | 301 | |
302 | for (i = 0; i < array_size(sigmap); i++) { | |
d7c0a89a | 303 | unsigned int j; |
d62a17ae | 304 | |
305 | for (j = 0; j < sigmap[i].nsigs; j++) { | |
306 | struct sigaction oact; | |
307 | if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0) | |
308 | && (oact.sa_handler == SIG_DFL)) { | |
309 | struct sigaction act; | |
310 | sigfillset(&act.sa_mask); | |
311 | if (sigmap[i].handler == NULL) { | |
312 | act.sa_handler = SIG_IGN; | |
313 | act.sa_flags = 0; | |
314 | } else { | |
d62a17ae | 315 | /* Request extra arguments to signal |
316 | * handler. */ | |
317 | act.sa_sigaction = sigmap[i].handler; | |
318 | act.sa_flags = SA_SIGINFO; | |
3f11a103 | 319 | #ifdef SA_RESETHAND |
d62a17ae | 320 | /* don't try to print backtraces |
321 | * recursively */ | |
322 | if (sigmap[i].handler == core_handler) | |
323 | act.sa_flags |= SA_RESETHAND; | |
31364274 | 324 | #endif |
d62a17ae | 325 | } |
326 | if (sigaction(sigmap[i].sigs[j], &act, NULL) | |
327 | < 0) | |
ff9d9d5b | 328 | flog_err( |
450971aa | 329 | EC_LIB_SYSTEM_CALL, |
d62a17ae | 330 | "Unable to set signal handler for signal %d: %s", |
331 | sigmap[i].sigs[j], | |
332 | safe_strerror(errno)); | |
333 | } | |
334 | } | |
335 | } | |
59a06a91 | 336 | } |
337 | ||
d62a17ae | 338 | void signal_init(struct thread_master *m, int sigc, |
339 | struct quagga_signal_t signals[]) | |
c49b3069 | 340 | { |
341 | ||
d62a17ae | 342 | int i = 0; |
343 | struct quagga_signal_t *sig; | |
344 | ||
345 | /* First establish some default handlers that can be overridden by | |
346 | the application. */ | |
347 | trap_default_signals(); | |
348 | ||
349 | while (i < sigc) { | |
350 | sig = &signals[i]; | |
351 | if (signal_set(sig->signal) < 0) | |
352 | exit(-1); | |
353 | i++; | |
354 | } | |
355 | ||
356 | sigmaster.sigc = sigc; | |
357 | sigmaster.signals = signals; | |
358 | ||
359 | #ifdef SIGEVENT_SCHEDULE_THREAD | |
360 | sigmaster.t = NULL; | |
361 | thread_add_timer(m, quagga_signal_timer, &sigmaster, | |
362 | QUAGGA_SIGNAL_TIMER_INTERVAL, &sigmaster.t); | |
05c447dd | 363 | #endif /* SIGEVENT_SCHEDULE_THREAD */ |
c49b3069 | 364 | } |