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