blob: 58ac2aaefb92e9e30d4c359a5079896b2e7f4636 [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
29class ScopedSignalHandler {
30 public:
31 using Fn = std::function<void(ScopedSignalHandler&, int, siginfo_t*, void*)>;
32
Chih-Hung Hsieh034c4752016-07-12 13:50:44 -070033 explicit ScopedSignalHandler(Allocator<Fn> allocator) : allocator_(allocator), signal_(-1) {}
Colin Crossa83881e2017-06-22 10:50:05 -070034 ~ScopedSignalHandler() { reset(); }
Colin Cross0965c022016-04-26 16:51:32 -070035
36 template <class F>
37 void install(int signal, F&& f) {
Elliott Hughes56731b42017-06-20 17:31:29 -070038 if (signal != -1) MEM_LOG_ALWAYS_FATAL("ScopedSignalHandler already installed");
Colin Cross0965c022016-04-26 16:51:32 -070039
40 handler_ = SignalFn(std::allocator_arg, allocator_,
Christopher Ferris47dea712017-05-03 17:34:29 -070041 [=](int signal, siginfo_t* si, void* uctx) { f(*this, signal, si, uctx); });
Colin Cross0965c022016-04-26 16:51:32 -070042
Christopher Ferris47dea712017-05-03 17:34:29 -070043 struct sigaction act {};
44 act.sa_sigaction = [](int signal, siginfo_t* si, void* uctx) { handler_(signal, si, uctx); };
Colin Cross0965c022016-04-26 16:51:32 -070045 act.sa_flags = SA_SIGINFO;
46
47 int ret = sigaction(signal, &act, &old_act_);
48 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070049 MEM_LOG_ALWAYS_FATAL("failed to install segfault handler: %s", strerror(errno));
Colin Cross0965c022016-04-26 16:51:32 -070050 }
51
52 signal_ = signal;
53 }
54
55 void reset() {
56 if (signal_ != -1) {
57 int ret = sigaction(signal_, &old_act_, NULL);
58 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -070059 MEM_ALOGE("failed to uninstall segfault handler");
Colin Cross0965c022016-04-26 16:51:32 -070060 }
61 handler_ = SignalFn{};
62 signal_ = -1;
63 }
64 }
65
Colin Cross0965c022016-04-26 16:51:32 -070066 private:
67 using SignalFn = std::function<void(int, siginfo_t*, void*)>;
68 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
69 Allocator<Fn> allocator_;
70 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 Crossa83881e2017-06-22 10:50:05 -070077#endif // LIBMEMUNREACHABLE_SCOPED_SIGNAL_HANDLER_H_