Back to index

glibc  2.9
sigwait.c
Go to the documentation of this file.
00001 /* Implementation of sigwait function from POSIX.1c.
00002    Copyright (C) 1996, 1997, 1999, 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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 <signal.h>
00023 #include <stddef.h>         /* For NULL.  */
00024 #include <sysdep-cancel.h>
00025 
00026 /* This is our dummy signal handler we use here.  */
00027 static void ignore_signal (int sig);
00028 
00029 /* Place where to remember which signal we got.  Please note that this
00030    implementation cannot be used for the threaded libc.  The
00031    libpthread must provide an own version.  */
00032 static int was_sig;
00033 
00034 
00035 static int
00036 do_sigwait (const sigset_t *set, int *sig)
00037 {
00038   sigset_t tmp_mask;
00039   struct sigaction saved[NSIG];
00040   struct sigaction action;
00041   int save_errno;
00042   int this;
00043 
00044   /* Prepare set.  */
00045   __sigfillset (&tmp_mask);
00046 
00047   /* Unblock all signals in the SET and register our nice handler.  */
00048   action.sa_handler = ignore_signal;
00049   action.sa_flags = 0;
00050   __sigfillset (&action.sa_mask);  /* Block all signals for handler.  */
00051 
00052   /* Make sure we recognize error conditions by setting WAS_SIG to a
00053      value which does not describe a legal signal number.  */
00054   was_sig = -1;
00055 
00056   for (this = 1; this < NSIG; ++this)
00057     if (__sigismember (set, this))
00058       {
00059        /* Unblock this signal.  */
00060        __sigdelset (&tmp_mask, this);
00061 
00062        /* Register temporary action handler.  */
00063        if (__sigaction (this, &action, &saved[this]) != 0)
00064          goto restore_handler;
00065       }
00066 
00067   /* Now we can wait for signals.  */
00068   __sigsuspend (&tmp_mask);
00069 
00070  restore_handler:
00071   save_errno = errno;
00072 
00073   while (--this >= 1)
00074     if (__sigismember (set, this))
00075       /* We ignore errors here since we must restore all handlers.  */
00076       __sigaction (this, &saved[this], NULL);
00077 
00078   __set_errno (save_errno);
00079 
00080   /* Store the result and return.  */
00081   *sig = was_sig;
00082   return was_sig == -1 ? -1 : 0;
00083 }
00084 
00085 
00086 int
00087 __sigwait (const sigset_t *set, int *sig)
00088 {
00089   if (SINGLE_THREAD_P)
00090     return do_sigwait (set, sig);
00091 
00092   int oldtype = LIBC_CANCEL_ASYNC ();
00093 
00094   int result = do_sigwait (set, sig);
00095 
00096   LIBC_CANCEL_RESET (oldtype);
00097 
00098   return result;
00099 }
00100 libc_hidden_def (__sigwait)
00101 weak_alias (__sigwait, sigwait)
00102 
00103 
00104 static void
00105 ignore_signal (int sig)
00106 {
00107   /* Remember the signal.  */
00108   was_sig = sig;
00109 }