Back to index

glibc  2.9
sigsuspend.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2002, 2007
00002      Free Software Foundation, Inc.
00003 
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <hurd.h>
00023 #include <hurd/signal.h>
00024 #include <hurd/msg.h>
00025 
00026 /* Change the set of blocked signals to SET,
00027    wait until a signal arrives, and restore the set of blocked signals.  */
00028 int
00029 __sigsuspend (set)
00030      const sigset_t *set;
00031 {
00032   struct hurd_sigstate *ss;
00033   sigset_t newmask, oldmask, pending;
00034   mach_port_t wait;
00035   mach_msg_header_t msg;
00036 
00037   if (set != NULL)
00038     /* Crash before locking.  */
00039     newmask = *set;
00040 
00041   /* Get a fresh port we will wait on.  */
00042   wait = __mach_reply_port ();
00043 
00044   ss = _hurd_self_sigstate ();
00045 
00046   __spin_lock (&ss->lock);
00047 
00048   oldmask = ss->blocked;
00049   if (set != NULL)
00050     /* Change to the new blocked signal mask.  */
00051     ss->blocked = newmask & ~_SIG_CANT_MASK;
00052 
00053   /* Notice if any pending signals just became unblocked.  */
00054   pending = ss->pending & ~ss->blocked;
00055 
00056   /* Tell the signal thread to message us when a signal arrives.  */
00057   ss->suspended = wait;
00058   __spin_unlock (&ss->lock);
00059 
00060   if (pending)
00061     /* Tell the signal thread to check for pending signals.  */
00062     __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
00063 
00064   /* Wait for the signal thread's message.  */
00065   __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof (msg), wait,
00066              MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
00067   __mach_port_destroy (__mach_task_self (), wait);
00068 
00069   __spin_lock (&ss->lock);
00070   ss->blocked = oldmask;    /* Restore the old mask.  */
00071   pending = ss->pending & ~ss->blocked;   /* Again check for pending signals.  */
00072   __spin_unlock (&ss->lock);
00073 
00074   if (pending)
00075     /* Tell the signal thread to check for pending signals.  */
00076     __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
00077 
00078   /* We've been interrupted!  And a good thing, too.
00079      Otherwise we'd never return.
00080      That's right; this function always returns an error.  */
00081   errno = EINTR;
00082   return -1;
00083 }
00084 libc_hidden_def (__sigsuspend)
00085 strong_alias (__sigsuspend, sigsuspend_not_cancel)
00086 weak_alias (__sigsuspend, sigsuspend)