Skip to content

Commit

Permalink
Handle sigterm, in the same way we handle sigint (ros2#1771)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
  • Loading branch information
ivanpauno authored and Nico Neumann committed Dec 15, 2021
1 parent 5266c21 commit c5cb627
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 146 deletions.
6 changes: 3 additions & 3 deletions rclcpp/include/rclcpp/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Context : public std::enable_shared_from_this<Context>
* Every context which is constructed is added to a global vector of contexts,
* which is used by the signal handler to conditionally shutdown each context
* on SIGINT.
* See the shutdown_on_sigint option in the InitOptions class.
* See the shutdown_on_signal option in the InitOptions class.
*/
RCLCPP_PUBLIC
Context();
Expand All @@ -93,7 +93,7 @@ class Context : public std::enable_shared_from_this<Context>
* Note that this function does not setup any signal handlers, so if you want
* it to be shutdown by the signal handler, then you need to either install
* them manually with rclcpp::install_signal_handlers() or use rclcpp::init().
* In addition to installing the signal handlers, the shutdown_on_sigint
* In addition to installing the signal handlers, the shutdown_on_signal
* of the InitOptions needs to be `true` for this context to be shutdown by
* the signal handler, otherwise it will be passed over.
*
Expand Down Expand Up @@ -268,7 +268,7 @@ class Context : public std::enable_shared_from_this<Context>
*
* - this context is shutdown()
* - this context is destructed (resulting in shutdown)
* - this context has shutdown_on_sigint=true and SIGINT occurs (resulting in shutdown)
* - this context has shutdown_on_signal=true and SIGINT/SIGTERM occurs (resulting in shutdown)
* - interrupt_all_sleep_for() is called
*
* \param[in] nanoseconds A std::chrono::duration representing how long to sleep for.
Expand Down
2 changes: 1 addition & 1 deletion rclcpp/include/rclcpp/init_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InitOptions
{
public:
/// If true, the context will be shutdown on SIGINT by the signal handler (if it was installed).
bool shutdown_on_sigint = true;
bool shutdown_on_signal = true;

/// Constructor
/**
Expand Down
33 changes: 31 additions & 2 deletions rclcpp/include/rclcpp/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ std::string to_string(T value)

namespace rclcpp
{

/// Option to indicate which signal handlers rclcpp should install.
enum class SignalHandlerOptions
{
/// Install both sigint and sigterm, this is the default behavior.
All,
/// Install only a sigint handler.
SigInt,
/// Install only a sigterm handler.
SigTerm,
/// Do not install any signal handler.
None,
};

/// Initialize communications via the rmw implementation and set up a global signal handler.
/**
* Initializes the global context which is accessible via the function
Expand All @@ -50,10 +64,16 @@ namespace rclcpp
* rclcpp::install_signal_handlers().
*
* \sa rclcpp::Context::init() for more details on arguments and possible exceptions
*
* \param signal_handler_options option to indicate which signal handlers should be installed.
*/
RCLCPP_PUBLIC
void
init(int argc, char const * const argv[], const InitOptions & init_options = InitOptions());
init(
int argc,
char const * const argv[],
const InitOptions & init_options = InitOptions(),
SignalHandlerOptions signal_handler_options = SignalHandlerOptions::All);

/// Install the global signal handler for rclcpp.
/**
Expand All @@ -67,17 +87,26 @@ init(int argc, char const * const argv[], const InitOptions & init_options = Ini
*
* This function is thread-safe.
*
* \param signal_handler_options option to indicate which signal handlers should be installed.
* \return true if signal handler was installed by this function, false if already installed.
*/
RCLCPP_PUBLIC
bool
install_signal_handlers();
install_signal_handlers(SignalHandlerOptions signal_handler_options = SignalHandlerOptions::All);

/// Return true if the signal handlers are installed, otherwise false.
RCLCPP_PUBLIC
bool
signal_handlers_installed();

/// Get the current signal handler options.
/**
* If no signal handler is installed, SignalHandlerOptions::None is returned.
*/
RCLCPP_PUBLIC
SignalHandlerOptions
get_current_signal_handler_options();

/// Uninstall the global signal handler for rclcpp.
/**
* This function does not necessarily need to be called, but can be used to
Expand Down
4 changes: 2 additions & 2 deletions rclcpp/src/rclcpp/init_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ InitOptions::InitOptions(const rcl_init_options_t & init_options)
InitOptions::InitOptions(const InitOptions & other)
: InitOptions(*other.get_rcl_init_options())
{
shutdown_on_sigint = other.shutdown_on_sigint;
shutdown_on_signal = other.shutdown_on_signal;
initialize_logging_ = other.initialize_logging_;
}

Expand All @@ -70,7 +70,7 @@ InitOptions::operator=(const InitOptions & other)
if (RCL_RET_OK != ret) {
rclcpp::exceptions::throw_from_rcl_error(ret, "failed to copy rcl init options");
}
this->shutdown_on_sigint = other.shutdown_on_sigint;
this->shutdown_on_signal = other.shutdown_on_signal;
this->initialize_logging_ = other.initialize_logging_;
}
return *this;
Expand Down
Loading

0 comments on commit c5cb627

Please sign in to comment.