]>
Commit | Line | Data |
---|---|---|
c49b3069 | 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 | ||
05c447dd | 26 | /* master signals descriptor struct */ |
c49b3069 | 27 | struct quagga_sigevent_master_t |
28 | { | |
c49b3069 | 29 | struct thread *t; |
30 | ||
05c447dd | 31 | struct quagga_signal_t *signals; |
c49b3069 | 32 | int sigc; |
05c447dd | 33 | |
34 | volatile sig_atomic_t caught; | |
35 | } sigmaster; | |
c49b3069 | 36 | |
37 | /* Generic signal handler | |
38 | * Schedules signal event thread | |
39 | */ | |
40 | void | |
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) | |
05c447dd | 51 | sig->caught = 1; |
c49b3069 | 52 | } |
05c447dd | 53 | |
54 | sigmaster.caught = 1; | |
c49b3069 | 55 | } |
56 | ||
05c447dd | 57 | /* check if signals have been caught and run appropriate handlers */ |
c49b3069 | 58 | int |
05c447dd | 59 | quagga_sigevent_process (void) |
c49b3069 | 60 | { |
c49b3069 | 61 | struct quagga_signal_t *sig; |
62 | int i; | |
05c447dd | 63 | #ifdef SIGEVENT_BLOCK_SIGNALS |
64 | /* shouldnt need to block signals, but potentially may be needed */ | |
65 | sigset_t newmask, oldmask; | |
c49b3069 | 66 | |
b7797131 | 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 | */ | |
c49b3069 | 72 | sigfillset (&newmask); |
b7797131 | 73 | sigdelset (&newmask, SIGTRAP); |
74 | sigdelset (&newmask, SIGKILL); | |
05c447dd | 75 | |
c49b3069 | 76 | if ( (sigprocmask (SIG_BLOCK, &newmask, &oldmask)) < 0) |
77 | { | |
78 | zlog_err ("quagga_signal_timer: couldnt block signals!"); | |
c49b3069 | 79 | return -1; |
80 | } | |
05c447dd | 81 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
82 | ||
83 | if (sigmaster.caught > 0) | |
c49b3069 | 84 | { |
05c447dd | 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++) | |
c49b3069 | 91 | { |
05c447dd | 92 | sig = &(sigmaster.signals[i]); |
93 | ||
94 | if (sig->caught > 0) | |
95 | { | |
96 | sig->caught = 0; | |
97 | sig->handler (); | |
98 | } | |
c49b3069 | 99 | } |
100 | } | |
c49b3069 | 101 | |
05c447dd | 102 | #ifdef SIGEVENT_BLOCK_SIGNALS |
c49b3069 | 103 | if ( sigprocmask (SIG_UNBLOCK, &oldmask, NULL) < 0 ); |
104 | return -1; | |
05c447dd | 105 | #endif /* SIGEVENT_BLOCK_SIGNALS */ |
106 | ||
c49b3069 | 107 | return 0; |
108 | } | |
109 | ||
05c447dd | 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 | ||
c49b3069 | 126 | /* Initialization of signal handles. */ |
127 | /* Signale wrapper. */ | |
128 | int | |
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 | ||
155 | void | |
05c447dd | 156 | signal_init (struct thread_master *m, int sigc, |
157 | struct quagga_signal_t signals[]) | |
c49b3069 | 158 | { |
159 | ||
160 | int i = 0; | |
161 | struct quagga_signal_t *sig; | |
162 | ||
163 | while (i < sigc) | |
164 | { | |
165 | sig = &signals[i]; | |
166 | if ( signal_set (sig->signal) < 0 ) | |
167 | exit (-1); | |
168 | i++; | |
169 | } | |
170 | ||
171 | sigmaster.sigc = sigc; | |
172 | sigmaster.signals = signals; | |
05c447dd | 173 | |
174 | #ifdef SIGEVENT_SCHEDULE_THREAD | |
c49b3069 | 175 | sigmaster.t = |
176 | thread_add_timer (m, quagga_signal_timer, &sigmaster, | |
177 | QUAGGA_SIGNAL_TIMER_INTERVAL); | |
05c447dd | 178 | #endif /* SIGEVENT_SCHEDULE_THREAD */ |
c49b3069 | 179 | } |