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 "bionic_macros.h" 22 23 class ScopedSignalBlocker { 24 public: 25 // Block all signals. ScopedSignalBlocker()26 explicit ScopedSignalBlocker() { 27 sigset64_t set; 28 sigfillset64(&set); 29 sigprocmask64(SIG_BLOCK, &set, &old_set_); 30 } 31 32 // Block just the specified signal. ScopedSignalBlocker(int signal)33 explicit ScopedSignalBlocker(int signal) { 34 sigset64_t set = {}; 35 sigaddset64(&set, signal); 36 sigprocmask64(SIG_BLOCK, &set, &old_set_); 37 } 38 ~ScopedSignalBlocker()39 ~ScopedSignalBlocker() { 40 reset(); 41 } 42 reset()43 void reset() { 44 sigprocmask64(SIG_SETMASK, &old_set_, nullptr); 45 } 46 47 sigset64_t old_set_; 48 49 BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker); 50 }; 51