1 /*
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  */
28 
29 #include <errno.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <sys/epoll.h>
34 #include <sys/signalfd.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <unistd.h>
38 
39 #include <platform/bionic/reserved_signals.h>
40 
41 #include "private/ErrnoRestorer.h"
42 #include "private/SigSetConverter.h"
43 
44 extern "C" int __rt_sigpending(const sigset64_t*, size_t);
45 extern "C" int __rt_sigqueueinfo(pid_t, int, siginfo_t*);
46 extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
47 extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
48 
pthread_sigmask(int how,const sigset_t * new_set,sigset_t * old_set)49 int 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 
pthread_sigmask64(int how,const sigset64_t * new_set,sigset64_t * old_set)54 int 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 
59 template <typename SigSetT>
SigAddSet(SigSetT * set,int sig)60 int SigAddSet(SigSetT* set, int sig) {
61   int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
62   unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
63   if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
64     errno = EINVAL;
65     return -1;
66   }
67   local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
68   return 0;
69 }
70 
sigaddset(sigset_t * set,int sig)71 int sigaddset(sigset_t* set, int sig) {
72   return SigAddSet(set, sig);
73 }
74 
sigaddset64(sigset64_t * set,int sig)75 int sigaddset64(sigset64_t* set, int sig) {
76   return SigAddSet(set, sig);
77 }
78 
79 union 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.
sigblock(int mask)85 extern "C" int sigblock(int mask) {
86   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.
sigsetmask(int mask)92 extern "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;
96 }
97 
98 template <typename SigSetT>
SigDelSet(SigSetT * set,int sig)99 int SigDelSet(SigSetT* set, int sig) {
100   int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
101   unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
102   if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
103     errno = EINVAL;
104     return -1;
105   }
106   local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
107   return 0;
108 }
109 
sigdelset(sigset_t * set,int sig)110 int sigdelset(sigset_t* set, int sig) {
111   return SigDelSet(set, sig);
112 }
113 
sigdelset64(sigset64_t * set,int sig)114 int sigdelset64(sigset64_t* set, int sig) {
115   return SigDelSet(set, sig);
116 }
117 
118 template <typename SigSetT>
SigEmptySet(SigSetT * set)119 int SigEmptySet(SigSetT* set) {
120   if (set == nullptr) {
121     errno = EINVAL;
122     return -1;
123   }
124   memset(set, 0, sizeof(*set));
125   return 0;
126 }
127 
sigemptyset(sigset_t * set)128 int sigemptyset(sigset_t* set) {
129   return SigEmptySet(set);
130 }
131 
sigemptyset64(sigset64_t * set)132 int sigemptyset64(sigset64_t* set) {
133   return SigEmptySet(set);
134 }
135 
136 template <typename SigSetT>
SigFillSet(SigSetT * set)137 int SigFillSet(SigSetT* set) {
138   if (set == nullptr) {
139     errno = EINVAL;
140     return -1;
141   }
142   memset(set, 0xff, sizeof(*set));
143   return 0;
144 }
145 
sigfillset(sigset_t * set)146 int sigfillset(sigset_t* set) {
147   return SigFillSet(set);
148 }
149 
sigfillset64(sigset64_t * set)150 int sigfillset64(sigset64_t* set) {
151   return SigFillSet(set);
152 }
153 
sighold(int sig)154 int sighold(int sig) {
155   sigset64_t set = {};
156   if (sigaddset64(&set, sig) == -1) return -1;
157   return sigprocmask64(SIG_BLOCK, &set, nullptr);
158 }
159 
sigignore(int sig)160 int sigignore(int sig) {
161   struct sigaction64 sa = { .sa_handler = SIG_IGN };
162   return sigaction64(sig, &sa, nullptr);
163 }
164 
siginterrupt(int sig,int flag)165 int siginterrupt(int sig, int flag) {
166   struct sigaction64 act;
167   sigaction64(sig, nullptr, &act);
168   if (flag) {
169     act.sa_flags &= ~SA_RESTART;
170   } else {
171     act.sa_flags |= SA_RESTART;
172   }
173   return sigaction64(sig, &act, nullptr);
174 }
175 
176 template <typename SigSetT>
SigIsMember(const SigSetT * set,int sig)177 int SigIsMember(const SigSetT* set, int sig) {
178   int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
179   const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
180   if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
181     errno = EINVAL;
182     return -1;
183   }
184   return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
185 }
186 
sigismember(const sigset_t * set,int sig)187 int sigismember(const sigset_t* set, int sig) {
188   return SigIsMember(set, sig);
189 }
190 
sigismember64(const sigset64_t * set,int sig)191 int sigismember64(const sigset64_t* set, int sig) {
192   return SigIsMember(set, sig);
193 }
194 
_signal(int sig,sighandler_t handler,int flags)195 __LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
196   struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
197   return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
198 }
199 
signal(int sig,sighandler_t handler)200 sighandler_t signal(int sig, sighandler_t handler) {
201   return _signal(sig, handler, SA_RESTART);
202 }
203 
sigpause(int sig)204 int sigpause(int sig) {
205   sigset64_t set = {};
206   if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
207   return sigsuspend64(&set);
208 }
209 
sigpending(sigset_t * bionic_set)210 int sigpending(sigset_t* bionic_set) {
211   SigSetConverter set{bionic_set};
212   if (__rt_sigpending(set.ptr, sizeof(sigset64_t)) == -1) return -1;
213   set.copy_out();
214   return 0;
215 }
216 
sigpending64(sigset64_t * set)217 int sigpending64(sigset64_t* set) {
218   return __rt_sigpending(set, sizeof(*set));
219 }
220 
sigqueue(pid_t pid,int sig,const sigval value)221 int sigqueue(pid_t pid, int sig, const sigval value) {
222   siginfo_t info;
223   memset(&info, 0, sizeof(siginfo_t));
224   info.si_signo = sig;
225   info.si_code = SI_QUEUE;
226   info.si_pid = getpid();
227   info.si_uid = getuid();
228   info.si_value = value;
229   return __rt_sigqueueinfo(pid, sig, &info);
230 }
231 
sigrelse(int sig)232 int sigrelse(int sig) {
233   sigset64_t set = {};
234   if (sigaddset64(&set, sig) == -1) return -1;
235   return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
236 }
237 
sigset(int sig,sighandler_t disp)238 sighandler_t sigset(int sig, sighandler_t disp) {
239   struct sigaction64 new_sa;
240   if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
241 
242   struct sigaction64 old_sa;
243   if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
244     return SIG_ERR;
245   }
246 
247   sigset64_t new_mask = {};
248   sigaddset64(&new_mask, sig);
249   sigset64_t old_mask;
250   if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
251     return SIG_ERR;
252   }
253 
254   return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
255 }
256 
sigsuspend(const sigset_t * bionic_set)257 int sigsuspend(const sigset_t* bionic_set) {
258   SigSetConverter set{bionic_set};
259   return sigsuspend64(set.ptr);
260 }
261 
sigsuspend64(const sigset64_t * set)262 int sigsuspend64(const sigset64_t* set) {
263   sigset64_t mutable_set;
264   sigset64_t* mutable_set_ptr = nullptr;
265   if (set) {
266     mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
267     mutable_set_ptr = &mutable_set;
268   }
269   return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
270 }
271 
sigtimedwait(const sigset_t * bionic_set,siginfo_t * info,const timespec * timeout)272 int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
273   SigSetConverter set{bionic_set};
274   return sigtimedwait64(set.ptr, info, timeout);
275 }
276 
sigtimedwait64(const sigset64_t * set,siginfo_t * info,const timespec * timeout)277 int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
278   sigset64_t mutable_set;
279   sigset64_t* mutable_set_ptr = nullptr;
280   if (set) {
281     mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
282     mutable_set_ptr = &mutable_set;
283   }
284   return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
285 }
286 
sigwait(const sigset_t * bionic_set,int * sig)287 int sigwait(const sigset_t* bionic_set, int* sig) {
288   SigSetConverter set{bionic_set};
289   return sigwait64(set.ptr, sig);
290 }
291 
sigwait64(const sigset64_t * set,int * sig)292 int sigwait64(const sigset64_t* set, int* sig) {
293   while (true) {
294     // __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
295     // around them since sigwait is only allowed to return EINVAL.
296     int result = sigtimedwait64(set, nullptr, nullptr);
297     if (result >= 0) {
298       *sig = result;
299       return 0;
300     }
301     if (errno != EAGAIN && errno != EINTR) return errno;
302   }
303 }
304 
sigwaitinfo(const sigset_t * set,siginfo_t * info)305 int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
306   return sigtimedwait(set, info, nullptr);
307 }
308 
sigwaitinfo64(const sigset64_t * set,siginfo_t * info)309 int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
310   return sigtimedwait64(set, info, nullptr);
311 }
312