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 | |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 17 | #ifdef __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> |
| 28 | |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 29 | #include "sigchain.h" |
| 30 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 31 | #if defined(__APPLE__) |
| 32 | #define _NSIG NSIG |
Dave Allison | 3868009 | 2014-08-29 12:29:34 -0700 | [diff] [blame] | 33 | #define sighandler_t sig_t |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 38 | typedef int (*SigActionFnPtr)(int, const struct sigaction*, struct sigaction*); |
| 39 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 40 | class SignalAction { |
| 41 | public: |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 42 | SignalAction() : claimed_(false), uses_old_style_(false), special_handler_(nullptr) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Claim the signal and keep the action specified. |
| 46 | void Claim(const struct sigaction& action) { |
| 47 | action_ = action; |
| 48 | claimed_ = true; |
| 49 | } |
| 50 | |
| 51 | // Unclaim the signal and restore the old action. |
| 52 | void Unclaim(int signal) { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 53 | claimed_ = false; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 54 | sigaction(signal, &action_, nullptr); // Restore old action. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Get the action associated with this signal. |
| 58 | const struct sigaction& GetAction() const { |
| 59 | return action_; |
| 60 | } |
| 61 | |
| 62 | // Is the signal claimed? |
| 63 | bool IsClaimed() const { |
| 64 | return claimed_; |
| 65 | } |
| 66 | |
| 67 | // Change the recorded action to that specified. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 68 | // If oldstyle is true then this action is from an older style signal() |
| 69 | // call as opposed to sigaction(). In this case the sa_handler is |
| 70 | // used when invoking the user's handler. |
| 71 | void SetAction(const struct sigaction& action, bool oldstyle) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 72 | action_ = action; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 73 | uses_old_style_ = oldstyle; |
| 74 | } |
| 75 | |
| 76 | bool OldStyle() const { |
| 77 | return uses_old_style_; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 80 | void SetSpecialHandler(SpecialSignalHandlerFn fn) { |
| 81 | special_handler_ = fn; |
| 82 | } |
| 83 | |
| 84 | SpecialSignalHandlerFn GetSpecialHandler() { |
| 85 | return special_handler_; |
| 86 | } |
| 87 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 88 | private: |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 89 | struct sigaction action_; // Action to be performed. |
| 90 | bool claimed_; // Whether signal is claimed or not. |
| 91 | bool uses_old_style_; // Action is created using signal(). Use sa_handler. |
| 92 | SpecialSignalHandlerFn special_handler_; // A special handler executed before user handlers. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // User's signal handlers |
| 96 | static SignalAction user_sigactions[_NSIG]; |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 97 | static bool initialized; |
| 98 | static void* linked_sigaction_sym; |
| 99 | static void* linked_sigprocmask_sym; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 100 | |
| 101 | static void log(const char* format, ...) { |
| 102 | char buf[256]; |
| 103 | va_list ap; |
| 104 | va_start(ap, format); |
| 105 | vsnprintf(buf, sizeof(buf), format, ap); |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 106 | #ifdef __ANDROID__ |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 107 | __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 108 | #else |
| 109 | std::cout << buf << "\n"; |
| 110 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 111 | va_end(ap); |
| 112 | } |
| 113 | |
| 114 | static void CheckSignalValid(int signal) { |
| 115 | if (signal <= 0 || signal >= _NSIG) { |
| 116 | log("Invalid signal %d", signal); |
| 117 | abort(); |
| 118 | } |
| 119 | } |
| 120 | |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 121 | // Sigchainlib's own handler so we can ensure a managed handler is called first even if nobody |
| 122 | // claimed a chain. Simply forward to InvokeUserSignalHandler. |
| 123 | static void sigchainlib_managed_handler_sigaction(int sig, siginfo_t* info, void* context) { |
| 124 | InvokeUserSignalHandler(sig, info, context); |
| 125 | } |
| 126 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 127 | // Claim a signal chain for a particular signal. |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 128 | extern "C" void ClaimSignalChain(int signal, struct sigaction* oldaction) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 129 | CheckSignalValid(signal); |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 130 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 131 | user_sigactions[signal].Claim(*oldaction); |
| 132 | } |
| 133 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 134 | extern "C" void UnclaimSignalChain(int signal) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 135 | CheckSignalValid(signal); |
| 136 | |
| 137 | user_sigactions[signal].Unclaim(signal); |
| 138 | } |
| 139 | |
| 140 | // Invoke the user's signal handler. |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 141 | extern "C" void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 142 | // Check the arguments. |
| 143 | CheckSignalValid(sig); |
| 144 | |
| 145 | // The signal must have been claimed in order to get here. Check it. |
| 146 | if (!user_sigactions[sig].IsClaimed()) { |
| 147 | abort(); |
| 148 | } |
| 149 | |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 150 | // Do we have a managed handler? If so, run it first. |
| 151 | SpecialSignalHandlerFn managed = user_sigactions[sig].GetSpecialHandler(); |
| 152 | if (managed != nullptr) { |
| 153 | // Call the handler. If it succeeds, we're done. |
| 154 | if (managed(sig, info, context)) { |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 159 | const struct sigaction& action = user_sigactions[sig].GetAction(); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 160 | if (user_sigactions[sig].OldStyle()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 161 | if (action.sa_handler != nullptr) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 162 | action.sa_handler(sig); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 163 | } else { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 164 | signal(sig, SIG_DFL); |
| 165 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 166 | } |
| 167 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 168 | if (action.sa_sigaction != nullptr) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 169 | action.sa_sigaction(sig, info, context); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 170 | } else { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 171 | signal(sig, SIG_DFL); |
| 172 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 177 | extern "C" void EnsureFrontOfChain(int signal, struct sigaction* expected_action) { |
| 178 | CheckSignalValid(signal); |
| 179 | // Read the current action without looking at the chain, it should be the expected action. |
| 180 | SigActionFnPtr linked_sigaction = reinterpret_cast<SigActionFnPtr>(linked_sigaction_sym); |
| 181 | struct sigaction current_action; |
| 182 | linked_sigaction(signal, nullptr, ¤t_action); |
| 183 | // If the sigactions don't match then we put the current action on the chain and make ourself as |
| 184 | // the main action. |
| 185 | if (current_action.sa_sigaction != expected_action->sa_sigaction) { |
| 186 | log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction); |
| 187 | user_sigactions[signal].Claim(current_action); |
| 188 | linked_sigaction(signal, expected_action, nullptr); |
| 189 | } |
| 190 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 191 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 192 | 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] | 193 | // If this signal has been claimed as a signal chain, record the user's |
| 194 | // action but don't pass it on to the kernel. |
| 195 | // Note that we check that the signal number is in range here. An out of range signal |
| 196 | // number should behave exactly as the libc sigaction. |
Dmitriy Ivanov | 34a0c20 | 2015-04-02 18:30:22 -0700 | [diff] [blame] | 197 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed() && |
| 198 | (new_action == nullptr || new_action->sa_handler != SIG_DFL)) { |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 199 | struct sigaction saved_action = user_sigactions[signal].GetAction(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 200 | if (new_action != nullptr) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 201 | user_sigactions[signal].SetAction(*new_action, false); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 202 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 203 | if (old_action != nullptr) { |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 204 | *old_action = saved_action; |
| 205 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | // Will only get here if the signal chain has not been claimed. We want |
| 210 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 211 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 212 | if (linked_sigaction_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 213 | // Perform lazy initialization. |
| 214 | // This will only occur outside of a signal context since we have |
| 215 | // not been initialized and therefore cannot be within the ART |
| 216 | // runtime. |
| 217 | InitializeSignalChain(); |
| 218 | } |
| 219 | |
| 220 | if (linked_sigaction_sym == nullptr) { |
| 221 | log("Unable to find next sigaction in signal chain"); |
| 222 | abort(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 223 | } |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 224 | SigActionFnPtr linked_sigaction = reinterpret_cast<SigActionFnPtr>(linked_sigaction_sym); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 225 | return linked_sigaction(signal, new_action, old_action); |
| 226 | } |
| 227 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 228 | extern "C" sighandler_t signal(int signal, sighandler_t handler) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 229 | struct sigaction sa; |
| 230 | sigemptyset(&sa.sa_mask); |
| 231 | sa.sa_handler = handler; |
| 232 | sa.sa_flags = SA_RESTART; |
| 233 | sighandler_t oldhandler; |
| 234 | |
| 235 | // If this signal has been claimed as a signal chain, record the user's |
| 236 | // action but don't pass it on to the kernel. |
| 237 | // Note that we check that the signal number is in range here. An out of range signal |
| 238 | // number should behave exactly as the libc sigaction. |
Dmitriy Ivanov | 34a0c20 | 2015-04-02 18:30:22 -0700 | [diff] [blame] | 239 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed() && handler != SIG_DFL) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 240 | oldhandler = reinterpret_cast<sighandler_t>(user_sigactions[signal].GetAction().sa_handler); |
| 241 | user_sigactions[signal].SetAction(sa, true); |
| 242 | return oldhandler; |
| 243 | } |
| 244 | |
| 245 | // Will only get here if the signal chain has not been claimed. We want |
| 246 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 247 | |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 248 | if (linked_sigaction_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 249 | // Perform lazy initialization. |
| 250 | InitializeSignalChain(); |
| 251 | } |
| 252 | |
| 253 | if (linked_sigaction_sym == nullptr) { |
| 254 | log("Unable to find next sigaction in signal chain"); |
| 255 | abort(); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); |
| 259 | SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); |
| 260 | if (linked_sigaction(signal, &sa, &sa) == -1) { |
| 261 | return SIG_ERR; |
| 262 | } |
| 263 | |
| 264 | return reinterpret_cast<sighandler_t>(sa.sa_handler); |
| 265 | } |
| 266 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 267 | 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] | 268 | const sigset_t* new_set_ptr = bionic_new_set; |
| 269 | sigset_t tmpset; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 270 | if (bionic_new_set != nullptr) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 271 | tmpset = *bionic_new_set; |
| 272 | |
| 273 | if (how == SIG_BLOCK) { |
| 274 | // Don't allow claimed signals in the mask. If a signal chain has been claimed |
| 275 | // we can't allow the user to block that signal. |
| 276 | for (int i = 0 ; i < _NSIG; ++i) { |
| 277 | if (user_sigactions[i].IsClaimed() && sigismember(&tmpset, i)) { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 278 | sigdelset(&tmpset, i); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | new_set_ptr = &tmpset; |
| 283 | } |
| 284 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 285 | if (linked_sigprocmask_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 286 | // Perform lazy initialization. |
| 287 | InitializeSignalChain(); |
| 288 | } |
| 289 | |
| 290 | if (linked_sigprocmask_sym == nullptr) { |
| 291 | log("Unable to find next sigprocmask in signal chain"); |
| 292 | abort(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | typedef int (*SigProcMask)(int how, const sigset_t*, sigset_t*); |
| 296 | SigProcMask linked_sigprocmask= reinterpret_cast<SigProcMask>(linked_sigprocmask_sym); |
| 297 | return linked_sigprocmask(how, new_set_ptr, bionic_old_set); |
| 298 | } |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 299 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 300 | extern "C" void InitializeSignalChain() { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 301 | // Warning. |
| 302 | // Don't call this from within a signal context as it makes calls to |
| 303 | // dlsym. Calling into the dynamic linker will result in locks being |
| 304 | // taken and if it so happens that a signal occurs while one of these |
| 305 | // locks is already taken, dlsym will block trying to reenter a |
| 306 | // mutex and we will never get out of it. |
| 307 | if (initialized) { |
| 308 | // Don't initialize twice. |
| 309 | return; |
| 310 | } |
| 311 | linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
| 312 | if (linked_sigaction_sym == nullptr) { |
| 313 | linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); |
| 314 | if (linked_sigaction_sym == nullptr || |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 315 | linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { |
| 316 | linked_sigaction_sym = nullptr; |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask"); |
| 321 | if (linked_sigprocmask_sym == nullptr) { |
| 322 | linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask"); |
| 323 | if (linked_sigprocmask_sym == nullptr || |
| 324 | linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) { |
Dmitriy Ivanov | a3164b9 | 2015-04-01 11:08:45 -0700 | [diff] [blame] | 325 | linked_sigprocmask_sym = nullptr; |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | initialized = true; |
| 329 | } |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 330 | |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 331 | extern "C" void SetSpecialSignalHandlerFn(int signal, SpecialSignalHandlerFn fn) { |
| 332 | CheckSignalValid(signal); |
| 333 | |
| 334 | // Set the managed_handler. |
| 335 | user_sigactions[signal].SetSpecialHandler(fn); |
| 336 | |
| 337 | // In case the chain isn't claimed, claim it for ourself so we can ensure the managed handler |
| 338 | // goes first. |
| 339 | if (!user_sigactions[signal].IsClaimed()) { |
jgu21 | 0c7c55c | 2015-07-24 13:40:33 +0800 | [diff] [blame] | 340 | struct sigaction act, old_act; |
| 341 | act.sa_sigaction = sigchainlib_managed_handler_sigaction; |
| 342 | sigemptyset(&act.sa_mask); |
| 343 | act.sa_flags = SA_SIGINFO | SA_ONSTACK; |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 344 | #if !defined(__APPLE__) && !defined(__mips__) |
jgu21 | 0c7c55c | 2015-07-24 13:40:33 +0800 | [diff] [blame] | 345 | act.sa_restorer = nullptr; |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 346 | #endif |
jgu21 | 0c7c55c | 2015-07-24 13:40:33 +0800 | [diff] [blame] | 347 | if (sigaction(signal, &act, &old_act) != -1) { |
| 348 | user_sigactions[signal].Claim(old_act); |
| 349 | } |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 353 | } // namespace art |
| 354 | |