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 | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 29 | #if defined(__APPLE__) |
| 30 | #define _NSIG NSIG |
Dave Allison | 3868009 | 2014-08-29 12:29:34 -0700 | [diff] [blame] | 31 | #define sighandler_t sig_t |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
| 36 | class SignalAction { |
| 37 | public: |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 38 | SignalAction() : claimed_(false), uses_old_style_(false) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Claim the signal and keep the action specified. |
| 42 | void Claim(const struct sigaction& action) { |
| 43 | action_ = action; |
| 44 | claimed_ = true; |
| 45 | } |
| 46 | |
| 47 | // Unclaim the signal and restore the old action. |
| 48 | void Unclaim(int signal) { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 49 | claimed_ = false; |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 50 | sigaction(signal, &action_, NULL); // Restore old action. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Get the action associated with this signal. |
| 54 | const struct sigaction& GetAction() const { |
| 55 | return action_; |
| 56 | } |
| 57 | |
| 58 | // Is the signal claimed? |
| 59 | bool IsClaimed() const { |
| 60 | return claimed_; |
| 61 | } |
| 62 | |
| 63 | // Change the recorded action to that specified. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 64 | // If oldstyle is true then this action is from an older style signal() |
| 65 | // call as opposed to sigaction(). In this case the sa_handler is |
| 66 | // used when invoking the user's handler. |
| 67 | void SetAction(const struct sigaction& action, bool oldstyle) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 68 | action_ = action; |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 69 | uses_old_style_ = oldstyle; |
| 70 | } |
| 71 | |
| 72 | bool OldStyle() const { |
| 73 | return uses_old_style_; |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | private: |
| 77 | struct sigaction action_; // Action to be performed. |
| 78 | bool claimed_; // Whether signal is claimed or not. |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 79 | bool uses_old_style_; // Action is created using signal(). Use sa_handler. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | // User's signal handlers |
| 83 | static SignalAction user_sigactions[_NSIG]; |
| 84 | |
| 85 | static void log(const char* format, ...) { |
| 86 | char buf[256]; |
| 87 | va_list ap; |
| 88 | va_start(ap, format); |
| 89 | vsnprintf(buf, sizeof(buf), format, ap); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 90 | #ifdef HAVE_ANDROID_OS |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 91 | __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 92 | #else |
| 93 | std::cout << buf << "\n"; |
| 94 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 95 | va_end(ap); |
| 96 | } |
| 97 | |
| 98 | static void CheckSignalValid(int signal) { |
| 99 | if (signal <= 0 || signal >= _NSIG) { |
| 100 | log("Invalid signal %d", signal); |
| 101 | abort(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Claim a signal chain for a particular signal. |
| 106 | void ClaimSignalChain(int signal, struct sigaction* oldaction) { |
| 107 | CheckSignalValid(signal); |
| 108 | user_sigactions[signal].Claim(*oldaction); |
| 109 | } |
| 110 | |
| 111 | void UnclaimSignalChain(int signal) { |
| 112 | CheckSignalValid(signal); |
| 113 | |
| 114 | user_sigactions[signal].Unclaim(signal); |
| 115 | } |
| 116 | |
| 117 | // Invoke the user's signal handler. |
| 118 | void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) { |
| 119 | // Check the arguments. |
| 120 | CheckSignalValid(sig); |
| 121 | |
| 122 | // The signal must have been claimed in order to get here. Check it. |
| 123 | if (!user_sigactions[sig].IsClaimed()) { |
| 124 | abort(); |
| 125 | } |
| 126 | |
| 127 | const struct sigaction& action = user_sigactions[sig].GetAction(); |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 128 | if (user_sigactions[sig].OldStyle()) { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 129 | if (action.sa_handler != NULL) { |
| 130 | action.sa_handler(sig); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 131 | } else { |
| 132 | signal(sig, SIG_DFL); |
| 133 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 134 | } |
| 135 | } else { |
| 136 | if (action.sa_sigaction != NULL) { |
| 137 | action.sa_sigaction(sig, info, context); |
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 | } |
| 143 | } |
| 144 | |
| 145 | extern "C" { |
| 146 | // These functions are C linkage since they replace the functions in libc. |
| 147 | |
| 148 | int sigaction(int signal, const struct sigaction* new_action, struct sigaction* old_action) { |
| 149 | // If this signal has been claimed as a signal chain, record the user's |
| 150 | // action but don't pass it on to the kernel. |
| 151 | // Note that we check that the signal number is in range here. An out of range signal |
| 152 | // number should behave exactly as the libc sigaction. |
| 153 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { |
| 154 | if (old_action != NULL) { |
| 155 | *old_action = user_sigactions[signal].GetAction(); |
| 156 | } |
| 157 | if (new_action != NULL) { |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 158 | user_sigactions[signal].SetAction(*new_action, false); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | // Will only get here if the signal chain has not been claimed. We want |
| 164 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 165 | |
| 166 | void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
| 167 | if (linked_sigaction_sym == nullptr) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 168 | linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); |
| 169 | if (linked_sigaction_sym == nullptr || |
| 170 | linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { |
| 171 | log("Unable to find next sigaction in signal chain"); |
| 172 | abort(); |
| 173 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); |
| 177 | SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); |
| 178 | return linked_sigaction(signal, new_action, old_action); |
| 179 | } |
| 180 | |
Dave Allison | 91a8366 | 2014-08-28 16:12:40 -0700 | [diff] [blame] | 181 | sighandler_t signal(int signal, sighandler_t handler) { |
| 182 | struct sigaction sa; |
| 183 | sigemptyset(&sa.sa_mask); |
| 184 | sa.sa_handler = handler; |
| 185 | sa.sa_flags = SA_RESTART; |
| 186 | sighandler_t oldhandler; |
| 187 | |
| 188 | // If this signal has been claimed as a signal chain, record the user's |
| 189 | // action but don't pass it on to the kernel. |
| 190 | // Note that we check that the signal number is in range here. An out of range signal |
| 191 | // number should behave exactly as the libc sigaction. |
| 192 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { |
| 193 | oldhandler = reinterpret_cast<sighandler_t>(user_sigactions[signal].GetAction().sa_handler); |
| 194 | user_sigactions[signal].SetAction(sa, true); |
| 195 | return oldhandler; |
| 196 | } |
| 197 | |
| 198 | // Will only get here if the signal chain has not been claimed. We want |
| 199 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 200 | |
| 201 | void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
| 202 | if (linked_sigaction_sym == nullptr) { |
| 203 | linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); |
| 204 | if (linked_sigaction_sym == nullptr || |
| 205 | linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { |
| 206 | log("Unable to find next sigaction in signal chain"); |
| 207 | abort(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); |
| 212 | SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); |
| 213 | if (linked_sigaction(signal, &sa, &sa) == -1) { |
| 214 | return SIG_ERR; |
| 215 | } |
| 216 | |
| 217 | return reinterpret_cast<sighandler_t>(sa.sa_handler); |
| 218 | } |
| 219 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 220 | int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) { |
| 221 | const sigset_t* new_set_ptr = bionic_new_set; |
| 222 | sigset_t tmpset; |
| 223 | if (bionic_new_set != NULL) { |
| 224 | tmpset = *bionic_new_set; |
| 225 | |
| 226 | if (how == SIG_BLOCK) { |
| 227 | // Don't allow claimed signals in the mask. If a signal chain has been claimed |
| 228 | // we can't allow the user to block that signal. |
| 229 | for (int i = 0 ; i < _NSIG; ++i) { |
| 230 | if (user_sigactions[i].IsClaimed() && sigismember(&tmpset, i)) { |
| 231 | sigdelset(&tmpset, i); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | new_set_ptr = &tmpset; |
| 236 | } |
| 237 | |
| 238 | void* linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask"); |
| 239 | if (linked_sigprocmask_sym == nullptr) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 240 | linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask"); |
| 241 | if (linked_sigprocmask_sym == nullptr || |
| 242 | linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) { |
| 243 | log("Unable to find next sigprocmask in signal chain"); |
| 244 | abort(); |
| 245 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | typedef int (*SigProcMask)(int how, const sigset_t*, sigset_t*); |
| 249 | SigProcMask linked_sigprocmask= reinterpret_cast<SigProcMask>(linked_sigprocmask_sym); |
| 250 | return linked_sigprocmask(how, new_set_ptr, bionic_old_set); |
| 251 | } |
| 252 | } // extern "C" |
| 253 | } // namespace art |
| 254 | |