blob: 8246cb4af1b464fa6e4da6f7a15618e80d839bc5 [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"
Josh Gao6fcba932018-02-09 13:38:32 -080041#include "private/sigrtmin.h"
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080042
Elliott Hughes5905d6f2018-01-30 15:09:51 -080043extern "C" int __rt_sigpending(const sigset64_t*, size_t);
Elliott Hughes50080a22019-06-19 12:47:53 -070044extern "C" int __rt_sigqueueinfo(pid_t, int, siginfo_t*);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080045extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
46extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080047
Elliott Hughes5905d6f2018-01-30 15:09:51 -080048int 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
53int 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
58template <typename SigSetT>
59int SigAddSet(SigSetT* set, int sig) {
60 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080061 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080062 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080063 errno = EINVAL;
64 return -1;
65 }
66 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
67 return 0;
68}
69
Elliott Hughes5905d6f2018-01-30 15:09:51 -080070int sigaddset(sigset_t* set, int sig) {
71 return SigAddSet(set, sig);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080072}
73
Elliott Hughes5905d6f2018-01-30 15:09:51 -080074int 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.
79extern "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
87template <typename SigSetT>
88int SigDelSet(SigSetT* set, int sig) {
89 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080090 unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080091 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080092 errno = EINVAL;
93 return -1;
94 }
95 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
96 return 0;
97}
98
Elliott Hughes5905d6f2018-01-30 15:09:51 -080099int sigdelset(sigset_t* set, int sig) {
100 return SigDelSet(set, sig);
101}
102
103int sigdelset64(sigset64_t* set, int sig) {
104 return SigDelSet(set, sig);
105}
106
107template <typename SigSetT>
108int SigEmptySet(SigSetT* set) {
109 if (set == nullptr) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800110 errno = EINVAL;
111 return -1;
112 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800113 memset(set, 0, sizeof(*set));
114 return 0;
115}
116
117int sigemptyset(sigset_t* set) {
118 return SigEmptySet(set);
119}
120
121int sigemptyset64(sigset64_t* set) {
122 return SigEmptySet(set);
123}
124
125template <typename SigSetT>
126int SigFillSet(SigSetT* set) {
127 if (set == nullptr) {
128 errno = EINVAL;
129 return -1;
130 }
131 memset(set, 0xff, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800132 return 0;
133}
134
135int sigfillset(sigset_t* set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800136 return SigFillSet(set);
137}
138
139int sigfillset64(sigset64_t* set) {
140 return SigFillSet(set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800141}
142
143int sighold(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800144 sigset64_t set = {};
145 if (sigaddset64(&set, sig) == -1) return -1;
146 return sigprocmask64(SIG_BLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800147}
148
149int sigignore(int sig) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800150 struct sigaction64 sa = { .sa_handler = SIG_IGN };
151 return sigaction64(sig, &sa, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800152}
153
154int siginterrupt(int sig, int flag) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800155 struct sigaction64 act;
156 sigaction64(sig, nullptr, &act);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800157 if (flag) {
158 act.sa_flags &= ~SA_RESTART;
159 } else {
160 act.sa_flags |= SA_RESTART;
161 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800162 return sigaction64(sig, &act, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800163}
164
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800165template <typename SigSetT>
166int SigIsMember(const SigSetT* set, int sig) {
167 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800168 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800169 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800170 errno = EINVAL;
171 return -1;
172 }
173 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
174}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800176int sigismember(const sigset_t* set, int sig) {
177 return SigIsMember(set, sig);
178}
179
180int sigismember64(const sigset64_t* set, int sig) {
181 return SigIsMember(set, sig);
182}
183
184__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800185 struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
186 return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187}
188
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800189sighandler_t signal(int sig, sighandler_t handler) {
190 return _signal(sig, handler, SA_RESTART);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700191}
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800192
193int sigpause(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800194 sigset64_t set = {};
195 if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
196 return sigsuspend64(&set);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800197}
198
199int sigpending(sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800200 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
207int sigpending64(sigset64_t* set) {
208 return __rt_sigpending(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800209}
210
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800211int sigqueue(pid_t pid, int sig, const sigval value) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800212 siginfo_t info;
213 memset(&info, 0, sizeof(siginfo_t));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800214 info.si_signo = sig;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800215 info.si_code = SI_QUEUE;
216 info.si_pid = getpid();
217 info.si_uid = getuid();
218 info.si_value = value;
Elliott Hughes50080a22019-06-19 12:47:53 -0700219 return __rt_sigqueueinfo(pid, sig, &info);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800220}
221
222int sigrelse(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800223 sigset64_t set = {};
224 if (sigaddset64(&set, sig) == -1) return -1;
225 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800226}
227
228sighandler_t sigset(int sig, sighandler_t disp) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800229 struct sigaction64 new_sa;
230 if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800231
Elliott Hughes3e235912018-02-01 14:21:51 -0800232 struct sigaction64 old_sa;
233 if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800234 return SIG_ERR;
235 }
236
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800237 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) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800241 return SIG_ERR;
242 }
243
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800244 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800245}
246
247// This isn't in our header files, but is exposed on all architectures.
248extern "C" int sigsetmask(int mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800249 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;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800254}
255
256int sigsuspend(const sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800257 SigSetConverter set = {};
258 set.sigset = *bionic_set;
Josh Gao6fcba932018-02-09 13:38:32 -0800259 return sigsuspend64(&set.sigset64);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800260}
261
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800262int sigsuspend64(const sigset64_t* set) {
Josh Gao6fcba932018-02-09 13:38:32 -0800263 sigset64_t mutable_set;
264 sigset64_t* mutable_set_ptr = nullptr;
265 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700266 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800267 mutable_set_ptr = &mutable_set;
268 }
269 return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800270}
271
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800272int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
273 SigSetConverter set = {};
274 set.sigset = *bionic_set;
Josh Gao6fcba932018-02-09 13:38:32 -0800275 return sigtimedwait64(&set.sigset64, info, timeout);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800276}
277
278int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
Josh Gao6fcba932018-02-09 13:38:32 -0800279 sigset64_t mutable_set;
280 sigset64_t* mutable_set_ptr = nullptr;
281 if (set) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700282 mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
Josh Gao6fcba932018-02-09 13:38:32 -0800283 mutable_set_ptr = &mutable_set;
284 }
285 return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800286}
287
288int sigwait(const sigset_t* bionic_set, int* sig) {
289 SigSetConverter set = {};
290 set.sigset = *bionic_set;
291 return sigwait64(&set.sigset64, sig);
292}
293
294int sigwait64(const sigset64_t* set, int* sig) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800295 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.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800298 int result = sigtimedwait64(set, nullptr, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800299 if (result >= 0) {
300 *sig = result;
301 return 0;
302 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800303 if (errno != EAGAIN && errno != EINTR) return errno;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800304 }
305}
306
307int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800308 return sigtimedwait(set, info, nullptr);
309}
310
311int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
312 return sigtimedwait64(set, info, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800313}