Blame sysdeps/mach/hurd/sigsuspend.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This file is part of the GNU C Library.
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 <hurd.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/msg.h>
Packit 6c4009
Packit 6c4009
/* Change the set of blocked signals to SET,
Packit 6c4009
   wait until a signal arrives, and restore the set of blocked signals.  */
Packit 6c4009
int
Packit 6c4009
__sigsuspend (const sigset_t *set)
Packit 6c4009
{
Packit 6c4009
  struct hurd_sigstate *ss;
Packit 6c4009
  sigset_t newmask, oldmask, pending;
Packit 6c4009
  mach_port_t wait;
Packit 6c4009
  mach_msg_header_t msg;
Packit 6c4009
Packit 6c4009
  if (set != NULL)
Packit 6c4009
    /* Crash before locking.  */
Packit 6c4009
    newmask = *set;
Packit 6c4009
Packit 6c4009
  /* Get a fresh port we will wait on.  */
Packit 6c4009
  wait = __mach_reply_port ();
Packit 6c4009
Packit 6c4009
  ss = _hurd_self_sigstate ();
Packit 6c4009
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
Packit 6c4009
  oldmask = ss->blocked;
Packit 6c4009
  if (set != NULL)
Packit 6c4009
    /* Change to the new blocked signal mask.  */
Packit 6c4009
    ss->blocked = newmask & ~_SIG_CANT_MASK;
Packit 6c4009
Packit 6c4009
  /* Notice if any pending signals just became unblocked.  */
Packit 6c4009
  pending = ss->pending & ~ss->blocked;
Packit 6c4009
Packit 6c4009
  /* Tell the signal thread to message us when a signal arrives.  */
Packit 6c4009
  ss->suspended = wait;
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  if (pending)
Packit 6c4009
    /* Tell the signal thread to check for pending signals.  */
Packit 6c4009
    __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
Packit 6c4009
Packit 6c4009
  /* Wait for the signal thread's message.  */
Packit 6c4009
  __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof (msg), wait,
Packit 6c4009
	      MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
Packit 6c4009
  __mach_port_destroy (__mach_task_self (), wait);
Packit 6c4009
Packit 6c4009
  __spin_lock (&ss->lock);
Packit 6c4009
  ss->blocked = oldmask;	/* Restore the old mask.  */
Packit 6c4009
  pending = ss->pending & ~ss->blocked;	/* Again check for pending signals.  */
Packit 6c4009
  __spin_unlock (&ss->lock);
Packit 6c4009
Packit 6c4009
  if (pending)
Packit 6c4009
    /* Tell the signal thread to check for pending signals.  */
Packit 6c4009
    __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
Packit 6c4009
Packit 6c4009
  /* We've been interrupted!  And a good thing, too.
Packit 6c4009
     Otherwise we'd never return.
Packit 6c4009
     That's right; this function always returns an error.  */
Packit 6c4009
  errno = EINTR;
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__sigsuspend)
Packit 6c4009
weak_alias (__sigsuspend, sigsuspend)