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