LCOV - code coverage report
Current view: top level - ipc/chromium/src/third_party/libevent - signal.c (source / functions) Hit Total Coverage
Test: output.info Lines: 10 156 6.4 %
Date: 2017-07-14 16:53:18 Functions: 1 12 8.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13