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 17 #pragma once 18 19 #include <signal.h> 20 21 #include "platform/bionic/macros.h" 22 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 29 class ScopedSignalBlocker { 30 public: 31 // Block all signals. ScopedSignalBlocker()32 explicit ScopedSignalBlocker() { 33 sigset64_t set; 34 sigfillset64(&set); 35 __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t)); 36 } 37 38 // Block just the specified signal. ScopedSignalBlocker(int signal)39 explicit ScopedSignalBlocker(int signal) { 40 sigset64_t set = {}; 41 sigaddset64(&set, signal); 42 __rt_sigprocmask(SIG_BLOCK, &set, &old_set_, sizeof(sigset64_t)); 43 } 44 ~ScopedSignalBlocker()45 ~ScopedSignalBlocker() { 46 reset(); 47 } 48 reset()49 void reset() { 50 __rt_sigprocmask(SIG_SETMASK, &old_set_, nullptr, sizeof(sigset64_t)); 51 } 52 53 sigset64_t old_set_; 54 55 BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker); 56 }; 57