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