Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 17 | #include <dlfcn.h> |
Luis Hector Chavez | 33cac0f | 2017-04-06 14:18:09 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Josh Gao | 99875e9 | 2017-04-17 15:58:36 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Luis Hector Chavez | 33cac0f | 2017-04-06 14:18:09 -0700 | [diff] [blame] | 23 | #include <string.h> |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 24 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 25 | #include <algorithm> |
Luis Hector Chavez | 33cac0f | 2017-04-06 14:18:09 -0700 | [diff] [blame] | 26 | #include <initializer_list> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 27 | #include <mutex> |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 28 | #include <type_traits> |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 29 | #include <utility> |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 30 | |
Josh Gao | cca6fc0 | 2018-03-12 16:37:21 -0700 | [diff] [blame] | 31 | #include "log.h" |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 32 | #include "sigchain.h" |
| 33 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 34 | #if defined(__APPLE__) |
| 35 | #define _NSIG NSIG |
Dave Allison | 3868009 | 2014-08-29 12:29:34 -0700 | [diff] [blame] | 36 | #define sighandler_t sig_t |
Josh Gao | dd241ae | 2017-03-20 14:17:23 -0700 | [diff] [blame] | 37 | |
| 38 | // Darwin has an #error when ucontext.h is included without _XOPEN_SOURCE defined. |
| 39 | #define _XOPEN_SOURCE |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Josh Gao | dd241ae | 2017-03-20 14:17:23 -0700 | [diff] [blame] | 42 | #include <ucontext.h> |
| 43 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 44 | // 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 Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 61 | |
Goran Jakovljevic | edef4ba | 2018-03-05 20:03:24 +0100 | [diff] [blame] | 62 | #if defined(__BIONIC__) && !defined(__LP64__) && !defined(__mips__) |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 63 | static int sigismember(const sigset64_t* sigset, int signum) { |
| 64 | return sigismember64(sigset, signum); |
| 65 | } |
| 66 | |
| 67 | static int sigemptyset(sigset64_t* sigset) { |
| 68 | return sigemptyset64(sigset); |
| 69 | } |
| 70 | |
| 71 | static int sigaddset(sigset64_t* sigset, int signum) { |
| 72 | return sigaddset64(sigset, signum); |
| 73 | } |
| 74 | |
| 75 | static int sigdelset(sigset64_t* sigset, int signum) { |
| 76 | return sigdelset64(sigset, signum); |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | template<typename SigsetType> |
| 81 | static int sigorset(SigsetType* dest, SigsetType* left, SigsetType* right) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 82 | sigemptyset(dest); |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 83 | for (size_t i = 0; i < sizeof(SigsetType) * CHAR_BIT; ++i) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 84 | if (sigismember(left, i) == 1 || sigismember(right, i) == 1) { |
| 85 | sigaddset(dest, i); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 86 | } |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | namespace art { |
| 92 | |
| 93 | static decltype(&sigaction) linked_sigaction; |
| 94 | static decltype(&sigprocmask) linked_sigprocmask; |
Josh Gao | 99875e9 | 2017-04-17 15:58:36 -0700 | [diff] [blame] | 95 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 96 | #if defined(__BIONIC__) |
| 97 | static decltype(&sigaction64) linked_sigaction64; |
| 98 | static decltype(&sigprocmask64) linked_sigprocmask64; |
| 99 | #endif |
| 100 | |
| 101 | template<typename T> |
| 102 | static void lookup_next_symbol(T* output, T wrapper, const char* name) { |
Andreas Gampe | 24c1422 | 2018-12-10 10:16:15 -0800 | [diff] [blame] | 103 | void* sym = dlsym(RTLD_NEXT, name); // NOLINT glibc triggers cert-dcl16-c with RTLD_NEXT. |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 104 | 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 Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 113 | __attribute__((constructor)) static void InitializeSignalChain() { |
| 114 | static std::once_flag once; |
| 115 | std::call_once(once, []() { |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 116 | lookup_next_symbol(&linked_sigaction, sigaction, "sigaction"); |
| 117 | lookup_next_symbol(&linked_sigprocmask, sigprocmask, "sigprocmask"); |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 118 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 119 | #if defined(__BIONIC__) |
| 120 | lookup_next_symbol(&linked_sigaction64, sigaction64, "sigaction64"); |
| 121 | lookup_next_symbol(&linked_sigprocmask64, sigprocmask64, "sigprocmask64"); |
| 122 | #endif |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 123 | }); |
| 124 | } |
| 125 | |
Josh Gao | 99875e9 | 2017-04-17 15:58:36 -0700 | [diff] [blame] | 126 | static 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 | |
| 138 | static bool GetHandlingSignal() { |
| 139 | void* result = pthread_getspecific(GetHandlingSignalKey()); |
| 140 | return reinterpret_cast<uintptr_t>(result); |
| 141 | } |
| 142 | |
| 143 | static void SetHandlingSignal(bool value) { |
| 144 | pthread_setspecific(GetHandlingSignalKey(), |
| 145 | reinterpret_cast<void*>(static_cast<uintptr_t>(value))); |
| 146 | } |
| 147 | |
| 148 | class 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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 160 | |
| 161 | class SignalChain { |
| 162 | public: |
| 163 | SignalChain() : claimed_(false) { |
Jin Qian | 33dca56 | 2017-03-18 02:51:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 166 | bool IsClaimed() { |
| 167 | return claimed_; |
| 168 | } |
| 169 | |
| 170 | void Claim(int signo) { |
| 171 | if (!claimed_) { |
| 172 | Register(signo); |
| 173 | claimed_ = true; |
Jin Qian | 33dca56 | 2017-03-18 02:51:37 +0000 | [diff] [blame] | 174 | } |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Register the signal chain with the kernel if needed. |
| 178 | void Register(int signo) { |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 179 | #if defined(__BIONIC__) |
| 180 | struct sigaction64 handler_action = {}; |
| 181 | sigfillset64(&handler_action.sa_mask); |
| 182 | #else |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 183 | struct sigaction handler_action = {}; |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 184 | sigfillset(&handler_action.sa_mask); |
| 185 | #endif |
| 186 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 187 | handler_action.sa_sigaction = SignalChain::Handler; |
| 188 | handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 189 | |
| 190 | #if defined(__BIONIC__) |
| 191 | linked_sigaction64(signo, &handler_action, &action_); |
| 192 | #else |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 193 | linked_sigaction(signo, &handler_action, &action_); |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 194 | #endif |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 197 | 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 Jakovljevic | edef4ba | 2018-03-05 20:03:24 +0100 | [diff] [blame] | 205 | #if defined(SA_RESTORER) |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 206 | result.sa_restorer = action_.sa_restorer; |
Goran Jakovljevic | edef4ba | 2018-03-05 20:03:24 +0100 | [diff] [blame] | 207 | #endif |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 208 | memcpy(&result.sa_mask, &action_.sa_mask, |
| 209 | std::min(sizeof(action_.sa_mask), sizeof(result.sa_mask))); |
| 210 | return result; |
| 211 | } |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 214 | 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 Jakovljevic | edef4ba | 2018-03-05 20:03:24 +0100 | [diff] [blame] | 221 | #if defined(SA_RESTORER) |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 222 | action_.sa_restorer = new_action->sa_restorer; |
Goran Jakovljevic | edef4ba | 2018-03-05 20:03:24 +0100 | [diff] [blame] | 223 | #endif |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 224 | 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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 230 | void AddSpecialHandler(SigchainAction* sa) { |
| 231 | for (SigchainAction& slot : special_handlers_) { |
| 232 | if (slot.sc_sigaction == nullptr) { |
| 233 | slot = *sa; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | fatal("too many special signal handlers"); |
| 239 | } |
| 240 | |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 241 | void RemoveSpecialHandler(bool (*fn)(int, siginfo_t*, void*)) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 242 | // 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 Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 245 | if (special_handlers_[i].sc_sigaction == fn) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 246 | for (size_t j = i; j < len - 1; ++j) { |
| 247 | special_handlers_[j] = special_handlers_[j + 1]; |
| 248 | } |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 249 | special_handlers_[len - 1].sc_sigaction = nullptr; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 250 | 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 Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 262 | #if defined(__BIONIC__) |
| 263 | struct sigaction64 action_; |
| 264 | #else |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 265 | struct sigaction action_; |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 266 | #endif |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 267 | SigchainAction special_handlers_[2]; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Josh Gao | 07d7a5d | 2018-02-26 14:12:34 -0800 | [diff] [blame] | 270 | // _NSIG is 1 greater than the highest valued signal, but signals start from 1. |
| 271 | // Leave an empty element at index 0 for convenience. |
| 272 | static SignalChain chains[_NSIG + 1]; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 273 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 274 | void 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 Gao | 99875e9 | 2017-04-17 15:58:36 -0700 | [diff] [blame] | 277 | if (!GetHandlingSignal()) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 278 | for (const auto& handler : chains[signo].special_handlers_) { |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 279 | 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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 298 | |
| 299 | linked_sigprocmask(SIG_SETMASK, &previous_mask, nullptr); |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 300 | } |
| 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 Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 306 | #if defined(__BIONIC__) |
| 307 | sigset64_t mask; |
| 308 | sigorset(&mask, &ucontext->uc_sigmask64, &chains[signo].action_.sa_mask); |
| 309 | #else |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 310 | sigset_t mask; |
| 311 | sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask); |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 312 | #endif |
Josh Gao | 04de4fe | 2017-05-31 20:24:51 -0700 | [diff] [blame] | 313 | if (!(handler_flags & SA_NODEFER)) { |
| 314 | sigaddset(&mask, signo); |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 315 | } |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 316 | |
| 317 | #if defined(__BIONIC__) |
| 318 | linked_sigprocmask64(SIG_SETMASK, &mask, nullptr); |
| 319 | #else |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 320 | linked_sigprocmask(SIG_SETMASK, &mask, nullptr); |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 321 | #endif |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 322 | |
| 323 | if ((handler_flags & SA_SIGINFO)) { |
| 324 | chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw); |
Jin Qian | 33dca56 | 2017-03-18 02:51:37 +0000 | [diff] [blame] | 325 | } else { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 326 | auto handler = chains[signo].action_.sa_handler; |
| 327 | if (handler == SIG_IGN) { |
| 328 | return; |
| 329 | } else if (handler == SIG_DFL) { |
Josh Gao | fb539a4 | 2017-03-20 16:23:41 -0700 | [diff] [blame] | 330 | fatal("exiting due to SIG_DFL handler for signal %d", signo); |
Jin Qian | 33dca56 | 2017-03-18 02:51:37 +0000 | [diff] [blame] | 331 | } else { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 332 | handler(signo); |
Jin Qian | 33dca56 | 2017-03-18 02:51:37 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 337 | template <typename SigactionType> |
| 338 | static int __sigaction(int signal, const SigactionType* new_action, |
| 339 | SigactionType* old_action, |
| 340 | int (*linked)(int, const SigactionType*, |
| 341 | SigactionType*)) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 342 | // 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 Gao | 07d7a5d | 2018-02-26 14:12:34 -0800 | [diff] [blame] | 346 | if (signal <= 0 || signal >= _NSIG) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 347 | errno = EINVAL; |
| 348 | return -1; |
| 349 | } |
| 350 | |
Josh Gao | 9d631dd | 2017-03-23 20:04:58 -0700 | [diff] [blame] | 351 | if (chains[signal].IsClaimed()) { |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 352 | SigactionType saved_action = chains[signal].GetAction<SigactionType>(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 353 | if (new_action != nullptr) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 354 | chains[signal].SetAction(new_action); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 355 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 356 | if (old_action != nullptr) { |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 357 | *old_action = saved_action; |
| 358 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 359 | 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 Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 364 | return linked(signal, new_action, old_action); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 367 | extern "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__) |
| 374 | extern "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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 381 | extern "C" sighandler_t signal(int signo, sighandler_t handler) { |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 382 | InitializeSignalChain(); |
| 383 | |
Josh Gao | 07d7a5d | 2018-02-26 14:12:34 -0800 | [diff] [blame] | 384 | if (signo <= 0 || signo >= _NSIG) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 385 | errno = EINVAL; |
| 386 | return SIG_ERR; |
| 387 | } |
| 388 | |
| 389 | struct sigaction sa = {}; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 390 | sigemptyset(&sa.sa_mask); |
| 391 | sa.sa_handler = handler; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 392 | sa.sa_flags = SA_RESTART | SA_ONSTACK; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 393 | 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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 397 | if (chains[signo].IsClaimed()) { |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 398 | oldhandler = reinterpret_cast<sighandler_t>( |
| 399 | chains[signo].GetAction<struct sigaction>().sa_handler); |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 400 | chains[signo].SetAction(&sa); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 401 | 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 Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 406 | if (linked_sigaction(signo, &sa, &sa) == -1) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 407 | return SIG_ERR; |
| 408 | } |
| 409 | |
| 410 | return reinterpret_cast<sighandler_t>(sa.sa_handler); |
| 411 | } |
| 412 | |
Dimitry Ivanov | 82b67b9 | 2016-08-01 11:19:03 -0700 | [diff] [blame] | 413 | #if !defined(__LP64__) |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 414 | extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) { |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 415 | InitializeSignalChain(); |
| 416 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 417 | return signal(signo, handler); |
Dimitry Ivanov | 82b67b9 | 2016-08-01 11:19:03 -0700 | [diff] [blame] | 418 | } |
| 419 | #endif |
| 420 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 421 | template <typename SigsetType> |
| 422 | int __sigprocmask(int how, const SigsetType* new_set, SigsetType* old_set, |
| 423 | int (*linked)(int, const SigsetType*, SigsetType*)) { |
Josh Gao | 9044455 | 2017-03-20 14:58:25 -0700 | [diff] [blame] | 424 | // When inside a signal handler, forward directly to the actual sigprocmask. |
Josh Gao | 99875e9 | 2017-04-17 15:58:36 -0700 | [diff] [blame] | 425 | if (GetHandlingSignal()) { |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 426 | return linked(how, new_set, old_set); |
Josh Gao | 9044455 | 2017-03-20 14:58:25 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 429 | const SigsetType* new_set_ptr = new_set; |
| 430 | SigsetType tmpset; |
| 431 | if (new_set != nullptr) { |
| 432 | tmpset = *new_set; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 433 | |
Josh Gao | f74caac | 2018-02-26 14:13:52 -0800 | [diff] [blame] | 434 | if (how == SIG_BLOCK || how == SIG_SETMASK) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 435 | // 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 Gao | 07d7a5d | 2018-02-26 14:12:34 -0800 | [diff] [blame] | 437 | for (int i = 1; i < _NSIG; ++i) { |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 438 | if (chains[i].IsClaimed() && sigismember(&tmpset, i)) { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 439 | sigdelset(&tmpset, i); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
| 443 | new_set_ptr = &tmpset; |
| 444 | } |
| 445 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 446 | return linked(how, new_set_ptr, old_set); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 447 | } |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 448 | |
Josh Gao | d32d79d | 2018-02-26 14:29:25 -0800 | [diff] [blame] | 449 | extern "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__) |
| 456 | extern "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 Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 463 | extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction* sa) { |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 464 | InitializeSignalChain(); |
| 465 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 466 | if (signal <= 0 || signal >= _NSIG) { |
| 467 | fatal("Invalid signal %d", signal); |
| 468 | } |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 469 | |
| 470 | // Set the managed_handler. |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 471 | chains[signal].AddSpecialHandler(sa); |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 472 | chains[signal].Claim(signal); |
| 473 | } |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 474 | |
Josh Gao | 6b2018f | 2017-05-04 13:55:28 -0700 | [diff] [blame] | 475 | extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t*, void*)) { |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 476 | InitializeSignalChain(); |
| 477 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 478 | if (signal <= 0 || signal >= _NSIG) { |
| 479 | fatal("Invalid signal %d", signal); |
| 480 | } |
| 481 | |
| 482 | chains[signal].RemoveSpecialHandler(fn); |
| 483 | } |
| 484 | |
| 485 | extern "C" void EnsureFrontOfChain(int signal) { |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 486 | InitializeSignalChain(); |
| 487 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 488 | 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 Gao | 3bef527 | 2018-09-04 14:33:23 -0700 | [diff] [blame] | 493 | #if defined(__BIONIC__) |
| 494 | struct sigaction64 current_action; |
| 495 | linked_sigaction64(signal, nullptr, ¤t_action); |
| 496 | #else |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 497 | struct sigaction current_action; |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 498 | linked_sigaction(signal, nullptr, ¤t_action); |
Josh Gao | 3bef527 | 2018-09-04 14:33:23 -0700 | [diff] [blame] | 499 | #endif |
Josh Gao | fd4d0d3 | 2017-04-26 19:09:47 -0700 | [diff] [blame] | 500 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 501 | // 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 Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 509 | } // namespace art |
| 510 | |