1 : /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey 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 : #ifdef HAVE_CONFIG_H
30 : #include "config.h"
31 : #endif
32 :
33 : #ifdef WIN32
34 : #define WIN32_LEAN_AND_MEAN
35 : #include <winsock2.h>
36 : #include <windows.h>
37 : #undef WIN32_LEAN_AND_MEAN
38 : #endif
39 : #include <sys/types.h>
40 : #ifdef HAVE_SYS_TIME_H
41 : #include <sys/time.h>
42 : #endif
43 : #include <sys/queue.h>
44 : #ifdef HAVE_SYS_SOCKET_H
45 : #include <sys/socket.h>
46 : #endif
47 : #include <signal.h>
48 : #include <stdio.h>
49 : #include <stdlib.h>
50 : #include <string.h>
51 : #ifdef HAVE_UNISTD_H
52 : #include <unistd.h>
53 : #endif
54 : #include <errno.h>
55 : #ifdef HAVE_FCNTL_H
56 : #include <fcntl.h>
57 : #endif
58 : #include <assert.h>
59 :
60 : #include "event.h"
61 : #include "event-internal.h"
62 : #include "evsignal.h"
63 : #include "evutil.h"
64 : #include "log.h"
65 :
66 : struct event_base *evsignal_base = NULL;
67 :
68 : static void evsignal_handler(int sig);
69 :
70 : /* Callback for when the signal handler write a byte to our signaling socket */
71 : static void
72 0 : evsignal_cb(int fd, short what, void *arg)
73 : {
74 : static char signals[100];
75 : #ifdef WIN32
76 : SSIZE_T n;
77 : #else
78 : ssize_t n;
79 : #endif
80 :
81 0 : n = recv(fd, signals, sizeof(signals), 0);
82 0 : if (n == -1)
83 0 : event_err(1, "%s: read", __func__);
84 0 : }
85 :
86 : #ifdef HAVE_SETFD
87 : #define FD_CLOSEONEXEC(x) do { \
88 : if (fcntl(x, F_SETFD, 1) == -1) \
89 : event_warn("fcntl(%d, F_SETFD)", x); \
90 : } while (0)
91 : #else
92 : #define FD_CLOSEONEXEC(x)
93 : #endif
94 :
95 : void
96 1420 : evsignal_init(struct event_base *base)
97 : {
98 : int i;
99 :
100 : /*
101 : * Our signal handler is going to write to one end of the socket
102 : * pair to wake up our event loop. The event loop then scans for
103 : * signals that got delivered.
104 : */
105 1420 : if (evutil_socketpair(
106 : AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1)
107 0 : event_err(1, "%s: socketpair", __func__);
108 :
109 1420 : FD_CLOSEONEXEC(base->sig.ev_signal_pair[0]);
110 1420 : FD_CLOSEONEXEC(base->sig.ev_signal_pair[1]);
111 1420 : base->sig.sh_old = NULL;
112 1420 : base->sig.sh_old_max = 0;
113 1420 : base->sig.evsignal_caught = 0;
114 1420 : memset(&base->sig.evsigcaught, 0, sizeof(sig_atomic_t)*NSIG);
115 : /* initialize the queues for all events */
116 93720 : for (i = 0; i < NSIG; ++i)
117 92300 : TAILQ_INIT(&base->sig.evsigevents[i]);
118 :
119 1420 : evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
120 :
121 1420 : event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1],
122 1420 : EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal);
123 1420 : base->sig.ev_signal.ev_base = base;
124 1420 : base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
125 1420 : }
126 :
127 : /* Helper: set the signal handler for evsignal to handler in base, so that
128 : * we can restore the original handler when we clear the current one. */
129 : int
130 0 : _evsignal_set_handler(struct event_base *base,
131 : int evsignal, void (*handler)(int))
132 : {
133 : #ifdef HAVE_SIGACTION
134 : struct sigaction sa;
135 : #else
136 : ev_sighandler_t sh;
137 : #endif
138 0 : struct evsignal_info *sig = &base->sig;
139 : void *p;
140 :
141 : /*
142 : * resize saved signal handler array up to the highest signal number.
143 : * a dynamic array is used to keep footprint on the low side.
144 : */
145 0 : if (evsignal >= sig->sh_old_max) {
146 0 : int new_max = evsignal + 1;
147 : event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing",
148 : __func__, evsignal, sig->sh_old_max));
149 0 : p = realloc(sig->sh_old, new_max * sizeof(*sig->sh_old));
150 0 : if (p == NULL) {
151 0 : event_warn("realloc");
152 0 : return (-1);
153 : }
154 :
155 0 : memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old),
156 0 : 0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old));
157 :
158 0 : sig->sh_old_max = new_max;
159 0 : sig->sh_old = p;
160 : }
161 :
162 : /* allocate space for previous handler out of dynamic array */
163 0 : sig->sh_old[evsignal] = malloc(sizeof *sig->sh_old[evsignal]);
164 0 : if (sig->sh_old[evsignal] == NULL) {
165 0 : event_warn("malloc");
166 0 : return (-1);
167 : }
168 :
169 : /* save previous handler and setup new handler */
170 : #ifdef HAVE_SIGACTION
171 0 : memset(&sa, 0, sizeof(sa));
172 0 : sa.sa_handler = handler;
173 0 : sa.sa_flags |= SA_RESTART;
174 0 : sigfillset(&sa.sa_mask);
175 :
176 0 : if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) {
177 0 : event_warn("sigaction");
178 0 : free(sig->sh_old[evsignal]);
179 0 : return (-1);
180 : }
181 : #else
182 : if ((sh = signal(evsignal, handler)) == SIG_ERR) {
183 : event_warn("signal");
184 : free(sig->sh_old[evsignal]);
185 : return (-1);
186 : }
187 : *sig->sh_old[evsignal] = sh;
188 : #endif
189 :
190 0 : return (0);
191 : }
192 :
193 : int
194 0 : evsignal_add(struct event *ev)
195 : {
196 : int evsignal;
197 0 : struct event_base *base = ev->ev_base;
198 0 : struct evsignal_info *sig = &ev->ev_base->sig;
199 :
200 0 : if (ev->ev_events & (EV_READ|EV_WRITE))
201 0 : event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
202 0 : evsignal = EVENT_SIGNAL(ev);
203 0 : assert(evsignal >= 0 && evsignal < NSIG);
204 0 : if (TAILQ_EMPTY(&sig->evsigevents[evsignal])) {
205 : event_debug(("%s: %p: changing signal handler", __func__, ev));
206 0 : if (_evsignal_set_handler(
207 : base, evsignal, evsignal_handler) == -1)
208 0 : return (-1);
209 :
210 : /* catch signals if they happen quickly */
211 0 : evsignal_base = base;
212 :
213 0 : if (!sig->ev_signal_added) {
214 0 : if (event_add(&sig->ev_signal, NULL))
215 0 : return (-1);
216 0 : sig->ev_signal_added = 1;
217 : }
218 : }
219 :
220 : /* multiple events may listen to the same signal */
221 0 : TAILQ_INSERT_TAIL(&sig->evsigevents[evsignal], ev, ev_signal_next);
222 :
223 0 : return (0);
224 : }
225 :
226 : int
227 0 : _evsignal_restore_handler(struct event_base *base, int evsignal)
228 : {
229 0 : int ret = 0;
230 0 : struct evsignal_info *sig = &base->sig;
231 : #ifdef HAVE_SIGACTION
232 : struct sigaction *sh;
233 : #else
234 : ev_sighandler_t *sh;
235 : #endif
236 :
237 : /* restore previous handler */
238 0 : sh = sig->sh_old[evsignal];
239 0 : sig->sh_old[evsignal] = NULL;
240 : #ifdef HAVE_SIGACTION
241 0 : if (sigaction(evsignal, sh, NULL) == -1) {
242 0 : event_warn("sigaction");
243 0 : ret = -1;
244 : }
245 : #else
246 : if (signal(evsignal, *sh) == SIG_ERR) {
247 : event_warn("signal");
248 : ret = -1;
249 : }
250 : #endif
251 0 : free(sh);
252 :
253 0 : return ret;
254 : }
255 :
256 : int
257 0 : evsignal_del(struct event *ev)
258 : {
259 0 : struct event_base *base = ev->ev_base;
260 0 : struct evsignal_info *sig = &base->sig;
261 0 : int evsignal = EVENT_SIGNAL(ev);
262 :
263 0 : assert(evsignal >= 0 && evsignal < NSIG);
264 :
265 : /* multiple events may listen to the same signal */
266 0 : TAILQ_REMOVE(&sig->evsigevents[evsignal], ev, ev_signal_next);
267 :
268 0 : if (!TAILQ_EMPTY(&sig->evsigevents[evsignal]))
269 0 : return (0);
270 :
271 : event_debug(("%s: %p: restoring signal handler", __func__, ev));
272 :
273 0 : return (_evsignal_restore_handler(ev->ev_base, EVENT_SIGNAL(ev)));
274 : }
275 :
276 : static void
277 0 : evsignal_handler(int sig)
278 : {
279 0 : int save_errno = errno;
280 :
281 0 : if (evsignal_base == NULL) {
282 0 : event_warn(
283 : "%s: received signal %d, but have no base configured",
284 : __func__, sig);
285 0 : return;
286 : }
287 :
288 0 : evsignal_base->sig.evsigcaught[sig]++;
289 0 : evsignal_base->sig.evsignal_caught = 1;
290 :
291 : #ifndef HAVE_SIGACTION
292 : signal(sig, evsignal_handler);
293 : #endif
294 :
295 : /* Wake up our notification mechanism */
296 0 : send(evsignal_base->sig.ev_signal_pair[0], "a", 1, 0);
297 0 : errno = save_errno;
298 : }
299 :
300 : void
301 52 : evsignal_process(struct event_base *base)
302 : {
303 52 : struct evsignal_info *sig = &base->sig;
304 : struct event *ev, *next_ev;
305 : sig_atomic_t ncalls;
306 : int i;
307 :
308 52 : base->sig.evsignal_caught = 0;
309 3380 : for (i = 1; i < NSIG; ++i) {
310 3328 : ncalls = sig->evsigcaught[i];
311 3328 : if (ncalls == 0)
312 3328 : continue;
313 :
314 0 : for (ev = TAILQ_FIRST(&sig->evsigevents[i]);
315 0 : ev != NULL; ev = next_ev) {
316 0 : next_ev = TAILQ_NEXT(ev, ev_signal_next);
317 0 : if (!(ev->ev_events & EV_PERSIST))
318 0 : event_del(ev);
319 0 : event_active(ev, EV_SIGNAL, ncalls);
320 : }
321 :
322 0 : sig->evsigcaught[i] = 0;
323 : }
324 52 : }
325 :
326 : void
327 1419 : evsignal_dealloc(struct event_base *base)
328 : {
329 1419 : int i = 0;
330 1419 : if (base->sig.ev_signal_added) {
331 0 : event_del(&base->sig.ev_signal);
332 0 : base->sig.ev_signal_added = 0;
333 : }
334 93654 : for (i = 0; i < NSIG; ++i) {
335 92235 : if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL)
336 0 : _evsignal_restore_handler(base, i);
337 : }
338 :
339 1419 : EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
340 1419 : base->sig.ev_signal_pair[0] = -1;
341 1419 : EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
342 1419 : base->sig.ev_signal_pair[1] = -1;
343 1419 : base->sig.sh_old_max = 0;
344 :
345 : /* per index frees are handled in evsignal_del() */
346 1419 : free(base->sig.sh_old);
347 1419 : }
|