/Users/deen/code/yugabyte-db/src/yb/util/signal_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | //-------------------------------------------------------------------------------------------------- |
13 | | // Utility functions and macros to manage process signals. |
14 | | |
15 | | #ifndef YB_UTIL_SIGNAL_UTIL_H |
16 | | #define YB_UTIL_SIGNAL_UTIL_H |
17 | | |
18 | | #include <vector> |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include "yb/util/result.h" |
23 | | |
24 | | namespace yb { |
25 | | |
26 | | // |
27 | | // Generic functions |
28 | | // |
29 | | |
30 | | // On current thread, block the given signals and return an old signal mask. |
31 | | // Signals will be accumulated by OS and delivered when unblocked. |
32 | | // This is needed before starting the thread if we only want those signals to be handled |
33 | | // at the main thread to avoid concurrency issues. |
34 | | Result<sigset_t> ThreadSignalMaskBlock(const std::vector<int>& signals_to_block); |
35 | | |
36 | | // Restore previous signal mask on the current thread. |
37 | | // Unblocking signals lets the blocked signals be delivered if they had been raised in the meantime. |
38 | | CHECKED_STATUS ThreadSignalMaskRestore(sigset_t old_mask); |
39 | | |
40 | | // |
41 | | // Specific functions |
42 | | // |
43 | | |
44 | | extern const std::vector<int> kYsqlHandledSignals; |
45 | | |
46 | | // Calls ThreadSignalMaskBlock to block signals with handlers installed by postgres layer. |
47 | | Result<sigset_t> ThreadYsqlSignalMaskBlock(); |
48 | | |
49 | | // Applies ThreadYsqlSignalMaskBlock, executes a given code (which should return a Status or Result) |
50 | | // and apples ThreadSignalMaskRestore, returning execution result. |
51 | | // Will attempt to revert a mask even if execution fails. |
52 | | // In case both execution and mask restoration fail, execution error status will be returned. |
53 | | template<typename Functor> |
54 | 902 | typename std::result_of<Functor()>::type WithMaskedYsqlSignals(Functor callback) { |
55 | 902 | sigset_t old_mask = VERIFY_RESULT(ThreadYsqlSignalMaskBlock()); |
56 | 902 | auto&& callback_status = callback(); |
57 | 902 | Status restore_status = yb::ThreadSignalMaskRestore(old_mask); |
58 | 902 | if (!restore_status.ok() && !callback_status.ok()) { |
59 | 0 | LOG(WARNING) << "Failed to restore thread signal mask: " << restore_status; |
60 | 0 | return std::move(callback_status); |
61 | 0 | } |
62 | 902 | RETURN_NOT_OK(restore_status); |
63 | 902 | return std::move(callback_status); |
64 | 902 | } |
65 | | |
66 | | } // namespace yb |
67 | | |
68 | | #endif // YB_UTIL_SIGNAL_UTIL_H |