1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2010 Niels Provos and Nick Mathewson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include "event2/event-config.h"
32 #define WIN32_LEAN_AND_MEAN
35 #undef WIN32_LEAN_AND_MEAN
37 #include <sys/types.h>
38 #ifdef _EVENT_HAVE_SYS_TIME_H
41 #include <sys/queue.h>
42 #ifdef _EVENT_HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
49 #ifdef _EVENT_HAVE_UNISTD_H
53 #ifdef _EVENT_HAVE_FCNTL_H
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"
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
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.
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.
85 /* Windows wants us to call our signal handlers as __cdecl. Nobody else
86 * expects you to do anything crazy like this. */
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 *);
93 static const struct eventop evsigops = {
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;
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;
113 static void __cdecl evsig_handler(int sig);
115 #define EVSIGBASE_LOCK() EVLOCK_LOCK(evsig_base_lock, 0)
116 #define EVSIGBASE_UNLOCK() EVLOCK_UNLOCK(evsig_base_lock, 0)
119 evsig_set_base(struct event_base *base)
123 evsig_base_n_signals_added = base->sig.ev_n_signals_added;
124 evsig_base_fd = base->sig.ev_signal_pair[0];
128 /* Callback for when the signal handler write a byte to our signaling socket */
130 evsig_cb(evutil_socket_t fd, short what, void *arg)
132 static char signals[1024];
136 struct event_base *base;
140 memset(&ncaught, 0, sizeof(ncaught));
143 n = recv(fd, signals, sizeof(signals), 0);
145 int err = evutil_socket_geterror(fd);
146 if (! EVUTIL_ERR_RW_RETRIABLE(err))
147 event_sock_err(1, fd, "%s: recv", __func__);
153 for (i = 0; i < n; ++i) {
154 ev_uint8_t sig = signals[i];
160 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
161 for (i = 0; i < NSIG; ++i) {
163 evmap_signal_active(base, i, ncaught[i]);
165 EVBASE_RELEASE_LOCK(base, th_base_lock);
169 evsig_init(struct event_base *base)
171 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
172 if (! evsig_base_lock)
173 EVTHREAD_ALLOC_LOCK(evsig_base_lock, 0);
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.
181 if (evutil_socketpair(
182 AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) {
184 /* Make this nonfatal on win32, where sometimes people
185 have localhost firewalled. */
186 event_sock_warn(-1, "%s: socketpair", __func__);
188 event_sock_err(1, -1, "%s: socketpair", __func__);
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;
198 evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
199 evutil_make_socket_nonblocking(base->sig.ev_signal_pair[1]);
201 event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[1],
202 EV_READ | EV_PERSIST, evsig_cb, base);
204 base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
205 event_priority_set(&base->sig.ev_signal, 0);
207 base->evsigsel = &evsigops;
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. */
215 _evsig_set_handler(struct event_base *base,
216 int evsignal, void (__cdecl *handler)(int))
218 #ifdef _EVENT_HAVE_SIGACTION
223 struct evsig_info *sig = &base->sig;
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.
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));
236 event_warn("realloc");
240 memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old),
241 0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old));
243 sig->sh_old_max = new_max;
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");
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);
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;
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;
274 *sig->sh_old[evsignal] = sh;
281 evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
283 struct evsig_info *sig = &base->sig;
286 EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
288 /* catch signals if they happen quickly */
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);
300 evsig_base_n_signals_added = ++sig->ev_n_signals_added;
301 evsig_base_fd = base->sig.ev_signal_pair[0];
304 event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal));
305 if (_evsig_set_handler(base, (int)evsignal, evsig_handler) == -1) {
310 if (!sig->ev_signal_added) {
311 if (event_add(&sig->ev_signal, NULL))
313 sig->ev_signal_added = 1;
320 --evsig_base_n_signals_added;
321 --sig->ev_n_signals_added;
327 _evsig_restore_handler(struct event_base *base, int evsignal)
330 struct evsig_info *sig = &base->sig;
331 #ifdef _EVENT_HAVE_SIGACTION
332 struct sigaction *sh;
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");
346 if (signal(evsignal, *sh) == SIG_ERR) {
347 event_warn("signal");
358 evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
360 EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
362 event_debug(("%s: %d: restoring signal handler", __func__, evsignal));
365 --evsig_base_n_signals_added;
366 --base->sig.ev_n_signals_added;
369 return (_evsig_restore_handler(base, (int)evsignal));
373 evsig_handler(int sig)
375 int save_errno = errno;
377 int socket_errno = EVUTIL_SOCKET_ERROR();
381 if (evsig_base == NULL) {
383 "%s: received signal %d, but have no base configured",
388 #ifndef _EVENT_HAVE_SIGACTION
389 signal(sig, evsig_handler);
392 /* Wake up our notification mechanism */
394 send(evsig_base_fd, (char*)&msg, 1, 0);
397 EVUTIL_SET_SOCKET_ERROR(socket_errno);
402 evsig_dealloc(struct event_base *base)
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;
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);
416 if (base == evsig_base) {
418 evsig_base_n_signals_added = 0;
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;
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;
431 base->sig.sh_old_max = 0;
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;