blob: 5979ed73d84e83720660cc6453427dfa5eb4dd55 [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
Elliott Hughes215baed2023-07-17 17:15:01 -070079union BsdSigSet {
80 int mask;
81 sigset64_t set;
82};
83
84// This isn't in our header files, but is exposed on all architectures except riscv64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080085extern "C" int sigblock(int mask) {
Elliott Hughes215baed2023-07-17 17:15:01 -070086 BsdSigSet in{.mask = mask}, out;
87 if (sigprocmask64(SIG_BLOCK, &in.set, &out.set) == -1) return -1;
88 return out.mask;
89}
90
91// This isn't in our header files, but is exposed on all architectures except riscv64.
92extern "C" int sigsetmask(int mask) {
93 BsdSigSet in{.mask = mask}, out;
94 if (sigprocmask64(SIG_SETMASK, &in.set, &out.set) == -1) return -1;
95 return out.mask;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080096}
97
98template <typename SigSetT>
99int SigDelSet(SigSetT* set, int sig) {
100 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800101 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800102 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800103 errno = EINVAL;
104 return -1;
105 }
106 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
107 return 0;
108}
109
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800110int sigdelset(sigset_t* set, int sig) {
111 return SigDelSet(set, sig);
112}
113
114int sigdelset64(sigset64_t* set, int sig) {
115 return SigDelSet(set, sig);
116}
117
118template <typename SigSetT>
119int SigEmptySet(SigSetT* set) {
120 if (set == nullptr) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800121 errno = EINVAL;
122 return -1;
123 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800124 memset(set, 0, sizeof(*set));
125 return 0;
126}
127
128int sigemptyset(sigset_t* set) {
129 return SigEmptySet(set);
130}
131
132int sigemptyset64(sigset64_t* set) {
133 return SigEmptySet(set);
134}
135
136template <typename SigSetT>
137int SigFillSet(SigSetT* set) {
138 if (set == nullptr) {
139 errno = EINVAL;
140 return -1;
141 }
142 memset(set, 0xff, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800143 return 0;
144}
145
146int sigfillset(sigset_t* set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800147 return SigFillSet(set);
148}
149
150int sigfillset64(sigset64_t* set) {
151 return SigFillSet(set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800152}
153
154int sighold(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800155 sigset64_t set = {};
156 if (sigaddset64(&set, sig) == -1) return -1;
157 return sigprocmask64(SIG_BLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800158}
159
160int sigignore(int sig) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800161 struct sigaction64 sa = { .sa_handler = SIG_IGN };
162 return sigaction64(sig, &sa, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800163}
164
165int siginterrupt(int sig, int flag) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800166 struct sigaction64 act;
167 sigaction64(sig, nullptr, &act);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800168 if (flag) {
169 act.sa_flags &= ~SA_RESTART;
170 } else {
171 act.sa_flags |= SA_RESTART;
172 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800173 return sigaction64(sig, &act, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800174}
175
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800176template <typename SigSetT>
177int SigIsMember(const SigSetT* set, int sig) {
178 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800179 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800180 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800181 errno = EINVAL;
182 return -1;
183 }
184 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
185}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800187int sigismember(const sigset_t* set, int sig) {
188 return SigIsMember(set, sig);
189}
190
191int sigismember64(const sigset64_t* set, int sig) {
192 return SigIsMember(set, sig);
193}
194
195__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800196 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
197 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198}
199
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800200sighandler_t signal(int sig, sighandler_t handler) {
201 return _signal(sig, handler, SA_RESTART);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700202}
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800203
204int sigpause(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800205 sigset64_t set = {};
206 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
207 return sigsuspend64(&set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800208}
209
210int sigpending(sigset_t* bionic_set) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700211 SigSetConverter set{bionic_set};
212 if (__rt_sigpending(set.ptr, sizeof(sigset64_t)) == -1) return -1;
213 set.copy_out();
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800214 return 0;
215}
216
217int sigpending64(sigset64_t* set) {
218 return __rt_sigpending(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800219}
220
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800221int sigqueue(pid_t pid, int sig, const sigval value) {
Elliott Hughes3bf404c2024-08-02 17:33:54 +0000222 siginfo_t info = { .si_code = SI_QUEUE };
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800223 info.si_signo = sig;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800224 info.si_pid = getpid();
225 info.si_uid = getuid();
226 info.si_value = value;
Elliott Hughes50080a22019-06-19 12:47:53 -0700227 return __rt_sigqueueinfo(pid, sig, &info);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800228}
229
230int sigrelse(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800231 sigset64_t set = {};
232 if (sigaddset64(&set, sig) == -1) return -1;
233 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800234}
235
236sighandler_t sigset(int sig, sighandler_t disp) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800237 struct sigaction64 new_sa;
238 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800239
Elliott Hughes3e235912018-02-01 14:21:51 -0800240 struct sigaction64 old_sa;
241 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800242 return SIG_ERR;
243 }
244
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800245 sigset64_t new_mask = {};
246 sigaddset64(&new_mask, sig);
247 sigset64_t old_mask;
248 if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800249 return SIG_ERR;
250 }
251
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800252 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800253}
254
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800255int sigsuspend(const sigset_t* bionic_set) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700256 SigSetConverter set{bionic_set};
257 return sigsuspend64(set.ptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800258}
259
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800260int sigsuspend64(const sigset64_t* set) {
Josh Gao6fcba932018-02-09 13:38:32 -0800261 sigset64_t mutable_set;
262 sigset64_t* mutable_set_ptr = nullptr;
263 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700264 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800265 mutable_set_ptr = &mutable_set;
266 }
267 return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800268}
269
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800270int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700271 SigSetConverter set{bionic_set};
272 return sigtimedwait64(set.ptr, info, timeout);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800273}
274
275int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
Josh Gao6fcba932018-02-09 13:38:32 -0800276 sigset64_t mutable_set;
277 sigset64_t* mutable_set_ptr = nullptr;
278 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700279 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800280 mutable_set_ptr = &mutable_set;
281 }
282 return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800283}
284
285int sigwait(const sigset_t* bionic_set, int* sig) {
Elliott Hughes215baed2023-07-17 17:15:01 -0700286 SigSetConverter set{bionic_set};
287 return sigwait64(set.ptr, sig);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800288}
289
290int sigwait64(const sigset64_t* set, int* sig) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800291 while (true) {
292 // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
293 // around them since sigwait is only allowed to return EINVAL.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800294 int result = sigtimedwait64(set, nullptr, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800295 if (result >= 0) {
296 *sig = result;
297 return 0;
298 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800299 if (errno != EAGAIN && errno != EINTR) return errno;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800300 }
301}
302
303int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800304 return sigtimedwait(set, info, nullptr);
305}
306
307int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
308 return sigtimedwait64(set, info, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800309}