blob: 50bfe70fb6af9871e9a562ba5c9673e4a73c854a [file] [log] [blame]
Dave Allisonf4b80bc2014-05-14 15:41:25 -07001/*
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 Allison69dfe512014-07-11 17:11:58 +000017#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070018#include <android/log.h>
Dave Allison69dfe512014-07-11 17:11:58 +000019#else
20#include <stdarg.h>
21#include <iostream>
22#endif
23
Dave Allisonf4b80bc2014-05-14 15:41:25 -070024#include <dlfcn.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28
Dave Allison69dfe512014-07-11 17:11:58 +000029#if defined(__APPLE__)
30#define _NSIG NSIG
31#endif
32
Dave Allisonf4b80bc2014-05-14 15:41:25 -070033namespace art {
34
35class SignalAction {
36 public:
Dave Allison91a83662014-08-28 16:12:40 -070037 SignalAction() : claimed_(false), uses_old_style_(false) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070038 }
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) {
Dave Allison1f8ef6f2014-08-20 17:38:41 -070048 claimed_ = false;
Dave Allison8ce6b902014-08-26 11:07:58 -070049 sigaction(signal, &action_, NULL); // Restore old action.
Dave Allisonf4b80bc2014-05-14 15:41:25 -070050 }
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.
Dave Allison91a83662014-08-28 16:12:40 -070063 // If oldstyle is true then this action is from an older style signal()
64 // call as opposed to sigaction(). In this case the sa_handler is
65 // used when invoking the user's handler.
66 void SetAction(const struct sigaction& action, bool oldstyle) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070067 action_ = action;
Dave Allison91a83662014-08-28 16:12:40 -070068 uses_old_style_ = oldstyle;
69 }
70
71 bool OldStyle() const {
72 return uses_old_style_;
Dave Allisonf4b80bc2014-05-14 15:41:25 -070073 }
74
75 private:
76 struct sigaction action_; // Action to be performed.
77 bool claimed_; // Whether signal is claimed or not.
Dave Allison91a83662014-08-28 16:12:40 -070078 bool uses_old_style_; // Action is created using signal(). Use sa_handler.
Dave Allisonf4b80bc2014-05-14 15:41:25 -070079};
80
81// User's signal handlers
82static SignalAction user_sigactions[_NSIG];
83
84static void log(const char* format, ...) {
85 char buf[256];
86 va_list ap;
87 va_start(ap, format);
88 vsnprintf(buf, sizeof(buf), format, ap);
Dave Allison69dfe512014-07-11 17:11:58 +000089#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070090 __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf);
Dave Allison69dfe512014-07-11 17:11:58 +000091#else
92 std::cout << buf << "\n";
93#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070094 va_end(ap);
95}
96
97static void CheckSignalValid(int signal) {
98 if (signal <= 0 || signal >= _NSIG) {
99 log("Invalid signal %d", signal);
100 abort();
101 }
102}
103
104// Claim a signal chain for a particular signal.
105void ClaimSignalChain(int signal, struct sigaction* oldaction) {
106 CheckSignalValid(signal);
107 user_sigactions[signal].Claim(*oldaction);
108}
109
110void UnclaimSignalChain(int signal) {
111 CheckSignalValid(signal);
112
113 user_sigactions[signal].Unclaim(signal);
114}
115
116// Invoke the user's signal handler.
117void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) {
118 // Check the arguments.
119 CheckSignalValid(sig);
120
121 // The signal must have been claimed in order to get here. Check it.
122 if (!user_sigactions[sig].IsClaimed()) {
123 abort();
124 }
125
126 const struct sigaction& action = user_sigactions[sig].GetAction();
Dave Allison91a83662014-08-28 16:12:40 -0700127 if (user_sigactions[sig].OldStyle()) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700128 if (action.sa_handler != NULL) {
129 action.sa_handler(sig);
Dave Allison69dfe512014-07-11 17:11:58 +0000130 } else {
131 signal(sig, SIG_DFL);
132 raise(sig);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700133 }
134 } else {
135 if (action.sa_sigaction != NULL) {
136 action.sa_sigaction(sig, info, context);
Dave Allison69dfe512014-07-11 17:11:58 +0000137 } else {
138 signal(sig, SIG_DFL);
139 raise(sig);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700140 }
141 }
142}
143
144extern "C" {
145// These functions are C linkage since they replace the functions in libc.
146
147int sigaction(int signal, const struct sigaction* new_action, struct sigaction* old_action) {
148 // If this signal has been claimed as a signal chain, record the user's
149 // action but don't pass it on to the kernel.
150 // Note that we check that the signal number is in range here. An out of range signal
151 // number should behave exactly as the libc sigaction.
152 if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) {
153 if (old_action != NULL) {
154 *old_action = user_sigactions[signal].GetAction();
155 }
156 if (new_action != NULL) {
Dave Allison91a83662014-08-28 16:12:40 -0700157 user_sigactions[signal].SetAction(*new_action, false);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700158 }
159 return 0;
160 }
161
162 // Will only get here if the signal chain has not been claimed. We want
163 // to pass the sigaction on to the kernel via the real sigaction in libc.
164
165 void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction");
166 if (linked_sigaction_sym == nullptr) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700167 linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction");
168 if (linked_sigaction_sym == nullptr ||
169 linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) {
170 log("Unable to find next sigaction in signal chain");
171 abort();
172 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700173 }
174
175 typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*);
176 SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym);
177 return linked_sigaction(signal, new_action, old_action);
178}
179
Dave Allison91a83662014-08-28 16:12:40 -0700180sighandler_t signal(int signal, sighandler_t handler) {
181 struct sigaction sa;
182 sigemptyset(&sa.sa_mask);
183 sa.sa_handler = handler;
184 sa.sa_flags = SA_RESTART;
185 sighandler_t oldhandler;
186
187 // If this signal has been claimed as a signal chain, record the user's
188 // action but don't pass it on to the kernel.
189 // Note that we check that the signal number is in range here. An out of range signal
190 // number should behave exactly as the libc sigaction.
191 if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) {
192 oldhandler = reinterpret_cast<sighandler_t>(user_sigactions[signal].GetAction().sa_handler);
193 user_sigactions[signal].SetAction(sa, true);
194 return oldhandler;
195 }
196
197 // Will only get here if the signal chain has not been claimed. We want
198 // to pass the sigaction on to the kernel via the real sigaction in libc.
199
200 void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction");
201 if (linked_sigaction_sym == nullptr) {
202 linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction");
203 if (linked_sigaction_sym == nullptr ||
204 linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) {
205 log("Unable to find next sigaction in signal chain");
206 abort();
207 }
208 }
209
210 typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*);
211 SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym);
212 if (linked_sigaction(signal, &sa, &sa) == -1) {
213 return SIG_ERR;
214 }
215
216 return reinterpret_cast<sighandler_t>(sa.sa_handler);
217}
218
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700219int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) {
220 const sigset_t* new_set_ptr = bionic_new_set;
221 sigset_t tmpset;
222 if (bionic_new_set != NULL) {
223 tmpset = *bionic_new_set;
224
225 if (how == SIG_BLOCK) {
226 // Don't allow claimed signals in the mask. If a signal chain has been claimed
227 // we can't allow the user to block that signal.
228 for (int i = 0 ; i < _NSIG; ++i) {
229 if (user_sigactions[i].IsClaimed() && sigismember(&tmpset, i)) {
230 sigdelset(&tmpset, i);
231 }
232 }
233 }
234 new_set_ptr = &tmpset;
235 }
236
237 void* linked_sigprocmask_sym = dlsym(RTLD_NEXT, "sigprocmask");
238 if (linked_sigprocmask_sym == nullptr) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700239 linked_sigprocmask_sym = dlsym(RTLD_DEFAULT, "sigprocmask");
240 if (linked_sigprocmask_sym == nullptr ||
241 linked_sigprocmask_sym == reinterpret_cast<void*>(sigprocmask)) {
242 log("Unable to find next sigprocmask in signal chain");
243 abort();
244 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700245 }
246
247 typedef int (*SigProcMask)(int how, const sigset_t*, sigset_t*);
248 SigProcMask linked_sigprocmask= reinterpret_cast<SigProcMask>(linked_sigprocmask_sym);
249 return linked_sigprocmask(how, new_set_ptr, bionic_old_set);
250}
251} // extern "C"
252} // namespace art
253