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 <platform/bionic/reserved_signals.h>
40
41 #include "private/ErrnoRestorer.h"
42 #include "private/SigSetConverter.h"
43
44 extern "C" int __rt_sigpending(const sigset64_t*, size_t);
45 extern "C" int __rt_sigqueueinfo(pid_t, int, siginfo_t*);
46 extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
47 extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
48
pthread_sigmask(int how,const sigset_t * new_set,sigset_t * old_set)49 int pthread_sigmask(int how, const sigset_t* new_set, sigset_t* old_set) {
50 ErrnoRestorer errno_restorer;
51 return (sigprocmask(how, new_set, old_set) == -1) ? errno : 0;
52 }
53
pthread_sigmask64(int how,const sigset64_t * new_set,sigset64_t * old_set)54 int pthread_sigmask64(int how, const sigset64_t* new_set, sigset64_t* old_set) {
55 ErrnoRestorer errno_restorer;
56 return (sigprocmask64(how, new_set, old_set) == -1) ? errno : 0;
57 }
58
59 template <typename SigSetT>
SigAddSet(SigSetT * set,int sig)60 int SigAddSet(SigSetT* set, int sig) {
61 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
62 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
63 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
64 errno = EINVAL;
65 return -1;
66 }
67 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
68 return 0;
69 }
70
sigaddset(sigset_t * set,int sig)71 int sigaddset(sigset_t* set, int sig) {
72 return SigAddSet(set, sig);
73 }
74
sigaddset64(sigset64_t * set,int sig)75 int sigaddset64(sigset64_t* set, int sig) {
76 return SigAddSet(set, sig);
77 }
78
79 // This isn't in our header files, but is exposed on all architectures.
sigblock(int mask)80 extern "C" int sigblock(int mask) {
81 SigSetConverter in, out;
82 sigemptyset(&in.sigset);
83 in.bsd = mask;
84 if (sigprocmask(SIG_BLOCK, &in.sigset, &out.sigset) == -1) return -1;
85 return out.bsd;
86 }
87
88 template <typename SigSetT>
SigDelSet(SigSetT * set,int sig)89 int SigDelSet(SigSetT* set, int sig) {
90 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
91 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
92 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
93 errno = EINVAL;
94 return -1;
95 }
96 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
97 return 0;
98 }
99
sigdelset(sigset_t * set,int sig)100 int sigdelset(sigset_t* set, int sig) {
101 return SigDelSet(set, sig);
102 }
103
sigdelset64(sigset64_t * set,int sig)104 int sigdelset64(sigset64_t* set, int sig) {
105 return SigDelSet(set, sig);
106 }
107
108 template <typename SigSetT>
SigEmptySet(SigSetT * set)109 int SigEmptySet(SigSetT* set) {
110 if (set == nullptr) {
111 errno = EINVAL;
112 return -1;
113 }
114 memset(set, 0, sizeof(*set));
115 return 0;
116 }
117
sigemptyset(sigset_t * set)118 int sigemptyset(sigset_t* set) {
119 return SigEmptySet(set);
120 }
121
sigemptyset64(sigset64_t * set)122 int sigemptyset64(sigset64_t* set) {
123 return SigEmptySet(set);
124 }
125
126 template <typename SigSetT>
SigFillSet(SigSetT * set)127 int SigFillSet(SigSetT* set) {
128 if (set == nullptr) {
129 errno = EINVAL;
130 return -1;
131 }
132 memset(set, 0xff, sizeof(*set));
133 return 0;
134 }
135
sigfillset(sigset_t * set)136 int sigfillset(sigset_t* set) {
137 return SigFillSet(set);
138 }
139
sigfillset64(sigset64_t * set)140 int sigfillset64(sigset64_t* set) {
141 return SigFillSet(set);
142 }
143
sighold(int sig)144 int sighold(int sig) {
145 sigset64_t set = {};
146 if (sigaddset64(&set, sig) == -1) return -1;
147 return sigprocmask64(SIG_BLOCK, &set, nullptr);
148 }
149
sigignore(int sig)150 int sigignore(int sig) {
151 struct sigaction64 sa = { .sa_handler = SIG_IGN };
152 return sigaction64(sig, &sa, nullptr);
153 }
154
siginterrupt(int sig,int flag)155 int siginterrupt(int sig, int flag) {
156 struct sigaction64 act;
157 sigaction64(sig, nullptr, &act);
158 if (flag) {
159 act.sa_flags &= ~SA_RESTART;
160 } else {
161 act.sa_flags |= SA_RESTART;
162 }
163 return sigaction64(sig, &act, nullptr);
164 }
165
166 template <typename SigSetT>
SigIsMember(const SigSetT * set,int sig)167 int SigIsMember(const SigSetT* set, int sig) {
168 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
169 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
170 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
171 errno = EINVAL;
172 return -1;
173 }
174 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
175 }
176
sigismember(const sigset_t * set,int sig)177 int sigismember(const sigset_t* set, int sig) {
178 return SigIsMember(set, sig);
179 }
180
sigismember64(const sigset64_t * set,int sig)181 int sigismember64(const sigset64_t* set, int sig) {
182 return SigIsMember(set, sig);
183 }
184
_signal(int sig,sighandler_t handler,int flags)185 __LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
186 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
187 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
188 }
189
signal(int sig,sighandler_t handler)190 sighandler_t signal(int sig, sighandler_t handler) {
191 return _signal(sig, handler, SA_RESTART);
192 }
193
sigpause(int sig)194 int sigpause(int sig) {
195 sigset64_t set = {};
196 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
197 return sigsuspend64(&set);
198 }
199
sigpending(sigset_t * bionic_set)200 int sigpending(sigset_t* bionic_set) {
201 SigSetConverter set = {};
202 set.sigset = *bionic_set;
203 if (__rt_sigpending(&set.sigset64, sizeof(set.sigset64)) == -1) return -1;
204 *bionic_set = set.sigset;
205 return 0;
206 }
207
sigpending64(sigset64_t * set)208 int sigpending64(sigset64_t* set) {
209 return __rt_sigpending(set, sizeof(*set));
210 }
211
sigqueue(pid_t pid,int sig,const sigval value)212 int sigqueue(pid_t pid, int sig, const sigval value) {
213 siginfo_t info;
214 memset(&info, 0, sizeof(siginfo_t));
215 info.si_signo = sig;
216 info.si_code = SI_QUEUE;
217 info.si_pid = getpid();
218 info.si_uid = getuid();
219 info.si_value = value;
220 return __rt_sigqueueinfo(pid, sig, &info);
221 }
222
sigrelse(int sig)223 int sigrelse(int sig) {
224 sigset64_t set = {};
225 if (sigaddset64(&set, sig) == -1) return -1;
226 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
227 }
228
sigset(int sig,sighandler_t disp)229 sighandler_t sigset(int sig, sighandler_t disp) {
230 struct sigaction64 new_sa;
231 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
232
233 struct sigaction64 old_sa;
234 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
235 return SIG_ERR;
236 }
237
238 sigset64_t new_mask = {};
239 sigaddset64(&new_mask, sig);
240 sigset64_t old_mask;
241 if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
242 return SIG_ERR;
243 }
244
245 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
246 }
247
248 // This isn't in our header files, but is exposed on all architectures.
sigsetmask(int mask)249 extern "C" int sigsetmask(int mask) {
250 SigSetConverter in, out;
251 sigemptyset(&in.sigset);
252 in.bsd = mask;
253 if (sigprocmask(SIG_SETMASK, &in.sigset, &out.sigset) == -1) return -1;
254 return out.bsd;
255 }
256
sigsuspend(const sigset_t * bionic_set)257 int sigsuspend(const sigset_t* bionic_set) {
258 SigSetConverter set = {};
259 set.sigset = *bionic_set;
260 return sigsuspend64(&set.sigset64);
261 }
262
sigsuspend64(const sigset64_t * set)263 int sigsuspend64(const sigset64_t* set) {
264 sigset64_t mutable_set;
265 sigset64_t* mutable_set_ptr = nullptr;
266 if (set) {
267 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
268 mutable_set_ptr = &mutable_set;
269 }
270 return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
271 }
272
sigtimedwait(const sigset_t * bionic_set,siginfo_t * info,const timespec * timeout)273 int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
274 SigSetConverter set = {};
275 set.sigset = *bionic_set;
276 return sigtimedwait64(&set.sigset64, info, timeout);
277 }
278
sigtimedwait64(const sigset64_t * set,siginfo_t * info,const timespec * timeout)279 int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
280 sigset64_t mutable_set;
281 sigset64_t* mutable_set_ptr = nullptr;
282 if (set) {
283 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
284 mutable_set_ptr = &mutable_set;
285 }
286 return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
287 }
288
sigwait(const sigset_t * bionic_set,int * sig)289 int sigwait(const sigset_t* bionic_set, int* sig) {
290 SigSetConverter set = {};
291 set.sigset = *bionic_set;
292 return sigwait64(&set.sigset64, sig);
293 }
294
sigwait64(const sigset64_t * set,int * sig)295 int sigwait64(const sigset64_t* set, int* sig) {
296 while (true) {
297 // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
298 // around them since sigwait is only allowed to return EINVAL.
299 int result = sigtimedwait64(set, nullptr, nullptr);
300 if (result >= 0) {
301 *sig = result;
302 return 0;
303 }
304 if (errno != EAGAIN && errno != EINTR) return errno;
305 }
306 }
307
sigwaitinfo(const sigset_t * set,siginfo_t * info)308 int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
309 return sigtimedwait(set, info, nullptr);
310 }
311
sigwaitinfo64(const sigset64_t * set,siginfo_t * info)312 int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
313 return sigtimedwait64(set, info, nullptr);
314 }
315