bump for release 1.0.29
[uclibc-ng.git] / libc / signal / sigwait.c
blob3557a039e8e2fb9e08e2b79ee001fbb061b7d992
1 /* sigwait
3 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
4 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with the GNU C Library; see the file COPYING.LIB. If
18 * not, see <http://www.gnu.org/licenses/>.
21 #define __need_NULL
22 #include <stddef.h>
23 #include <sys/syscall.h>
24 #include <signal.h>
25 #include <cancel.h>
27 #if defined __NR_rt_sigtimedwait && defined __UCLIBC_HAS_REALTIME__
29 #include <string.h>
31 /* Return any pending signal or wait for one for the given time. */
32 static int __NC(sigwait)(const sigset_t *set, int *sig)
34 int ret;
37 /* we might as well use sigtimedwait and do not care about cancellation */
38 ret = __NC(sigtimedwait)(set, NULL, NULL);
39 while (ret == -1 && errno == EINTR);
40 if (ret != -1) {
41 *sig = ret;
42 ret = 0;
43 } else
44 ret = errno;
46 return ret;
49 #else /* __NR_rt_sigtimedwait */
51 /* variant without REALTIME extensions */
52 #include <unistd.h> /* smallint */
54 static smallint was_sig; /* obviously not thread-safe */
56 static void ignore_signal(int sig)
58 was_sig = sig;
61 static int __NC(sigwait)(const sigset_t *set, int *sig)
63 sigset_t tmp_mask;
64 struct sigaction saved[NSIG];
65 struct sigaction action;
66 int save_errno;
67 int this;
69 /* Prepare set. */
70 __sigfillset (&tmp_mask);
72 /* Unblock all signals in the SET and register our nice handler. */
73 action.sa_handler = ignore_signal;
74 action.sa_flags = 0;
75 __sigfillset (&action.sa_mask); /* Block all signals for handler. */
77 /* Make sure we recognize error conditions by setting WAS_SIG to a
78 value which does not describe a legal signal number. */
79 was_sig = -1;
81 for (this = 1; this < NSIG; ++this)
82 if (__sigismember (set, this))
84 /* Unblock this signal. */
85 __sigdelset (&tmp_mask, this);
87 /* Register temporary action handler. */
88 /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
89 /* (so, will it work correctly if set has, say, SIGSTOP?) */
90 if (sigaction (this, &action, &saved[this]) != 0)
91 goto restore_handler;
94 /* Now we can wait for signals. */
95 __NC(sigsuspend)(&tmp_mask);
97 restore_handler:
98 save_errno = errno;
100 while (--this >= 1)
101 if (__sigismember (set, this))
102 /* We ignore errors here since we must restore all handlers. */
103 sigaction (this, &saved[this], NULL);
105 __set_errno (save_errno);
107 /* Store the result and return. */
108 *sig = was_sig;
109 return was_sig == -1 ? -1 : 0;
111 #endif /* __NR_rt_sigtimedwait */
113 CANCELLABLE_SYSCALL(int, sigwait, (const sigset_t *set, int *sig), (set, sig))