blob: f58fe213b3d04f86839918bdb8c63da5fc2c45ee [file] [log] [blame]
Dave Allisonf4b80bc2014-05-14 15:41:25 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dave Allisonf4b80bc2014-05-14 15:41:25 -070017#include <dlfcn.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070018#include <errno.h>
Josh Gao99875e92017-04-17 15:58:36 -070019#include <pthread.h>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070020#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070023#include <string.h>
Josh Gao85a78cf2017-03-20 16:26:42 -070024
Josh Gaod32d79d2018-02-26 14:29:25 -080025#include <algorithm>
Luis Hector Chavez33cac0f2017-04-06 14:18:09 -070026#include <initializer_list>
Igor Murashkin5573c372017-11-16 13:34:30 -080027#include <mutex>
Josh Gaod32d79d2018-02-26 14:29:25 -080028#include <type_traits>
Josh Gao85a78cf2017-03-20 16:26:42 -070029#include <utility>
Dave Allisonf4b80bc2014-05-14 15:41:25 -070030
Josh Gaocca6fc02018-03-12 16:37:21 -070031#include "log.h"
Dave Allisoncefcea82014-09-16 10:01:01 -070032#include "sigchain.h"
33
Dave Allison69dfe512014-07-11 17:11:58 +000034#if defined(__APPLE__)
35#define _NSIG NSIG
Dave Allison38680092014-08-29 12:29:34 -070036#define sighandler_t sig_t
Josh Gaodd241ae2017-03-20 14:17:23 -070037
38// Darwin has an #error when ucontext.h is included without _XOPEN_SOURCE defined.
39#define _XOPEN_SOURCE
Dave Allison69dfe512014-07-11 17:11:58 +000040#endif
41
Josh Gaodd241ae2017-03-20 14:17:23 -070042#include <ucontext.h>
43
Josh Gao85a78cf2017-03-20 16:26:42 -070044// libsigchain provides an interception layer for signal handlers, to allow ART and others to give
45// their signal handlers the first stab at handling signals before passing them on to user code.
46//
47// It implements wrapper functions for signal, sigaction, and sigprocmask, and a handler that
48// forwards signals appropriately.
49//
50// In our handler, we start off with all signals blocked, fetch the original signal mask from the
51// passed in ucontext, and then adjust our signal mask appropriately for the user handler.
52//
53// It's somewhat tricky for us to properly handle some flag cases:
54// SA_NOCLDSTOP and SA_NOCLDWAIT: shouldn't matter, we don't have special handlers for SIGCHLD.
55// SA_NODEFER: unimplemented, we can manually change the signal mask appropriately.
56// ~SA_ONSTACK: always silently enable this
57// SA_RESETHAND: unimplemented, but we can probably do this?
58// ~SA_RESTART: unimplemented, maybe we can reserve an RT signal, register an empty handler that
59// doesn't have SA_RESTART, and raise the signal to avoid restarting syscalls that are
60// expected to be interrupted?
Dave Allisonf4b80bc2014-05-14 15:41:25 -070061
Vladimir Markobe0d3cf2020-02-12 10:52:22 +000062#if defined(__BIONIC__) && !defined(__LP64__)
Josh Gaod32d79d2018-02-26 14:29:25 -080063static int sigismember(const sigset64_t* sigset, int signum) {
64 return sigismember64(sigset, signum);
65}
66
67static int sigemptyset(sigset64_t* sigset) {
68 return sigemptyset64(sigset);
69}
70
71static int sigaddset(sigset64_t* sigset, int signum) {
72 return sigaddset64(sigset, signum);
73}
74
75static int sigdelset(sigset64_t* sigset, int signum) {
76 return sigdelset64(sigset, signum);
77}
78#endif
79
80template<typename SigsetType>
81static int sigorset(SigsetType* dest, SigsetType* left, SigsetType* right) {
Josh Gao85a78cf2017-03-20 16:26:42 -070082 sigemptyset(dest);
Josh Gaod32d79d2018-02-26 14:29:25 -080083 for (size_t i = 0; i < sizeof(SigsetType) * CHAR_BIT; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -070084 if (sigismember(left, i) == 1 || sigismember(right, i) == 1) {
85 sigaddset(dest, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -070086 }
Josh Gao85a78cf2017-03-20 16:26:42 -070087 }
88 return 0;
89}
90
91namespace art {
92
93static decltype(&sigaction) linked_sigaction;
94static decltype(&sigprocmask) linked_sigprocmask;
Josh Gao99875e92017-04-17 15:58:36 -070095
Josh Gaod32d79d2018-02-26 14:29:25 -080096#if defined(__BIONIC__)
97static decltype(&sigaction64) linked_sigaction64;
98static decltype(&sigprocmask64) linked_sigprocmask64;
99#endif
100
Josh Gao6e4deea2019-07-18 15:33:16 -0700101template <typename T>
102static void lookup_libc_symbol(T* output, T wrapper, const char* name) {
103#if defined(__BIONIC__)
104 constexpr const char* libc_name = "libc.so";
105#elif defined(__GLIBC__)
106#if __GNU_LIBRARY__ != 6
107#error unsupported glibc version
108#endif
109 constexpr const char* libc_name = "libc.so.6";
110#else
111#error unsupported libc: not bionic or glibc?
112#endif
113
114 static void* libc = []() {
115 void* result = dlopen(libc_name, RTLD_LOCAL | RTLD_LAZY);
116 if (!result) {
117 fatal("failed to dlopen %s: %s", libc_name, dlerror());
118 }
119 return result;
120 }();
121
122 void* sym = dlsym(libc, name); // NOLINT glibc triggers cert-dcl16-c with RTLD_NEXT.
Josh Gaod32d79d2018-02-26 14:29:25 -0800123 if (sym == nullptr) {
124 sym = dlsym(RTLD_DEFAULT, name);
125 if (sym == wrapper || sym == sigaction) {
126 fatal("Unable to find next %s in signal chain", name);
127 }
128 }
129 *output = reinterpret_cast<T>(sym);
130}
131
Josh Gaofd4d0d32017-04-26 19:09:47 -0700132__attribute__((constructor)) static void InitializeSignalChain() {
133 static std::once_flag once;
134 std::call_once(once, []() {
Josh Gao6e4deea2019-07-18 15:33:16 -0700135 lookup_libc_symbol(&linked_sigaction, sigaction, "sigaction");
136 lookup_libc_symbol(&linked_sigprocmask, sigprocmask, "sigprocmask");
Josh Gaofd4d0d32017-04-26 19:09:47 -0700137
Josh Gaod32d79d2018-02-26 14:29:25 -0800138#if defined(__BIONIC__)
Josh Gao6e4deea2019-07-18 15:33:16 -0700139 lookup_libc_symbol(&linked_sigaction64, sigaction64, "sigaction64");
140 lookup_libc_symbol(&linked_sigprocmask64, sigprocmask64, "sigprocmask64");
Josh Gaod32d79d2018-02-26 14:29:25 -0800141#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700142 });
143}
144
Josh Gao99875e92017-04-17 15:58:36 -0700145static pthread_key_t GetHandlingSignalKey() {
146 static pthread_key_t key;
147 static std::once_flag once;
148 std::call_once(once, []() {
149 int rc = pthread_key_create(&key, nullptr);
150 if (rc != 0) {
151 fatal("failed to create sigchain pthread key: %s", strerror(rc));
152 }
153 });
154 return key;
155}
156
157static bool GetHandlingSignal() {
158 void* result = pthread_getspecific(GetHandlingSignalKey());
159 return reinterpret_cast<uintptr_t>(result);
160}
161
162static void SetHandlingSignal(bool value) {
163 pthread_setspecific(GetHandlingSignalKey(),
164 reinterpret_cast<void*>(static_cast<uintptr_t>(value)));
165}
166
167class ScopedHandlingSignal {
168 public:
169 ScopedHandlingSignal() : original_value_(GetHandlingSignal()) {
170 }
171
172 ~ScopedHandlingSignal() {
173 SetHandlingSignal(original_value_);
174 }
175
176 private:
177 bool original_value_;
178};
Josh Gao85a78cf2017-03-20 16:26:42 -0700179
180class SignalChain {
181 public:
182 SignalChain() : claimed_(false) {
Jin Qian33dca562017-03-18 02:51:37 +0000183 }
184
Josh Gao85a78cf2017-03-20 16:26:42 -0700185 bool IsClaimed() {
186 return claimed_;
187 }
188
189 void Claim(int signo) {
190 if (!claimed_) {
191 Register(signo);
192 claimed_ = true;
Jin Qian33dca562017-03-18 02:51:37 +0000193 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700194 }
195
196 // Register the signal chain with the kernel if needed.
197 void Register(int signo) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800198#if defined(__BIONIC__)
199 struct sigaction64 handler_action = {};
200 sigfillset64(&handler_action.sa_mask);
201#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700202 struct sigaction handler_action = {};
Josh Gaod32d79d2018-02-26 14:29:25 -0800203 sigfillset(&handler_action.sa_mask);
204#endif
205
Josh Gao85a78cf2017-03-20 16:26:42 -0700206 handler_action.sa_sigaction = SignalChain::Handler;
207 handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Josh Gaod32d79d2018-02-26 14:29:25 -0800208
209#if defined(__BIONIC__)
210 linked_sigaction64(signo, &handler_action, &action_);
211#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700212 linked_sigaction(signo, &handler_action, &action_);
Josh Gaod32d79d2018-02-26 14:29:25 -0800213#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700214 }
215
Josh Gaod32d79d2018-02-26 14:29:25 -0800216 template <typename SigactionType>
217 SigactionType GetAction() {
218 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
219 return action_;
220 } else {
221 SigactionType result;
222 result.sa_flags = action_.sa_flags;
223 result.sa_handler = action_.sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100224#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800225 result.sa_restorer = action_.sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100226#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800227 memcpy(&result.sa_mask, &action_.sa_mask,
228 std::min(sizeof(action_.sa_mask), sizeof(result.sa_mask)));
229 return result;
230 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700231 }
232
Josh Gaod32d79d2018-02-26 14:29:25 -0800233 template <typename SigactionType>
234 void SetAction(const SigactionType* new_action) {
235 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
236 action_ = *new_action;
237 } else {
238 action_.sa_flags = new_action->sa_flags;
239 action_.sa_handler = new_action->sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100240#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800241 action_.sa_restorer = new_action->sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100242#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800243 sigemptyset(&action_.sa_mask);
244 memcpy(&action_.sa_mask, &new_action->sa_mask,
245 std::min(sizeof(action_.sa_mask), sizeof(new_action->sa_mask)));
246 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700247 }
248
Josh Gao6b2018f2017-05-04 13:55:28 -0700249 void AddSpecialHandler(SigchainAction* sa) {
250 for (SigchainAction& slot : special_handlers_) {
251 if (slot.sc_sigaction == nullptr) {
252 slot = *sa;
Josh Gao85a78cf2017-03-20 16:26:42 -0700253 return;
254 }
255 }
256
257 fatal("too many special signal handlers");
258 }
259
Josh Gao6b2018f2017-05-04 13:55:28 -0700260 void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700261 // This isn't thread safe, but it's unlikely to be a real problem.
262 size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_);
263 for (size_t i = 0; i < len; ++i) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700264 if (special_handlers_[i].sc_sigaction == fn) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700265 for (size_t j = i; j < len - 1; ++j) {
266 special_handlers_[j] = special_handlers_[j + 1];
267 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700268 special_handlers_[len - 1].sc_sigaction = nullptr;
Josh Gao85a78cf2017-03-20 16:26:42 -0700269 return;
270 }
271 }
272
273 fatal("failed to find special handler to remove");
274 }
275
276
277 static void Handler(int signo, siginfo_t* siginfo, void*);
278
279 private:
280 bool claimed_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800281#if defined(__BIONIC__)
282 struct sigaction64 action_;
283#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700284 struct sigaction action_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800285#endif
Josh Gao6b2018f2017-05-04 13:55:28 -0700286 SigchainAction special_handlers_[2];
Josh Gao85a78cf2017-03-20 16:26:42 -0700287};
288
Josh Gao07d7a5d2018-02-26 14:12:34 -0800289// _NSIG is 1 greater than the highest valued signal, but signals start from 1.
290// Leave an empty element at index 0 for convenience.
291static SignalChain chains[_NSIG + 1];
Josh Gao85a78cf2017-03-20 16:26:42 -0700292
randy.jeong5bef0222019-05-27 10:29:09 +0900293static bool is_signal_hook_debuggable = false;
294
Josh Gao85a78cf2017-03-20 16:26:42 -0700295void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) {
296 // Try the special handlers first.
297 // If one of them crashes, we'll reenter this handler and pass that crash onto the user handler.
Josh Gao99875e92017-04-17 15:58:36 -0700298 if (!GetHandlingSignal()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700299 for (const auto& handler : chains[signo].special_handlers_) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700300 if (handler.sc_sigaction == nullptr) {
301 break;
302 }
303
304 // The native bridge signal handler might not return.
305 // Avoid setting the thread local flag in this case, since we'll never
306 // get a chance to restore it.
307 bool handler_noreturn = (handler.sc_flags & SIGCHAIN_ALLOW_NORETURN);
308 sigset_t previous_mask;
309 linked_sigprocmask(SIG_SETMASK, &handler.sc_mask, &previous_mask);
310
311 ScopedHandlingSignal restorer;
312 if (!handler_noreturn) {
313 SetHandlingSignal(true);
314 }
315
316 if (handler.sc_sigaction(signo, siginfo, ucontext_raw)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700317 return;
318 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700319
320 linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700321 }
322 }
323
324 // Forward to the user's signal handler.
325 int handler_flags = chains[signo].action_.sa_flags;
326 ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw);
Josh Gaod32d79d2018-02-26 14:29:25 -0800327#if defined(__BIONIC__)
328 sigset64_t mask;
329 sigorset(&mask, &ucontext->uc_sigmask64, &chains[signo].action_.sa_mask);
330#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700331 sigset_t mask;
332 sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask);
Josh Gaod32d79d2018-02-26 14:29:25 -0800333#endif
Josh Gao04de4fe2017-05-31 20:24:51 -0700334 if (!(handler_flags & SA_NODEFER)) {
335 sigaddset(&mask, signo);
Josh Gao85a78cf2017-03-20 16:26:42 -0700336 }
Josh Gaod32d79d2018-02-26 14:29:25 -0800337
338#if defined(__BIONIC__)
339 linked_sigprocmask64(SIG_SETMASK, &mask, nullptr);
340#else
Josh Gao6b2018f2017-05-04 13:55:28 -0700341 linked_sigprocmask(SIG_SETMASK, &mask, nullptr);
Josh Gaod32d79d2018-02-26 14:29:25 -0800342#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700343
344 if ((handler_flags & SA_SIGINFO)) {
345 chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw);
Jin Qian33dca562017-03-18 02:51:37 +0000346 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700347 auto handler = chains[signo].action_.sa_handler;
348 if (handler == SIG_IGN) {
349 return;
350 } else if (handler == SIG_DFL) {
Josh Gaofb539a42017-03-20 16:23:41 -0700351 fatal("exiting due to SIG_DFL handler for signal %d", signo);
Jin Qian33dca562017-03-18 02:51:37 +0000352 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700353 handler(signo);
Jin Qian33dca562017-03-18 02:51:37 +0000354 }
355 }
356}
357
Josh Gaod32d79d2018-02-26 14:29:25 -0800358template <typename SigactionType>
359static int __sigaction(int signal, const SigactionType* new_action,
360 SigactionType* old_action,
361 int (*linked)(int, const SigactionType*,
362 SigactionType*)) {
randy.jeong5bef0222019-05-27 10:29:09 +0900363 if (is_signal_hook_debuggable) {
364 return 0;
365 }
366
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700367 // If this signal has been claimed as a signal chain, record the user's
368 // action but don't pass it on to the kernel.
369 // Note that we check that the signal number is in range here. An out of range signal
370 // number should behave exactly as the libc sigaction.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800371 if (signal <= 0 || signal >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700372 errno = EINVAL;
373 return -1;
374 }
375
Josh Gao9d631dd2017-03-23 20:04:58 -0700376 if (chains[signal].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800377 SigactionType saved_action = chains[signal].GetAction<SigactionType>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 if (new_action != nullptr) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700379 chains[signal].SetAction(new_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700380 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700381 if (old_action != nullptr) {
Dmitriy Ivanovc01683b2015-01-06 14:55:26 -0800382 *old_action = saved_action;
383 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700384 return 0;
385 }
386
387 // Will only get here if the signal chain has not been claimed. We want
388 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gaod32d79d2018-02-26 14:29:25 -0800389 return linked(signal, new_action, old_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700390}
391
Josh Gaod32d79d2018-02-26 14:29:25 -0800392extern "C" int sigaction(int signal, const struct sigaction* new_action,
393 struct sigaction* old_action) {
394 InitializeSignalChain();
395 return __sigaction(signal, new_action, old_action, linked_sigaction);
396}
397
398#if defined(__BIONIC__)
399extern "C" int sigaction64(int signal, const struct sigaction64* new_action,
400 struct sigaction64* old_action) {
401 InitializeSignalChain();
402 return __sigaction(signal, new_action, old_action, linked_sigaction64);
403}
404#endif
405
Josh Gao85a78cf2017-03-20 16:26:42 -0700406extern "C" sighandler_t signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700407 InitializeSignalChain();
408
Josh Gao07d7a5d2018-02-26 14:12:34 -0800409 if (signo <= 0 || signo >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700410 errno = EINVAL;
411 return SIG_ERR;
412 }
413
414 struct sigaction sa = {};
Dave Allison91a83662014-08-28 16:12:40 -0700415 sigemptyset(&sa.sa_mask);
416 sa.sa_handler = handler;
Josh Gao85a78cf2017-03-20 16:26:42 -0700417 sa.sa_flags = SA_RESTART | SA_ONSTACK;
Dave Allison91a83662014-08-28 16:12:40 -0700418 sighandler_t oldhandler;
419
420 // If this signal has been claimed as a signal chain, record the user's
421 // action but don't pass it on to the kernel.
Josh Gao85a78cf2017-03-20 16:26:42 -0700422 if (chains[signo].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800423 oldhandler = reinterpret_cast<sighandler_t>(
424 chains[signo].GetAction<struct sigaction>().sa_handler);
Josh Gao85a78cf2017-03-20 16:26:42 -0700425 chains[signo].SetAction(&sa);
Dave Allison91a83662014-08-28 16:12:40 -0700426 return oldhandler;
427 }
428
429 // Will only get here if the signal chain has not been claimed. We want
430 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gao85a78cf2017-03-20 16:26:42 -0700431 if (linked_sigaction(signo, &sa, &sa) == -1) {
Dave Allison91a83662014-08-28 16:12:40 -0700432 return SIG_ERR;
433 }
434
435 return reinterpret_cast<sighandler_t>(sa.sa_handler);
436}
437
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700438#if !defined(__LP64__)
Josh Gao85a78cf2017-03-20 16:26:42 -0700439extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700440 InitializeSignalChain();
441
Josh Gao85a78cf2017-03-20 16:26:42 -0700442 return signal(signo, handler);
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700443}
444#endif
445
Josh Gaod32d79d2018-02-26 14:29:25 -0800446template <typename SigsetType>
447int __sigprocmask(int how, const SigsetType* new_set, SigsetType* old_set,
448 int (*linked)(int, const SigsetType*, SigsetType*)) {
Josh Gao90444552017-03-20 14:58:25 -0700449 // When inside a signal handler, forward directly to the actual sigprocmask.
Josh Gao99875e92017-04-17 15:58:36 -0700450 if (GetHandlingSignal()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800451 return linked(how, new_set, old_set);
Josh Gao90444552017-03-20 14:58:25 -0700452 }
453
Josh Gaod32d79d2018-02-26 14:29:25 -0800454 const SigsetType* new_set_ptr = new_set;
455 SigsetType tmpset;
456 if (new_set != nullptr) {
457 tmpset = *new_set;
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700458
Josh Gaof74caac2018-02-26 14:13:52 -0800459 if (how == SIG_BLOCK || how == SIG_SETMASK) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700460 // Don't allow claimed signals in the mask. If a signal chain has been claimed
461 // we can't allow the user to block that signal.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800462 for (int i = 1; i < _NSIG; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700463 if (chains[i].IsClaimed() && sigismember(&tmpset, i)) {
Dmitriy Ivanova3164b92015-04-01 11:08:45 -0700464 sigdelset(&tmpset, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700465 }
466 }
467 }
468 new_set_ptr = &tmpset;
469 }
470
Josh Gaod32d79d2018-02-26 14:29:25 -0800471 return linked(how, new_set_ptr, old_set);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700472}
Dave Allisoncefcea82014-09-16 10:01:01 -0700473
Josh Gaod32d79d2018-02-26 14:29:25 -0800474extern "C" int sigprocmask(int how, const sigset_t* new_set,
475 sigset_t* old_set) {
476 InitializeSignalChain();
477 return __sigprocmask(how, new_set, old_set, linked_sigprocmask);
478}
479
480#if defined(__BIONIC__)
481extern "C" int sigprocmask64(int how, const sigset64_t* new_set,
482 sigset64_t* old_set) {
483 InitializeSignalChain();
484 return __sigprocmask(how, new_set, old_set, linked_sigprocmask64);
485}
486#endif
487
Josh Gao6b2018f2017-05-04 13:55:28 -0700488extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700489 InitializeSignalChain();
490
Josh Gao85a78cf2017-03-20 16:26:42 -0700491 if (signal <= 0 || signal >= _NSIG) {
492 fatal("Invalid signal %d", signal);
493 }
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700494
495 // Set the managed_handler.
Josh Gao6b2018f2017-05-04 13:55:28 -0700496 chains[signal].AddSpecialHandler(sa);
Josh Gao85a78cf2017-03-20 16:26:42 -0700497 chains[signal].Claim(signal);
498}
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700499
Josh Gao6b2018f2017-05-04 13:55:28 -0700500extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700501 InitializeSignalChain();
502
Josh Gao85a78cf2017-03-20 16:26:42 -0700503 if (signal <= 0 || signal >= _NSIG) {
504 fatal("Invalid signal %d", signal);
505 }
506
507 chains[signal].RemoveSpecialHandler(fn);
508}
509
510extern "C" void EnsureFrontOfChain(int signal) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700511 InitializeSignalChain();
512
Josh Gao85a78cf2017-03-20 16:26:42 -0700513 if (signal <= 0 || signal >= _NSIG) {
514 fatal("Invalid signal %d", signal);
515 }
516
517 // Read the current action without looking at the chain, it should be the expected action.
Josh Gao3bef5272018-09-04 14:33:23 -0700518#if defined(__BIONIC__)
519 struct sigaction64 current_action;
520 linked_sigaction64(signal, nullptr, &current_action);
521#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700522 struct sigaction current_action;
Josh Gao85a78cf2017-03-20 16:26:42 -0700523 linked_sigaction(signal, nullptr, &current_action);
Josh Gao3bef5272018-09-04 14:33:23 -0700524#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700525
Josh Gao85a78cf2017-03-20 16:26:42 -0700526 // If the sigactions don't match then we put the current action on the chain and make ourself as
527 // the main action.
528 if (current_action.sa_sigaction != SignalChain::Handler) {
529 log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction);
530 chains[signal].Register(signal);
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700531 }
532}
533
randy.jeong5bef0222019-05-27 10:29:09 +0900534extern "C" void SkipAddSignalHandler(bool value) {
535 is_signal_hook_debuggable = value;
536}
537
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700538} // namespace art
539