Merge pull request #7397 from Icinga/bugfix/umbrella-logging-systemd-7394

icinga2 daemon --close-stdio: keep console log open during first config validation
This commit is contained in:
Michael Friedrich 2019-08-07 13:44:07 +02:00 committed by GitHub
commit 686f5d8ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -193,19 +193,23 @@ static Atomic<bool> l_AllowedToWork (false);
* Do the actual work (config loading, ...) * Do the actual work (config loading, ...)
* *
* @param configs Files to read config from * @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 * @return Exit code
*/ */
static inline static inline
int RunWorker(const std::vector<std::string>& configs) int RunWorker(const std::vector<std::string>& configs, bool closeConsoleLog = false, const String& stderrFile = String())
{ {
Log(LogInformation, "cli", "Loading configuration file(s)."); Log(LogInformation, "cli", "Loading configuration file(s).");
{ {
std::vector<ConfigItem::Ptr> newItems; std::vector<ConfigItem::Ptr> 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; return EXIT_FAILURE;
}
#ifndef _WIN32 #ifndef _WIN32
Log(LogNotice, "cli") Log(LogNotice, "cli")
@ -216,6 +220,11 @@ int RunWorker(const std::vector<std::string>& configs)
Log(LogNotice, "cli") Log(LogNotice, "cli")
<< "Waiting for the umbrella process to let us doing the actual work"; << "Waiting for the umbrella process to let us doing the actual work";
if (closeConsoleLog) {
CloseStdIO(stderrFile);
Logger::DisableConsoleLog();
}
while (!l_AllowedToWork.load()) { while (!l_AllowedToWork.load()) {
Utility::Sleep(0.2); Utility::Sleep(0.2);
} }
@ -397,10 +406,12 @@ static void NotifyWatchdog()
* Starts seemless worker process doing the actual work (config loading, ...) * Starts seemless worker process doing the actual work (config loading, ...)
* *
* @param configs Files to read config from * @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) * @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<std::string>& configs) static pid_t StartUnixWorker(const std::vector<std::string>& configs, bool closeConsoleLog = false, const String& stderrFile = String())
{ {
Log(LogNotice, "cli") Log(LogNotice, "cli")
<< "Spawning seemless worker process doing the actual work"; << "Spawning seemless worker process doing the actual work";
@ -461,7 +472,7 @@ static pid_t StartUnixWorker(const std::vector<std::string>& configs)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
_exit(RunWorker(configs)); _exit(RunWorker(configs, closeConsoleLog, stderrFile));
} catch (...) { } catch (...) {
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
@ -562,8 +573,10 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
std::vector<ConfigItem::Ptr> newItems; std::vector<ConfigItem::Ptr> 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; return EXIT_FAILURE;
}
Log(LogInformation, "cli", "Finished validating the configuration file(s)."); Log(LogInformation, "cli", "Finished validating the configuration file(s).");
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -604,7 +617,7 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
}); });
#endif /* _WIN32 */ #endif /* _WIN32 */
if (vm.count("daemonize") || vm.count("close-stdio")) { if (vm.count("daemonize")) {
// After disabling the console log, any further errors will go to the configured log only. // 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. // Let's try to make this clear and say good bye.
Log(LogInformation, "cli", "Closing console log."); Log(LogInformation, "cli", "Closing console log.");
@ -638,13 +651,28 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
(void)sigaction(SIGHUP, &sa, nullptr); (void)sigaction(SIGHUP, &sa, nullptr);
} }
bool closeConsoleLog = !vm.count("daemonize") && vm.count("close-stdio");
String errorLog;
if (vm.count("errorlog"))
errorLog = vm["errorlog"].as<std::string>();
// The PID of the current seemless worker // The PID of the current seemless worker
pid_t currentWorker = StartUnixWorker(configs); pid_t currentWorker = StartUnixWorker(configs, closeConsoleLog, errorLog);
if (currentWorker == -1) { if (currentWorker == -1) {
return EXIT_FAILURE; 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 // Immediately allow the first (non-reload) worker to continue working beyond config validation
(void)kill(currentWorker, SIGUSR2); (void)kill(currentWorker, SIGUSR2);