From 6df169dce346ed41812291f247310acfd0196aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA?= Date: Fri, 24 Mar 2023 16:04:09 +0300 Subject: [PATCH] Fix #36971: Watchdog: exit with non-zero code if child process exits --- src/Daemon/BaseDaemon.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Daemon/BaseDaemon.cpp b/src/Daemon/BaseDaemon.cpp index e050124e497..345d6e765fe 100644 --- a/src/Daemon/BaseDaemon.cpp +++ b/src/Daemon/BaseDaemon.cpp @@ -1125,16 +1125,21 @@ void BaseDaemon::setupWatchdog() logger().information("Child process no longer exists."); _exit(WEXITSTATUS(status)); } - else if (WIFEXITED(status)) + + if (WIFEXITED(status)) { logger().information(fmt::format("Child process exited normally with code {}.", WEXITSTATUS(status))); _exit(WEXITSTATUS(status)); } + int exit_code; + if (WIFSIGNALED(status)) { int sig = WTERMSIG(status); + exit_code = 128 + sig; + if (sig == SIGKILL) { logger().fatal(fmt::format("Child process was terminated by signal {} (KILL)." @@ -1146,12 +1151,14 @@ void BaseDaemon::setupWatchdog() logger().fatal(fmt::format("Child process was terminated by signal {}.", sig)); if (sig == SIGINT || sig == SIGTERM || sig == SIGQUIT) - _exit(128 + sig); + _exit(exit_code); } } else { + // According to POSIX, this should never happen. logger().fatal("Child process was not exited normally by unknown reason."); + exit_code = 42; } if (restart) @@ -1161,7 +1168,7 @@ void BaseDaemon::setupWatchdog() memcpy(argv0, original_process_name.c_str(), original_process_name.size()); } else - _exit(WEXITSTATUS(status)); + _exit(exit_code); } }