From 6418c2ebb74e8e8f9e73a5576e43cff5e66cc20e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 5 Aug 2019 18:30:45 +0200 Subject: [PATCH 1/2] icinga2 daemon --close-stdio: keep console log open during first config validation refs #7394 --- lib/cli/daemoncommand.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/cli/daemoncommand.cpp b/lib/cli/daemoncommand.cpp index 8091c403e..4975536ac 100644 --- a/lib/cli/daemoncommand.cpp +++ b/lib/cli/daemoncommand.cpp @@ -193,11 +193,13 @@ static Atomic l_AllowedToWork (false); * Do the actual work (config loading, ...) * * @param configs Files to read config from + * @param closeConsoleLog Whether to close the console log after config loading + * @param stderrFile Where to log errors * * @return Exit code */ static inline -int RunWorker(const std::vector& configs) +int RunWorker(const std::vector& configs, bool closeConsoleLog = false, const String& stderrFile = String()) { Log(LogInformation, "cli", "Loading configuration file(s)."); @@ -216,6 +218,11 @@ int RunWorker(const std::vector& configs) Log(LogNotice, "cli") << "Waiting for the umbrella process to let us doing the actual work"; + if (closeConsoleLog) { + CloseStdIO(stderrFile); + Logger::DisableConsoleLog(); + } + while (!l_AllowedToWork.load()) { Utility::Sleep(0.2); } @@ -397,10 +404,12 @@ static void NotifyWatchdog() * Starts seemless worker process doing the actual work (config loading, ...) * * @param configs Files to read config from + * @param closeConsoleLog Whether to close the console log after config loading + * @param stderrFile Where to log errors * * @return The worker's PID on success, -1 on failure (if the worker couldn't load its config) */ -static pid_t StartUnixWorker(const std::vector& configs) +static pid_t StartUnixWorker(const std::vector& configs, bool closeConsoleLog = false, const String& stderrFile = String()) { Log(LogNotice, "cli") << "Spawning seemless worker process doing the actual work"; @@ -461,7 +470,7 @@ static pid_t StartUnixWorker(const std::vector& configs) _exit(EXIT_FAILURE); } - _exit(RunWorker(configs)); + _exit(RunWorker(configs, closeConsoleLog, stderrFile)); } catch (...) { _exit(EXIT_FAILURE); } @@ -604,7 +613,7 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector(); + // The PID of the current seemless worker - pid_t currentWorker = StartUnixWorker(configs); + pid_t currentWorker = StartUnixWorker(configs, closeConsoleLog, errorLog); if (currentWorker == -1) { return EXIT_FAILURE; } + if (closeConsoleLog) { + // After disabling the console log, any further errors will go to the configured log only. + // Let's try to make this clear and say good bye. + Log(LogInformation, "cli", "Closing console log."); + + CloseStdIO(errorLog); + Logger::DisableConsoleLog(); + } + // Immediately allow the first (non-reload) worker to continue working beyond config validation (void)kill(currentWorker, SIGUSR2); From 01fe243f232dbcbb3e0e17a7008cc88fcbd519b4 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 7 Aug 2019 12:03:17 +0200 Subject: [PATCH 2/2] Log a hint after failed config validation for systemd users --- lib/cli/daemoncommand.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/cli/daemoncommand.cpp b/lib/cli/daemoncommand.cpp index 4975536ac..a9619c34a 100644 --- a/lib/cli/daemoncommand.cpp +++ b/lib/cli/daemoncommand.cpp @@ -206,8 +206,10 @@ int RunWorker(const std::vector& configs, bool closeConsoleLog = fa { std::vector newItems; - if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) + if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) { + Log(LogCritical, "cli", "Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config."); return EXIT_FAILURE; + } #ifndef _WIN32 Log(LogNotice, "cli") @@ -571,8 +573,10 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector newItems; - if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) + if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath)) { + Log(LogCritical, "cli", "Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config."); return EXIT_FAILURE; + } Log(LogInformation, "cli", "Finished validating the configuration file(s)."); return EXIT_SUCCESS;