Update.
[platform/upstream/glibc.git] / sysdeps / posix / sigwait.c
1 /* Implementation of sigwait function from POSIX.1c.
2    Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <errno.h>
22 #include <signal.h>
23 #include <stddef.h>             /* For NULL.  */
24
25 /* This is our dummy signal handler we use here.  */
26 static void ignore_signal (int sig);
27
28 /* Place where to remember which signal we got.  Please note that this
29    implementation cannot be used for the threaded libc.  The
30    libpthread must provide an own version.  */
31 static int was_sig;
32
33
34 int
35 __sigwait (const sigset_t *set, int *sig)
36 {
37   sigset_t tmp_mask;
38   struct sigaction saved[NSIG];
39   struct sigaction action;
40   int save_errno;
41   int this;
42
43   /* Prepare set.  */
44   __sigfillset (&tmp_mask);
45
46   /* Unblock all signals in the SET and register our nice handler.  */
47   action.sa_handler = ignore_signal;
48   action.sa_flags = 0;
49   __sigfillset (&action.sa_mask);       /* Block all signals for handler.  */
50
51   /* Make sure we recognize error conditions by setting WAS_SIG to a
52      value which does not describe a legal signal number.  */
53   was_sig = -1;
54
55   for (this = 1; this < NSIG; ++this)
56     if (__sigismember (set, this))
57       {
58         /* Unblock this signal.  */
59         __sigdelset (&tmp_mask, this);
60
61         /* Register temporary action handler.  */
62         if (__sigaction (this, &action, &saved[this]) != 0)
63           goto restore_handler;
64       }
65
66   /* Now we can wait for signals.  */
67   __sigsuspend (&tmp_mask);
68
69  restore_handler:
70   save_errno = errno;
71
72   while (--this >= 1)
73     if (__sigismember (set, this))
74       /* We ignore errors here since we must restore all handlers.  */
75       __sigaction (this, &saved[this], NULL);
76
77   __set_errno (save_errno);
78
79   /* Store the result and return.  */
80   *sig = was_sig;
81   return was_sig == -1 ? -1 : 0;
82 }
83 weak_alias (__sigwait, sigwait)
84
85
86 static void
87 ignore_signal (int sig)
88 {
89   /* Remember the signal.  */
90   was_sig = sig;
91 }