1 /* sigwait
2 *
3 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
4 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
5 *
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.
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 * Lesser General Public License for more details.
15 *
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/>.
19 */
20
21 #define __need_NULL
22 #include <stddef.h>
23 #include <sys/syscall.h>
24 #include <signal.h>
25 #include <cancel.h>
26
27 #if (defined(__NR_rt_sigtimedwait) || (defined(__UCLIBC_USE_TIME64__) && defined(__NR_rt_sigtimedwait_time64))) && \
28 defined(__UCLIBC_HAS_REALTIME__)
29
30 #include <string.h>
31
32 /* Return any pending signal or wait for one for the given time. */
__NC(sigwait)33 static int __NC(sigwait)(const sigset_t *set, int *sig)
34 {
35 int ret;
36
37 do
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;
46
47 return ret;
48 }
49
50 #else /* __NR_rt_sigtimedwait */
51
52 /* variant without REALTIME extensions */
53 #include <unistd.h> /* smallint */
54
55 static smallint was_sig; /* obviously not thread-safe */
56
ignore_signal(int sig)57 static void ignore_signal(int sig)
58 {
59 was_sig = sig;
60 }
61
__NC(sigwait)62 static int __NC(sigwait)(const sigset_t *set, int *sig)
63 {
64 sigset_t tmp_mask;
65 struct sigaction saved[NSIG];
66 struct sigaction action;
67 int save_errno;
68 int this;
69
70 /* Prepare set. */
71 __sigfillset (&tmp_mask);
72
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. */
77
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;
81
82 for (this = 1; this < NSIG; ++this)
83 if (__sigismember (set, this))
84 {
85 /* Unblock this signal. */
86 __sigdelset (&tmp_mask, this);
87
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;
93 }
94
95 /* Now we can wait for signals. */
96 __NC(sigsuspend)(&tmp_mask);
97
98 restore_handler:
99 save_errno = errno;
100
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);
105
106 __set_errno (save_errno);
107
108 /* Store the result and return. */
109 *sig = was_sig;
110 return was_sig == -1 ? -1 : 0;
111 }
112 #endif /* __NR_rt_sigtimedwait */
113
114 CANCELLABLE_SYSCALL(int, sigwait, (const sigset_t *set, int *sig), (set, sig))
115