michael@0: /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ michael@0: michael@0: /* michael@0: * Copyright 2000-2007 Niels Provos michael@0: * Copyright 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifdef WIN32 michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #include michael@0: #undef WIN32_LEAN_AND_MEAN michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_SOCKET_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_UNISTD_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_FCNTL_H michael@0: #include michael@0: #endif michael@0: michael@0: #include "event2/event.h" michael@0: #include "event2/event_struct.h" michael@0: #include "event-internal.h" michael@0: #include "event2/util.h" michael@0: #include "evsignal-internal.h" michael@0: #include "log-internal.h" michael@0: #include "evmap-internal.h" michael@0: #include "evthread-internal.h" michael@0: michael@0: /* michael@0: signal.c michael@0: michael@0: This is the signal-handling implementation we use for backends that don't michael@0: have a better way to do signal handling. It uses sigaction() or signal() michael@0: to set a signal handler, and a socket pair to tell the event base when michael@0: michael@0: Note that I said "the event base" : only one event base can be set up to use michael@0: this at a time. For historical reasons and backward compatibility, if you michael@0: add an event for a signal to event_base A, then add an event for a signal michael@0: (any signal!) to event_base B, event_base B will get informed about the michael@0: signal, but event_base A won't. michael@0: michael@0: It would be neat to change this behavior in some future version of Libevent. michael@0: kqueue already does something far more sensible. We can make all backends michael@0: on Linux do a reasonable thing using signalfd. michael@0: */ michael@0: michael@0: #ifndef WIN32 michael@0: /* Windows wants us to call our signal handlers as __cdecl. Nobody else michael@0: * expects you to do anything crazy like this. */ michael@0: #define __cdecl michael@0: #endif michael@0: michael@0: static int evsig_add(struct event_base *, evutil_socket_t, short, short, void *); michael@0: static int evsig_del(struct event_base *, evutil_socket_t, short, short, void *); michael@0: michael@0: static const struct eventop evsigops = { michael@0: "signal", michael@0: NULL, michael@0: evsig_add, michael@0: evsig_del, michael@0: NULL, michael@0: NULL, michael@0: 0, 0, 0 michael@0: }; michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: /* Lock for evsig_base and evsig_base_n_signals_added fields. */ michael@0: static void *evsig_base_lock = NULL; michael@0: #endif michael@0: /* The event base that's currently getting informed about signals. */ michael@0: static struct event_base *evsig_base = NULL; michael@0: /* A copy of evsig_base->sigev_n_signals_added. */ michael@0: static int evsig_base_n_signals_added = 0; michael@0: static evutil_socket_t evsig_base_fd = -1; michael@0: michael@0: static void __cdecl evsig_handler(int sig); michael@0: michael@0: #define EVSIGBASE_LOCK() EVLOCK_LOCK(evsig_base_lock, 0) michael@0: #define EVSIGBASE_UNLOCK() EVLOCK_UNLOCK(evsig_base_lock, 0) michael@0: michael@0: void michael@0: evsig_set_base(struct event_base *base) michael@0: { michael@0: EVSIGBASE_LOCK(); michael@0: evsig_base = base; michael@0: evsig_base_n_signals_added = base->sig.ev_n_signals_added; michael@0: evsig_base_fd = base->sig.ev_signal_pair[0]; michael@0: EVSIGBASE_UNLOCK(); michael@0: } michael@0: michael@0: /* Callback for when the signal handler write a byte to our signaling socket */ michael@0: static void michael@0: evsig_cb(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: static char signals[1024]; michael@0: ev_ssize_t n; michael@0: int i; michael@0: int ncaught[NSIG]; michael@0: struct event_base *base; michael@0: michael@0: base = arg; michael@0: michael@0: memset(&ncaught, 0, sizeof(ncaught)); michael@0: michael@0: while (1) { michael@0: n = recv(fd, signals, sizeof(signals), 0); michael@0: if (n == -1) { michael@0: int err = evutil_socket_geterror(fd); michael@0: if (! EVUTIL_ERR_RW_RETRIABLE(err)) michael@0: event_sock_err(1, fd, "%s: recv", __func__); michael@0: break; michael@0: } else if (n == 0) { michael@0: /* XXX warn? */ michael@0: break; michael@0: } michael@0: for (i = 0; i < n; ++i) { michael@0: ev_uint8_t sig = signals[i]; michael@0: if (sig < NSIG) michael@0: ncaught[sig]++; michael@0: } michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: for (i = 0; i < NSIG; ++i) { michael@0: if (ncaught[i]) michael@0: evmap_signal_active(base, i, ncaught[i]); michael@0: } michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: michael@0: int michael@0: evsig_init(struct event_base *base) michael@0: { michael@0: /* michael@0: * Our signal handler is going to write to one end of the socket michael@0: * pair to wake up our event loop. The event loop then scans for michael@0: * signals that got delivered. michael@0: */ michael@0: if (evutil_socketpair( michael@0: AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) { michael@0: #ifdef WIN32 michael@0: /* Make this nonfatal on win32, where sometimes people michael@0: have localhost firewalled. */ michael@0: event_sock_warn(-1, "%s: socketpair", __func__); michael@0: #else michael@0: event_sock_err(1, -1, "%s: socketpair", __func__); michael@0: #endif michael@0: return -1; michael@0: } michael@0: michael@0: evutil_make_socket_closeonexec(base->sig.ev_signal_pair[0]); michael@0: evutil_make_socket_closeonexec(base->sig.ev_signal_pair[1]); michael@0: base->sig.sh_old = NULL; michael@0: base->sig.sh_old_max = 0; michael@0: michael@0: evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]); michael@0: evutil_make_socket_nonblocking(base->sig.ev_signal_pair[1]); michael@0: michael@0: event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[1], michael@0: EV_READ | EV_PERSIST, evsig_cb, base); michael@0: michael@0: base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL; michael@0: event_priority_set(&base->sig.ev_signal, 0); michael@0: michael@0: base->evsigsel = &evsigops; michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* Helper: set the signal handler for evsignal to handler in base, so that michael@0: * we can restore the original handler when we clear the current one. */ michael@0: int michael@0: _evsig_set_handler(struct event_base *base, michael@0: int evsignal, void (__cdecl *handler)(int)) michael@0: { michael@0: #ifdef _EVENT_HAVE_SIGACTION michael@0: struct sigaction sa; michael@0: #else michael@0: ev_sighandler_t sh; michael@0: #endif michael@0: struct evsig_info *sig = &base->sig; michael@0: void *p; michael@0: michael@0: /* michael@0: * resize saved signal handler array up to the highest signal number. michael@0: * a dynamic array is used to keep footprint on the low side. michael@0: */ michael@0: if (evsignal >= sig->sh_old_max) { michael@0: int new_max = evsignal + 1; michael@0: event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing", michael@0: __func__, evsignal, sig->sh_old_max)); michael@0: p = mm_realloc(sig->sh_old, new_max * sizeof(*sig->sh_old)); michael@0: if (p == NULL) { michael@0: event_warn("realloc"); michael@0: return (-1); michael@0: } michael@0: michael@0: memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old), michael@0: 0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old)); michael@0: michael@0: sig->sh_old_max = new_max; michael@0: sig->sh_old = p; michael@0: } michael@0: michael@0: /* allocate space for previous handler out of dynamic array */ michael@0: sig->sh_old[evsignal] = mm_malloc(sizeof *sig->sh_old[evsignal]); michael@0: if (sig->sh_old[evsignal] == NULL) { michael@0: event_warn("malloc"); michael@0: return (-1); michael@0: } michael@0: michael@0: /* save previous handler and setup new handler */ michael@0: #ifdef _EVENT_HAVE_SIGACTION michael@0: memset(&sa, 0, sizeof(sa)); michael@0: sa.sa_handler = handler; michael@0: sa.sa_flags |= SA_RESTART; michael@0: sigfillset(&sa.sa_mask); michael@0: michael@0: if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) { michael@0: event_warn("sigaction"); michael@0: mm_free(sig->sh_old[evsignal]); michael@0: sig->sh_old[evsignal] = NULL; michael@0: return (-1); michael@0: } michael@0: #else michael@0: if ((sh = signal(evsignal, handler)) == SIG_ERR) { michael@0: event_warn("signal"); michael@0: mm_free(sig->sh_old[evsignal]); michael@0: sig->sh_old[evsignal] = NULL; michael@0: return (-1); michael@0: } michael@0: *sig->sh_old[evsignal] = sh; michael@0: #endif michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p) michael@0: { michael@0: struct evsig_info *sig = &base->sig; michael@0: (void)p; michael@0: michael@0: EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG); michael@0: michael@0: /* catch signals if they happen quickly */ michael@0: EVSIGBASE_LOCK(); michael@0: if (evsig_base != base && evsig_base_n_signals_added) { michael@0: event_warnx("Added a signal to event base %p with signals " michael@0: "already added to event_base %p. Only one can have " michael@0: "signals at a time with the %s backend. The base with " michael@0: "the most recently added signal or the most recent " michael@0: "event_base_loop() call gets preference; do " michael@0: "not rely on this behavior in future Libevent versions.", michael@0: base, evsig_base, base->evsel->name); michael@0: } michael@0: evsig_base = base; michael@0: evsig_base_n_signals_added = ++sig->ev_n_signals_added; michael@0: evsig_base_fd = base->sig.ev_signal_pair[0]; michael@0: EVSIGBASE_UNLOCK(); michael@0: michael@0: event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal)); michael@0: if (_evsig_set_handler(base, (int)evsignal, evsig_handler) == -1) { michael@0: goto err; michael@0: } michael@0: michael@0: michael@0: if (!sig->ev_signal_added) { michael@0: if (event_add(&sig->ev_signal, NULL)) michael@0: goto err; michael@0: sig->ev_signal_added = 1; michael@0: } michael@0: michael@0: return (0); michael@0: michael@0: err: michael@0: EVSIGBASE_LOCK(); michael@0: --evsig_base_n_signals_added; michael@0: --sig->ev_n_signals_added; michael@0: EVSIGBASE_UNLOCK(); michael@0: return (-1); michael@0: } michael@0: michael@0: int michael@0: _evsig_restore_handler(struct event_base *base, int evsignal) michael@0: { michael@0: int ret = 0; michael@0: struct evsig_info *sig = &base->sig; michael@0: #ifdef _EVENT_HAVE_SIGACTION michael@0: struct sigaction *sh; michael@0: #else michael@0: ev_sighandler_t *sh; michael@0: #endif michael@0: michael@0: /* restore previous handler */ michael@0: sh = sig->sh_old[evsignal]; michael@0: sig->sh_old[evsignal] = NULL; michael@0: #ifdef _EVENT_HAVE_SIGACTION michael@0: if (sigaction(evsignal, sh, NULL) == -1) { michael@0: event_warn("sigaction"); michael@0: ret = -1; michael@0: } michael@0: #else michael@0: if (signal(evsignal, *sh) == SIG_ERR) { michael@0: event_warn("signal"); michael@0: ret = -1; michael@0: } michael@0: #endif michael@0: michael@0: mm_free(sh); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: static int michael@0: evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p) michael@0: { michael@0: EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG); michael@0: michael@0: event_debug(("%s: "EV_SOCK_FMT": restoring signal handler", michael@0: __func__, EV_SOCK_ARG(evsignal))); michael@0: michael@0: EVSIGBASE_LOCK(); michael@0: --evsig_base_n_signals_added; michael@0: --base->sig.ev_n_signals_added; michael@0: EVSIGBASE_UNLOCK(); michael@0: michael@0: return (_evsig_restore_handler(base, (int)evsignal)); michael@0: } michael@0: michael@0: static void __cdecl michael@0: evsig_handler(int sig) michael@0: { michael@0: int save_errno = errno; michael@0: #ifdef WIN32 michael@0: int socket_errno = EVUTIL_SOCKET_ERROR(); michael@0: #endif michael@0: ev_uint8_t msg; michael@0: michael@0: if (evsig_base == NULL) { michael@0: event_warnx( michael@0: "%s: received signal %d, but have no base configured", michael@0: __func__, sig); michael@0: return; michael@0: } michael@0: michael@0: #ifndef _EVENT_HAVE_SIGACTION michael@0: signal(sig, evsig_handler); michael@0: #endif michael@0: michael@0: /* Wake up our notification mechanism */ michael@0: msg = sig; michael@0: send(evsig_base_fd, (char*)&msg, 1, 0); michael@0: errno = save_errno; michael@0: #ifdef WIN32 michael@0: EVUTIL_SET_SOCKET_ERROR(socket_errno); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: evsig_dealloc(struct event_base *base) michael@0: { michael@0: int i = 0; michael@0: if (base->sig.ev_signal_added) { michael@0: event_del(&base->sig.ev_signal); michael@0: base->sig.ev_signal_added = 0; michael@0: } michael@0: /* debug event is created in evsig_init/event_assign even when michael@0: * ev_signal_added == 0, so unassign is required */ michael@0: event_debug_unassign(&base->sig.ev_signal); michael@0: michael@0: for (i = 0; i < NSIG; ++i) { michael@0: if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL) michael@0: _evsig_restore_handler(base, i); michael@0: } michael@0: EVSIGBASE_LOCK(); michael@0: if (base == evsig_base) { michael@0: evsig_base = NULL; michael@0: evsig_base_n_signals_added = 0; michael@0: evsig_base_fd = -1; michael@0: } michael@0: EVSIGBASE_UNLOCK(); michael@0: michael@0: if (base->sig.ev_signal_pair[0] != -1) { michael@0: evutil_closesocket(base->sig.ev_signal_pair[0]); michael@0: base->sig.ev_signal_pair[0] = -1; michael@0: } michael@0: if (base->sig.ev_signal_pair[1] != -1) { michael@0: evutil_closesocket(base->sig.ev_signal_pair[1]); michael@0: base->sig.ev_signal_pair[1] = -1; michael@0: } michael@0: base->sig.sh_old_max = 0; michael@0: michael@0: /* per index frees are handled in evsig_del() */ michael@0: if (base->sig.sh_old) { michael@0: mm_free(base->sig.sh_old); michael@0: base->sig.sh_old = NULL; michael@0: } michael@0: } michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: int michael@0: evsig_global_setup_locks_(const int enable_locks) michael@0: { michael@0: EVTHREAD_SETUP_GLOBAL_LOCK(evsig_base_lock, 0); michael@0: return 0; michael@0: } michael@0: #endif