package/uclibc: Fix removal of libintl.h
[uclibc-ng.git] / libc / signal / sigwait.c
blob3e7286566862696626950c7eb89f910cdbcee7d9
1 /* vi: set sw=4 ts=4: */
2 /* sigwait
4 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
5 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the GNU C Library; see the file COPYING.LIB. If
19 * not, see <http://www.gnu.org/licenses/>.
22 #define __need_NULL
23 #include <stddef.h>
24 #include <sys/syscall.h>
25 #include <signal.h>
26 #include <cancel.h>
28 #if defined __NR_rt_sigtimedwait && defined __UCLIBC_HAS_REALTIME__
30 #include <string.h>
32 /* Return any pending signal or wait for one for the given time. */
33 static int __NC(sigwait)(const sigset_t *set, int *sig)
35 int ret;
38 /* we might as well use sigtimedwait and do not care about cancellation */
39 ret = __NC(sigtimedwait)(set, NULL, NULL);
40 while (ret == -1 && errno == EINTR);
41 if (ret != -1) {
42 *sig = ret;
43 ret = 0;
44 } else
45 ret = errno;
47 return ret;
50 #else /* __NR_rt_sigtimedwait */
52 /* variant without REALTIME extensions */
53 #include <unistd.h> /* smallint */
55 static smallint was_sig; /* obviously not thread-safe */
57 static void ignore_signal(int sig)
59 was_sig = sig;
62 static int __NC(sigwait)(const sigset_t *set, int *sig)
64 sigset_t tmp_mask;
65 struct sigaction saved[NSIG];
66 struct sigaction action;
67 int save_errno;
68 int this;
70 /* Prepare set. */
71 __sigfillset (&tmp_mask);
73 /* Unblock all signals in the SET and register our nice handler. */
74 action.sa_handler = ignore_signal;
75 action.sa_flags = 0;
76 __sigfillset (&action.sa_mask); /* Block all signals for handler. */
78 /* Make sure we recognize error conditions by setting WAS_SIG to a
79 value which does not describe a legal signal number. */
80 was_sig = -1;
82 for (this = 1; this < NSIG; ++this)
83 if (__sigismember (set, this))
85 /* Unblock this signal. */
86 __sigdelset (&tmp_mask, this);
88 /* Register temporary action handler. */
89 /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
90 /* (so, will it work correctly if set has, say, SIGSTOP?) */
91 if (sigaction (this, &action, &saved[this]) != 0)
92 goto restore_handler;
95 /* Now we can wait for signals. */
96 __NC(sigsuspend)(&tmp_mask);
98 restore_handler:
99 save_errno = errno;
101 while (--this >= 1)
102 if (__sigismember (set, this))
103 /* We ignore errors here since we must restore all handlers. */
104 sigaction (this, &saved[this], NULL);
106 __set_errno (save_errno);
108 /* Store the result and return. */
109 *sig = was_sig;
110 return was_sig == -1 ? -1 : 0;
112 #endif /* __NR_rt_sigtimedwait */
114 CANCELLABLE_SYSCALL(int, sigwait, (const sigset_t *set, int *sig), (set, sig))