Blame sysdeps/posix/sigwait.c

Packit 6c4009
/* Implementation of sigwait function from POSIX.1c.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stddef.h>		/* For NULL.  */
Packit 6c4009
#include <sysdep-cancel.h>
Packit 6c4009
Packit 6c4009
/* This is our dummy signal handler we use here.  */
Packit 6c4009
static void ignore_signal (int sig);
Packit 6c4009
Packit 6c4009
/* Place where to remember which signal we got.  Please note that this
Packit 6c4009
   implementation cannot be used for the threaded libc.  The
Packit 6c4009
   libpthread must provide an own version.  */
Packit 6c4009
static int was_sig;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_sigwait (const sigset_t *set, int *sig)
Packit 6c4009
{
Packit 6c4009
  sigset_t tmp_mask;
Packit 6c4009
  struct sigaction saved[NSIG];
Packit 6c4009
  struct sigaction action;
Packit 6c4009
  int save_errno;
Packit 6c4009
  int this;
Packit 6c4009
Packit 6c4009
  /* Prepare set.  */
Packit 6c4009
  __sigfillset (&tmp_mask);
Packit 6c4009
Packit 6c4009
  /* Unblock all signals in the SET and register our nice handler.  */
Packit 6c4009
  action.sa_handler = ignore_signal;
Packit 6c4009
  action.sa_flags = 0;
Packit 6c4009
  __sigfillset (&action.sa_mask);	/* Block all signals for handler.  */
Packit 6c4009
Packit 6c4009
  /* Make sure we recognize error conditions by setting WAS_SIG to a
Packit 6c4009
     value which does not describe a legal signal number.  */
Packit 6c4009
  was_sig = -1;
Packit 6c4009
Packit 6c4009
  for (this = 1; this < NSIG; ++this)
Packit 6c4009
    if (__sigismember (set, this))
Packit 6c4009
      {
Packit 6c4009
	/* Unblock this signal.  */
Packit 6c4009
	__sigdelset (&tmp_mask, this);
Packit 6c4009
Packit 6c4009
	/* Register temporary action handler.  */
Packit 6c4009
	if (__sigaction (this, &action, &saved[this]) != 0)
Packit 6c4009
	  goto restore_handler;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* Now we can wait for signals.  */
Packit 6c4009
  __sigsuspend (&tmp_mask);
Packit 6c4009
Packit 6c4009
 restore_handler:
Packit 6c4009
  save_errno = errno;
Packit 6c4009
Packit 6c4009
  while (--this >= 1)
Packit 6c4009
    if (__sigismember (set, this))
Packit 6c4009
      /* We ignore errors here since we must restore all handlers.  */
Packit 6c4009
      __sigaction (this, &saved[this], NULL);
Packit 6c4009
Packit 6c4009
  __set_errno (save_errno);
Packit 6c4009
Packit 6c4009
  /* Store the result and return.  */
Packit 6c4009
  *sig = was_sig;
Packit 6c4009
  return was_sig == -1 ? -1 : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__sigwait (const sigset_t *set, int *sig)
Packit 6c4009
{
Packit 6c4009
  if (SINGLE_THREAD_P)
Packit 6c4009
    return do_sigwait (set, sig);
Packit 6c4009
Packit 6c4009
  int oldtype = LIBC_CANCEL_ASYNC ();
Packit 6c4009
Packit 6c4009
  int result = do_sigwait (set, sig);
Packit 6c4009
Packit 6c4009
  LIBC_CANCEL_RESET (oldtype);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__sigwait)
Packit 6c4009
weak_alias (__sigwait, sigwait)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
ignore_signal (int sig)
Packit 6c4009
{
Packit 6c4009
  /* Remember the signal.  */
Packit 6c4009
  was_sig = sig;
Packit 6c4009
}