Skip to content

iceoryx_hoofs/posix_wrapper/signal_handler.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::posix

Classes🔗

Name
class iox::posix::SignalGuard
The SignalGuard is a class returned by registerSignalHandler. When it goes out of scope it restores the previous signal action. Typical use case: One would like to override the signal action in main() or some C posix makes it necessary to override the standard signal action before and after the call.

Source code🔗

// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_POSIX_WRAPPER_SIGNAL_HANDLER_HPP
#define IOX_HOOFS_POSIX_WRAPPER_SIGNAL_HANDLER_HPP

#include "iceoryx_hoofs/platform/signal.hpp"

namespace iox
{
namespace posix
{
using SignalHandlerCallback_t = void (*)(int);

enum class Signal : int
{
    BUS = SIGBUS,
    INT = SIGINT,
    TERM = SIGTERM,
    HUP = SIGHUP,
    ABORT = SIGABRT,
};

class SignalGuard
{
  public:
    SignalGuard(SignalGuard&& rhs) noexcept;
    SignalGuard(const SignalGuard&) = delete;
    ~SignalGuard() noexcept;

    SignalGuard& operator=(const SignalGuard& rhs) = delete;
    SignalGuard& operator=(SignalGuard&& rhs) = delete;

    friend SignalGuard registerSignalHandler(const Signal, const SignalHandlerCallback_t) noexcept;

  private:
    void restorePreviousAction() noexcept;
    SignalGuard() noexcept = default;
    SignalGuard(const Signal signal, const struct sigaction& previousAction) noexcept;

  private:
    Signal m_signal;
    struct sigaction m_previousAction = {};
    bool m_doRestorePreviousAction{false};
};

SignalGuard registerSignalHandler(const Signal signal, const SignalHandlerCallback_t callback) noexcept;
} // namespace posix
} // namespace iox
#endif

Updated on 18 December 2023 at 13:11:43 CET