blob: b581b5a2e346e92fd1c0a6a78cb09113540b3074 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Elliott Hughesc7e9b232013-10-16 22:27:54 -070028
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080029#include <errno.h>
30#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <signal.h>
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080032#include <string.h>
Elliott Hughes5905d6f2018-01-30 15:09:51 -080033#include <sys/epoll.h>
34#include <sys/signalfd.h>
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080035#include <sys/types.h>
36#include <time.h>
37#include <unistd.h>
38
Josh Gao5074e7d2019-12-13 13:55:53 -080039#include <platform/bionic/reserved_signals.h>
40
Elliott Hughes5905d6f2018-01-30 15:09:51 -080041#include "private/ErrnoRestorer.h"
42#include "private/SigSetConverter.h"
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080043
Elliott Hughes5905d6f2018-01-30 15:09:51 -080044extern "C" int __rt_sigpending(const sigset64_t*, size_t);
Elliott Hughes50080a22019-06-19 12:47:53 -070045extern "C" int __rt_sigqueueinfo(pid_t, int, siginfo_t*);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080046extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
47extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080048
Elliott Hughes5905d6f2018-01-30 15:09:51 -080049int 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
54int 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
59template <typename SigSetT>
60int SigAddSet(SigSetT* set, int sig) {
61 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080062 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080063 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080064 errno = EINVAL;
65 return -1;
66 }
67 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
68 return 0;
69}
70
Elliott Hughes5905d6f2018-01-30 15:09:51 -080071int sigaddset(sigset_t* set, int sig) {
72 return SigAddSet(set, sig);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080073}
74
Elliott Hughes5905d6f2018-01-30 15:09:51 -080075int 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.
80extern "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
88template <typename SigSetT>
89int SigDelSet(SigSetT* set, int sig) {
90 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080091 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080092 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080093 errno = EINVAL;
94 return -1;
95 }
96 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
97 return 0;
98}
99
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800100int sigdelset(sigset_t* set, int sig) {
101 return SigDelSet(set, sig);
102}
103
104int sigdelset64(sigset64_t* set, int sig) {
105 return SigDelSet(set, sig);
106}
107
108template <typename SigSetT>
109int SigEmptySet(SigSetT* set) {
110 if (set == nullptr) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800111 errno = EINVAL;
112 return -1;
113 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800114 memset(set, 0, sizeof(*set));
115 return 0;
116}
117
118int sigemptyset(sigset_t* set) {
119 return SigEmptySet(set);
120}
121
122int sigemptyset64(sigset64_t* set) {
123 return SigEmptySet(set);
124}
125
126template <typename SigSetT>
127int SigFillSet(SigSetT* set) {
128 if (set == nullptr) {
129 errno = EINVAL;
130 return -1;
131 }
132 memset(set, 0xff, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800133 return 0;
134}
135
136int sigfillset(sigset_t* set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800137 return SigFillSet(set);
138}
139
140int sigfillset64(sigset64_t* set) {
141 return SigFillSet(set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800142}
143
144int sighold(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800145 sigset64_t set = {};
146 if (sigaddset64(&set, sig) == -1) return -1;
147 return sigprocmask64(SIG_BLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800148}
149
150int sigignore(int sig) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800151 struct sigaction64 sa = { .sa_handler = SIG_IGN };
152 return sigaction64(sig, &sa, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800153}
154
155int siginterrupt(int sig, int flag) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800156 struct sigaction64 act;
157 sigaction64(sig, nullptr, &act);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800158 if (flag) {
159 act.sa_flags &= ~SA_RESTART;
160 } else {
161 act.sa_flags |= SA_RESTART;
162 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800163 return sigaction64(sig, &act, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800164}
165
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800166template <typename SigSetT>
167int SigIsMember(const SigSetT* set, int sig) {
168 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800169 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800170 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800171 errno = EINVAL;
172 return -1;
173 }
174 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
175}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800177int sigismember(const sigset_t* set, int sig) {
178 return SigIsMember(set, sig);
179}
180
181int sigismember64(const sigset64_t* set, int sig) {
182 return SigIsMember(set, sig);
183}
184
185__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800186 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
187 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188}
189
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800190sighandler_t signal(int sig, sighandler_t handler) {
191 return _signal(sig, handler, SA_RESTART);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700192}
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800193
194int sigpause(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800195 sigset64_t set = {};
196 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
197 return sigsuspend64(&set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800198}
199
200int sigpending(sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800201 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
208int sigpending64(sigset64_t* set) {
209 return __rt_sigpending(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800210}
211
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800212int sigqueue(pid_t pid, int sig, const sigval value) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800213 siginfo_t info;
214 memset(&info, 0, sizeof(siginfo_t));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800215 info.si_signo = sig;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800216 info.si_code = SI_QUEUE;
217 info.si_pid = getpid();
218 info.si_uid = getuid();
219 info.si_value = value;
Elliott Hughes50080a22019-06-19 12:47:53 -0700220 return __rt_sigqueueinfo(pid, sig, &info);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800221}
222
223int sigrelse(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800224 sigset64_t set = {};
225 if (sigaddset64(&set, sig) == -1) return -1;
226 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800227}
228
229sighandler_t sigset(int sig, sighandler_t disp) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800230 struct sigaction64 new_sa;
231 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800232
Elliott Hughes3e235912018-02-01 14:21:51 -0800233 struct sigaction64 old_sa;
234 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800235 return SIG_ERR;
236 }
237
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800238 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) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800242 return SIG_ERR;
243 }
244
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800245 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800246}
247
248// This isn't in our header files, but is exposed on all architectures.
249extern "C" int sigsetmask(int mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800250 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;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800255}
256
257int sigsuspend(const sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800258 SigSetConverter set = {};
259 set.sigset = *bionic_set;
Josh Gao6fcba932018-02-09 13:38:32 -0800260 return sigsuspend64(&set.sigset64);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800261}
262
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800263int sigsuspend64(const sigset64_t* set) {
Josh Gao6fcba932018-02-09 13:38:32 -0800264 sigset64_t mutable_set;
265 sigset64_t* mutable_set_ptr = nullptr;
266 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700267 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800268 mutable_set_ptr = &mutable_set;
269 }
270 return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800271}
272
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800273int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
274 SigSetConverter set = {};
275 set.sigset = *bionic_set;
Josh Gao6fcba932018-02-09 13:38:32 -0800276 return sigtimedwait64(&set.sigset64, info, timeout);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800277}
278
279int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
Josh Gao6fcba932018-02-09 13:38:32 -0800280 sigset64_t mutable_set;
281 sigset64_t* mutable_set_ptr = nullptr;
282 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700283 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800284 mutable_set_ptr = &mutable_set;
285 }
286 return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800287}
288
289int sigwait(const sigset_t* bionic_set, int* sig) {
290 SigSetConverter set = {};
291 set.sigset = *bionic_set;
292 return sigwait64(&set.sigset64, sig);
293}
294
295int sigwait64(const sigset64_t* set, int* sig) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800296 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.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800299 int result = sigtimedwait64(set, nullptr, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800300 if (result >= 0) {
301 *sig = result;
302 return 0;
303 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800304 if (errno != EAGAIN && errno != EINTR) return errno;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800305 }
306}
307
308int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800309 return sigtimedwait(set, info, nullptr);
310}
311
312int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
313 return sigtimedwait64(set, info, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800314}