Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 17 | #pragma once |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <signal.h> |
| 20 | |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 21 | #include "platform/bionic/macros.h" |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 3093e71 | 2020-04-23 15:53:17 -0700 | [diff] [blame] | 23 | // This code needs to really block all the signals, not just the user-visible |
| 24 | // ones. We call __rt_sigprocmask(2) directly so we don't mask out our own |
| 25 | // signals (https://issuetracker.google.com/153624226 was a pthread_exit(3) |
| 26 | // crash because a request to dump the thread's stack came in as it was exiting). |
| 27 | extern "C" int __rt_sigprocmask(int, const sigset64_t*, sigset64_t*, size_t); |
| 28 | |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 29 | class ScopedSignalBlocker { |
| 30 | public: |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 31 | // Block all signals. |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 32 | explicit ScopedSignalBlocker() { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 33 | sigset64_t set; |
| 34 | sigfillset64(&set); |
Elliott Hughes | 3093e71 | 2020-04-23 15:53:17 -0700 | [diff] [blame] | 35 | __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t)); |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Block just the specified signal. |
| 39 | explicit ScopedSignalBlocker(int signal) { |
| 40 | sigset64_t set = {}; |
| 41 | sigaddset64(&set, signal); |
Elliott Hughes | 3093e71 | 2020-04-23 15:53:17 -0700 | [diff] [blame] | 42 | __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t)); |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ~ScopedSignalBlocker() { |
| 46 | reset(); |
| 47 | } |
| 48 | |
| 49 | void reset() { |
Elliott Hughes | 3093e71 | 2020-04-23 15:53:17 -0700 | [diff] [blame] | 50 | __rt_sigprocmask(SIG_SETMASK, &old_set_, nullptr, sizeof(sigset64_t)); |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 53 | sigset64_t old_set_; |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 55 | BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker); |
Elliott Hughes | 14e3ff9 | 2017-10-06 16:58:36 -0700 | [diff] [blame] | 56 | }; |