paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 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 | * |
| 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 |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | #include <sigevent.h> |
| 24 | #include <log.h> |
| 25 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 26 | #ifdef SA_SIGINFO |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 27 | #ifdef HAVE_UCONTEXT_H |
| 28 | #ifdef GNU_LINUX |
| 29 | /* get REG_EIP from ucontext.h */ |
ajs | 67bf16c | 2005-04-08 19:02:04 +0000 | [diff] [blame] | 30 | #ifndef __USE_GNU |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 31 | #define __USE_GNU |
ajs | 67bf16c | 2005-04-08 19:02:04 +0000 | [diff] [blame] | 32 | #endif /* __USE_GNU */ |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 33 | #endif /* GNU_LINUX */ |
| 34 | #include <ucontext.h> |
| 35 | #endif /* HAVE_UCONTEXT_H */ |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 36 | #endif /* SA_SIGINFO */ |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 37 | |
| 38 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 39 | /* master signals descriptor struct */ |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 40 | struct quagga_sigevent_master_t |
| 41 | { |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 42 | struct thread *t; |
| 43 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 44 | struct quagga_signal_t *signals; |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 45 | int sigc; |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 46 | |
| 47 | volatile sig_atomic_t caught; |
| 48 | } sigmaster; |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 49 | |
| 50 | /* Generic signal handler |
| 51 | * Schedules signal event thread |
| 52 | */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 53 | static void |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 54 | quagga_signal_handler (int signo) |
| 55 | { |
| 56 | int i; |
| 57 | struct quagga_signal_t *sig; |
| 58 | |
| 59 | for (i = 0; i < sigmaster.sigc; i++) |
| 60 | { |
| 61 | sig = &(sigmaster.signals[i]); |
| 62 | |
| 63 | if (sig->signal == signo) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 64 | sig->caught = 1; |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 65 | } |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 66 | |
| 67 | sigmaster.caught = 1; |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 70 | /* check if signals have been caught and run appropriate handlers */ |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 71 | int |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 72 | quagga_sigevent_process (void) |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 73 | { |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 74 | struct quagga_signal_t *sig; |
| 75 | int i; |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 76 | #ifdef SIGEVENT_BLOCK_SIGNALS |
| 77 | /* shouldnt need to block signals, but potentially may be needed */ |
| 78 | sigset_t newmask, oldmask; |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 79 | |
gdt | b779713 | 2004-07-13 13:47:25 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Block most signals, but be careful not to defer SIGTRAP because |
| 82 | * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to |
| 83 | * block SIGKILL, just because we shouldn't be able to do so. |
| 84 | */ |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 85 | sigfillset (&newmask); |
gdt | b779713 | 2004-07-13 13:47:25 +0000 | [diff] [blame] | 86 | sigdelset (&newmask, SIGTRAP); |
| 87 | sigdelset (&newmask, SIGKILL); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 88 | |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 89 | if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0) |
| 90 | { |
| 91 | zlog_err ("quagga_signal_timer: couldnt block signals!"); |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 92 | return -1; |
| 93 | } |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 94 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
| 95 | |
| 96 | if (sigmaster.caught > 0) |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 97 | { |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 98 | sigmaster.caught = 0; |
| 99 | /* must not read or set sigmaster.caught after here, |
| 100 | * race condition with per-sig caught flags if one does |
| 101 | */ |
| 102 | |
| 103 | for (i = 0; i < sigmaster.sigc; i++) |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 104 | { |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 105 | sig = &(sigmaster.signals[i]); |
| 106 | |
| 107 | if (sig->caught > 0) |
| 108 | { |
| 109 | sig->caught = 0; |
| 110 | sig->handler (); |
| 111 | } |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 114 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 115 | #ifdef SIGEVENT_BLOCK_SIGNALS |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 116 | if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 ); |
| 117 | return -1; |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 118 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
| 119 | |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 123 | #ifdef SIGEVENT_SCHEDULE_THREAD |
| 124 | /* timer thread to check signals. Shouldnt be needed */ |
| 125 | int |
| 126 | quagga_signal_timer (struct thread *t) |
| 127 | { |
| 128 | struct quagga_sigevent_master_t *sigm; |
| 129 | struct quagga_signal_t *sig; |
| 130 | int i; |
| 131 | |
| 132 | sigm = THREAD_ARG (t); |
| 133 | sigm->t = thread_add_timer (sigm->t->master, quagga_signal_timer, &sigmaster, |
| 134 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 135 | return quagga_sigevent_process (); |
| 136 | } |
| 137 | #endif /* SIGEVENT_SCHEDULE_THREAD */ |
| 138 | |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 139 | /* Initialization of signal handles. */ |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 140 | /* Signal wrapper. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 141 | static int |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 142 | signal_set (int signo) |
| 143 | { |
| 144 | int ret; |
| 145 | struct sigaction sig; |
| 146 | struct sigaction osig; |
| 147 | |
| 148 | sig.sa_handler = &quagga_signal_handler; |
| 149 | sigfillset (&sig.sa_mask); |
| 150 | sig.sa_flags = 0; |
| 151 | if (signo == SIGALRM) { |
| 152 | #ifdef SA_INTERRUPT |
| 153 | sig.sa_flags |= SA_INTERRUPT; /* SunOS */ |
| 154 | #endif |
| 155 | } else { |
| 156 | #ifdef SA_RESTART |
| 157 | sig.sa_flags |= SA_RESTART; |
| 158 | #endif /* SA_RESTART */ |
| 159 | } |
| 160 | |
| 161 | ret = sigaction (signo, &sig, &osig); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | else |
| 165 | return 0; |
| 166 | } |
| 167 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 168 | #ifdef SA_SIGINFO |
| 169 | |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 170 | /* XXX This function should be enhanced to support more platforms |
| 171 | (it currently works only on Linux/x86). */ |
| 172 | static void * |
| 173 | program_counter(void *context) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 174 | { |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 175 | #ifdef HAVE_UCONTEXT_H |
| 176 | #ifdef GNU_LINUX |
| 177 | #ifdef REG_EIP |
| 178 | if (context) |
| 179 | return (void *)(((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP]); |
| 180 | #endif /* REG_EIP */ |
| 181 | #endif /* GNU_LINUX */ |
| 182 | #endif /* HAVE_UCONTEXT_H */ |
| 183 | return NULL; |
| 184 | } |
| 185 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 186 | #endif /* SA_SIGINFO */ |
| 187 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 188 | static void __attribute__ ((noreturn)) |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 189 | exit_handler(int signo |
| 190 | #ifdef SA_SIGINFO |
| 191 | , siginfo_t *siginfo, void *context |
| 192 | #endif |
| 193 | ) |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 194 | { |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 195 | zlog_signal(signo, "exiting..." |
| 196 | #ifdef SA_SIGINFO |
| 197 | , siginfo, program_counter(context) |
| 198 | #endif |
| 199 | ); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 200 | _exit(128+signo); |
| 201 | } |
| 202 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 203 | static void __attribute__ ((noreturn)) |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 204 | core_handler(int signo |
| 205 | #ifdef SA_SIGINFO |
| 206 | , siginfo_t *siginfo, void *context |
| 207 | #endif |
| 208 | ) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 209 | { |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 210 | zlog_signal(signo, "aborting..." |
| 211 | #ifdef SA_SIGINFO |
| 212 | , siginfo, program_counter(context) |
| 213 | #endif |
| 214 | ); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 215 | abort(); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | trap_default_signals(void) |
| 220 | { |
| 221 | static const int core_signals[] = { |
| 222 | SIGQUIT, |
| 223 | SIGILL, |
| 224 | #ifdef SIGEMT |
| 225 | SIGEMT, |
| 226 | #endif |
| 227 | SIGFPE, |
| 228 | SIGBUS, |
| 229 | SIGSEGV, |
| 230 | #ifdef SIGSYS |
| 231 | SIGSYS, |
| 232 | #endif |
| 233 | #ifdef SIGXCPU |
| 234 | SIGXCPU, |
| 235 | #endif |
| 236 | #ifdef SIGXFSZ |
| 237 | SIGXFSZ, |
| 238 | #endif |
| 239 | }; |
| 240 | static const int exit_signals[] = { |
| 241 | SIGHUP, |
| 242 | SIGINT, |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 243 | SIGALRM, |
| 244 | SIGTERM, |
| 245 | SIGUSR1, |
| 246 | SIGUSR2, |
| 247 | #ifdef SIGPOLL |
| 248 | SIGPOLL, |
| 249 | #endif |
| 250 | #ifdef SIGVTALRM |
| 251 | SIGVTALRM, |
| 252 | #endif |
| 253 | #ifdef SIGSTKFLT |
| 254 | SIGSTKFLT, |
| 255 | #endif |
| 256 | }; |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 257 | static const int ignore_signals[] = { |
| 258 | SIGPIPE, |
| 259 | }; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 260 | static const struct { |
| 261 | const int *sigs; |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 262 | u_int nsigs; |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 263 | void (*handler)(int signo |
| 264 | #ifdef SA_SIGINFO |
| 265 | , siginfo_t *info, void *context |
| 266 | #endif |
| 267 | ); |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 268 | } sigmap[] = { |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 269 | { core_signals, sizeof(core_signals)/sizeof(core_signals[0]), core_handler}, |
| 270 | { exit_signals, sizeof(exit_signals)/sizeof(exit_signals[0]), exit_handler}, |
| 271 | { ignore_signals, sizeof(ignore_signals)/sizeof(ignore_signals[0]), NULL}, |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 272 | }; |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 273 | u_int i; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 274 | |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 275 | for (i = 0; i < sizeof(sigmap)/sizeof(sigmap[0]); i++) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 276 | { |
ajs | 81fc57c | 2004-12-15 17:41:14 +0000 | [diff] [blame] | 277 | u_int j; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 278 | |
| 279 | for (j = 0; j < sigmap[i].nsigs; j++) |
| 280 | { |
| 281 | struct sigaction oact; |
| 282 | if ((sigaction(sigmap[i].sigs[j],NULL,&oact) == 0) && |
| 283 | (oact.sa_handler == SIG_DFL)) |
| 284 | { |
| 285 | struct sigaction act; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 286 | sigfillset (&act.sa_mask); |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 287 | if (sigmap[i].handler == NULL) |
| 288 | { |
| 289 | act.sa_handler = SIG_IGN; |
| 290 | act.sa_flags = 0; |
| 291 | } |
| 292 | else |
| 293 | { |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 294 | #ifdef SA_SIGINFO |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 295 | /* Request extra arguments to signal handler. */ |
| 296 | act.sa_sigaction = sigmap[i].handler; |
| 297 | act.sa_flags = SA_SIGINFO; |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 298 | #else |
| 299 | act.sa_handler = sigmap[i].handler; |
| 300 | act.sa_flags = 0; |
| 301 | #endif |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 302 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 303 | if (sigaction(sigmap[i].sigs[j],&act,NULL) < 0) |
| 304 | zlog_warn("Unable to set signal handler for signal %d: %s", |
| 305 | sigmap[i].sigs[j],safe_strerror(errno)); |
| 306 | |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 312 | void |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 313 | signal_init (struct thread_master *m, int sigc, |
| 314 | struct quagga_signal_t signals[]) |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 315 | { |
| 316 | |
| 317 | int i = 0; |
| 318 | struct quagga_signal_t *sig; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 319 | |
| 320 | /* First establish some default handlers that can be overridden by |
| 321 | the application. */ |
| 322 | trap_default_signals(); |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 323 | |
| 324 | while (i < sigc) |
| 325 | { |
| 326 | sig = &signals[i]; |
| 327 | if ( signal_set (sig->signal) < 0 ) |
| 328 | exit (-1); |
| 329 | i++; |
| 330 | } |
| 331 | |
| 332 | sigmaster.sigc = sigc; |
| 333 | sigmaster.signals = signals; |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 334 | |
| 335 | #ifdef SIGEVENT_SCHEDULE_THREAD |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 336 | sigmaster.t = |
| 337 | thread_add_timer (m, quagga_signal_timer, &sigmaster, |
| 338 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 339 | #endif /* SIGEVENT_SCHEDULE_THREAD */ |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 340 | } |