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 | |
| 26 | struct quagga_sigevent_master_t |
| 27 | { |
| 28 | struct thread_master *tm; |
| 29 | struct thread *t; |
| 30 | |
| 31 | struct quagga_signal_t *signals; |
| 32 | int sigc; |
| 33 | |
| 34 | } sigmaster; |
| 35 | |
| 36 | /* Generic signal handler |
| 37 | * Schedules signal event thread |
| 38 | */ |
| 39 | void |
| 40 | quagga_signal_handler (int signo) |
| 41 | { |
| 42 | int i; |
| 43 | struct quagga_signal_t *sig; |
| 44 | |
| 45 | for (i = 0; i < sigmaster.sigc; i++) |
| 46 | { |
| 47 | sig = &(sigmaster.signals[i]); |
| 48 | |
| 49 | if (sig->signal == signo) |
| 50 | sig->caught++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int |
| 55 | quagga_signal_timer (struct thread *t) |
| 56 | { |
| 57 | sigset_t newmask, oldmask; |
| 58 | struct quagga_sigevent_master_t *sigm; |
| 59 | struct quagga_signal_t *sig; |
| 60 | int i; |
| 61 | |
| 62 | sigm = THREAD_ARG (t); |
| 63 | |
gdt | b779713 | 2004-07-13 13:47:25 +0000 | [diff] [blame] | 64 | /* |
| 65 | * Block most signals, but be careful not to defer SIGTRAP because |
| 66 | * doing so breaks gdb, at least on NetBSD 2.0. Avoid asking to |
| 67 | * block SIGKILL, just because we shouldn't be able to do so. |
| 68 | */ |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 69 | sigfillset (&newmask); |
gdt | b779713 | 2004-07-13 13:47:25 +0000 | [diff] [blame] | 70 | sigdelset (&newmask, SIGTRAP); |
| 71 | sigdelset (&newmask, SIGKILL); |
| 72 | |
paul | c49b306 | 2004-01-19 21:23:37 +0000 | [diff] [blame] | 73 | if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0) |
| 74 | { |
| 75 | zlog_err ("quagga_signal_timer: couldnt block signals!"); |
| 76 | sigm->t = thread_add_timer (sigm->tm, quagga_signal_timer, |
| 77 | &sigmaster, QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | for (i = 0; i < sigm->sigc; i++) |
| 82 | { |
| 83 | sig = &(sigm->signals[i]); |
| 84 | if (sig->caught > 0) |
| 85 | { |
| 86 | sig->caught = 0; |
| 87 | sig->handler(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | sigm->t = thread_add_timer (sigm->tm, quagga_signal_timer, &sigmaster, |
| 92 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 93 | |
| 94 | if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 ); |
| 95 | return -1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* Initialization of signal handles. */ |
| 101 | /* Signale wrapper. */ |
| 102 | int |
| 103 | signal_set (int signo) |
| 104 | { |
| 105 | int ret; |
| 106 | struct sigaction sig; |
| 107 | struct sigaction osig; |
| 108 | |
| 109 | sig.sa_handler = &quagga_signal_handler; |
| 110 | sigfillset (&sig.sa_mask); |
| 111 | sig.sa_flags = 0; |
| 112 | if (signo == SIGALRM) { |
| 113 | #ifdef SA_INTERRUPT |
| 114 | sig.sa_flags |= SA_INTERRUPT; /* SunOS */ |
| 115 | #endif |
| 116 | } else { |
| 117 | #ifdef SA_RESTART |
| 118 | sig.sa_flags |= SA_RESTART; |
| 119 | #endif /* SA_RESTART */ |
| 120 | } |
| 121 | |
| 122 | ret = sigaction (signo, &sig, &osig); |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | else |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | signal_init (struct thread_master *m, |
| 131 | int sigc, struct quagga_signal_t signals[]) |
| 132 | { |
| 133 | |
| 134 | int i = 0; |
| 135 | struct quagga_signal_t *sig; |
| 136 | |
| 137 | while (i < sigc) |
| 138 | { |
| 139 | sig = &signals[i]; |
| 140 | if ( signal_set (sig->signal) < 0 ) |
| 141 | exit (-1); |
| 142 | i++; |
| 143 | } |
| 144 | |
| 145 | sigmaster.sigc = sigc; |
| 146 | sigmaster.signals = signals; |
| 147 | sigmaster.tm = m; |
| 148 | |
| 149 | sigmaster.t = |
| 150 | thread_add_timer (m, quagga_signal_timer, &sigmaster, |
| 151 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 152 | |
| 153 | } |
| 154 | |