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 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 17 | #ifdef ART_TARGET_ANDROID |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 18 | #include <android/log.h> |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 19 | #else |
| 20 | #include <stdarg.h> |
| 21 | #include <iostream> |
| 22 | #endif |
| 23 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 24 | #include <dlfcn.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 28 | #include <ucontext.h> |
| 29 | |
| 30 | #include <utility> |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 31 | |
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 |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 39 | // libsigchain provides an interception layer for signal handlers, to allow ART and others to give |
| 40 | // their signal handlers the first stab at handling signals before passing them on to user code. |
| 41 | // |
| 42 | // It implements wrapper functions for signal, sigaction, and sigprocmask, and a handler that |
| 43 | // forwards signals appropriately. |
| 44 | // |
| 45 | // In our handler, we start off with all signals blocked, fetch the original signal mask from the |
| 46 | // passed in ucontext, and then adjust our signal mask appropriately for the user handler. |
| 47 | // |
| 48 | // It's somewhat tricky for us to properly handle some flag cases: |
| 49 | // SA_NOCLDSTOP and SA_NOCLDWAIT: shouldn't matter, we don't have special handlers for SIGCHLD. |
| 50 | // SA_NODEFER: unimplemented, we can manually change the signal mask appropriately. |
| 51 | // ~SA_ONSTACK: always silently enable this |
| 52 | // SA_RESETHAND: unimplemented, but we can probably do this? |
| 53 | // ~SA_RESTART: unimplemented, maybe we can reserve an RT signal, register an empty handler that |
| 54 | // doesn't have SA_RESTART, and raise the signal to avoid restarting syscalls that are |
| 55 | // expected to be interrupted? |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 56 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 57 | |
| 58 | static void log(const char* format, ...) { |
| 59 | char buf[256]; |
| 60 | va_list ap; |
| 61 | va_start(ap, format); |
| 62 | vsnprintf(buf, sizeof(buf), format, ap); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 63 | #ifdef ART_TARGET_ANDROID |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 64 | __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 65 | #else |
| 66 | std::cout << buf << "\n"; |
| 67 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 68 | va_end(ap); |
| 69 | } |
| 70 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 71 | #define fatal(...) log(__VA_ARGS__); abort() |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 72 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 73 | static int sigorset(sigset_t* dest, sigset_t* left, sigset_t* right) { |
| 74 | sigemptyset(dest); |
| 75 | for (size_t i = 0; i < sizeof(sigset_t) * CHAR_BIT; ++i) { |
| 76 | if (sigismember(left, i) == 1 || sigismember(right, i) == 1) { |
| 77 | sigaddset(dest, i); |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 78 | } |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | namespace art { |
| 84 | |
| 85 | static decltype(&sigaction) linked_sigaction; |
| 86 | static decltype(&sigprocmask) linked_sigprocmask; |
| 87 | __thread bool handling_signal; |
| 88 | |
| 89 | class SignalChain { |
| 90 | public: |
| 91 | SignalChain() : claimed_(false) { |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 94 | bool IsClaimed() { |
| 95 | return claimed_; |
| 96 | } |
| 97 | |
| 98 | void Claim(int signo) { |
| 99 | if (!claimed_) { |
| 100 | Register(signo); |
| 101 | claimed_ = true; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 102 | } |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 103 | } |
| 104 | |
| 105 | // Register the signal chain with the kernel if needed. |
| 106 | void Register(int signo) { |
| 107 | struct sigaction handler_action = {}; |
| 108 | handler_action.sa_sigaction = SignalChain::Handler; |
| 109 | handler_action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 110 | sigfillset(&handler_action.sa_mask); |
| 111 | linked_sigaction(signo, &handler_action, &action_); |
| 112 | } |
| 113 | |
| 114 | void SetAction(const struct sigaction* action) { |
| 115 | action_ = *action; |
| 116 | } |
| 117 | |
| 118 | struct sigaction GetAction() { |
| 119 | return action_; |
| 120 | } |
| 121 | |
| 122 | void AddSpecialHandler(SpecialSignalHandlerFn fn) { |
| 123 | for (SpecialSignalHandlerFn& slot : special_handlers_) { |
| 124 | if (slot == nullptr) { |
| 125 | slot = fn; |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | fatal("too many special signal handlers"); |
| 131 | } |
| 132 | |
| 133 | void RemoveSpecialHandler(SpecialSignalHandlerFn fn) { |
| 134 | // This isn't thread safe, but it's unlikely to be a real problem. |
| 135 | size_t len = sizeof(special_handlers_)/sizeof(*special_handlers_); |
| 136 | for (size_t i = 0; i < len; ++i) { |
| 137 | if (special_handlers_[i] == fn) { |
| 138 | for (size_t j = i; j < len - 1; ++j) { |
| 139 | special_handlers_[j] = special_handlers_[j + 1]; |
| 140 | } |
| 141 | special_handlers_[len - 1] = nullptr; |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fatal("failed to find special handler to remove"); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static void Handler(int signo, siginfo_t* siginfo, void*); |
| 151 | |
| 152 | private: |
| 153 | bool claimed_; |
| 154 | struct sigaction action_; |
| 155 | SpecialSignalHandlerFn special_handlers_[2]; |
| 156 | }; |
| 157 | |
| 158 | static SignalChain chains[_NSIG]; |
| 159 | |
| 160 | class ScopedFlagSetter { |
| 161 | public: |
| 162 | explicit ScopedFlagSetter(bool* flag) : flag_(flag) { |
| 163 | *flag_ = true; |
| 164 | } |
| 165 | |
| 166 | ~ScopedFlagSetter() { |
| 167 | *flag_ = false; |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | bool* flag_; |
| 172 | }; |
| 173 | |
| 174 | class ScopedSignalUnblocker { |
| 175 | public: |
| 176 | explicit ScopedSignalUnblocker(const std::initializer_list<int>& signals) { |
| 177 | sigset_t new_mask; |
| 178 | sigemptyset(&new_mask); |
| 179 | for (int signal : signals) { |
| 180 | sigaddset(&new_mask, signal); |
| 181 | } |
| 182 | if (sigprocmask(SIG_UNBLOCK, &new_mask, &previous_mask_) != 0) { |
| 183 | fatal("failed to unblock signals: %s", strerror(errno)); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | ~ScopedSignalUnblocker() { |
| 188 | if (sigprocmask(SIG_SETMASK, &previous_mask_, nullptr) != 0) { |
| 189 | fatal("failed to unblock signals: %s", strerror(errno)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | sigset_t previous_mask_; |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | void SignalChain::Handler(int signo, siginfo_t* siginfo, void* ucontext_raw) { |
| 199 | // Try the special handlers first. |
| 200 | // If one of them crashes, we'll reenter this handler and pass that crash onto the user handler. |
| 201 | if (!handling_signal) { |
| 202 | ScopedFlagSetter flag(&handling_signal); |
| 203 | ScopedSignalUnblocker unblocked { SIGABRT, SIGSEGV, SIGBUS }; // NOLINT |
| 204 | |
| 205 | for (const auto& handler : chains[signo].special_handlers_) { |
| 206 | if (handler != nullptr && handler(signo, siginfo, ucontext_raw)) { |
| 207 | return; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Forward to the user's signal handler. |
| 213 | int handler_flags = chains[signo].action_.sa_flags; |
| 214 | ucontext_t* ucontext = static_cast<ucontext_t*>(ucontext_raw); |
| 215 | sigset_t mask; |
| 216 | sigorset(&mask, &ucontext->uc_sigmask, &chains[signo].action_.sa_mask); |
| 217 | if ((handler_flags & SA_NODEFER)) { |
| 218 | sigdelset(&mask, signo); |
| 219 | } |
| 220 | sigprocmask(SIG_SETMASK, &mask, nullptr); |
| 221 | |
| 222 | if ((handler_flags & SA_SIGINFO)) { |
| 223 | chains[signo].action_.sa_sigaction(signo, siginfo, ucontext_raw); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 224 | } else { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 225 | auto handler = chains[signo].action_.sa_handler; |
| 226 | if (handler == SIG_IGN) { |
| 227 | return; |
| 228 | } else if (handler == SIG_DFL) { |
| 229 | raise(signo); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 230 | } else { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 231 | handler(signo); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 236 | extern "C" int sigaction(int signal, const struct sigaction* new_action, struct sigaction* old_action) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 237 | // If this signal has been claimed as a signal chain, record the user's |
| 238 | // action but don't pass it on to the kernel. |
| 239 | // Note that we check that the signal number is in range here. An out of range signal |
| 240 | // number should behave exactly as the libc sigaction. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 241 | if (signal < 0 || signal >= _NSIG) { |
| 242 | errno = EINVAL; |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (chains[signal].IsClaimed() && new_action != nullptr) { |
| 247 | struct sigaction saved_action = chains[signal].GetAction(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 248 | if (new_action != nullptr) { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 249 | chains[signal].SetAction(new_action); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 250 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 251 | if (old_action != nullptr) { |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 252 | *old_action = saved_action; |
| 253 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | // Will only get here if the signal chain has not been claimed. We want |
| 258 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 259 | InitializeSignalChain(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 260 | return linked_sigaction(signal, new_action, old_action); |
| 261 | } |
| 262 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 263 | extern "C" sighandler_t signal(int signo, sighandler_t handler) { |
| 264 | if (signo < 0 || signo > _NSIG) { |
| 265 | errno = EINVAL; |
| 266 | return SIG_ERR; |
| 267 | } |
| 268 | |
| 269 | struct sigaction sa = {}; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 270 | sigemptyset(&sa.sa_mask); |
| 271 | sa.sa_handler = handler; |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 272 | sa.sa_flags = SA_RESTART | SA_ONSTACK; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 273 | sighandler_t oldhandler; |
| 274 | |
| 275 | // If this signal has been claimed as a signal chain, record the user's |
| 276 | // action but don't pass it on to the kernel. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 277 | if (chains[signo].IsClaimed()) { |
| 278 | oldhandler = reinterpret_cast<sighandler_t>(chains[signo].GetAction().sa_handler); |
| 279 | chains[signo].SetAction(&sa); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 280 | return oldhandler; |
| 281 | } |
| 282 | |
| 283 | // Will only get here if the signal chain has not been claimed. We want |
| 284 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 285 | InitializeSignalChain(); |
| 286 | if (linked_sigaction(signo, &sa, &sa) == -1) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 287 | return SIG_ERR; |
| 288 | } |
| 289 | |
| 290 | return reinterpret_cast<sighandler_t>(sa.sa_handler); |
| 291 | } |
| 292 | |
Dimitry Ivanov | 82b67b9 | 2016-08-01 11:19:03 -0700 | [diff] [blame] | 293 | #if !defined(__LP64__) |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 294 | extern "C" sighandler_t bsd_signal(int signo, sighandler_t handler) { |
| 295 | return signal(signo, handler); |
Dimitry Ivanov | 82b67b9 | 2016-08-01 11:19:03 -0700 | [diff] [blame] | 296 | } |
| 297 | #endif |
| 298 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 299 | extern "C" int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 300 | const sigset_t* new_set_ptr = bionic_new_set; |
| 301 | sigset_t tmpset; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 302 | if (bionic_new_set != nullptr) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 303 | tmpset = *bionic_new_set; |
| 304 | |
| 305 | if (how == SIG_BLOCK) { |
| 306 | // Don't allow claimed signals in the mask. If a signal chain has been claimed |
| 307 | // we can't allow the user to block that signal. |
| 308 | for (int i = 0 ; i < _NSIG; ++i) { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 309 | if (chains[i].IsClaimed() && sigismember(&tmpset, i)) { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 310 | sigdelset(&tmpset, i); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | new_set_ptr = &tmpset; |
| 315 | } |
| 316 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 317 | InitializeSignalChain(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 318 | return linked_sigprocmask(how, new_set_ptr, bionic_old_set); |
| 319 | } |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 320 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 321 | extern "C" void InitializeSignalChain() { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 322 | // Warning. |
| 323 | // Don't call this from within a signal context as it makes calls to |
| 324 | // dlsym. Calling into the dynamic linker will result in locks being |
| 325 | // taken and if it so happens that a signal occurs while one of these |
| 326 | // locks is already taken, dlsym will block trying to reenter a |
| 327 | // mutex and we will never get out of it. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 328 | static bool initialized = false; |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 329 | if (initialized) { |
| 330 | // Don't initialize twice. |
| 331 | return; |
| 332 | } |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 333 | |
| 334 | void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 335 | if (linked_sigaction_sym == nullptr) { |
| 336 | linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); |
| 337 | if (linked_sigaction_sym == nullptr || |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 338 | linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 339 | fatal("Unable to find next sigaction in signal chain"); |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 343 | void* linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask"); |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 344 | if (linked_sigprocmask_sym == nullptr) { |
| 345 | linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask"); |
| 346 | if (linked_sigprocmask_sym == nullptr || |
| 347 | linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) { |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 348 | fatal("Unable to find next sigprocmask in signal chain"); |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 351 | |
| 352 | linked_sigaction = reinterpret_cast<decltype(linked_sigaction)>(linked_sigaction_sym); |
| 353 | linked_sigprocmask = reinterpret_cast<decltype(linked_sigprocmask)>(linked_sigprocmask_sym); |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 354 | initialized = true; |
| 355 | } |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 356 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 357 | extern "C" void AddSpecialSignalHandlerFn(int signal, SpecialSignalHandlerFn fn) { |
| 358 | if (signal <= 0 || signal >= _NSIG) { |
| 359 | fatal("Invalid signal %d", signal); |
| 360 | } |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 361 | |
| 362 | // Set the managed_handler. |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 363 | chains[signal].AddSpecialHandler(fn); |
| 364 | chains[signal].Claim(signal); |
| 365 | } |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 366 | |
Josh Gao | 7600fa9 | 2017-03-15 17:40:42 -0700 | [diff] [blame^] | 367 | extern "C" void RemoveSpecialSignalHandlerFn(int signal, SpecialSignalHandlerFn fn) { |
| 368 | if (signal <= 0 || signal >= _NSIG) { |
| 369 | fatal("Invalid signal %d", signal); |
| 370 | } |
| 371 | |
| 372 | chains[signal].RemoveSpecialHandler(fn); |
| 373 | } |
| 374 | |
| 375 | extern "C" void EnsureFrontOfChain(int signal) { |
| 376 | if (signal <= 0 || signal >= _NSIG) { |
| 377 | fatal("Invalid signal %d", signal); |
| 378 | } |
| 379 | |
| 380 | // Read the current action without looking at the chain, it should be the expected action. |
| 381 | struct sigaction current_action; |
| 382 | InitializeSignalChain(); |
| 383 | linked_sigaction(signal, nullptr, ¤t_action); |
| 384 | // If the sigactions don't match then we put the current action on the chain and make ourself as |
| 385 | // the main action. |
| 386 | if (current_action.sa_sigaction != SignalChain::Handler) { |
| 387 | log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction); |
| 388 | chains[signal].Register(signal); |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 392 | } // namespace art |
| 393 | |