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 | |
| 64 | /* block all signals */ |
| 65 | sigfillset (&newmask); |
| 66 | if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0) |
| 67 | { |
| 68 | zlog_err ("quagga_signal_timer: couldnt block signals!"); |
| 69 | sigm->t = thread_add_timer (sigm->tm, quagga_signal_timer, |
| 70 | &sigmaster, QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | for (i = 0; i < sigm->sigc; i++) |
| 75 | { |
| 76 | sig = &(sigm->signals[i]); |
| 77 | if (sig->caught > 0) |
| 78 | { |
| 79 | sig->caught = 0; |
| 80 | sig->handler(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | sigm->t = thread_add_timer (sigm->tm, quagga_signal_timer, &sigmaster, |
| 85 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 86 | |
| 87 | if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 ); |
| 88 | return -1; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* Initialization of signal handles. */ |
| 94 | /* Signale wrapper. */ |
| 95 | int |
| 96 | signal_set (int signo) |
| 97 | { |
| 98 | int ret; |
| 99 | struct sigaction sig; |
| 100 | struct sigaction osig; |
| 101 | |
| 102 | sig.sa_handler = &quagga_signal_handler; |
| 103 | sigfillset (&sig.sa_mask); |
| 104 | sig.sa_flags = 0; |
| 105 | if (signo == SIGALRM) { |
| 106 | #ifdef SA_INTERRUPT |
| 107 | sig.sa_flags |= SA_INTERRUPT; /* SunOS */ |
| 108 | #endif |
| 109 | } else { |
| 110 | #ifdef SA_RESTART |
| 111 | sig.sa_flags |= SA_RESTART; |
| 112 | #endif /* SA_RESTART */ |
| 113 | } |
| 114 | |
| 115 | ret = sigaction (signo, &sig, &osig); |
| 116 | if (ret < 0) |
| 117 | return ret; |
| 118 | else |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | signal_init (struct thread_master *m, |
| 124 | int sigc, struct quagga_signal_t signals[]) |
| 125 | { |
| 126 | |
| 127 | int i = 0; |
| 128 | struct quagga_signal_t *sig; |
| 129 | |
| 130 | while (i < sigc) |
| 131 | { |
| 132 | sig = &signals[i]; |
| 133 | if ( signal_set (sig->signal) < 0 ) |
| 134 | exit (-1); |
| 135 | i++; |
| 136 | } |
| 137 | |
| 138 | sigmaster.sigc = sigc; |
| 139 | sigmaster.signals = signals; |
| 140 | sigmaster.tm = m; |
| 141 | |
| 142 | sigmaster.t = |
| 143 | thread_add_timer (m, quagga_signal_timer, &sigmaster, |
| 144 | QUAGGA_SIGNAL_TIMER_INTERVAL); |
| 145 | |
| 146 | } |
| 147 | |