| Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2012 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 | #pragma once | 
|  | 18 |  | 
|  | 19 | #include <signal.h> | 
|  | 20 |  | 
|  | 21 | class ScopedSignalHandler { | 
|  | 22 | public: | 
|  | 23 | ScopedSignalHandler(int signal_number, void (*handler)(int), int sa_flags = 0) | 
|  | 24 | : signal_number_(signal_number) { | 
|  | 25 | action_ = { .sa_flags = sa_flags, .sa_handler = handler }; | 
|  | 26 | sigaction64(signal_number_, &action_, &old_action_); | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | ScopedSignalHandler(int signal_number, void (*action)(int, siginfo_t*, void*), | 
|  | 30 | int sa_flags = SA_SIGINFO) | 
|  | 31 | : signal_number_(signal_number) { | 
|  | 32 | action_ = { .sa_flags = sa_flags, .sa_sigaction = action }; | 
|  | 33 | sigaction64(signal_number_, &action_, &old_action_); | 
|  | 34 | } | 
|  | 35 |  | 
| Chih-Hung Hsieh | 770032d | 2019-01-02 10:59:48 -0800 | [diff] [blame] | 36 | explicit ScopedSignalHandler(int signal_number) : signal_number_(signal_number) { | 
| Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 37 | sigaction64(signal_number, nullptr, &old_action_); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | ~ScopedSignalHandler() { | 
|  | 41 | sigaction64(signal_number_, &old_action_, nullptr); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | struct sigaction64 action_; | 
|  | 45 | struct sigaction64 old_action_; | 
|  | 46 | const int signal_number_; | 
|  | 47 | }; |