blob: 9e08a8ecc600dd2f593652adae19a673e7179541 [file] [log] [blame]
Colin Cross0965c022016-04-26 16:51:32 -07001/*
2 * Copyright (C) 2016 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
17#ifndef LIBMEMUNREACHABLE_SCOPED_SIGNAL_HANDLER_H_
18#define LIBMEMUNREACHABLE_SCOPED_SIGNAL_HANDLER_H_
19
20#include <errno.h>
21#include <signal.h>
22
23#include <functional>
24
25#include "android-base/macros.h"
26
27#include "log.h"
28
Colin Crossa9939e92017-06-21 13:13:00 -070029namespace android {
30
Colin Cross0965c022016-04-26 16:51:32 -070031class ScopedSignalHandler {
32 public:
33 using Fn = std::function<void(ScopedSignalHandler&, int, siginfo_t*, void*)>;
34
Pirama Arumuga Nainar02ab36e2018-09-26 09:00:24 -070035 explicit ScopedSignalHandler() : signal_(-1) {}
Colin Crossa83881e2017-06-22 10:50:05 -070036 ~ScopedSignalHandler() { reset(); }
Colin Cross0965c022016-04-26 16:51:32 -070037
38 template <class F>
39 void install(int signal, F&& f) {
Colin Crosseac4ecc2017-06-29 17:12:01 -070040 if (signal_ != -1) MEM_LOG_ALWAYS_FATAL("ScopedSignalHandler already installed");
Colin Cross0965c022016-04-26 16:51:32 -070041
Pirama Arumuga Nainar02ab36e2018-09-26 09:00:24 -070042 handler_ = SignalFn([=](int signal, siginfo_t* si, void* uctx) { f(*this, signal, si, uctx); });
Colin Cross0965c022016-04-26 16:51:32 -070043
Christopher Ferris47dea712017-05-03 17:34:29 -070044 struct sigaction act {};
45 act.sa_sigaction = [](int signal, siginfo_t* si, void* uctx) { handler_(signal, si, uctx); };
Colin Cross0965c022016-04-26 16:51:32 -070046 act.sa_flags = SA_SIGINFO;
47
48 int ret = sigaction(signal, &act, &old_act_);
49 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070050 MEM_LOG_ALWAYS_FATAL("failed to install segfault handler: %s", strerror(errno));
Colin Cross0965c022016-04-26 16:51:32 -070051 }
52
53 signal_ = signal;
54 }
55
56 void reset() {
57 if (signal_ != -1) {
58 int ret = sigaction(signal_, &old_act_, NULL);
59 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070060 MEM_ALOGE("failed to uninstall segfault handler");
Colin Cross0965c022016-04-26 16:51:32 -070061 }
62 handler_ = SignalFn{};
63 signal_ = -1;
64 }
65 }
66
Colin Cross0965c022016-04-26 16:51:32 -070067 private:
68 using SignalFn = std::function<void(int, siginfo_t*, void*)>;
69 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
Colin Cross0965c022016-04-26 16:51:32 -070070 int signal_;
71 struct sigaction old_act_;
72 // TODO(ccross): to support multiple ScopedSignalHandlers handler_ would need
73 // to be a static map of signals to handlers, but allocated with Allocator.
74 static SignalFn handler_;
75};
76
Colin Crossa9939e92017-06-21 13:13:00 -070077} // namespace android
78
Colin Crossa83881e2017-06-22 10:50:05 -070079#endif // LIBMEMUNREACHABLE_SCOPED_SIGNAL_HANDLER_H_