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