blob: ce0ae64d03db47dda30c55c89c7de51a6db7cfa9 [file] [log] [blame]
Elliott Hughes14e3ff92017-10-06 16:58:36 -07001/*
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 Hughes71ba5892018-02-07 12:44:45 -080017#pragma once
Elliott Hughes14e3ff92017-10-06 16:58:36 -070018
19#include <signal.h>
20
Josh Gao4956c372019-12-19 16:35:51 -080021#include "platform/bionic/macros.h"
Elliott Hughes14e3ff92017-10-06 16:58:36 -070022
23class ScopedSignalBlocker {
24 public:
Elliott Hughes71ba5892018-02-07 12:44:45 -080025 // Block all signals.
Elliott Hughes14e3ff92017-10-06 16:58:36 -070026 explicit ScopedSignalBlocker() {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080027 sigset64_t set;
28 sigfillset64(&set);
Elliott Hughes71ba5892018-02-07 12:44:45 -080029 sigprocmask64(SIG_BLOCK, &set, &old_set_);
30 }
31
32 // Block just the specified signal.
33 explicit ScopedSignalBlocker(int signal) {
34 sigset64_t set = {};
35 sigaddset64(&set, signal);
36 sigprocmask64(SIG_BLOCK, &set, &old_set_);
Elliott Hughes14e3ff92017-10-06 16:58:36 -070037 }
38
39 ~ScopedSignalBlocker() {
40 reset();
41 }
42
43 void reset() {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080044 sigprocmask64(SIG_SETMASK, &old_set_, nullptr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -070045 }
46
Elliott Hughes5905d6f2018-01-30 15:09:51 -080047 sigset64_t old_set_;
Elliott Hughes14e3ff92017-10-06 16:58:36 -070048
Elliott Hughes5e62b342018-10-25 11:00:00 -070049 BIONIC_DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker);
Elliott Hughes14e3ff92017-10-06 16:58:36 -070050};