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 | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 17 | #ifdef HAVE_ANDROID_OS |
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: |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 42 | SignalAction() : claimed_(false), uses_old_style_(false) { |
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; |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 54 | sigaction(signal, &action_, NULL); // 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 | |
| 80 | private: |
| 81 | struct sigaction action_; // Action to be performed. |
| 82 | bool claimed_; // Whether signal is claimed or not. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 83 | bool uses_old_style_; // Action is created using signal(). Use sa_handler. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // User's signal handlers |
| 87 | static SignalAction user_sigactions[_NSIG]; |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 88 | static bool initialized; |
| 89 | static void* linked_sigaction_sym; |
| 90 | static void* linked_sigprocmask_sym; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 91 | |
| 92 | static void log(const char* format, ...) { |
| 93 | char buf[256]; |
| 94 | va_list ap; |
| 95 | va_start(ap, format); |
| 96 | vsnprintf(buf, sizeof(buf), format, ap); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 97 | #ifdef HAVE_ANDROID_OS |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 98 | __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 99 | #else |
| 100 | std::cout << buf << "\n"; |
| 101 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 102 | va_end(ap); |
| 103 | } |
| 104 | |
| 105 | static void CheckSignalValid(int signal) { |
| 106 | if (signal <= 0 || signal >= _NSIG) { |
| 107 | log("Invalid signal %d", signal); |
| 108 | abort(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Claim a signal chain for a particular signal. |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 113 | extern "C" void ClaimSignalChain(int signal, struct sigaction* oldaction) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 114 | CheckSignalValid(signal); |
| 115 | user_sigactions[signal].Claim(*oldaction); |
| 116 | } |
| 117 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 118 | extern "C" void UnclaimSignalChain(int signal) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 119 | CheckSignalValid(signal); |
| 120 | |
| 121 | user_sigactions[signal].Unclaim(signal); |
| 122 | } |
| 123 | |
| 124 | // Invoke the user's signal handler. |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 125 | extern "C" void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 126 | // Check the arguments. |
| 127 | CheckSignalValid(sig); |
| 128 | |
| 129 | // The signal must have been claimed in order to get here. Check it. |
| 130 | if (!user_sigactions[sig].IsClaimed()) { |
| 131 | abort(); |
| 132 | } |
| 133 | |
| 134 | const struct sigaction& action = user_sigactions[sig].GetAction(); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 135 | if (user_sigactions[sig].OldStyle()) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 136 | if (action.sa_handler != NULL) { |
| 137 | action.sa_handler(sig); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 138 | } else { |
| 139 | signal(sig, SIG_DFL); |
| 140 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 141 | } |
| 142 | } else { |
| 143 | if (action.sa_sigaction != NULL) { |
| 144 | action.sa_sigaction(sig, info, context); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 145 | } else { |
| 146 | signal(sig, SIG_DFL); |
| 147 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 152 | extern "C" void EnsureFrontOfChain(int signal, struct sigaction* expected_action) { |
| 153 | CheckSignalValid(signal); |
| 154 | // Read the current action without looking at the chain, it should be the expected action. |
| 155 | SigActionFnPtr linked_sigaction = reinterpret_cast<SigActionFnPtr>(linked_sigaction_sym); |
| 156 | struct sigaction current_action; |
| 157 | linked_sigaction(signal, nullptr, ¤t_action); |
| 158 | // If the sigactions don't match then we put the current action on the chain and make ourself as |
| 159 | // the main action. |
| 160 | if (current_action.sa_sigaction != expected_action->sa_sigaction) { |
| 161 | log("Warning: Unexpected sigaction action found %p\n", current_action.sa_sigaction); |
| 162 | user_sigactions[signal].Claim(current_action); |
| 163 | linked_sigaction(signal, expected_action, nullptr); |
| 164 | } |
| 165 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 166 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 167 | 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] | 168 | // If this signal has been claimed as a signal chain, record the user's |
| 169 | // action but don't pass it on to the kernel. |
| 170 | // Note that we check that the signal number is in range here. An out of range signal |
| 171 | // number should behave exactly as the libc sigaction. |
| 172 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 173 | struct sigaction saved_action = user_sigactions[signal].GetAction(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 174 | if (new_action != NULL) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 175 | user_sigactions[signal].SetAction(*new_action, false); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 176 | } |
Dmitriy Ivanov | c01683b | 2015-01-06 14:55:26 -0800 | [diff] [blame] | 177 | if (old_action != NULL) { |
| 178 | *old_action = saved_action; |
| 179 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | // Will only get here if the signal chain has not been claimed. We want |
| 184 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 185 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 186 | if (linked_sigaction_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 187 | // Perform lazy initialization. |
| 188 | // This will only occur outside of a signal context since we have |
| 189 | // not been initialized and therefore cannot be within the ART |
| 190 | // runtime. |
| 191 | InitializeSignalChain(); |
| 192 | } |
| 193 | |
| 194 | if (linked_sigaction_sym == nullptr) { |
| 195 | log("Unable to find next sigaction in signal chain"); |
| 196 | abort(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 197 | } |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 198 | SigActionFnPtr linked_sigaction = reinterpret_cast<SigActionFnPtr>(linked_sigaction_sym); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 199 | return linked_sigaction(signal, new_action, old_action); |
| 200 | } |
| 201 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 202 | extern "C" sighandler_t signal(int signal, sighandler_t handler) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 203 | struct sigaction sa; |
| 204 | sigemptyset(&sa.sa_mask); |
| 205 | sa.sa_handler = handler; |
| 206 | sa.sa_flags = SA_RESTART; |
| 207 | sighandler_t oldhandler; |
| 208 | |
| 209 | // If this signal has been claimed as a signal chain, record the user's |
| 210 | // action but don't pass it on to the kernel. |
| 211 | // Note that we check that the signal number is in range here. An out of range signal |
| 212 | // number should behave exactly as the libc sigaction. |
| 213 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { |
| 214 | oldhandler = reinterpret_cast<sighandler_t>(user_sigactions[signal].GetAction().sa_handler); |
| 215 | user_sigactions[signal].SetAction(sa, true); |
| 216 | return oldhandler; |
| 217 | } |
| 218 | |
| 219 | // Will only get here if the signal chain has not been claimed. We want |
| 220 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 221 | |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 222 | if (linked_sigaction_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 223 | // Perform lazy initialization. |
| 224 | InitializeSignalChain(); |
| 225 | } |
| 226 | |
| 227 | if (linked_sigaction_sym == nullptr) { |
| 228 | log("Unable to find next sigaction in signal chain"); |
| 229 | abort(); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); |
| 233 | SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); |
| 234 | if (linked_sigaction(signal, &sa, &sa) == -1) { |
| 235 | return SIG_ERR; |
| 236 | } |
| 237 | |
| 238 | return reinterpret_cast<sighandler_t>(sa.sa_handler); |
| 239 | } |
| 240 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 241 | 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] | 242 | const sigset_t* new_set_ptr = bionic_new_set; |
| 243 | sigset_t tmpset; |
| 244 | if (bionic_new_set != NULL) { |
| 245 | tmpset = *bionic_new_set; |
| 246 | |
| 247 | if (how == SIG_BLOCK) { |
| 248 | // Don't allow claimed signals in the mask. If a signal chain has been claimed |
| 249 | // we can't allow the user to block that signal. |
| 250 | for (int i = 0 ; i < _NSIG; ++i) { |
| 251 | if (user_sigactions[i].IsClaimed() && sigismember(&tmpset, i)) { |
| 252 | sigdelset(&tmpset, i); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | new_set_ptr = &tmpset; |
| 257 | } |
| 258 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 259 | if (linked_sigprocmask_sym == nullptr) { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 260 | // Perform lazy initialization. |
| 261 | InitializeSignalChain(); |
| 262 | } |
| 263 | |
| 264 | if (linked_sigprocmask_sym == nullptr) { |
| 265 | log("Unable to find next sigprocmask in signal chain"); |
| 266 | abort(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | typedef int (*SigProcMask)(int how, const sigset_t*, sigset_t*); |
| 270 | SigProcMask linked_sigprocmask= reinterpret_cast<SigProcMask>(linked_sigprocmask_sym); |
| 271 | return linked_sigprocmask(how, new_set_ptr, bionic_old_set); |
| 272 | } |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 273 | |
Dmitriy Ivanov | f57874d | 2014-10-07 13:43:23 -0700 | [diff] [blame] | 274 | extern "C" void InitializeSignalChain() { |
Dave Allison | cefcea8 | 2014-09-16 10:01:01 -0700 | [diff] [blame] | 275 | // Warning. |
| 276 | // Don't call this from within a signal context as it makes calls to |
| 277 | // dlsym. Calling into the dynamic linker will result in locks being |
| 278 | // taken and if it so happens that a signal occurs while one of these |
| 279 | // locks is already taken, dlsym will block trying to reenter a |
| 280 | // mutex and we will never get out of it. |
| 281 | if (initialized) { |
| 282 | // Don't initialize twice. |
| 283 | return; |
| 284 | } |
| 285 | linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
| 286 | if (linked_sigaction_sym == nullptr) { |
| 287 | linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); |
| 288 | if (linked_sigaction_sym == nullptr || |
| 289 | linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { |
| 290 | linked_sigaction_sym = nullptr; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask"); |
| 295 | if (linked_sigprocmask_sym == nullptr) { |
| 296 | linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask"); |
| 297 | if (linked_sigprocmask_sym == nullptr || |
| 298 | linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) { |
| 299 | linked_sigprocmask_sym = nullptr; |
| 300 | } |
| 301 | } |
| 302 | initialized = true; |
| 303 | } |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 304 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 305 | } // namespace art |
| 306 | |