]>
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 | |
05c447dd | 66 | /* check if signals have been caught and run appropriate handlers */ |
d62a17ae | 67 | int quagga_sigevent_process(void) |
c49b3069 | 68 | { |
d62a17ae | 69 | struct quagga_signal_t *sig; |
70 | int i; | |
05c447dd | 71 | #ifdef SIGEVENT_BLOCK_SIGNALS |
d62a17ae | 72 | /* shouldnt need to block signals, but potentially may be needed */ |
73 | sigset_t newmask, oldmask; | |
74 | ||
75 | /* | |
76 | * Block most signals, but be careful not to defer SIGTRAP because | |
77 | * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to | |
78 | * block SIGKILL, just because we shouldn't be able to do so. | |
79 | */ | |
80 | sigfillset(&newmask); | |
81 | sigdelset(&newmask, SIGTRAP); | |
82 | sigdelset(&newmask, SIGKILL); | |
83 | ||
84 | if ((sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) { | |
450971aa | 85 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 | 86 | "quagga_signal_timer: couldnt block signals!"); |
d62a17ae | 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 | ||
40abf239 | 159 | /* XXX This function should be enhanced to support more platforms |
160 | (it currently works only on Linux/x86). */ | |
d62a17ae | 161 | static void *program_counter(void *context) |
40abf239 | 162 | { |
163 | #ifdef HAVE_UCONTEXT_H | |
164 | #ifdef GNU_LINUX | |
d62a17ae | 165 | /* these are from GNU libc, rather than Linux, strictly speaking */ |
166 | #if defined(REG_EIP) | |
bccbd141 | 167 | # define REG_INDEX REG_EIP |
d62a17ae | 168 | #elif defined(REG_RIP) |
bccbd141 | 169 | # define REG_INDEX REG_RIP |
d62a17ae | 170 | #elif defined(__powerpc__) |
bccbd141 | 171 | # define REG_INDEX 32 |
d62a17ae | 172 | #endif |
bccbd141 JT |
173 | #elif defined(SUNOS_5) /* !GNU_LINUX */ |
174 | # define REG_INDEX REG_PC | |
d62a17ae | 175 | #endif /* GNU_LINUX */ |
bccbd141 JT |
176 | |
177 | #ifdef REG_INDEX | |
d62a17ae | 178 | #ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS |
bccbd141 | 179 | # define REGS gregs[REG_INDEX] |
d62a17ae | 180 | #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_UC_REGS) |
bccbd141 | 181 | # define REGS uc_regs->gregs[REG_INDEX] |
d62a17ae | 182 | #endif /* HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS */ |
bccbd141 JT |
183 | #endif /* REG_INDEX */ |
184 | ||
185 | #ifdef REGS | |
d62a17ae | 186 | if (context) |
187 | return (void *)(((ucontext_t *)context)->uc_mcontext.REGS); | |
188 | #elif defined(HAVE_UCONTEXT_T_UC_MCONTEXT_REGS__NIP) | |
189 | /* older Linux / struct pt_regs ? */ | |
190 | if (context) | |
191 | return (void *)(((ucontext_t *)context)->uc_mcontext.regs->nip); | |
bccbd141 JT |
192 | #endif /* REGS */ |
193 | ||
40abf239 | 194 | #endif /* HAVE_UCONTEXT_H */ |
d62a17ae | 195 | return NULL; |
40abf239 | 196 | } |
197 | ||
d62a17ae | 198 | static void __attribute__((noreturn)) |
5e4f10b1 | 199 | exit_handler(int signo, siginfo_t *siginfo, void *context) |
59a06a91 | 200 | { |
c22f6d8c | 201 | void *pc = program_counter(context); |
c22f6d8c DL |
202 | |
203 | zlog_signal(signo, "exiting...", siginfo, pc); | |
d62a17ae | 204 | _exit(128 + signo); |
59a06a91 | 205 | } |
206 | ||
d62a17ae | 207 | static void __attribute__((noreturn)) |
5e4f10b1 | 208 | core_handler(int signo, siginfo_t *siginfo, void *context) |
59a06a91 | 209 | { |
c22f6d8c | 210 | void *pc = program_counter(context); |
c22f6d8c | 211 | |
d62a17ae | 212 | /* make sure we don't hang in here. default for SIGALRM is terminate. |
213 | * - if we're in backtrace for more than a second, abort. */ | |
214 | struct sigaction sa_default = {.sa_handler = SIG_DFL}; | |
215 | sigaction(SIGALRM, &sa_default, NULL); | |
3f11a103 | 216 | |
d62a17ae | 217 | sigset_t sigset; |
218 | sigemptyset(&sigset); | |
219 | sigaddset(&sigset, SIGALRM); | |
220 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); | |
3f11a103 | 221 | |
d62a17ae | 222 | alarm(1); |
3f11a103 | 223 | |
c22f6d8c DL |
224 | zlog_signal(signo, "aborting...", siginfo, pc); |
225 | ||
d62a17ae | 226 | /* dump memory stats on core */ |
9eed278b | 227 | log_memstats(stderr, "core_handler"); |
d62a17ae | 228 | abort(); |
59a06a91 | 229 | } |
230 | ||
d62a17ae | 231 | static void trap_default_signals(void) |
59a06a91 | 232 | { |
d62a17ae | 233 | static const int core_signals[] = { |
234 | SIGQUIT, SIGILL, | |
59a06a91 | 235 | #ifdef SIGEMT |
d62a17ae | 236 | SIGEMT, |
59a06a91 | 237 | #endif |
d62a17ae | 238 | SIGFPE, SIGBUS, SIGSEGV, |
59a06a91 | 239 | #ifdef SIGSYS |
d62a17ae | 240 | SIGSYS, |
59a06a91 | 241 | #endif |
242 | #ifdef SIGXCPU | |
d62a17ae | 243 | SIGXCPU, |
59a06a91 | 244 | #endif |
245 | #ifdef SIGXFSZ | |
d62a17ae | 246 | SIGXFSZ, |
59a06a91 | 247 | #endif |
d62a17ae | 248 | }; |
249 | static const int exit_signals[] = { | |
250 | SIGHUP, SIGINT, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2, | |
59a06a91 | 251 | #ifdef SIGPOLL |
d62a17ae | 252 | SIGPOLL, |
59a06a91 | 253 | #endif |
254 | #ifdef SIGVTALRM | |
d62a17ae | 255 | SIGVTALRM, |
59a06a91 | 256 | #endif |
257 | #ifdef SIGSTKFLT | |
d62a17ae | 258 | SIGSTKFLT, |
59a06a91 | 259 | #endif |
d62a17ae | 260 | }; |
261 | static const int ignore_signals[] = { | |
262 | SIGPIPE, | |
263 | }; | |
264 | static const struct { | |
265 | const int *sigs; | |
d7c0a89a | 266 | unsigned int nsigs; |
5e4f10b1 | 267 | void (*handler)(int signo, siginfo_t *info, void *context); |
d62a17ae | 268 | } sigmap[] = { |
269 | {core_signals, array_size(core_signals), core_handler}, | |
270 | {exit_signals, array_size(exit_signals), exit_handler}, | |
271 | {ignore_signals, array_size(ignore_signals), NULL}, | |
272 | }; | |
d7c0a89a | 273 | unsigned int i; |
d62a17ae | 274 | |
275 | for (i = 0; i < array_size(sigmap); i++) { | |
d7c0a89a | 276 | unsigned int j; |
d62a17ae | 277 | |
278 | for (j = 0; j < sigmap[i].nsigs; j++) { | |
279 | struct sigaction oact; | |
280 | if ((sigaction(sigmap[i].sigs[j], NULL, &oact) == 0) | |
281 | && (oact.sa_handler == SIG_DFL)) { | |
282 | struct sigaction act; | |
283 | sigfillset(&act.sa_mask); | |
284 | if (sigmap[i].handler == NULL) { | |
285 | act.sa_handler = SIG_IGN; | |
286 | act.sa_flags = 0; | |
287 | } else { | |
d62a17ae | 288 | /* Request extra arguments to signal |
289 | * handler. */ | |
290 | act.sa_sigaction = sigmap[i].handler; | |
291 | act.sa_flags = SA_SIGINFO; | |
3f11a103 | 292 | #ifdef SA_RESETHAND |
d62a17ae | 293 | /* don't try to print backtraces |
294 | * recursively */ | |
295 | if (sigmap[i].handler == core_handler) | |
296 | act.sa_flags |= SA_RESETHAND; | |
31364274 | 297 | #endif |
d62a17ae | 298 | } |
299 | if (sigaction(sigmap[i].sigs[j], &act, NULL) | |
300 | < 0) | |
ff9d9d5b | 301 | flog_err( |
450971aa | 302 | EC_LIB_SYSTEM_CALL, |
d62a17ae | 303 | "Unable to set signal handler for signal %d: %s", |
304 | sigmap[i].sigs[j], | |
305 | safe_strerror(errno)); | |
306 | } | |
307 | } | |
308 | } | |
59a06a91 | 309 | } |
310 | ||
d62a17ae | 311 | void signal_init(struct thread_master *m, int sigc, |
312 | struct quagga_signal_t signals[]) | |
c49b3069 | 313 | { |
314 | ||
d62a17ae | 315 | int i = 0; |
316 | struct quagga_signal_t *sig; | |
317 | ||
318 | /* First establish some default handlers that can be overridden by | |
319 | the application. */ | |
320 | trap_default_signals(); | |
321 | ||
322 | while (i < sigc) { | |
323 | sig = &signals[i]; | |
324 | if (signal_set(sig->signal) < 0) | |
325 | exit(-1); | |
326 | i++; | |
327 | } | |
328 | ||
329 | sigmaster.sigc = sigc; | |
330 | sigmaster.signals = signals; | |
331 | ||
332 | #ifdef SIGEVENT_SCHEDULE_THREAD | |
333 | sigmaster.t = NULL; | |
334 | thread_add_timer(m, quagga_signal_timer, &sigmaster, | |
335 | QUAGGA_SIGNAL_TIMER_INTERVAL, &sigmaster.t); | |
05c447dd | 336 | #endif /* SIGEVENT_SCHEDULE_THREAD */ |
c49b3069 | 337 | } |