Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [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 | #ifndef _BIONIC_TESTS_SCOPED_SIGNAL_HANDLER_H |
| 18 | #define _BIONIC_TESTS_SCOPED_SIGNAL_HANDLER_H |
| 19 | |
| 20 | #include <signal.h> |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 21 | #include <string.h> |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 23 | #if defined(__GLIBC__) |
| 24 | #define posix_spawnattr_getsigdefault64 posix_spawnattr_getsigdefault |
| 25 | #define posix_spawnattr_getsigmask64 posix_spawnattr_getsigmask |
| 26 | #define posix_spawnattr_setsigdefault64 posix_spawnattr_setsigdefault |
| 27 | #define posix_spawnattr_setsigmask64 posix_spawnattr_setsigmask |
| 28 | #define pthread_sigmask64 pthread_sigmask |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 29 | #define sigaction64 sigaction |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 30 | #define sigaddset64 sigaddset |
| 31 | #define sigdelset64 sigdelset |
| 32 | #define sigemptyset64 sigemptyset |
| 33 | #define sigfillset64 sigfillset |
| 34 | #define sigismember64 sigismember |
| 35 | #define sigpending64 sigpending |
| 36 | #define sigprocmask64 sigprocmask |
| 37 | #define sigset64_t sigset_t |
| 38 | #define sigsuspend64 sigsuspend |
| 39 | #define sigtimedwait64 sigtimedwait |
| 40 | #define sigwait64 sigwait |
| 41 | #define sigwaitinfo64 sigwaitinfo |
| 42 | #endif |
| 43 | |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 44 | class ScopedSignalHandler { |
| 45 | public: |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 46 | ScopedSignalHandler(int signal_number, void (*handler)(int), int sa_flags = 0) |
| 47 | : signal_number_(signal_number) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 48 | memset(&action_, 0, sizeof(action_)); |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 49 | action_.sa_flags = sa_flags; |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 50 | action_.sa_handler = handler; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 51 | sigaction64(signal_number_, &action_, &old_action_); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 54 | ScopedSignalHandler(int signal_number, void (*action)(int, siginfo_t*, void*), |
| 55 | int sa_flags = SA_SIGINFO) |
| 56 | : signal_number_(signal_number) { |
| 57 | memset(&action_, 0, sizeof(action_)); |
| 58 | action_.sa_flags = sa_flags; |
| 59 | action_.sa_sigaction = action; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 60 | sigaction64(signal_number_, &action_, &old_action_); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 63 | ScopedSignalHandler(int signal_number) : signal_number_(signal_number) { |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 64 | sigaction64(signal_number, nullptr, &old_action_); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 67 | ~ScopedSignalHandler() { |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 68 | sigaction64(signal_number_, &old_action_, NULL); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private: |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 72 | struct sigaction64 action_; |
| 73 | struct sigaction64 old_action_; |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 74 | const int signal_number_; |
| 75 | }; |
| 76 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 77 | class SignalMaskRestorer { |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 78 | public: |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 79 | SignalMaskRestorer() { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 80 | sigprocmask64(SIG_SETMASK, nullptr, &old_mask_); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 83 | ~SignalMaskRestorer() { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 84 | sigprocmask64(SIG_SETMASK, &old_mask_, nullptr); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 88 | sigset64_t old_mask_; |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 91 | #endif // _BIONIC_TESTS_SCOPED_SIGNAL_HANDLER_H |