diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index af2ca239f..fd4cbeba3 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -132,20 +132,25 @@ int main(int argc, char **argv) Component::AddSearchDir(ICINGA_LIBDIR); #endif /* ICINGA_LIBDIR */ - DynamicObject::BeginTx(); + try { + DynamicObject::BeginTx(); - /* load config file */ - String configFile = argv[2]; - vector configItems = ConfigCompiler::CompileFile(configFile); + /* load config file */ + String configFile = argv[2]; + vector configItems = ConfigCompiler::CompileFile(configFile); - Logger::Write(LogInformation, "icinga", "Executing config items..."); + Logger::Write(LogInformation, "icinga", "Executing config items..."); - BOOST_FOREACH(const ConfigItem::Ptr& item, configItems) { - item->Commit(); + BOOST_FOREACH(const ConfigItem::Ptr& item, configItems) { + item->Commit(); + } + + DynamicObject::FinishTx(); + } catch (const exception& ex) { + Logger::Write(LogCritical, "icinga", "Configuration error: " + String(ex.what())); + return EXIT_FAILURE; } - DynamicObject::FinishTx(); - Application::Ptr app = Application::GetInstance(); if (!app) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 5071be06f..a445a1c20 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -167,11 +167,19 @@ void Application::TimeWatchThreadProc(void) * Signals the application to shut down during the next * execution of the event loop. */ -void Application::Shutdown(void) +void Application::RequestShutdown(void) { m_ShuttingDown = true; } +/** + * Terminates the application. + */ +void Application::Terminate(int exitCode) +{ + _exit(exitCode); +} + /** * Retrieves the full path of the executable. * @@ -283,7 +291,7 @@ void Application::SigIntHandler(int signum) if (!instance) return; - instance->Shutdown(); + instance->RequestShutdown(); struct sigaction sa; memset(&sa, 0, sizeof(sa)); @@ -302,7 +310,7 @@ BOOL WINAPI Application::CtrlHandler(DWORD type) if (!instance) return TRUE; - instance->GetInstance()->Shutdown(); + instance->GetInstance()->RequestShutdown(); SetConsoleCtrlHandler(NULL, FALSE); return TRUE; @@ -347,7 +355,8 @@ int Application::Run(int argc, char **argv) } /** - * Grabs the PID file lock and updates the PID. + * Grabs the PID file lock and updates the PID. Terminates the application + * if the PID file is already locked by another instance of the application. * * @param filename The name of the PID file. */ @@ -366,9 +375,11 @@ void Application::UpdatePidFile(const String& filename) if (flock(fileno(m_PidFile), LOCK_EX | LOCK_NB) < 0) { ClosePidFile(); - throw_exception(runtime_error("Another instance of the application is " + Logger::Write(LogCritical, "base", + "Another instance of the application is " "already running. Remove the '" + filename + "' file if " - "you're certain that this is not the case.")); + "you're certain that this is not the case."); + Terminate(EXIT_FAILURE); } #endif /* _WIN32 */ diff --git a/lib/base/application.h b/lib/base/application.h index 485d43640..3d9b644f6 100644 --- a/lib/base/application.h +++ b/lib/base/application.h @@ -49,7 +49,8 @@ public: */ virtual int Main(const vector& args) = 0; - static void Shutdown(void); + static void RequestShutdown(void); + static void Terminate(int exitCode); static bool IsDebugging(void);