blob: 75399904df9fc0bc6942e0ca781d228a6618ad7d [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
Dave Allison38680092014-08-29 12:29:34 -070031#define sighandler_t sig_t
Dave Allison69dfe512014-07-11 17:11:58 +000032#endif
33
Dave Allisonf4b80bc2014-05-14 15:41:25 -070034namespace art {
35
36class SignalAction {
37 public:
Dave Allison91a83662014-08-28 16:12:40 -070038 SignalAction() : claimed_(false), uses_old_style_(false) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070039 }
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 Allison1f8ef6f2014-08-20 17:38:41 -070049 claimed_ = false;
Dave Allison8ce6b902014-08-26 11:07:58 -070050 sigaction(signal, &action_, NULL); // Restore old action.
Dave Allisonf4b80bc2014-05-14 15:41:25 -070051 }
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 Allison91a83662014-08-28 16:12:40 -070064 // 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 Allisonf4b80bc2014-05-14 15:41:25 -070068 action_ = action;
Dave Allison91a83662014-08-28 16:12:40 -070069 uses_old_style_ = oldstyle;
70 }
71
72 bool OldStyle() const {
73 return uses_old_style_;
Dave Allisonf4b80bc2014-05-14 15:41:25 -070074 }
75
76 private:
77 struct sigaction action_; // Action to be performed.
78 bool claimed_; // Whether signal is claimed or not.
Dave Allison91a83662014-08-28 16:12:40 -070079 bool uses_old_style_; // Action is created using signal(). Use sa_handler.
Dave Allisonf4b80bc2014-05-14 15:41:25 -070080};
81
82// User's signal handlers
83static SignalAction user_sigactions[_NSIG];
84
85static 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 Allison69dfe512014-07-11 17:11:58 +000090#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070091 __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf);
Dave Allison69dfe512014-07-11 17:11:58 +000092#else
93 std::cout << buf << "\n";
94#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070095 va_end(ap);
96}
97
98static 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.
106void ClaimSignalChain(int signal, struct sigaction* oldaction) {
107 CheckSignalValid(signal);
108 user_sigactions[signal].Claim(*oldaction);
109}
110
111void UnclaimSignalChain(int signal) {
112 CheckSignalValid(signal);
113
114 user_sigactions[signal].Unclaim(signal);
115}
116
117// Invoke the user's signal handler.
118void 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 Allison91a83662014-08-28 16:12:40 -0700128 if (user_sigactions[sig].OldStyle()) {
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700129 if (action.sa_handler != NULL) {
130 action.sa_handler(sig);
Dave Allison69dfe512014-07-11 17:11:58 +0000131 } else {
132 signal(sig, SIG_DFL);
133 raise(sig);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700134 }
135 } else {
136 if (action.sa_sigaction != NULL) {
137 action.sa_sigaction(sig, info, context);
Dave Allison69dfe512014-07-11 17:11:58 +0000138 } else {
139 signal(sig, SIG_DFL);
140 raise(sig);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700141 }
142 }
143}
144
145extern "C" {
146// These functions are C linkage since they replace the functions in libc.
147
148int 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 Allison91a83662014-08-28 16:12:40 -0700158 user_sigactions[signal].SetAction(*new_action, false);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700159 }
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 Allison8ce6b902014-08-26 11:07:58 -0700168 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 Allisonf4b80bc2014-05-14 15:41:25 -0700174 }
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 Allison91a83662014-08-28 16:12:40 -0700181sighandler_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 Allisonf4b80bc2014-05-14 15:41:25 -0700220int 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 Allison8ce6b902014-08-26 11:07:58 -0700240 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 Allisonf4b80bc2014-05-14 15:41:25 -0700246 }
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