2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
#include "cli/daemoncommand.hpp"
|
2015-01-22 12:10:32 +01:00
|
|
|
#include "cli/daemonutility.hpp"
|
2016-05-11 12:50:08 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2016-06-03 14:16:52 +02:00
|
|
|
#include "remote/configobjectutility.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "config/configcompilercontext.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include "config/configitembuilder.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-12-14 11:33:45 +01:00
|
|
|
#include "base/scriptglobal.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "config.h"
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <iostream>
|
2015-12-16 15:57:59 +01:00
|
|
|
#include <fstream>
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
static po::variables_map g_AppParams;
|
|
|
|
|
|
|
|
REGISTER_CLICOMMAND("daemon", DaemonCommand);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
static void SigHupHandler(int)
|
|
|
|
{
|
|
|
|
Application::RequestRestart();
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2018-05-30 13:21:39 +02:00
|
|
|
/*
|
|
|
|
* Daemonize(). On error, this function logs by itself and exits (i.e. does not return).
|
|
|
|
*
|
|
|
|
* Implementation note: We're only supposed to call exit() in one of the forked processes.
|
|
|
|
* The other process calls _exit(). This prevents issues with exit handlers like atexit().
|
|
|
|
*/
|
|
|
|
static void Daemonize() noexcept
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2018-05-30 13:21:39 +02:00
|
|
|
try {
|
|
|
|
Application::UninitializeBase();
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Failed to stop thread pool before daemonizing, unexpected error: " << DiagnosticInformation(ex);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-11-27 08:04:07 +01:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
2018-05-30 13:21:39 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "fork() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
|
|
|
exit(EXIT_FAILURE);
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid) {
|
|
|
|
// systemd requires that the pidfile of the daemon is written before the forking
|
|
|
|
// process terminates. So wait till either the forked daemon has written a pidfile or died.
|
|
|
|
|
|
|
|
int status;
|
|
|
|
int ret;
|
|
|
|
pid_t readpid;
|
|
|
|
do {
|
|
|
|
Utility::Sleep(0.1);
|
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
readpid = Application::ReadPidFile(Configuration::PidPath);
|
2014-10-06 14:21:18 +02:00
|
|
|
ret = waitpid(pid, &status, WNOHANG);
|
|
|
|
} while (readpid != pid && ret == 0);
|
|
|
|
|
|
|
|
if (ret == pid) {
|
|
|
|
Log(LogCritical, "cli", "The daemon could not be started. See log output for details.");
|
2015-02-26 17:09:45 +01:00
|
|
|
_exit(EXIT_FAILURE);
|
2014-10-06 14:21:18 +02:00
|
|
|
} else if (ret == -1) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "waitpid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2015-02-26 17:09:45 +01:00
|
|
|
_exit(EXIT_FAILURE);
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
2015-02-26 17:09:45 +01:00
|
|
|
_exit(EXIT_SUCCESS);
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
2014-11-27 08:04:07 +01:00
|
|
|
|
2018-07-19 13:34:12 +02:00
|
|
|
Log(LogDebug, "Daemonize()")
|
|
|
|
<< "Child process with PID " << Utility::GetPid() << " continues; re-initializing base.";
|
|
|
|
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
// Detach from controlling terminal
|
|
|
|
pid_t sid = setsid();
|
|
|
|
if (sid == -1) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "setsid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:21:39 +02:00
|
|
|
try {
|
|
|
|
Application::InitializeBase();
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Failed to re-initialize thread pool after daemonizing: " << DiagnosticInformation(ex);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-10-06 14:21:18 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
static void CloseStdIO(const String& stderrFile)
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fdnull = open("/dev/null", O_RDWR);
|
|
|
|
if (fdnull >= 0) {
|
|
|
|
if (fdnull != 0)
|
|
|
|
dup2(fdnull, 0);
|
|
|
|
|
|
|
|
if (fdnull != 1)
|
|
|
|
dup2(fdnull, 1);
|
|
|
|
|
|
|
|
if (fdnull > 1)
|
|
|
|
close(fdnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *errPath = "/dev/null";
|
|
|
|
|
|
|
|
if (!stderrFile.IsEmpty())
|
|
|
|
errPath = stderrFile.CStr();
|
|
|
|
|
|
|
|
int fderr = open(errPath, O_WRONLY | O_APPEND);
|
|
|
|
|
|
|
|
if (fderr < 0 && errno == ENOENT)
|
|
|
|
fderr = open(errPath, O_CREAT | O_WRONLY | O_APPEND, 0600);
|
|
|
|
|
2015-03-03 07:36:43 +01:00
|
|
|
if (fderr >= 0) {
|
2014-10-06 14:21:18 +02:00
|
|
|
if (fderr != 2)
|
|
|
|
dup2(fderr, 2);
|
|
|
|
|
|
|
|
if (fderr > 2)
|
|
|
|
close(fderr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String DaemonCommand::GetDescription() const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
return "Starts Icinga 2.";
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String DaemonCommand::GetShortDescription() const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
return "starts Icinga 2";
|
|
|
|
}
|
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
void DaemonCommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2017-12-19 15:50:05 +01:00
|
|
|
boost::program_options::options_description& hiddenDesc) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
2014-10-10 11:08:24 +02:00
|
|
|
visibleDesc.add_options()
|
2014-10-06 14:21:18 +02:00
|
|
|
("config,c", po::value<std::vector<std::string> >(), "parse a configuration file")
|
|
|
|
("no-config,z", "start without a configuration file")
|
|
|
|
("validate,C", "exit after validating the configuration")
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize or --close-stdio)")
|
2014-10-06 14:21:18 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
("daemonize,d", "detach from the controlling terminal")
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
("close-stdio", "do not log to stdout (or stderr) after startup")
|
2014-10-06 14:21:18 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
;
|
2014-10-10 11:08:24 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
hiddenDesc.add_options()
|
|
|
|
("reload-internal", po::value<int>(), "used internally to implement config reload: do not call manually, send SIGHUP instead");
|
|
|
|
#endif /* _WIN32 */
|
2014-10-17 15:54:46 +02:00
|
|
|
}
|
2014-10-14 16:45:00 +02:00
|
|
|
|
2014-10-17 15:54:46 +02:00
|
|
|
std::vector<String> DaemonCommand::GetArgumentSuggestions(const String& argument, const String& word) const
|
|
|
|
{
|
|
|
|
if (argument == "config" || argument == "errorlog")
|
|
|
|
return GetBashCompletionSuggestions("file", word);
|
|
|
|
else
|
|
|
|
return CLICommand::GetArgumentSuggestions(argument, word);
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The entry point for the "daemon" CLI command.
|
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2014-10-13 18:07:52 +02:00
|
|
|
int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
2018-05-03 11:35:29 +02:00
|
|
|
Logger::EnableTimestamp();
|
2014-10-31 22:01:36 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Icinga application loader (version: " << Application::GetAppVersion()
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "; debug"
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2017-12-19 15:50:05 +01:00
|
|
|
<< ")";
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
if (!vm.count("validate") && !vm.count("reload-internal")) {
|
2018-08-09 15:37:23 +02:00
|
|
|
pid_t runningpid = Application::ReadPidFile(Configuration::PidPath);
|
2014-10-06 14:21:18 +02:00
|
|
|
if (runningpid > 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Another instance of Icinga already running with PID " << runningpid;
|
2014-10-06 14:21:18 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:10:32 +01:00
|
|
|
std::vector<std::string> configs;
|
|
|
|
if (vm.count("config") > 0)
|
2017-11-30 08:19:58 +01:00
|
|
|
configs = vm["config"].as<std::vector<std::string> >();
|
2018-07-31 11:59:09 +02:00
|
|
|
else if (!vm.count("no-config")) {
|
2018-08-07 18:33:59 +02:00
|
|
|
/* The implicit string assignment is needed for Windows builds. */
|
2018-08-09 15:37:23 +02:00
|
|
|
String configDir = Configuration::ConfigDir;
|
2018-07-31 11:59:09 +02:00
|
|
|
configs.push_back(configDir + "/icinga2.conf");
|
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
|
2016-07-05 15:43:48 +02:00
|
|
|
Log(LogInformation, "cli", "Loading configuration file(s).");
|
|
|
|
|
2015-11-19 19:38:20 +01:00
|
|
|
std::vector<ConfigItem::Ptr> newItems;
|
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
if (!DaemonUtility::LoadConfigFiles(configs, newItems, Configuration::ObjectsPath, Configuration::VarsPath))
|
2014-10-06 14:21:18 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (vm.count("validate")) {
|
|
|
|
Log(LogInformation, "cli", "Finished validating the configuration file(s).");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-01-28 22:00:36 +01:00
|
|
|
#ifndef _WIN32
|
2015-02-11 15:47:45 +01:00
|
|
|
if (vm.count("reload-internal")) {
|
2018-01-17 13:52:23 +01:00
|
|
|
/* We went through validation and now ask the old process kindly to die */
|
|
|
|
Log(LogInformation, "cli", "Requesting to take over.");
|
|
|
|
int rc = kill(vm["reload-internal"].as<int>(), SIGUSR2);
|
|
|
|
if (rc) {
|
2018-03-14 10:01:11 +01:00
|
|
|
Log(LogCritical, "cli")
|
2018-01-17 13:52:23 +01:00
|
|
|
<< "Failed to send signal to \"" << vm["reload-internal"].as<int>() << "\" with " << strerror(errno);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-03-14 10:01:11 +01:00
|
|
|
|
|
|
|
double start = Utility::GetTime();
|
|
|
|
while (kill(vm["reload-internal"].as<int>(), SIGCHLD) == 0)
|
|
|
|
Utility::Sleep(0.2);
|
|
|
|
|
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Waited for " << Utility::FormatDuration(Utility::GetTime() - start) << " on old process to exit.";
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
2018-01-28 22:00:36 +01:00
|
|
|
#endif /* _WIN32 */
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
if (vm.count("daemonize")) {
|
|
|
|
if (!vm.count("reload-internal")) {
|
|
|
|
// no additional fork neccessary on reload
|
2018-05-30 13:21:39 +02:00
|
|
|
|
|
|
|
// this subroutine either succeeds, or logs an error
|
|
|
|
// and terminates the process (does not return).
|
|
|
|
Daemonize();
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:38:20 +01:00
|
|
|
/* restore the previous program state */
|
|
|
|
try {
|
2018-08-09 15:37:23 +02:00
|
|
|
ConfigObject::RestoreObjects(Configuration::StatePath);
|
2015-11-19 19:38:20 +01:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Failed to restore state file: " << DiagnosticInformation(ex);
|
2015-11-19 19:38:20 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
{
|
2018-08-09 15:37:23 +02:00
|
|
|
WorkQueue upq(25000, Configuration::Concurrency);
|
2016-08-09 12:39:07 +02:00
|
|
|
upq.SetName("DaemonCommand::Run");
|
2015-08-13 09:02:52 +02:00
|
|
|
|
|
|
|
// activate config only after daemonization: it starts threads and that is not compatible with fork()
|
2017-05-03 11:59:51 +02:00
|
|
|
if (!ConfigItem::ActivateItems(upq, newItems, false, false, true)) {
|
2015-08-13 09:02:52 +02:00
|
|
|
Log(LogCritical, "cli", "Error activating configuration.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
if (vm.count("daemonize") || vm.count("close-stdio")) {
|
|
|
|
// 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.");
|
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
String errorLog;
|
|
|
|
if (vm.count("errorlog"))
|
|
|
|
errorLog = vm["errorlog"].as<std::string>();
|
|
|
|
|
Fix logging under systemd
icinga2.service used `-e ${ICINGA2_ERROR_LOG}`, but this is documented
as having no effect without `-d`. Furthermore, icinga2 under systemd
unconditionally logged everything to the system log (but without setting
the log level etc), which contradicted the documentation. (Issue #6339)
Stop icinga2 on systemd from logging to stdout - and hence the system log -
once it has finished starting up. Just like when you start icinga2 from a
terminal using `-d`. And just like -d, we stop logging fatal errors to
stderr, and instead write to the log file passed with `-e`.
As per docs, mainlog (icinga2.log) is already enabled by default. And
pre-startup messages including config errors will still appear in the
system log.
This uses a new option --close-stdio, which has the same effect on logging as
--daemonize, but does not fork or call setsid().
For this purpose, I moved setsid() up and into Daemonize().
Consequence of that last point: if anyone is weird enough to specify a TTY
device file as the fatal error log (-e option), that will become icinga's
controlling terminal, which you generally don't want as a daemon. This
makes it consistent with the existing behaviour for icinga mainlog. For
this reason you're supposed to use O_NOCTTY in Linux daemons. But I wasn't
sure where icinga would want to put the ugly `#ifdef _WIN32 ... #else ...`.
2018-05-29 18:24:05 +02:00
|
|
|
CloseStdIO(errorLog);
|
2014-10-06 14:21:18 +02:00
|
|
|
Logger::DisableConsoleLog();
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2016-06-03 14:16:52 +02:00
|
|
|
/* Remove ignored Downtime/Comment objects. */
|
|
|
|
ConfigItem::RemoveIgnoredItems(ConfigObjectUtility::GetConfigDir());
|
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = &SigHupHandler;
|
2017-12-14 15:37:20 +01:00
|
|
|
sigaction(SIGHUP, &sa, nullptr);
|
2014-10-06 14:21:18 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2016-05-11 13:04:39 +02:00
|
|
|
ApiListener::UpdateObjectAuthority();
|
2016-05-11 12:50:08 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
return Application::GetInstance()->Run();
|
|
|
|
}
|