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 |
| 31 | #endif |
| 32 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
| 35 | class SignalAction { |
| 36 | public: |
| 37 | SignalAction() : claimed_(false) { |
| 38 | } |
| 39 | |
| 40 | // Claim the signal and keep the action specified. |
| 41 | void Claim(const struct sigaction& action) { |
| 42 | action_ = action; |
| 43 | claimed_ = true; |
| 44 | } |
| 45 | |
| 46 | // Unclaim the signal and restore the old action. |
| 47 | void Unclaim(int signal) { |
| 48 | claimed_ = false; |
| 49 | sigaction(signal, &action_, NULL); // Restore old action. |
| 50 | } |
| 51 | |
| 52 | // Get the action associated with this signal. |
| 53 | const struct sigaction& GetAction() const { |
| 54 | return action_; |
| 55 | } |
| 56 | |
| 57 | // Is the signal claimed? |
| 58 | bool IsClaimed() const { |
| 59 | return claimed_; |
| 60 | } |
| 61 | |
| 62 | // Change the recorded action to that specified. |
| 63 | void SetAction(const struct sigaction& action) { |
| 64 | action_ = action; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | struct sigaction action_; // Action to be performed. |
| 69 | bool claimed_; // Whether signal is claimed or not. |
| 70 | }; |
| 71 | |
| 72 | // User's signal handlers |
| 73 | static SignalAction user_sigactions[_NSIG]; |
| 74 | |
| 75 | static void log(const char* format, ...) { |
| 76 | char buf[256]; |
| 77 | va_list ap; |
| 78 | va_start(ap, format); |
| 79 | vsnprintf(buf, sizeof(buf), format, ap); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame^] | 80 | #ifdef HAVE_ANDROID_OS |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 81 | __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame^] | 82 | #else |
| 83 | std::cout << buf << "\n"; |
| 84 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 85 | va_end(ap); |
| 86 | } |
| 87 | |
| 88 | static void CheckSignalValid(int signal) { |
| 89 | if (signal <= 0 || signal >= _NSIG) { |
| 90 | log("Invalid signal %d", signal); |
| 91 | abort(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Claim a signal chain for a particular signal. |
| 96 | void ClaimSignalChain(int signal, struct sigaction* oldaction) { |
| 97 | CheckSignalValid(signal); |
| 98 | user_sigactions[signal].Claim(*oldaction); |
| 99 | } |
| 100 | |
| 101 | void UnclaimSignalChain(int signal) { |
| 102 | CheckSignalValid(signal); |
| 103 | |
| 104 | user_sigactions[signal].Unclaim(signal); |
| 105 | } |
| 106 | |
| 107 | // Invoke the user's signal handler. |
| 108 | void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) { |
| 109 | // Check the arguments. |
| 110 | CheckSignalValid(sig); |
| 111 | |
| 112 | // The signal must have been claimed in order to get here. Check it. |
| 113 | if (!user_sigactions[sig].IsClaimed()) { |
| 114 | abort(); |
| 115 | } |
| 116 | |
| 117 | const struct sigaction& action = user_sigactions[sig].GetAction(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 118 | if ((action.sa_flags & SA_SIGINFO) == 0) { |
| 119 | if (action.sa_handler != NULL) { |
| 120 | action.sa_handler(sig); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame^] | 121 | } else { |
| 122 | signal(sig, SIG_DFL); |
| 123 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 124 | } |
| 125 | } else { |
| 126 | if (action.sa_sigaction != NULL) { |
| 127 | action.sa_sigaction(sig, info, context); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame^] | 128 | } else { |
| 129 | signal(sig, SIG_DFL); |
| 130 | raise(sig); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | extern "C" { |
| 136 | // These functions are C linkage since they replace the functions in libc. |
| 137 | |
| 138 | int sigaction(int signal, const struct sigaction* new_action, struct sigaction* old_action) { |
| 139 | // If this signal has been claimed as a signal chain, record the user's |
| 140 | // action but don't pass it on to the kernel. |
| 141 | // Note that we check that the signal number is in range here. An out of range signal |
| 142 | // number should behave exactly as the libc sigaction. |
| 143 | if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { |
| 144 | if (old_action != NULL) { |
| 145 | *old_action = user_sigactions[signal].GetAction(); |
| 146 | } |
| 147 | if (new_action != NULL) { |
| 148 | user_sigactions[signal].SetAction(*new_action); |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | // Will only get here if the signal chain has not been claimed. We want |
| 154 | // to pass the sigaction on to the kernel via the real sigaction in libc. |
| 155 | |
| 156 | void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); |
| 157 | if (linked_sigaction_sym == nullptr) { |
| 158 | log("Unable to find next sigaction in signal chain"); |
| 159 | abort(); |
| 160 | } |
| 161 | |
| 162 | typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); |
| 163 | SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); |
| 164 | return linked_sigaction(signal, new_action, old_action); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) { |
| 169 | const sigset_t* new_set_ptr = bionic_new_set; |
| 170 | sigset_t tmpset; |
| 171 | if (bionic_new_set != NULL) { |
| 172 | tmpset = *bionic_new_set; |
| 173 | |
| 174 | if (how == SIG_BLOCK) { |
| 175 | // Don't allow claimed signals in the mask. If a signal chain has been claimed |
| 176 | // we can't allow the user to block that signal. |
| 177 | for (int i = 0 ; i < _NSIG; ++i) { |
| 178 | if (user_sigactions[i].IsClaimed() && sigismember(&tmpset, i)) { |
| 179 | sigdelset(&tmpset, i); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | new_set_ptr = &tmpset; |
| 184 | } |
| 185 | |
| 186 | void* linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask"); |
| 187 | if (linked_sigprocmask_sym == nullptr) { |
| 188 | log("Unable to find next sigprocmask in signal chain"); |
| 189 | abort(); |
| 190 | } |
| 191 | |
| 192 | typedef int (*SigProcMask)(int how, const sigset_t*, sigset_t*); |
| 193 | SigProcMask linked_sigprocmask= reinterpret_cast<SigProcMask>(linked_sigprocmask_sym); |
| 194 | return linked_sigprocmask(how, new_set_ptr, bionic_old_set); |
| 195 | } |
| 196 | } // extern "C" |
| 197 | } // namespace art |
| 198 | |