Blame lib/sigaction.c

Packit 1ac44c
/* POSIX compatible signal blocking.
Packit 1ac44c
   Copyright (C) 2008-2018 Free Software Foundation, Inc.
Packit 1ac44c
   Written by Eric Blake <ebb9@byu.net>, 2008.
Packit 1ac44c
Packit 1ac44c
   This program is free software: you can redistribute it and/or modify
Packit 1ac44c
   it under the terms of the GNU General Public License as published by
Packit 1ac44c
   the Free Software Foundation; either version 3 of the License, or
Packit 1ac44c
   (at your option) any later version.
Packit 1ac44c
Packit 1ac44c
   This program is distributed in the hope that it will be useful,
Packit 1ac44c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ac44c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1ac44c
   GNU General Public License for more details.
Packit 1ac44c
Packit 1ac44c
   You should have received a copy of the GNU General Public License
Packit 1ac44c
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 1ac44c
Packit 1ac44c
#include <config.h>
Packit 1ac44c
Packit 1ac44c
/* Specification.  */
Packit 1ac44c
#include <signal.h>
Packit 1ac44c
Packit 1ac44c
#include <errno.h>
Packit 1ac44c
#include <stdint.h>
Packit 1ac44c
#include <stdlib.h>
Packit 1ac44c
Packit 1ac44c
/* This implementation of sigaction is tailored to native Windows behavior:
Packit 1ac44c
   signal() has SysV semantics (ie. the handler is uninstalled before
Packit 1ac44c
   it is invoked).  This is an inherent data race if an asynchronous
Packit 1ac44c
   signal is sent twice in a row before we can reinstall our handler,
Packit 1ac44c
   but there's nothing we can do about it.  Meanwhile, sigprocmask()
Packit 1ac44c
   is not present, and while we can use the gnulib replacement to
Packit 1ac44c
   provide critical sections, it too suffers from potential data races
Packit 1ac44c
   in the face of an ill-timed asynchronous signal.  And we compound
Packit 1ac44c
   the situation by reading static storage in a signal handler, which
Packit 1ac44c
   POSIX warns is not generically async-signal-safe.  Oh well.
Packit 1ac44c
Packit 1ac44c
   Additionally:
Packit 1ac44c
     - We don't implement SA_NOCLDSTOP or SA_NOCLDWAIT, because SIGCHLD
Packit 1ac44c
       is not defined.
Packit 1ac44c
     - We don't implement SA_ONSTACK, because sigaltstack() is not present.
Packit 1ac44c
     - We ignore SA_RESTART, because blocking native Windows API calls are
Packit 1ac44c
       not interrupted anyway when an asynchronous signal occurs, and the
Packit 1ac44c
       MSVCRT runtime never sets errno to EINTR.
Packit 1ac44c
     - We don't implement SA_SIGINFO because it is impossible to do so
Packit 1ac44c
       portably.
Packit 1ac44c
Packit 1ac44c
   POSIX states that an application should not mix signal() and
Packit 1ac44c
   sigaction().  We support the use of signal() within the gnulib
Packit 1ac44c
   sigprocmask() substitute, but all other application code linked
Packit 1ac44c
   with this module should stick with only sigaction().  */
Packit 1ac44c
Packit 1ac44c
/* Check some of our assumptions.  */
Packit 1ac44c
#if defined SIGCHLD || defined HAVE_SIGALTSTACK || defined HAVE_SIGINTERRUPT
Packit 1ac44c
# error "Revisit the assumptions made in the sigaction module"
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* Out-of-range substitutes make a good fallback for uncatchable
Packit 1ac44c
   signals.  */
Packit 1ac44c
#ifndef SIGKILL
Packit 1ac44c
# define SIGKILL (-1)
Packit 1ac44c
#endif
Packit 1ac44c
#ifndef SIGSTOP
Packit 1ac44c
# define SIGSTOP (-1)
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* On native Windows, as of 2008, the signal SIGABRT_COMPAT is an alias
Packit 1ac44c
   for the signal SIGABRT.  Only one signal handler is stored for both
Packit 1ac44c
   SIGABRT and SIGABRT_COMPAT.  SIGABRT_COMPAT is not a signal of its own.  */
Packit 1ac44c
#if defined _WIN32 && ! defined __CYGWIN__
Packit 1ac44c
# undef SIGABRT_COMPAT
Packit 1ac44c
# define SIGABRT_COMPAT 6
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* A signal handler.  */
Packit 1ac44c
typedef void (*handler_t) (int signal);
Packit 1ac44c
Packit 1ac44c
/* Set of current actions.  If sa_handler for an entry is NULL, then
Packit 1ac44c
   that signal is not currently handled by the sigaction handler.  */
Packit 1ac44c
static struct sigaction volatile action_array[NSIG] /* = 0 */;
Packit 1ac44c
Packit 1ac44c
/* Signal handler that is installed for signals.  */
Packit 1ac44c
static void
Packit 1ac44c
sigaction_handler (int sig)
Packit 1ac44c
{
Packit 1ac44c
  handler_t handler;
Packit 1ac44c
  sigset_t mask;
Packit 1ac44c
  sigset_t oldmask;
Packit 1ac44c
  int saved_errno = errno;
Packit 1ac44c
  if (sig < 0 || NSIG <= sig || !action_array[sig].sa_handler)
Packit 1ac44c
    {
Packit 1ac44c
      /* Unexpected situation; be careful to avoid recursive abort.  */
Packit 1ac44c
      if (sig == SIGABRT)
Packit 1ac44c
        signal (SIGABRT, SIG_DFL);
Packit 1ac44c
      abort ();
Packit 1ac44c
    }
Packit 1ac44c
Packit 1ac44c
  /* Reinstall the signal handler when required; otherwise update the
Packit 1ac44c
     bookkeeping so that the user's handler may call sigaction and get
Packit 1ac44c
     accurate results.  We know the signal isn't currently blocked, or
Packit 1ac44c
     we wouldn't be in its handler, therefore we know that we are not
Packit 1ac44c
     interrupting a sigaction() call.  There is a race where any
Packit 1ac44c
     asynchronous instance of the same signal occurring before we
Packit 1ac44c
     reinstall the handler will trigger the default handler; oh
Packit 1ac44c
     well.  */
Packit 1ac44c
  handler = action_array[sig].sa_handler;
Packit 1ac44c
  if ((action_array[sig].sa_flags & SA_RESETHAND) == 0)
Packit 1ac44c
    signal (sig, sigaction_handler);
Packit 1ac44c
  else
Packit 1ac44c
    action_array[sig].sa_handler = NULL;
Packit 1ac44c
Packit 1ac44c
  /* Block appropriate signals.  */
Packit 1ac44c
  mask = action_array[sig].sa_mask;
Packit 1ac44c
  if ((action_array[sig].sa_flags & SA_NODEFER) == 0)
Packit 1ac44c
    sigaddset (&mask, sig);
Packit 1ac44c
  sigprocmask (SIG_BLOCK, &mask, &oldmask);
Packit 1ac44c
Packit 1ac44c
  /* Invoke the user's handler, then restore prior mask.  */
Packit 1ac44c
  errno = saved_errno;
Packit 1ac44c
  handler (sig);
Packit 1ac44c
  saved_errno = errno;
Packit 1ac44c
  sigprocmask (SIG_SETMASK, &oldmask, NULL);
Packit 1ac44c
  errno = saved_errno;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Change and/or query the action that will be taken on delivery of
Packit 1ac44c
   signal SIG.  If not NULL, ACT describes the new behavior.  If not
Packit 1ac44c
   NULL, OACT is set to the prior behavior.  Return 0 on success, or
Packit 1ac44c
   set errno and return -1 on failure.  */
Packit 1ac44c
int
Packit 1ac44c
sigaction (int sig, const struct sigaction *restrict act,
Packit 1ac44c
           struct sigaction *restrict oact)
Packit 1ac44c
{
Packit 1ac44c
  sigset_t mask;
Packit 1ac44c
  sigset_t oldmask;
Packit 1ac44c
  int saved_errno;
Packit 1ac44c
Packit 1ac44c
  if (sig < 0 || NSIG <= sig || sig == SIGKILL || sig == SIGSTOP
Packit 1ac44c
      || (act && act->sa_handler == SIG_ERR))
Packit 1ac44c
    {
Packit 1ac44c
      errno = EINVAL;
Packit 1ac44c
      return -1;
Packit 1ac44c
    }
Packit 1ac44c
Packit 1ac44c
#ifdef SIGABRT_COMPAT
Packit 1ac44c
  if (sig == SIGABRT_COMPAT)
Packit 1ac44c
    sig = SIGABRT;
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
  /* POSIX requires sigaction() to be async-signal-safe.  In other
Packit 1ac44c
     words, if an asynchronous signal can occur while we are anywhere
Packit 1ac44c
     inside this function, the user's handler could then call
Packit 1ac44c
     sigaction() recursively and expect consistent results.  We meet
Packit 1ac44c
     this rule by using sigprocmask to block all signals before
Packit 1ac44c
     modifying any data structure that could be read from a signal
Packit 1ac44c
     handler; this works since we know that the gnulib sigprocmask
Packit 1ac44c
     replacement does not try to use sigaction() from its handler.  */
Packit 1ac44c
  if (!act && !oact)
Packit 1ac44c
    return 0;
Packit 1ac44c
  sigfillset (&mask);
Packit 1ac44c
  sigprocmask (SIG_BLOCK, &mask, &oldmask);
Packit 1ac44c
  if (oact)
Packit 1ac44c
    {
Packit 1ac44c
      if (action_array[sig].sa_handler)
Packit 1ac44c
        *oact = action_array[sig];
Packit 1ac44c
      else
Packit 1ac44c
        {
Packit 1ac44c
          /* Safe to change the handler at will here, since all
Packit 1ac44c
             signals are currently blocked.  */
Packit 1ac44c
          oact->sa_handler = signal (sig, SIG_DFL);
Packit 1ac44c
          if (oact->sa_handler == SIG_ERR)
Packit 1ac44c
            goto failure;
Packit 1ac44c
          signal (sig, oact->sa_handler);
Packit 1ac44c
          oact->sa_flags = SA_RESETHAND | SA_NODEFER;
Packit 1ac44c
          sigemptyset (&oact->sa_mask);
Packit 1ac44c
        }
Packit 1ac44c
    }
Packit 1ac44c
Packit 1ac44c
  if (act)
Packit 1ac44c
    {
Packit 1ac44c
      /* Safe to install the handler before updating action_array,
Packit 1ac44c
         since all signals are currently blocked.  */
Packit 1ac44c
      if (act->sa_handler == SIG_DFL || act->sa_handler == SIG_IGN)
Packit 1ac44c
        {
Packit 1ac44c
          if (signal (sig, act->sa_handler) == SIG_ERR)
Packit 1ac44c
            goto failure;
Packit 1ac44c
          action_array[sig].sa_handler = NULL;
Packit 1ac44c
        }
Packit 1ac44c
      else
Packit 1ac44c
        {
Packit 1ac44c
          if (signal (sig, sigaction_handler) == SIG_ERR)
Packit 1ac44c
            goto failure;
Packit 1ac44c
          action_array[sig] = *act;
Packit 1ac44c
        }
Packit 1ac44c
    }
Packit 1ac44c
  sigprocmask (SIG_SETMASK, &oldmask, NULL);
Packit 1ac44c
  return 0;
Packit 1ac44c
Packit 1ac44c
 failure:
Packit 1ac44c
  saved_errno = errno;
Packit 1ac44c
  sigprocmask (SIG_SETMASK, &oldmask, NULL);
Packit 1ac44c
  errno = saved_errno;
Packit 1ac44c
  return -1;
Packit 1ac44c
}