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