1 |
|
|
/* $OpenBSD: signal.c,v 1.25 2015/01/06 11:42:37 bluhm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions |
9 |
|
|
* are met: |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer. |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the distribution. |
15 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
16 |
|
|
* derived from this software without specific prior written permission. |
17 |
|
|
* |
18 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
#include <sys/types.h> |
31 |
|
|
#include <sys/time.h> |
32 |
|
|
#include <sys/queue.h> |
33 |
|
|
#include <sys/socket.h> |
34 |
|
|
|
35 |
|
|
#include <signal.h> |
36 |
|
|
#include <stdio.h> |
37 |
|
|
#include <stdlib.h> |
38 |
|
|
#include <string.h> |
39 |
|
|
#include <unistd.h> |
40 |
|
|
#include <errno.h> |
41 |
|
|
#include <fcntl.h> |
42 |
|
|
#include <assert.h> |
43 |
|
|
|
44 |
|
|
#include "event.h" |
45 |
|
|
#include "event-internal.h" |
46 |
|
|
#include "evsignal.h" |
47 |
|
|
#include "log.h" |
48 |
|
|
|
49 |
|
|
struct event_base *evsignal_base = NULL; |
50 |
|
|
|
51 |
|
|
static void evsignal_handler(int sig); |
52 |
|
|
|
53 |
|
|
/* Callback for when the signal handler write a byte to our signaling socket */ |
54 |
|
|
static void |
55 |
|
|
evsignal_cb(int fd, short what, void *arg) |
56 |
|
|
{ |
57 |
|
|
static char signals[1]; |
58 |
|
|
ssize_t n; |
59 |
|
|
|
60 |
|
242 |
n = recv(fd, signals, sizeof(signals), 0); |
61 |
✗✓ |
121 |
if (n == -1) { |
62 |
|
|
if (errno != EAGAIN) |
63 |
|
|
event_err(1, "%s: read", __func__); |
64 |
|
|
} |
65 |
|
121 |
} |
66 |
|
|
|
67 |
|
|
int |
68 |
|
|
evsignal_init(struct event_base *base) |
69 |
|
|
{ |
70 |
|
|
int i; |
71 |
|
|
|
72 |
|
|
/* |
73 |
|
|
* Our signal handler is going to write to one end of the socket |
74 |
|
|
* pair to wake up our event loop. The event loop then scans for |
75 |
|
|
* signals that got delivered. |
76 |
|
|
*/ |
77 |
✗✓ |
540 |
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, |
78 |
|
810 |
0, base->sig.ev_signal_pair) == -1) { |
79 |
|
|
event_err(1, "%s: socketpair", __func__); |
80 |
|
|
return -1; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
270 |
base->sig.sh_old = NULL; |
84 |
|
270 |
base->sig.sh_old_max = 0; |
85 |
|
270 |
base->sig.evsignal_caught = 0; |
86 |
|
270 |
memset(&base->sig.evsigcaught, 0, sizeof(sig_atomic_t)*NSIG); |
87 |
|
|
/* initialize the queues for all events */ |
88 |
✓✓ |
18360 |
for (i = 0; i < NSIG; ++i) |
89 |
|
8910 |
TAILQ_INIT(&base->sig.evsigevents[i]); |
90 |
|
|
|
91 |
|
540 |
event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1], |
92 |
|
270 |
EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal); |
93 |
|
270 |
base->sig.ev_signal.ev_base = base; |
94 |
|
270 |
base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL; |
95 |
|
|
|
96 |
|
270 |
return 0; |
97 |
|
270 |
} |
98 |
|
|
|
99 |
|
|
/* Helper: set the signal handler for evsignal to handler in base, so that |
100 |
|
|
* we can restore the original handler when we clear the current one. */ |
101 |
|
|
int |
102 |
|
|
_evsignal_set_handler(struct event_base *base, |
103 |
|
|
int evsignal, void (*handler)(int)) |
104 |
|
|
{ |
105 |
|
3678 |
struct sigaction sa; |
106 |
|
1839 |
struct evsignal_info *sig = &base->sig; |
107 |
|
|
void *p; |
108 |
|
|
|
109 |
|
|
/* |
110 |
|
|
* resize saved signal handler array up to the highest signal number. |
111 |
|
|
* a dynamic array is used to keep footprint on the low side. |
112 |
|
|
*/ |
113 |
✓✓ |
1839 |
if (evsignal >= sig->sh_old_max) { |
114 |
|
1150 |
int new_max = evsignal + 1; |
115 |
|
|
event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing", |
116 |
|
|
__func__, evsignal, sig->sh_old_max)); |
117 |
|
1150 |
p = reallocarray(sig->sh_old, new_max, sizeof(*sig->sh_old)); |
118 |
✗✓ |
1150 |
if (p == NULL) { |
119 |
|
|
event_warn("realloc"); |
120 |
|
|
return (-1); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
2300 |
memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old), |
124 |
|
1150 |
0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old)); |
125 |
|
|
|
126 |
|
1150 |
sig->sh_old_max = new_max; |
127 |
|
1150 |
sig->sh_old = p; |
128 |
✓✗ |
1150 |
} |
129 |
|
|
|
130 |
|
|
/* allocate space for previous handler out of dynamic array */ |
131 |
|
1839 |
sig->sh_old[evsignal] = malloc(sizeof *sig->sh_old[evsignal]); |
132 |
✗✓ |
1839 |
if (sig->sh_old[evsignal] == NULL) { |
133 |
|
|
event_warn("malloc"); |
134 |
|
|
return (-1); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/* save previous handler and setup new handler */ |
138 |
|
1839 |
memset(&sa, 0, sizeof(sa)); |
139 |
|
1839 |
sa.sa_handler = handler; |
140 |
|
1839 |
sa.sa_flags |= SA_RESTART; |
141 |
|
1839 |
sigfillset(&sa.sa_mask); |
142 |
|
|
|
143 |
✗✓ |
1839 |
if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) { |
144 |
|
|
event_warn("sigaction"); |
145 |
|
|
free(sig->sh_old[evsignal]); |
146 |
|
|
sig->sh_old[evsignal] = NULL; |
147 |
|
|
return (-1); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
1839 |
return (0); |
151 |
|
1839 |
} |
152 |
|
|
|
153 |
|
|
int |
154 |
|
|
evsignal_add(struct event *ev) |
155 |
|
|
{ |
156 |
|
|
int evsignal; |
157 |
|
312 |
struct event_base *base = ev->ev_base; |
158 |
|
156 |
struct evsignal_info *sig = &ev->ev_base->sig; |
159 |
|
|
|
160 |
✗✓ |
156 |
if (ev->ev_events & (EV_READ|EV_WRITE)) |
161 |
|
|
event_errx(1, "%s: EV_SIGNAL incompatible use", __func__); |
162 |
|
156 |
evsignal = EVENT_SIGNAL(ev); |
163 |
|
|
assert(evsignal >= 0 && evsignal < NSIG); |
164 |
✓✓ |
156 |
if (TAILQ_EMPTY(&sig->evsigevents[evsignal])) { |
165 |
|
|
event_debug(("%s: %p: changing signal handler", __func__, ev)); |
166 |
✗✓ |
290 |
if (_evsignal_set_handler( |
167 |
|
145 |
base, evsignal, evsignal_handler) == -1) |
168 |
|
|
return (-1); |
169 |
|
|
|
170 |
|
|
/* catch signals if they happen quickly */ |
171 |
|
145 |
evsignal_base = base; |
172 |
|
|
|
173 |
✓✓ |
145 |
if (!sig->ev_signal_added) { |
174 |
✗✓ |
101 |
if (event_add(&sig->ev_signal, NULL)) |
175 |
|
|
return (-1); |
176 |
|
101 |
sig->ev_signal_added = 1; |
177 |
|
101 |
} |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/* multiple events may listen to the same signal */ |
181 |
|
156 |
TAILQ_INSERT_TAIL(&sig->evsigevents[evsignal], ev, ev_signal_next); |
182 |
|
|
|
183 |
|
156 |
return (0); |
184 |
|
156 |
} |
185 |
|
|
|
186 |
|
|
int |
187 |
|
|
_evsignal_restore_handler(struct event_base *base, int evsignal) |
188 |
|
|
{ |
189 |
|
|
int ret = 0; |
190 |
|
446 |
struct evsignal_info *sig = &base->sig; |
191 |
|
|
struct sigaction *sh; |
192 |
|
|
|
193 |
|
|
/* restore previous handler */ |
194 |
|
223 |
sh = sig->sh_old[evsignal]; |
195 |
|
223 |
sig->sh_old[evsignal] = NULL; |
196 |
✗✓ |
223 |
if (sigaction(evsignal, sh, NULL) == -1) { |
197 |
|
|
event_warn("sigaction"); |
198 |
|
|
ret = -1; |
199 |
|
|
} |
200 |
|
223 |
free(sh); |
201 |
|
|
|
202 |
|
223 |
return ret; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
int |
206 |
|
|
evsignal_del(struct event *ev) |
207 |
|
|
{ |
208 |
|
288 |
struct event_base *base = ev->ev_base; |
209 |
|
144 |
struct evsignal_info *sig = &base->sig; |
210 |
|
144 |
int evsignal = EVENT_SIGNAL(ev); |
211 |
|
|
|
212 |
|
|
assert(evsignal >= 0 && evsignal < NSIG); |
213 |
|
|
|
214 |
|
|
/* multiple events may listen to the same signal */ |
215 |
✓✓ |
432 |
TAILQ_REMOVE(&sig->evsigevents[evsignal], ev, ev_signal_next); |
216 |
|
|
|
217 |
✓✓ |
144 |
if (!TAILQ_EMPTY(&sig->evsigevents[evsignal])) |
218 |
|
11 |
return (0); |
219 |
|
|
|
220 |
|
|
event_debug(("%s: %p: restoring signal handler", __func__, ev)); |
221 |
|
|
|
222 |
|
133 |
return (_evsignal_restore_handler(ev->ev_base, EVENT_SIGNAL(ev))); |
223 |
|
144 |
} |
224 |
|
|
|
225 |
|
|
static void |
226 |
|
|
evsignal_handler(int sig) |
227 |
|
|
{ |
228 |
|
242 |
int save_errno = errno; |
229 |
|
|
|
230 |
✗✓ |
121 |
if (evsignal_base == NULL) { |
231 |
|
|
event_warn( |
232 |
|
|
"%s: received signal %d, but have no base configured", |
233 |
|
|
__func__, sig); |
234 |
|
|
return; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
121 |
evsignal_base->sig.evsigcaught[sig]++; |
238 |
|
121 |
evsignal_base->sig.evsignal_caught = 1; |
239 |
|
|
|
240 |
|
|
/* Wake up our notification mechanism */ |
241 |
|
121 |
send(evsignal_base->sig.ev_signal_pair[0], "a", 1, 0); |
242 |
|
121 |
errno = save_errno; |
243 |
|
242 |
} |
244 |
|
|
|
245 |
|
|
void |
246 |
|
|
evsignal_process(struct event_base *base) |
247 |
|
|
{ |
248 |
|
242 |
struct evsignal_info *sig = &base->sig; |
249 |
|
|
struct event *ev, *next_ev; |
250 |
|
|
sig_atomic_t ncalls; |
251 |
|
|
int i; |
252 |
|
|
|
253 |
|
121 |
base->sig.evsignal_caught = 0; |
254 |
✓✓ |
7986 |
for (i = 1; i < NSIG; ++i) { |
255 |
|
3872 |
ncalls = sig->evsigcaught[i]; |
256 |
✓✓ |
3872 |
if (ncalls == 0) |
257 |
|
|
continue; |
258 |
|
121 |
sig->evsigcaught[i] -= ncalls; |
259 |
|
|
|
260 |
✓✓ |
506 |
for (ev = TAILQ_FIRST(&sig->evsigevents[i]); |
261 |
|
253 |
ev != NULL; ev = next_ev) { |
262 |
|
132 |
next_ev = TAILQ_NEXT(ev, ev_signal_next); |
263 |
✗✓ |
132 |
if (!(ev->ev_events & EV_PERSIST)) |
264 |
|
|
event_del(ev); |
265 |
|
132 |
event_active(ev, EV_SIGNAL, ncalls); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
} |
269 |
|
121 |
} |
270 |
|
|
|
271 |
|
|
void |
272 |
|
|
evsignal_dealloc(struct event_base *base) |
273 |
|
|
{ |
274 |
|
|
int i = 0; |
275 |
✓✓ |
552 |
if (base->sig.ev_signal_added) { |
276 |
|
66 |
event_del(&base->sig.ev_signal); |
277 |
|
66 |
base->sig.ev_signal_added = 0; |
278 |
|
66 |
} |
279 |
✓✓ |
18768 |
for (i = 0; i < NSIG; ++i) { |
280 |
✓✓✓✓
|
12461 |
if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL) |
281 |
|
18 |
_evsignal_restore_handler(base, i); |
282 |
|
|
} |
283 |
|
|
|
284 |
✓✓ |
276 |
if (base->sig.ev_signal_pair[0] != -1) { |
285 |
|
180 |
close(base->sig.ev_signal_pair[0]); |
286 |
|
180 |
base->sig.ev_signal_pair[0] = -1; |
287 |
|
180 |
} |
288 |
✓✓ |
276 |
if (base->sig.ev_signal_pair[1] != -1) { |
289 |
|
180 |
close(base->sig.ev_signal_pair[1]); |
290 |
|
180 |
base->sig.ev_signal_pair[1] = -1; |
291 |
|
180 |
} |
292 |
|
276 |
base->sig.sh_old_max = 0; |
293 |
|
|
|
294 |
|
|
/* per index frees are handled in evsignal_del() */ |
295 |
✓✓ |
276 |
if (base->sig.sh_old) { |
296 |
|
120 |
free(base->sig.sh_old); |
297 |
|
120 |
base->sig.sh_old = NULL; |
298 |
|
120 |
} |
299 |
|
276 |
} |