1 /* 2 * Copyright (C) 2023 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 #ifndef BERBERIS_GUEST_OS_PRIMITIVES_SCOPED_SIGNAL_BLOCKER_H_ 18 #define BERBERIS_GUEST_OS_PRIMITIVES_SCOPED_SIGNAL_BLOCKER_H_ 19 20 #include <signal.h> 21 22 #include "berberis/base/host_signal.h" 23 24 namespace berberis { 25 26 // Disable signals for scope. 27 // Might be called recursively. 28 // ATTENTION: Don't call (pthread_)sigmask while inside guarded scope! 29 class ScopedSignalBlocker { 30 public: ScopedSignalBlocker()31 ScopedSignalBlocker() { 32 HostSigset mask; 33 HostSigfillset(&mask); 34 Init(&mask); 35 } 36 ScopedSignalBlocker(const ScopedSignalBlocker&) = delete; 37 ScopedSignalBlocker& operator=(const ScopedSignalBlocker&) = delete; 38 ScopedSignalBlocker(const HostSigset * mask)39 explicit ScopedSignalBlocker(const HostSigset* mask) { Init(mask); } 40 ~ScopedSignalBlocker()41 ~ScopedSignalBlocker() { RTSigprocmaskSyscallOrDie(SIG_SETMASK, &old_mask_, nullptr); } 42 old_mask()43 [[nodiscard]] const HostSigset* old_mask() const { return &old_mask_; } 44 45 private: Init(const HostSigset * mask)46 void Init(const HostSigset* mask) { RTSigprocmaskSyscallOrDie(SIG_BLOCK, mask, &old_mask_); } 47 48 HostSigset old_mask_; 49 }; 50 51 } // namespace berberis 52 53 #endif // BERBERIS_GUEST_OS_PRIMITIVES_SCOPED_SIGNAL_BLOCKER_H_