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