]> arthur.barton.de Git - netatalk.git/blob - libevent/signal.c
Update libevent to 2.0.12
[netatalk.git] / libevent / signal.c
1 /*      $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $       */
2
3 /*
4  * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright 2007-2010 Niels Provos and Nick Mathewson
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 #include "event2/event-config.h"
30
31 #ifdef WIN32
32 #define WIN32_LEAN_AND_MEAN
33 #include <winsock2.h>
34 #include <windows.h>
35 #undef WIN32_LEAN_AND_MEAN
36 #endif
37 #include <sys/types.h>
38 #ifdef _EVENT_HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #include <sys/queue.h>
42 #ifdef _EVENT_HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #ifdef _EVENT_HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #include <errno.h>
53 #ifdef _EVENT_HAVE_FCNTL_H
54 #include <fcntl.h>
55 #endif
56
57 #include "event2/event.h"
58 #include "event2/event_struct.h"
59 #include "event-internal.h"
60 #include "event2/util.h"
61 #include "evsignal-internal.h"
62 #include "log-internal.h"
63 #include "evmap-internal.h"
64 #include "evthread-internal.h"
65
66 /*
67   signal.c
68
69   This is the signal-handling implementation we use for backends that don't
70   have a better way to do signal handling.  It uses sigaction() or signal()
71   to set a signal handler, and a socket pair to tell the event base when
72
73   Note that I said "the event base" : only one event base can be set up to use
74   this at a time.  For historical reasons and backward compatibility, if you
75   add an event for a signal to event_base A, then add an event for a signal
76   (any signal!) to event_base B, event_base B will get informed about the
77   signal, but event_base A won't.
78
79   It would be neat to change this behavior in some future version of Libevent.
80   kqueue already does something far more sensible.  We can make all backends
81   on Linux do a reasonable thing using signalfd.
82 */
83
84 #ifndef WIN32
85 /* Windows wants us to call our signal handlers as __cdecl.  Nobody else
86  * expects you to do anything crazy like this. */
87 #define __cdecl
88 #endif
89
90 static int evsig_add(struct event_base *, evutil_socket_t, short, short, void *);
91 static int evsig_del(struct event_base *, evutil_socket_t, short, short, void *);
92
93 static const struct eventop evsigops = {
94         "signal",
95         NULL,
96         evsig_add,
97         evsig_del,
98         NULL,
99         NULL,
100         0, 0, 0
101 };
102
103 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
104 /* Lock for evsig_base and evsig_base_n_signals_added fields. */
105 static void *evsig_base_lock = NULL;
106 #endif
107 /* The event base that's currently getting informed about signals. */
108 static struct event_base *evsig_base = NULL;
109 /* A copy of evsig_base->sigev_n_signals_added. */
110 static int evsig_base_n_signals_added = 0;
111 static evutil_socket_t evsig_base_fd = -1;
112
113 static void __cdecl evsig_handler(int sig);
114
115 #define EVSIGBASE_LOCK() EVLOCK_LOCK(evsig_base_lock, 0)
116 #define EVSIGBASE_UNLOCK() EVLOCK_UNLOCK(evsig_base_lock, 0)
117
118 void
119 evsig_set_base(struct event_base *base)
120 {
121         EVSIGBASE_LOCK();
122         evsig_base = base;
123         evsig_base_n_signals_added = base->sig.ev_n_signals_added;
124         evsig_base_fd = base->sig.ev_signal_pair[0];
125         EVSIGBASE_UNLOCK();
126 }
127
128 /* Callback for when the signal handler write a byte to our signaling socket */
129 static void
130 evsig_cb(evutil_socket_t fd, short what, void *arg)
131 {
132         static char signals[1024];
133         ev_ssize_t n;
134         int i;
135         int ncaught[NSIG];
136         struct event_base *base;
137
138         base = arg;
139
140         memset(&ncaught, 0, sizeof(ncaught));
141
142         while (1) {
143                 n = recv(fd, signals, sizeof(signals), 0);
144                 if (n == -1) {
145                         int err = evutil_socket_geterror(fd);
146                         if (! EVUTIL_ERR_RW_RETRIABLE(err))
147                                 event_sock_err(1, fd, "%s: recv", __func__);
148                         break;
149                 } else if (n == 0) {
150                         /* XXX warn? */
151                         break;
152                 }
153                 for (i = 0; i < n; ++i) {
154                         ev_uint8_t sig = signals[i];
155                         if (sig < NSIG)
156                                 ncaught[sig]++;
157                 }
158         }
159
160         EVBASE_ACQUIRE_LOCK(base, th_base_lock);
161         for (i = 0; i < NSIG; ++i) {
162                 if (ncaught[i])
163                         evmap_signal_active(base, i, ncaught[i]);
164         }
165         EVBASE_RELEASE_LOCK(base, th_base_lock);
166 }
167
168 int
169 evsig_init(struct event_base *base)
170 {
171 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
172         if (! evsig_base_lock)
173                 EVTHREAD_ALLOC_LOCK(evsig_base_lock, 0);
174 #endif
175
176         /*
177          * Our signal handler is going to write to one end of the socket
178          * pair to wake up our event loop.  The event loop then scans for
179          * signals that got delivered.
180          */
181         if (evutil_socketpair(
182                     AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) {
183 #ifdef WIN32
184                 /* Make this nonfatal on win32, where sometimes people
185                    have localhost firewalled. */
186                 event_sock_warn(-1, "%s: socketpair", __func__);
187 #else
188                 event_sock_err(1, -1, "%s: socketpair", __func__);
189 #endif
190                 return -1;
191         }
192
193         evutil_make_socket_closeonexec(base->sig.ev_signal_pair[0]);
194         evutil_make_socket_closeonexec(base->sig.ev_signal_pair[1]);
195         base->sig.sh_old = NULL;
196         base->sig.sh_old_max = 0;
197
198         evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
199         evutil_make_socket_nonblocking(base->sig.ev_signal_pair[1]);
200
201         event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[1],
202                 EV_READ | EV_PERSIST, evsig_cb, base);
203
204         base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
205         event_priority_set(&base->sig.ev_signal, 0);
206
207         base->evsigsel = &evsigops;
208
209         return 0;
210 }
211
212 /* Helper: set the signal handler for evsignal to handler in base, so that
213  * we can restore the original handler when we clear the current one. */
214 int
215 _evsig_set_handler(struct event_base *base,
216     int evsignal, void (__cdecl *handler)(int))
217 {
218 #ifdef _EVENT_HAVE_SIGACTION
219         struct sigaction sa;
220 #else
221         ev_sighandler_t sh;
222 #endif
223         struct evsig_info *sig = &base->sig;
224         void *p;
225
226         /*
227          * resize saved signal handler array up to the highest signal number.
228          * a dynamic array is used to keep footprint on the low side.
229          */
230         if (evsignal >= sig->sh_old_max) {
231                 int new_max = evsignal + 1;
232                 event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing",
233                             __func__, evsignal, sig->sh_old_max));
234                 p = mm_realloc(sig->sh_old, new_max * sizeof(*sig->sh_old));
235                 if (p == NULL) {
236                         event_warn("realloc");
237                         return (-1);
238                 }
239
240                 memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old),
241                     0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old));
242
243                 sig->sh_old_max = new_max;
244                 sig->sh_old = p;
245         }
246
247         /* allocate space for previous handler out of dynamic array */
248         sig->sh_old[evsignal] = mm_malloc(sizeof *sig->sh_old[evsignal]);
249         if (sig->sh_old[evsignal] == NULL) {
250                 event_warn("malloc");
251                 return (-1);
252         }
253
254         /* save previous handler and setup new handler */
255 #ifdef _EVENT_HAVE_SIGACTION
256         memset(&sa, 0, sizeof(sa));
257         sa.sa_handler = handler;
258         sa.sa_flags |= SA_RESTART;
259         sigfillset(&sa.sa_mask);
260
261         if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) {
262                 event_warn("sigaction");
263                 mm_free(sig->sh_old[evsignal]);
264                 sig->sh_old[evsignal] = NULL;
265                 return (-1);
266         }
267 #else
268         if ((sh = signal(evsignal, handler)) == SIG_ERR) {
269                 event_warn("signal");
270                 mm_free(sig->sh_old[evsignal]);
271                 sig->sh_old[evsignal] = NULL;
272                 return (-1);
273         }
274         *sig->sh_old[evsignal] = sh;
275 #endif
276
277         return (0);
278 }
279
280 static int
281 evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
282 {
283         struct evsig_info *sig = &base->sig;
284         (void)p;
285
286         EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
287
288         /* catch signals if they happen quickly */
289         EVSIGBASE_LOCK();
290         if (evsig_base != base && evsig_base_n_signals_added) {
291                 event_warnx("Added a signal to event base %p with signals "
292                     "already added to event_base %p.  Only one can have "
293                     "signals at a time with the %s backend.  The base with "
294                     "the most recently added signal or the most recent "
295                     "event_base_loop() call gets preference; do "
296                     "not rely on this behavior in future Libevent versions.",
297                     base, evsig_base, base->evsel->name);
298         }
299         evsig_base = base;
300         evsig_base_n_signals_added = ++sig->ev_n_signals_added;
301         evsig_base_fd = base->sig.ev_signal_pair[0];
302         EVSIGBASE_UNLOCK();
303
304         event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal));
305         if (_evsig_set_handler(base, (int)evsignal, evsig_handler) == -1) {
306                 goto err;
307         }
308
309
310         if (!sig->ev_signal_added) {
311                 if (event_add(&sig->ev_signal, NULL))
312                         goto err;
313                 sig->ev_signal_added = 1;
314         }
315
316         return (0);
317
318 err:
319         EVSIGBASE_LOCK();
320         --evsig_base_n_signals_added;
321         --sig->ev_n_signals_added;
322         EVSIGBASE_UNLOCK();
323         return (-1);
324 }
325
326 int
327 _evsig_restore_handler(struct event_base *base, int evsignal)
328 {
329         int ret = 0;
330         struct evsig_info *sig = &base->sig;
331 #ifdef _EVENT_HAVE_SIGACTION
332         struct sigaction *sh;
333 #else
334         ev_sighandler_t *sh;
335 #endif
336
337         /* restore previous handler */
338         sh = sig->sh_old[evsignal];
339         sig->sh_old[evsignal] = NULL;
340 #ifdef _EVENT_HAVE_SIGACTION
341         if (sigaction(evsignal, sh, NULL) == -1) {
342                 event_warn("sigaction");
343                 ret = -1;
344         }
345 #else
346         if (signal(evsignal, *sh) == SIG_ERR) {
347                 event_warn("signal");
348                 ret = -1;
349         }
350 #endif
351
352         mm_free(sh);
353
354         return ret;
355 }
356
357 static int
358 evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
359 {
360         EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
361
362         event_debug(("%s: %d: restoring signal handler", __func__, evsignal));
363
364         EVSIGBASE_LOCK();
365         --evsig_base_n_signals_added;
366         --base->sig.ev_n_signals_added;
367         EVSIGBASE_UNLOCK();
368
369         return (_evsig_restore_handler(base, (int)evsignal));
370 }
371
372 static void __cdecl
373 evsig_handler(int sig)
374 {
375         int save_errno = errno;
376 #ifdef WIN32
377         int socket_errno = EVUTIL_SOCKET_ERROR();
378 #endif
379         ev_uint8_t msg;
380
381         if (evsig_base == NULL) {
382                 event_warnx(
383                         "%s: received signal %d, but have no base configured",
384                         __func__, sig);
385                 return;
386         }
387
388 #ifndef _EVENT_HAVE_SIGACTION
389         signal(sig, evsig_handler);
390 #endif
391
392         /* Wake up our notification mechanism */
393         msg = sig;
394         send(evsig_base_fd, (char*)&msg, 1, 0);
395         errno = save_errno;
396 #ifdef WIN32
397         EVUTIL_SET_SOCKET_ERROR(socket_errno);
398 #endif
399 }
400
401 void
402 evsig_dealloc(struct event_base *base)
403 {
404         int i = 0;
405         if (base->sig.ev_signal_added) {
406                 event_del(&base->sig.ev_signal);
407                 event_debug_unassign(&base->sig.ev_signal);
408                 base->sig.ev_signal_added = 0;
409         }
410
411         for (i = 0; i < NSIG; ++i) {
412                 if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL)
413                         _evsig_restore_handler(base, i);
414         }
415         EVSIGBASE_LOCK();
416         if (base == evsig_base) {
417                 evsig_base = NULL;
418                 evsig_base_n_signals_added = 0;
419                 evsig_base_fd = -1;
420         }
421         EVSIGBASE_UNLOCK();
422
423         if (base->sig.ev_signal_pair[0] != -1) {
424                 evutil_closesocket(base->sig.ev_signal_pair[0]);
425                 base->sig.ev_signal_pair[0] = -1;
426         }
427         if (base->sig.ev_signal_pair[1] != -1) {
428                 evutil_closesocket(base->sig.ev_signal_pair[1]);
429                 base->sig.ev_signal_pair[1] = -1;
430         }
431         base->sig.sh_old_max = 0;
432
433         /* per index frees are handled in evsig_del() */
434         if (base->sig.sh_old) {
435                 mm_free(base->sig.sh_old);
436                 base->sig.sh_old = NULL;
437         }
438 }