114e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes/*
214e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * Copyright (C) 2017 The Android Open Source Project
314e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes *
414e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
514e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * you may not use this file except in compliance with the License.
614e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * You may obtain a copy of the License at
714e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes *
814e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
914e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes *
1014e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1114e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1214e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * See the License for the specific language governing permissions and
1414e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes * limitations under the License.
1514e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes */
1614e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
1714e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes#ifndef SCOPED_SIGNAL_BLOCKER_H
1814e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes#define SCOPED_SIGNAL_BLOCKER_H
1914e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
2014e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes#include <signal.h>
2114e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
2214e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes#include "bionic_macros.h"
2314e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
2414e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughesclass ScopedSignalBlocker {
2514e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes public:
2614e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  explicit ScopedSignalBlocker() {
275905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes    sigset64_t set;
285905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes    sigfillset64(&set);
295905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes    sigprocmask64(SIG_SETMASK, &set, &old_set_);
3014e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  }
3114e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
3214e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  ~ScopedSignalBlocker() {
3314e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes    reset();
3414e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  }
3514e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
3614e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  void reset() {
375905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes    sigprocmask64(SIG_SETMASK, &old_set_, nullptr);
3814e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  }
3914e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
4014e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes private:
415905d6f8797056ca4178d42bf1220b6692e557a5Elliott Hughes  sigset64_t old_set_;
4214e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
4314e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker);
4414e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes};
4514e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes
4614e3ff9f09fdd52db43628ccd6f39a6d3fb41740Elliott Hughes#endif
47