blob: 099944ad882766b7cc57dcb3599c7b1f8e3f2b9e [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);
43extern "C" int __rt_sigprocmask(int, const sigset64_t*, sigset64_t*, size_t);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -080044extern "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) {
150 struct sigaction sa;
151 memset(&sa, 0, sizeof(sa));
152 if (sigemptyset(&sa.sa_mask) == -1) return -1;
153 sa.sa_handler = SIG_IGN;
154 return sigaction(sig, &sa, nullptr);
155}
156
157int siginterrupt(int sig, int flag) {
158 struct sigaction act;
159 sigaction(sig, nullptr, &act);
160 if (flag) {
161 act.sa_flags &= ~SA_RESTART;
162 } else {
163 act.sa_flags |= SA_RESTART;
164 }
165 return sigaction(sig, &act, nullptr);
166}
167
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800168template <typename SigSetT>
169int SigIsMember(const SigSetT* set, int sig) {
170 int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800171 const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800172 if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800173 errno = EINVAL;
174 return -1;
175 }
176 return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
177}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800179int sigismember(const sigset_t* set, int sig) {
180 return SigIsMember(set, sig);
181}
182
183int sigismember64(const sigset64_t* set, int sig) {
184 return SigIsMember(set, sig);
185}
186
187__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700188 struct sigaction sa;
189 sigemptyset(&sa.sa_mask);
190 sa.sa_handler = handler;
191 sa.sa_flags = flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800193 if (sigaction(sig, &sa, &sa) == -1) {
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700194 return SIG_ERR;
195 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
Elliott Hughes8b5df392015-01-21 16:19:07 -0800197 return 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 Hughes5905d6f2018-01-30 15:09:51 -0800211 SigSetConverter set = {};
212 set.sigset = *bionic_set;
213 if (__rt_sigpending(&set.sigset64, sizeof(set.sigset64)) == -1) return -1;
214 *bionic_set = set.sigset;
215 return 0;
216}
217
218int sigpending64(sigset64_t* set) {
219 return __rt_sigpending(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800220}
221
222int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800223 SigSetConverter new_set;
224 sigset64_t* new_set_ptr = nullptr;
225 if (bionic_new_set != nullptr) {
226 sigemptyset64(&new_set.sigset64);
227 new_set.sigset = *bionic_new_set;
228 new_set_ptr = &new_set.sigset64;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800229 }
230
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800231 SigSetConverter old_set;
232 if (sigprocmask64(how, new_set_ptr, &old_set.sigset64) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800233 return -1;
234 }
235
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800236 if (bionic_old_set != nullptr) {
237 *bionic_old_set = old_set.sigset;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800238 }
239
240 return 0;
241}
242
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800243int sigprocmask64(int how, const sigset64_t* new_set, sigset64_t* old_set) {
244 return __rt_sigprocmask(how, new_set, old_set, sizeof(*new_set));
245}
246
247int sigqueue(pid_t pid, int sig, const sigval value) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800248 siginfo_t info;
249 memset(&info, 0, sizeof(siginfo_t));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800250 info.si_signo = sig;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800251 info.si_code = SI_QUEUE;
252 info.si_pid = getpid();
253 info.si_uid = getuid();
254 info.si_value = value;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800255 return ___rt_sigqueueinfo(pid, sig, &info);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800256}
257
258int sigrelse(int sig) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800259 sigset64_t set = {};
260 if (sigaddset64(&set, sig) == -1) return -1;
261 return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800262}
263
264sighandler_t sigset(int sig, sighandler_t disp) {
265 struct sigaction new_sa;
266 if (disp != SIG_HOLD) {
267 memset(&new_sa, 0, sizeof(new_sa));
268 new_sa.sa_handler = disp;
269 sigemptyset(&new_sa.sa_mask);
270 }
271
272 struct sigaction old_sa;
273 if (sigaction(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
274 return SIG_ERR;
275 }
276
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800277 sigset64_t new_mask = {};
278 sigaddset64(&new_mask, sig);
279 sigset64_t old_mask;
280 if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800281 return SIG_ERR;
282 }
283
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800284 return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800285}
286
287// This isn't in our header files, but is exposed on all architectures.
288extern "C" int sigsetmask(int mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800289 SigSetConverter in, out;
290 sigemptyset(&in.sigset);
291 in.bsd = mask;
292 if (sigprocmask(SIG_SETMASK, &in.sigset, &out.sigset) == -1) return -1;
293 return out.bsd;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800294}
295
296int sigsuspend(const sigset_t* bionic_set) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800297 SigSetConverter set = {};
298 set.sigset = *bionic_set;
299 return __rt_sigsuspend(&set.sigset64, sizeof(set.sigset64));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800300}
301
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800302int sigsuspend64(const sigset64_t* set) {
303 return __rt_sigsuspend(set, sizeof(*set));
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800304}
305
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800306int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
307 SigSetConverter set = {};
308 set.sigset = *bionic_set;
309 return __rt_sigtimedwait(&set.sigset64, info, timeout, sizeof(set.sigset64));
310}
311
312int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
313 return __rt_sigtimedwait(set, info, timeout, sizeof(*set));
314}
315
316int sigwait(const sigset_t* bionic_set, int* sig) {
317 SigSetConverter set = {};
318 set.sigset = *bionic_set;
319 return sigwait64(&set.sigset64, sig);
320}
321
322int sigwait64(const sigset64_t* set, int* sig) {
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800323 while (true) {
324 // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
325 // around them since sigwait is only allowed to return EINVAL.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800326 int result = sigtimedwait64(set, nullptr, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800327 if (result >= 0) {
328 *sig = result;
329 return 0;
330 }
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800331 if (errno != EAGAIN && errno != EINTR) return errno;
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800332 }
333}
334
335int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800336 return sigtimedwait(set, info, nullptr);
337}
338
339int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
340 return sigtimedwait64(set, info, nullptr);
Elliott Hughes6dafb4a2018-01-26 17:47:56 -0800341}