blob: 8fd7ea81f779022ffa9b467a2cddf56bbf356f7b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* vi: set sw=4 ts=4: */
2/* sigwait
3 *
4 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
5 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the GNU C Library; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA. */
21
22#include <errno.h>
23#include <signal.h>
24#include <string.h>
25#include <unistd.h>
26
27#ifdef __UCLIBC_HAS_THREADS_NATIVE__
28# include <sysdep-cancel.h>
29
30# ifdef __NR_rt_sigtimedwait
31
32/* Return any pending signal or wait for one for the given time. */
33static int do_sigwait(const sigset_t *set, int *sig)
34{
35 int ret;
36
37# ifdef SIGCANCEL
38 sigset_t tmpset;
39 if (set != NULL
40 && (__builtin_expect (__sigismember (set, SIGCANCEL), 0)
41# ifdef SIGSETXID
42 || __builtin_expect (__sigismember (set, SIGSETXID), 0)
43# endif
44 ))
45 {
46 /* Create a temporary mask without the bit for SIGCANCEL set. */
47 // We are not copying more than we have to.
48 memcpy(&tmpset, set, _NSIG / 8);
49 __sigdelset(&tmpset, SIGCANCEL);
50# ifdef SIGSETXID
51 __sigdelset(&tmpset, SIGSETXID);
52# endif
53 set = &tmpset;
54 }
55# endif
56
57 /* XXX The size argument hopefully will have to be changed to the
58 real size of the user-level sigset_t. */
59 INTERNAL_SYSCALL_DECL(err);
60 do
61 ret = INTERNAL_SYSCALL (rt_sigtimedwait, err, 4, set, NULL,
62 NULL, _NSIG / 8);
63 while (INTERNAL_SYSCALL_ERROR_P (ret, err)
64 && INTERNAL_SYSCALL_ERRNO (ret, err) == EINTR);
65 if (! INTERNAL_SYSCALL_ERROR_P (ret, err))
66 {
67 *sig = ret;
68 ret = 0;
69 }
70else
71 ret = INTERNAL_SYSCALL_ERRNO (ret, err);
72
73 return ret;
74}
75
76int sigwait (const sigset_t *set, int *sig)
77{
78 if(SINGLE_THREAD_P)
79 return do_sigwait(set, sig);
80
81 int oldtype = LIBC_CANCEL_ASYNC();
82
83 int result = do_sigwait(set, sig);
84
85 LIBC_CANCEL_RESET(oldtype);
86
87 return result;
88}
89# else /* __NR_rt_sigtimedwait */
90# error We must have rt_sigtimedwait defined!!!
91# endif
92#else /* __UCLIBC_HAS_THREADS_NATIVE__ */
93
94# if defined __UCLIBC_HAS_REALTIME__
95
96int sigwait (const sigset_t *set, int *sig)
97{
98 int ret = 1;
99 if ((ret = sigwaitinfo(set, NULL)) != -1) {
100 *sig = ret;
101 return 0;
102 }
103 return 1;
104}
105
106# else /* __UCLIBC_HAS_REALTIME__ */
107/* variant without REALTIME extensions */
108
109static smallint was_sig; /* obviously not thread-safe */
110
111static void ignore_signal(int sig)
112{
113 was_sig = sig;
114}
115
116int sigwait (const sigset_t *set, int *sig)
117{
118 sigset_t tmp_mask;
119 struct sigaction saved[NSIG];
120 struct sigaction action;
121 int save_errno;
122 int this;
123
124 /* Prepare set. */
125 __sigfillset (&tmp_mask);
126
127 /* Unblock all signals in the SET and register our nice handler. */
128 action.sa_handler = ignore_signal;
129 action.sa_flags = 0;
130 __sigfillset (&action.sa_mask); /* Block all signals for handler. */
131
132 /* Make sure we recognize error conditions by setting WAS_SIG to a
133 value which does not describe a legal signal number. */
134 was_sig = -1;
135
136 for (this = 1; this < NSIG; ++this)
137 if (__sigismember (set, this))
138 {
139 /* Unblock this signal. */
140 __sigdelset (&tmp_mask, this);
141
142 /* Register temporary action handler. */
143 /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
144 /* (so, will it work correctly if set has, say, SIGSTOP?) */
145 if (sigaction (this, &action, &saved[this]) != 0)
146 goto restore_handler;
147 }
148
149 /* Now we can wait for signals. */
150 sigsuspend (&tmp_mask);
151
152 restore_handler:
153 save_errno = errno;
154
155 while (--this >= 1)
156 if (__sigismember (set, this))
157 /* We ignore errors here since we must restore all handlers. */
158 sigaction (this, &saved[this], NULL);
159
160 __set_errno (save_errno);
161
162 /* Store the result and return. */
163 *sig = was_sig;
164 return was_sig == -1 ? -1 : 0;
165}
166# endif /* __UCLIBC_HAS_REALTIME__ */
167#endif /* __UCLIBC_HAS_THREADS_NATIVE__ */