From 4a9fb5d8eb2caed6f3e109957d600182dc9608cb Mon Sep 17 00:00:00 2001 From: Oleksiy Protas Date: Mon, 18 Dec 2023 22:52:09 +0200 Subject: [PATCH] AP_HAL_Linux: delay signal handlers setup Allow default signals before full initialization in Linux, this makes sure we don't get an unkillable process if it hangs on initialization Exit flag marked volatile to counteract possible compiler optimization due to the handler code running in a different context --- libraries/AP_HAL_Linux/HAL_Linux_Class.cpp | 8 +++++++- libraries/AP_HAL_Linux/HAL_Linux_Class.h | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp index d89296b6a0fec..4177d86ab2e77 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp @@ -463,7 +463,11 @@ void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const } } - setup_signal_handlers(); + // NOTE: signal handlers are only set before the main loop, so + // that if anything before the loops hangs, the default signals + // can still stop the process proprely, although without proper + // teardown. + // This isn't perfect, but still prevents an unkillable process. scheduler->init(); gpio->init(); @@ -493,6 +497,8 @@ void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const AP_Module::call_hook_setup_complete(); #endif + setup_signal_handlers(); + while (!_should_exit) { callbacks->loop(); } diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.h b/libraries/AP_HAL_Linux/HAL_Linux_Class.h index 87089427aaa06..a0dc7f20453ee 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.h +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.h @@ -2,6 +2,8 @@ #include +#include + class HAL_Linux : public AP_HAL::HAL { public: HAL_Linux(); @@ -12,7 +14,7 @@ class HAL_Linux : public AP_HAL::HAL { static void exit_signal_handler(int); protected: - bool _should_exit = false; + volatile sig_atomic_t _should_exit = false; }; #if HAL_NUM_CAN_IFACES