blob: ff53fad8f3f084a2f9c36ea84663bba0d45934e0 [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
Chih-Hung Hsieh034c4752016-07-12 13:50:44 -070035 explicit ScopedSignalHandler(Allocator<Fn> allocator) : allocator_(allocator), 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
42 handler_ = SignalFn(std::allocator_arg, allocator_,
Christopher Ferris47dea712017-05-03 17:34:29 -070043 [=](int signal, siginfo_t* si, void* uctx) { f(*this, signal, si, uctx); });
Colin Cross0965c022016-04-26 16:51:32 -070044
Christopher Ferris47dea712017-05-03 17:34:29 -070045 struct sigaction act {};
46 act.sa_sigaction = [](int signal, siginfo_t* si, void* uctx) { handler_(signal, si, uctx); };
Colin Cross0965c022016-04-26 16:51:32 -070047 act.sa_flags = SA_SIGINFO;
48
49 int ret = sigaction(signal, &act, &old_act_);
50 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070051 MEM_LOG_ALWAYS_FATAL("failed to install segfault handler: %s", strerror(errno));
Colin Cross0965c022016-04-26 16:51:32 -070052 }
53
54 signal_ = signal;
55 }
56
57 void reset() {
58 if (signal_ != -1) {
59 int ret = sigaction(signal_, &old_act_, NULL);
60 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070061 MEM_ALOGE("failed to uninstall segfault handler");
Colin Cross0965c022016-04-26 16:51:32 -070062 }
63 handler_ = SignalFn{};
64 signal_ = -1;
65 }
66 }
67
Colin Cross0965c022016-04-26 16:51:32 -070068 private:
69 using SignalFn = std::function<void(int, siginfo_t*, void*)>;
70 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
71 Allocator<Fn> allocator_;
72 int signal_;
73 struct sigaction old_act_;
74 // TODO(ccross): to support multiple ScopedSignalHandlers handler_ would need
75 // to be a static map of signals to handlers, but allocated with Allocator.
76 static SignalFn handler_;
77};
78
Colin Crossa9939e92017-06-21 13:13:00 -070079} // namespace android
80
Colin Crossa83881e2017-06-22 10:50:05 -070081#endif // LIBMEMUNREACHABLE_SCOPED_SIGNAL_HANDLER_H_