1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <errno.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <sys/epoll.h>
34 #include <sys/signalfd.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <unistd.h>
38
39 #include "private/ErrnoRestorer.h"
40 #include "private/SigSetConverter.h"
41 #include "private/sigrtmin.h"
42
43 extern "C" int __rt_sigpending(const sigset64_t*, size_t);
44 extern "C" int ___rt_sigqueueinfo(pid_t, int, siginfo_t*);
45 extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
46 extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
47
pthread_sigmask(int how,const sigset_t * new_set,sigset_t * old_set)48 int pthread_sigmask(int how, const sigset_t* new_set, sigset_t* old_set) {
49 ErrnoRestorer errno_restorer;
50 return (sigprocmask(how, new_set, old_set) == -1) ? errno : 0;
51 }
52
pthread_sigmask64(int how,const sigset64_t * new_set,sigset64_t * old_set)53 int pthread_sigmask64(int how, const sigset64_t* new_set, sigset64_t* old_set) {
54 ErrnoRestorer errno_restorer;
55 return (sigprocmask64(how, new_set, old_set) == -1) ? errno : 0;
56 }
57
58 template <typename SigSetT>
SigAddSet(SigSetT * set,int sig)59 int SigAddSet(SigSetT* set, int sig) {
60 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
61 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
62 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
63 errno = EINVAL;
64 return -1;
65 }
66 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
67 return 0;
68 }
69
sigaddset(sigset_t * set,int sig)70 int sigaddset(sigset_t* set, int sig) {
71 return SigAddSet(set, sig);
72 }
73
sigaddset64(sigset64_t * set,int sig)74 int sigaddset64(sigset64_t* set, int sig) {
75 return SigAddSet(set, sig);
76 }
77
78 // This isn't in our header files, but is exposed on all architectures.
sigblock(int mask)79 extern "C" int sigblock(int mask) {
80 SigSetConverter in, out;
81 sigemptyset(&in.sigset);
82 in.bsd = mask;
83 if (sigprocmask(SIG_BLOCK, &in.sigset, &out.sigset) == -1) return -1;
84 return out.bsd;
85 }
86
87 template <typename SigSetT>
SigDelSet(SigSetT * set,int sig)88 int SigDelSet(SigSetT* set, int sig) {
89 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
90 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
91 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
92 errno = EINVAL;
93 return -1;
94 }
95 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
96 return 0;
97 }
98
sigdelset(sigset_t * set,int sig)99 int sigdelset(sigset_t* set, int sig) {
100 return SigDelSet(set, sig);
101 }
102
sigdelset64(sigset64_t * set,int sig)103 int sigdelset64(sigset64_t* set, int sig) {
104 return SigDelSet(set, sig);
105 }
106
107 template <typename SigSetT>
SigEmptySet(SigSetT * set)108 int SigEmptySet(SigSetT* set) {
109 if (set == nullptr) {
110 errno = EINVAL;
111 return -1;
112 }
113 memset(set, 0, sizeof(*set));
114 return 0;
115 }
116
sigemptyset(sigset_t * set)117 int sigemptyset(sigset_t* set) {
118 return SigEmptySet(set);
119 }
120
sigemptyset64(sigset64_t * set)121 int sigemptyset64(sigset64_t* set) {
122 return SigEmptySet(set);
123 }
124
125 template <typename SigSetT>
SigFillSet(SigSetT * set)126 int SigFillSet(SigSetT* set) {
127 if (set == nullptr) {
128 errno = EINVAL;
129 return -1;
130 }
131 memset(set, 0xff, sizeof(*set));
132 return 0;
133 }
134
sigfillset(sigset_t * set)135 int sigfillset(sigset_t* set) {
136 return SigFillSet(set);
137 }
138
sigfillset64(sigset64_t * set)139 int sigfillset64(sigset64_t* set) {
140 return SigFillSet(set);
141 }
142
sighold(int sig)143 int sighold(int sig) {
144 sigset64_t set = {};
145 if (sigaddset64(&set, sig) == -1) return -1;
146 return sigprocmask64(SIG_BLOCK, &set, nullptr);
147 }
148
sigignore(int sig)149 int sigignore(int sig) {
150 struct sigaction64 sa = { .sa_handler = SIG_IGN };
151 return sigaction64(sig, &sa, nullptr);
152 }
153
siginterrupt(int sig,int flag)154 int siginterrupt(int sig, int flag) {
155 struct sigaction64 act;
156 sigaction64(sig, nullptr, &act);
157 if (flag) {
158 act.sa_flags &= ~SA_RESTART;
159 } else {
160 act.sa_flags |= SA_RESTART;
161 }
162 return sigaction64(sig, &act, nullptr);
163 }
164
165 template <typename SigSetT>
SigIsMember(const SigSetT * set,int sig)166 int SigIsMember(const SigSetT* set, int sig) {
167 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
168 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
169 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
170 errno = EINVAL;
171 return -1;
172 }
173 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
174 }
175
sigismember(const sigset_t * set,int sig)176 int sigismember(const sigset_t* set, int sig) {
177 return SigIsMember(set, sig);
178 }
179
sigismember64(const sigset64_t * set,int sig)180 int sigismember64(const sigset64_t* set, int sig) {
181 return SigIsMember(set, sig);
182 }
183
_signal(int sig,sighandler_t handler,int flags)184 __LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
185 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
186 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
187 }
188
signal(int sig,sighandler_t handler)189 sighandler_t signal(int sig, sighandler_t handler) {
190 return _signal(sig, handler, SA_RESTART);
191 }
192
sigpause(int sig)193 int sigpause(int sig) {
194 sigset64_t set = {};
195 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
196 return sigsuspend64(&set);
197 }
198
sigpending(sigset_t * bionic_set)199 int sigpending(sigset_t* bionic_set) {
200 SigSetConverter set = {};
201 set.sigset = *bionic_set;
202 if (__rt_sigpending(&set.sigset64, sizeof(set.sigset64)) == -1) return -1;
203 *bionic_set = set.sigset;
204 return 0;
205 }
206
sigpending64(sigset64_t * set)207 int sigpending64(sigset64_t* set) {
208 return __rt_sigpending(set, sizeof(*set));
209 }
210
sigqueue(pid_t pid,int sig,const sigval value)211 int sigqueue(pid_t pid, int sig, const sigval value) {
212 siginfo_t info;
213 memset(&info, 0, sizeof(siginfo_t));
214 info.si_signo = sig;
215 info.si_code = SI_QUEUE;
216 info.si_pid = getpid();
217 info.si_uid = getuid();
218 info.si_value = value;
219 return ___rt_sigqueueinfo(pid, sig, &info);
220 }
221
sigrelse(int sig)222 int sigrelse(int sig) {
223 sigset64_t set = {};
224 if (sigaddset64(&set, sig) == -1) return -1;
225 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
226 }
227
sigset(int sig,sighandler_t disp)228 sighandler_t sigset(int sig, sighandler_t disp) {
229 struct sigaction64 new_sa;
230 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
231
232 struct sigaction64 old_sa;
233 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
234 return SIG_ERR;
235 }
236
237 sigset64_t new_mask = {};
238 sigaddset64(&new_mask, sig);
239 sigset64_t old_mask;
240 if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
241 return SIG_ERR;
242 }
243
244 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
245 }
246
247 // This isn't in our header files, but is exposed on all architectures.
sigsetmask(int mask)248 extern "C" int sigsetmask(int mask) {
249 SigSetConverter in, out;
250 sigemptyset(&in.sigset);
251 in.bsd = mask;
252 if (sigprocmask(SIG_SETMASK, &in.sigset, &out.sigset) == -1) return -1;
253 return out.bsd;
254 }
255
sigsuspend(const sigset_t * bionic_set)256 int sigsuspend(const sigset_t* bionic_set) {
257 SigSetConverter set = {};
258 set.sigset = *bionic_set;
259 return sigsuspend64(&set.sigset64);
260 }
261
sigsuspend64(const sigset64_t * set)262 int sigsuspend64(const sigset64_t* set) {
263 sigset64_t mutable_set;
264 sigset64_t* mutable_set_ptr = nullptr;
265 if (set) {
266 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
267 mutable_set_ptr = &mutable_set;
268 }
269 return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
270 }
271
sigtimedwait(const sigset_t * bionic_set,siginfo_t * info,const timespec * timeout)272 int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
273 SigSetConverter set = {};
274 set.sigset = *bionic_set;
275 return sigtimedwait64(&set.sigset64, info, timeout);
276 }
277
sigtimedwait64(const sigset64_t * set,siginfo_t * info,const timespec * timeout)278 int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
279 sigset64_t mutable_set;
280 sigset64_t* mutable_set_ptr = nullptr;
281 if (set) {
282 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
283 mutable_set_ptr = &mutable_set;
284 }
285 return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
286 }
287
sigwait(const sigset_t * bionic_set,int * sig)288 int sigwait(const sigset_t* bionic_set, int* sig) {
289 SigSetConverter set = {};
290 set.sigset = *bionic_set;
291 return sigwait64(&set.sigset64, sig);
292 }
293
sigwait64(const sigset64_t * set,int * sig)294 int sigwait64(const sigset64_t* set, int* sig) {
295 while (true) {
296 // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
297 // around them since sigwait is only allowed to return EINVAL.
298 int result = sigtimedwait64(set, nullptr, nullptr);
299 if (result >= 0) {
300 *sig = result;
301 return 0;
302 }
303 if (errno != EAGAIN && errno != EINTR) return errno;
304 }
305 }
306
sigwaitinfo(const sigset_t * set,siginfo_t * info)307 int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
308 return sigtimedwait(set, info, nullptr);
309 }
310
sigwaitinfo64(const sigset64_t * set,siginfo_t * info)311 int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
312 return sigtimedwait64(set, info, nullptr);
313 }
314