blob: 08ee690e0ecc40b7bba8ffce07734813f8d0e180 [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
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +010062#if defined(__BIONIC__) && !defined(__LP64__) && !defined(__mips__)
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
101template<typename T>
102static void lookup_next_symbol(T* output, T wrapper, const char* name) {
Andreas Gampe24c14222018-12-10 10:16:15 -0800103 void* sym = dlsym(RTLD_NEXT, name); // NOLINT glibc triggers cert-dcl16-c with RTLD_NEXT.
Josh Gaod32d79d2018-02-26 14:29:25 -0800104 if (sym == nullptr) {
105 sym = dlsym(RTLD_DEFAULT, name);
106 if (sym == wrapper || sym == sigaction) {
107 fatal("Unable to find next %s in signal chain", name);
108 }
109 }
110 *output = reinterpret_cast<T>(sym);
111}
112
Josh Gaofd4d0d32017-04-26 19:09:47 -0700113__attribute__((constructor)) static void InitializeSignalChain() {
114 static std::once_flag once;
115 std::call_once(once, []() {
Josh Gaod32d79d2018-02-26 14:29:25 -0800116 lookup_next_symbol(&linked_sigaction, sigaction, "sigaction");
117 lookup_next_symbol(&linked_sigprocmask, sigprocmask, "sigprocmask");
Josh Gaofd4d0d32017-04-26 19:09:47 -0700118
Josh Gaod32d79d2018-02-26 14:29:25 -0800119#if defined(__BIONIC__)
120 lookup_next_symbol(&linked_sigaction64, sigaction64, "sigaction64");
121 lookup_next_symbol(&linked_sigprocmask64, sigprocmask64, "sigprocmask64");
122#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700123 });
124}
125
Josh Gao99875e92017-04-17 15:58:36 -0700126static pthread_key_t GetHandlingSignalKey() {
127 static pthread_key_t key;
128 static std::once_flag once;
129 std::call_once(once, []() {
130 int rc = pthread_key_create(&key, nullptr);
131 if (rc != 0) {
132 fatal("failed to create sigchain pthread key: %s", strerror(rc));
133 }
134 });
135 return key;
136}
137
138static bool GetHandlingSignal() {
139 void* result = pthread_getspecific(GetHandlingSignalKey());
140 return reinterpret_cast<uintptr_t>(result);
141}
142
143static void SetHandlingSignal(bool value) {
144 pthread_setspecific(GetHandlingSignalKey(),
145 reinterpret_cast<void*>(static_cast<uintptr_t>(value)));
146}
147
148class ScopedHandlingSignal {
149 public:
150 ScopedHandlingSignal() : original_value_(GetHandlingSignal()) {
151 }
152
153 ~ScopedHandlingSignal() {
154 SetHandlingSignal(original_value_);
155 }
156
157 private:
158 bool original_value_;
159};
Josh Gao85a78cf2017-03-20 16:26:42 -0700160
161class SignalChain {
162 public:
163 SignalChain() : claimed_(false) {
Jin Qian33dca562017-03-18 02:51:37 +0000164 }
165
Josh Gao85a78cf2017-03-20 16:26:42 -0700166 bool IsClaimed() {
167 return claimed_;
168 }
169
170 void Claim(int signo) {
171 if (!claimed_) {
172 Register(signo);
173 claimed_ = true;
Jin Qian33dca562017-03-18 02:51:37 +0000174 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700175 }
176
177 // Register the signal chain with the kernel if needed.
178 void Register(int signo) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800179#if defined(__BIONIC__)
180 struct sigaction64 handler_action = {};
181 sigfillset64(&handler_action.sa_mask);
182#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700183 struct sigaction handler_action = {};
Josh Gaod32d79d2018-02-26 14:29:25 -0800184 sigfillset(&handler_action.sa_mask);
185#endif
186
Josh Gao85a78cf2017-03-20 16:26:42 -0700187 handler_action.sa_sigaction = SignalChain::Handler;
188 handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
Josh Gaod32d79d2018-02-26 14:29:25 -0800189
190#if defined(__BIONIC__)
191 linked_sigaction64(signo, &handler_action, &action_);
192#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700193 linked_sigaction(signo, &handler_action, &action_);
Josh Gaod32d79d2018-02-26 14:29:25 -0800194#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700195 }
196
Josh Gaod32d79d2018-02-26 14:29:25 -0800197 template <typename SigactionType>
198 SigactionType GetAction() {
199 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
200 return action_;
201 } else {
202 SigactionType result;
203 result.sa_flags = action_.sa_flags;
204 result.sa_handler = action_.sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100205#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800206 result.sa_restorer = action_.sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100207#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800208 memcpy(&result.sa_mask, &action_.sa_mask,
209 std::min(sizeof(action_.sa_mask), sizeof(result.sa_mask)));
210 return result;
211 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700212 }
213
Josh Gaod32d79d2018-02-26 14:29:25 -0800214 template <typename SigactionType>
215 void SetAction(const SigactionType* new_action) {
216 if constexpr (std::is_same_v<decltype(action_), SigactionType>) {
217 action_ = *new_action;
218 } else {
219 action_.sa_flags = new_action->sa_flags;
220 action_.sa_handler = new_action->sa_handler;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100221#if defined(SA_RESTORER)
Josh Gaod32d79d2018-02-26 14:29:25 -0800222 action_.sa_restorer = new_action->sa_restorer;
Goran Jakovljevicedef4ba2018-03-05 20:03:24 +0100223#endif
Josh Gaod32d79d2018-02-26 14:29:25 -0800224 sigemptyset(&action_.sa_mask);
225 memcpy(&action_.sa_mask, &new_action->sa_mask,
226 std::min(sizeof(action_.sa_mask), sizeof(new_action->sa_mask)));
227 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700228 }
229
Josh Gao6b2018f2017-05-04 13:55:28 -0700230 void AddSpecialHandler(SigchainAction* sa) {
231 for (SigchainAction& slot : special_handlers_) {
232 if (slot.sc_sigaction == nullptr) {
233 slot = *sa;
Josh Gao85a78cf2017-03-20 16:26:42 -0700234 return;
235 }
236 }
237
238 fatal("too many special signal handlers");
239 }
240
Josh Gao6b2018f2017-05-04 13:55:28 -0700241 void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700242 // This isn't thread safe, but it's unlikely to be a real problem.
243 size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_);
244 for (size_t i = 0; i < len; ++i) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700245 if (special_handlers_[i].sc_sigaction == fn) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700246 for (size_t j = i; j < len - 1; ++j) {
247 special_handlers_[j] = special_handlers_[j + 1];
248 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700249 special_handlers_[len - 1].sc_sigaction = nullptr;
Josh Gao85a78cf2017-03-20 16:26:42 -0700250 return;
251 }
252 }
253
254 fatal("failed to find special handler to remove");
255 }
256
257
258 static void Handler(int signo, siginfo_t* siginfo, void*);
259
260 private:
261 bool claimed_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800262#if defined(__BIONIC__)
263 struct sigaction64 action_;
264#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700265 struct sigaction action_;
Josh Gaod32d79d2018-02-26 14:29:25 -0800266#endif
Josh Gao6b2018f2017-05-04 13:55:28 -0700267 SigchainAction special_handlers_[2];
Josh Gao85a78cf2017-03-20 16:26:42 -0700268};
269
Josh Gao07d7a5d2018-02-26 14:12:34 -0800270// _NSIG is 1 greater than the highest valued signal, but signals start from 1.
271// Leave an empty element at index 0 for convenience.
272static SignalChain chains[_NSIG + 1];
Josh Gao85a78cf2017-03-20 16:26:42 -0700273
Josh Gao85a78cf2017-03-20 16:26:42 -0700274void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) {
275 // Try the special handlers first.
276 // 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 -0700277 if (!GetHandlingSignal()) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700278 for (const auto& handler : chains[signo].special_handlers_) {
Josh Gao6b2018f2017-05-04 13:55:28 -0700279 if (handler.sc_sigaction == nullptr) {
280 break;
281 }
282
283 // The native bridge signal handler might not return.
284 // Avoid setting the thread local flag in this case, since we'll never
285 // get a chance to restore it.
286 bool handler_noreturn = (handler.sc_flags & SIGCHAIN_ALLOW_NORETURN);
287 sigset_t previous_mask;
288 linked_sigprocmask(SIG_SETMASK, &handler.sc_mask, &previous_mask);
289
290 ScopedHandlingSignal restorer;
291 if (!handler_noreturn) {
292 SetHandlingSignal(true);
293 }
294
295 if (handler.sc_sigaction(signo, siginfo, ucontext_raw)) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700296 return;
297 }
Josh Gao6b2018f2017-05-04 13:55:28 -0700298
299 linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr);
Josh Gao85a78cf2017-03-20 16:26:42 -0700300 }
301 }
302
303 // Forward to the user's signal handler.
304 int handler_flags = chains[signo].action_.sa_flags;
305 ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw);
Josh Gaod32d79d2018-02-26 14:29:25 -0800306#if defined(__BIONIC__)
307 sigset64_t mask;
308 sigorset(&mask, &ucontext->uc_sigmask64, &chains[signo].action_.sa_mask);
309#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700310 sigset_t mask;
311 sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask);
Josh Gaod32d79d2018-02-26 14:29:25 -0800312#endif
Josh Gao04de4fe2017-05-31 20:24:51 -0700313 if (!(handler_flags & SA_NODEFER)) {
314 sigaddset(&mask, signo);
Josh Gao85a78cf2017-03-20 16:26:42 -0700315 }
Josh Gaod32d79d2018-02-26 14:29:25 -0800316
317#if defined(__BIONIC__)
318 linked_sigprocmask64(SIG_SETMASK, &mask, nullptr);
319#else
Josh Gao6b2018f2017-05-04 13:55:28 -0700320 linked_sigprocmask(SIG_SETMASK, &mask, nullptr);
Josh Gaod32d79d2018-02-26 14:29:25 -0800321#endif
Josh Gao85a78cf2017-03-20 16:26:42 -0700322
323 if ((handler_flags & SA_SIGINFO)) {
324 chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw);
Jin Qian33dca562017-03-18 02:51:37 +0000325 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700326 auto handler = chains[signo].action_.sa_handler;
327 if (handler == SIG_IGN) {
328 return;
329 } else if (handler == SIG_DFL) {
Josh Gaofb539a42017-03-20 16:23:41 -0700330 fatal("exiting due to SIG_DFL handler for signal %d", signo);
Jin Qian33dca562017-03-18 02:51:37 +0000331 } else {
Josh Gao85a78cf2017-03-20 16:26:42 -0700332 handler(signo);
Jin Qian33dca562017-03-18 02:51:37 +0000333 }
334 }
335}
336
Josh Gaod32d79d2018-02-26 14:29:25 -0800337template <typename SigactionType>
338static int __sigaction(int signal, const SigactionType* new_action,
339 SigactionType* old_action,
340 int (*linked)(int, const SigactionType*,
341 SigactionType*)) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700342 // If this signal has been claimed as a signal chain, record the user's
343 // action but don't pass it on to the kernel.
344 // Note that we check that the signal number is in range here. An out of range signal
345 // number should behave exactly as the libc sigaction.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800346 if (signal <= 0 || signal >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700347 errno = EINVAL;
348 return -1;
349 }
350
Josh Gao9d631dd2017-03-23 20:04:58 -0700351 if (chains[signal].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800352 SigactionType saved_action = chains[signal].GetAction<SigactionType>();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700353 if (new_action != nullptr) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700354 chains[signal].SetAction(new_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700355 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700356 if (old_action != nullptr) {
Dmitriy Ivanovc01683b2015-01-06 14:55:26 -0800357 *old_action = saved_action;
358 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700359 return 0;
360 }
361
362 // Will only get here if the signal chain has not been claimed. We want
363 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gaod32d79d2018-02-26 14:29:25 -0800364 return linked(signal, new_action, old_action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700365}
366
Josh Gaod32d79d2018-02-26 14:29:25 -0800367extern "C" int sigaction(int signal, const struct sigaction* new_action,
368 struct sigaction* old_action) {
369 InitializeSignalChain();
370 return __sigaction(signal, new_action, old_action, linked_sigaction);
371}
372
373#if defined(__BIONIC__)
374extern "C" int sigaction64(int signal, const struct sigaction64* new_action,
375 struct sigaction64* old_action) {
376 InitializeSignalChain();
377 return __sigaction(signal, new_action, old_action, linked_sigaction64);
378}
379#endif
380
Josh Gao85a78cf2017-03-20 16:26:42 -0700381extern "C" sighandler_t signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700382 InitializeSignalChain();
383
Josh Gao07d7a5d2018-02-26 14:12:34 -0800384 if (signo <= 0 || signo >= _NSIG) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700385 errno = EINVAL;
386 return SIG_ERR;
387 }
388
389 struct sigaction sa = {};
Dave Allison91a83662014-08-28 16:12:40 -0700390 sigemptyset(&sa.sa_mask);
391 sa.sa_handler = handler;
Josh Gao85a78cf2017-03-20 16:26:42 -0700392 sa.sa_flags = SA_RESTART | SA_ONSTACK;
Dave Allison91a83662014-08-28 16:12:40 -0700393 sighandler_t oldhandler;
394
395 // If this signal has been claimed as a signal chain, record the user's
396 // action but don't pass it on to the kernel.
Josh Gao85a78cf2017-03-20 16:26:42 -0700397 if (chains[signo].IsClaimed()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800398 oldhandler = reinterpret_cast<sighandler_t>(
399 chains[signo].GetAction<struct sigaction>().sa_handler);
Josh Gao85a78cf2017-03-20 16:26:42 -0700400 chains[signo].SetAction(&sa);
Dave Allison91a83662014-08-28 16:12:40 -0700401 return oldhandler;
402 }
403
404 // Will only get here if the signal chain has not been claimed. We want
405 // to pass the sigaction on to the kernel via the real sigaction in libc.
Josh Gao85a78cf2017-03-20 16:26:42 -0700406 if (linked_sigaction(signo, &sa, &sa) == -1) {
Dave Allison91a83662014-08-28 16:12:40 -0700407 return SIG_ERR;
408 }
409
410 return reinterpret_cast<sighandler_t>(sa.sa_handler);
411}
412
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700413#if !defined(__LP64__)
Josh Gao85a78cf2017-03-20 16:26:42 -0700414extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700415 InitializeSignalChain();
416
Josh Gao85a78cf2017-03-20 16:26:42 -0700417 return signal(signo, handler);
Dimitry Ivanov82b67b92016-08-01 11:19:03 -0700418}
419#endif
420
Josh Gaod32d79d2018-02-26 14:29:25 -0800421template <typename SigsetType>
422int __sigprocmask(int how, const SigsetType* new_set, SigsetType* old_set,
423 int (*linked)(int, const SigsetType*, SigsetType*)) {
Josh Gao90444552017-03-20 14:58:25 -0700424 // When inside a signal handler, forward directly to the actual sigprocmask.
Josh Gao99875e92017-04-17 15:58:36 -0700425 if (GetHandlingSignal()) {
Josh Gaod32d79d2018-02-26 14:29:25 -0800426 return linked(how, new_set, old_set);
Josh Gao90444552017-03-20 14:58:25 -0700427 }
428
Josh Gaod32d79d2018-02-26 14:29:25 -0800429 const SigsetType* new_set_ptr = new_set;
430 SigsetType tmpset;
431 if (new_set != nullptr) {
432 tmpset = *new_set;
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700433
Josh Gaof74caac2018-02-26 14:13:52 -0800434 if (how == SIG_BLOCK || how == SIG_SETMASK) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700435 // Don't allow claimed signals in the mask. If a signal chain has been claimed
436 // we can't allow the user to block that signal.
Josh Gao07d7a5d2018-02-26 14:12:34 -0800437 for (int i = 1; i < _NSIG; ++i) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700438 if (chains[i].IsClaimed() && sigismember(&tmpset, i)) {
Dmitriy Ivanova3164b92015-04-01 11:08:45 -0700439 sigdelset(&tmpset, i);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700440 }
441 }
442 }
443 new_set_ptr = &tmpset;
444 }
445
Josh Gaod32d79d2018-02-26 14:29:25 -0800446 return linked(how, new_set_ptr, old_set);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700447}
Dave Allisoncefcea82014-09-16 10:01:01 -0700448
Josh Gaod32d79d2018-02-26 14:29:25 -0800449extern "C" int sigprocmask(int how, const sigset_t* new_set,
450 sigset_t* old_set) {
451 InitializeSignalChain();
452 return __sigprocmask(how, new_set, old_set, linked_sigprocmask);
453}
454
455#if defined(__BIONIC__)
456extern "C" int sigprocmask64(int how, const sigset64_t* new_set,
457 sigset64_t* old_set) {
458 InitializeSignalChain();
459 return __sigprocmask(how, new_set, old_set, linked_sigprocmask64);
460}
461#endif
462
Josh Gao6b2018f2017-05-04 13:55:28 -0700463extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700464 InitializeSignalChain();
465
Josh Gao85a78cf2017-03-20 16:26:42 -0700466 if (signal <= 0 || signal >= _NSIG) {
467 fatal("Invalid signal %d", signal);
468 }
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700469
470 // Set the managed_handler.
Josh Gao6b2018f2017-05-04 13:55:28 -0700471 chains[signal].AddSpecialHandler(sa);
Josh Gao85a78cf2017-03-20 16:26:42 -0700472 chains[signal].Claim(signal);
473}
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700474
Josh Gao6b2018f2017-05-04 13:55:28 -0700475extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700476 InitializeSignalChain();
477
Josh Gao85a78cf2017-03-20 16:26:42 -0700478 if (signal <= 0 || signal >= _NSIG) {
479 fatal("Invalid signal %d", signal);
480 }
481
482 chains[signal].RemoveSpecialHandler(fn);
483}
484
485extern "C" void EnsureFrontOfChain(int signal) {
Josh Gaofd4d0d32017-04-26 19:09:47 -0700486 InitializeSignalChain();
487
Josh Gao85a78cf2017-03-20 16:26:42 -0700488 if (signal <= 0 || signal >= _NSIG) {
489 fatal("Invalid signal %d", signal);
490 }
491
492 // Read the current action without looking at the chain, it should be the expected action.
Josh Gao3bef5272018-09-04 14:33:23 -0700493#if defined(__BIONIC__)
494 struct sigaction64 current_action;
495 linked_sigaction64(signal, nullptr, &current_action);
496#else
Josh Gao85a78cf2017-03-20 16:26:42 -0700497 struct sigaction current_action;
Josh Gao85a78cf2017-03-20 16:26:42 -0700498 linked_sigaction(signal, nullptr, &current_action);
Josh Gao3bef5272018-09-04 14:33:23 -0700499#endif
Josh Gaofd4d0d32017-04-26 19:09:47 -0700500
Josh Gao85a78cf2017-03-20 16:26:42 -0700501 // If the sigactions don't match then we put the current action on the chain and make ourself as
502 // the main action.
503 if (current_action.sa_sigaction != SignalChain::Handler) {
504 log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction);
505 chains[signal].Register(signal);
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700506 }
507}
508
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700509} // namespace art
510