iceoryx_doc  1.0.1
signal_handler.hpp
1 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_POSIX_WRAPPER_SIGNAL_HANDLER_HPP
17 #define IOX_UTILS_POSIX_WRAPPER_SIGNAL_HANDLER_HPP
18 
19 #include "iceoryx_utils/platform/signal.hpp"
20 
21 namespace iox
22 {
23 namespace posix
24 {
25 using SignalHandlerCallback_t = void (*)(int);
26 
29 enum class Signal : int
30 {
31  BUS = SIGBUS,
32  INT = SIGINT,
33  TERM = SIGTERM,
34  HUP = SIGHUP
37 };
38 
54 {
55  public:
56  SignalGuard(SignalGuard&& rhs) noexcept;
57  SignalGuard(const SignalGuard&) = delete;
58  ~SignalGuard() noexcept;
59 
60  SignalGuard& operator=(const SignalGuard& rhs) = delete;
61  SignalGuard& operator=(SignalGuard&& rhs) = delete;
62 
63  friend SignalGuard registerSignalHandler(const Signal, const SignalHandlerCallback_t) noexcept;
64 
65  private:
66  void restorePreviousAction() noexcept;
67  SignalGuard() noexcept = default;
68  SignalGuard(const Signal signal, const struct sigaction& previousAction) noexcept;
69 
70  private:
71  Signal m_signal;
72  struct sigaction m_previousAction;
73  bool m_doRestorePreviousAction{false};
74 };
75 
85 SignalGuard registerSignalHandler(const Signal signal, const SignalHandlerCallback_t callback) noexcept;
86 } // namespace posix
87 } // namespace iox
88 #endif
The SignalGuard is a class returned by registerSignalHandler. When it goes out of scope it restores t...
Definition: signal_handler.hpp:54
friend SignalGuard registerSignalHandler(const Signal, const SignalHandlerCallback_t) noexcept
Register a callback for a specific posix signal (SIG***).
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28