blob: f2e19c25e3665bca9233d481cdd26cff3d8d24d8 [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
Elliott Hughes5905d6f2018-01-30 15:09:51 -080039#include "private/ErrnoRestorer.h"
40#include "private/SigSetConverter.h"
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080041
Elliott Hughes5905d6f2018-01-30 15:09:51 -080042extern "C" int __rt_sigpending(const sigset64_t*, size_t);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080043extern "C" int ___rt_sigqueueinfo(pid_t, int, siginfo_t*);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080044extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
45extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080046
Elliott Hughes5905d6f2018-01-30 15:09:51 -080047int pthread_sigmask(int how, const sigset_t* new_set, sigset_t* old_set) {
48 ErrnoRestorer errno_restorer;
49 return (sigprocmask(how, new_set, old_set) == -1) ? errno : 0;
50}
51
52int pthread_sigmask64(int how, const sigset64_t* new_set, sigset64_t* old_set) {
53 ErrnoRestorer errno_restorer;
54 return (sigprocmask64(how, new_set, old_set) == -1) ? errno : 0;
55}
56
57template <typename SigSetT>
58int SigAddSet(SigSetT* set, int sig) {
59 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080060 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080061 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080062 errno = EINVAL;
63 return -1;
64 }
65 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
66 return 0;
67}
68
Elliott Hughes5905d6f2018-01-30 15:09:51 -080069int sigaddset(sigset_t* set, int sig) {
70 return SigAddSet(set, sig);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080071}
72
Elliott Hughes5905d6f2018-01-30 15:09:51 -080073int sigaddset64(sigset64_t* set, int sig) {
74 return SigAddSet(set, sig);
75}
76
77// This isn't in our header files, but is exposed on all architectures.
78extern "C" int sigblock(int mask) {
79 SigSetConverter in, out;
80 sigemptyset(&in.sigset);
81 in.bsd = mask;
82 if (sigprocmask(SIG_BLOCK, &in.sigset, &out.sigset) == -1) return -1;
83 return out.bsd;
84}
85
86template <typename SigSetT>
87int SigDelSet(SigSetT* set, int sig) {
88 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080089 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080090 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080091 errno = EINVAL;
92 return -1;
93 }
94 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
95 return 0;
96}
97
Elliott Hughes5905d6f2018-01-30 15:09:51 -080098int sigdelset(sigset_t* set, int sig) {
99 return SigDelSet(set, sig);
100}
101
102int sigdelset64(sigset64_t* set, int sig) {
103 return SigDelSet(set, sig);
104}
105
106template <typename SigSetT>
107int SigEmptySet(SigSetT* set) {
108 if (set == nullptr) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800109 errno = EINVAL;
110 return -1;
111 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800112 memset(set, 0, sizeof(*set));
113 return 0;
114}
115
116int sigemptyset(sigset_t* set) {
117 return SigEmptySet(set);
118}
119
120int sigemptyset64(sigset64_t* set) {
121 return SigEmptySet(set);
122}
123
124template <typename SigSetT>
125int SigFillSet(SigSetT* set) {
126 if (set == nullptr) {
127 errno = EINVAL;
128 return -1;
129 }
130 memset(set, 0xff, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800131 return 0;
132}
133
134int sigfillset(sigset_t* set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800135 return SigFillSet(set);
136}
137
138int sigfillset64(sigset64_t* set) {
139 return SigFillSet(set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800140}
141
142int sighold(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800143 sigset64_t set = {};
144 if (sigaddset64(&set, sig) == -1) return -1;
145 return sigprocmask64(SIG_BLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800146}
147
148int sigignore(int sig) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800149 struct sigaction64 sa = { .sa_handler = SIG_IGN };
150 return sigaction64(sig, &sa, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800151}
152
153int siginterrupt(int sig, int flag) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800154 struct sigaction64 act;
155 sigaction64(sig, nullptr, &act);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800156 if (flag) {
157 act.sa_flags &= ~SA_RESTART;
158 } else {
159 act.sa_flags |= SA_RESTART;
160 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800161 return sigaction64(sig, &act, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800162}
163
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800164template <typename SigSetT>
165int SigIsMember(const SigSetT* set, int sig) {
166 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800167 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800168 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800169 errno = EINVAL;
170 return -1;
171 }
172 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
173}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800175int sigismember(const sigset_t* set, int sig) {
176 return SigIsMember(set, sig);
177}
178
179int sigismember64(const sigset64_t* set, int sig) {
180 return SigIsMember(set, sig);
181}
182
183__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800184 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
185 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186}
187
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800188sighandler_t signal(int sig, sighandler_t handler) {
189 return _signal(sig, handler, SA_RESTART);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700190}
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800191
192int sigpause(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800193 sigset64_t set = {};
194 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
195 return sigsuspend64(&set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800196}
197
198int sigpending(sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800199 SigSetConverter set = {};
200 set.sigset = *bionic_set;
201 if (__rt_sigpending(&set.sigset64, sizeof(set.sigset64)) == -1) return -1;
202 *bionic_set = set.sigset;
203 return 0;
204}
205
206int sigpending64(sigset64_t* set) {
207 return __rt_sigpending(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800208}
209
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800210int sigqueue(pid_t pid, int sig, const sigval value) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800211 siginfo_t info;
212 memset(&info, 0, sizeof(siginfo_t));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800213 info.si_signo = sig;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800214 info.si_code = SI_QUEUE;
215 info.si_pid = getpid();
216 info.si_uid = getuid();
217 info.si_value = value;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800218 return ___rt_sigqueueinfo(pid, sig, &info);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800219}
220
221int sigrelse(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800222 sigset64_t set = {};
223 if (sigaddset64(&set, sig) == -1) return -1;
224 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800225}
226
227sighandler_t sigset(int sig, sighandler_t disp) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800228 struct sigaction64 new_sa;
229 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800230
Elliott Hughes3e235912018-02-01 14:21:51 -0800231 struct sigaction64 old_sa;
232 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800233 return SIG_ERR;
234 }
235
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800236 sigset64_t new_mask = {};
237 sigaddset64(&new_mask, sig);
238 sigset64_t old_mask;
239 if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800240 return SIG_ERR;
241 }
242
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800243 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800244}
245
246// This isn't in our header files, but is exposed on all architectures.
247extern "C" int sigsetmask(int mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800248 SigSetConverter in, out;
249 sigemptyset(&in.sigset);
250 in.bsd = mask;
251 if (sigprocmask(SIG_SETMASK, &in.sigset, &out.sigset) == -1) return -1;
252 return out.bsd;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800253}
254
255int sigsuspend(const sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800256 SigSetConverter set = {};
257 set.sigset = *bionic_set;
258 return __rt_sigsuspend(&set.sigset64, sizeof(set.sigset64));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800259}
260
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800261int sigsuspend64(const sigset64_t* set) {
262 return __rt_sigsuspend(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800263}
264
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800265int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
266 SigSetConverter set = {};
267 set.sigset = *bionic_set;
268 return __rt_sigtimedwait(&set.sigset64, info, timeout, sizeof(set.sigset64));
269}
270
271int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
272 return __rt_sigtimedwait(set, info, timeout, sizeof(*set));
273}
274
275int sigwait(const sigset_t* bionic_set, int* sig) {
276 SigSetConverter set = {};
277 set.sigset = *bionic_set;
278 return sigwait64(&set.sigset64, sig);
279}
280
281int sigwait64(const sigset64_t* set, int* sig) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800282 while (true) {
283 // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
284 // around them since sigwait is only allowed to return EINVAL.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800285 int result = sigtimedwait64(set, nullptr, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800286 if (result >= 0) {
287 *sig = result;
288 return 0;
289 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800290 if (errno != EAGAIN && errno != EINTR) return errno;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800291 }
292}
293
294int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800295 return sigtimedwait(set, info, nullptr);
296}
297
298int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
299 return sigtimedwait64(set, info, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800300}