]> granicus.if.org Git - icinga2/commitdiff
Handle some exceptions that previously caused crashes.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 27 Sep 2012 07:58:16 +0000 (09:58 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 27 Sep 2012 07:58:16 +0000 (09:58 +0200)
icinga-app/icinga.cpp
lib/base/application.cpp
lib/base/application.h

index af2ca239fe9687c4b5e97c5df08f8953c88b17bc..fd4cbeba3e17a0572805b2574338cdbbc4fae312 100644 (file)
@@ -132,19 +132,24 @@ 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<ConfigItem::Ptr> configItems = ConfigCompiler::CompileFile(configFile);
+               /* load config file */
+               String configFile = argv[2];
+               vector<ConfigItem::Ptr> 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();
+               DynamicObject::FinishTx();
+       } catch (const exception& ex) {
+               Logger::Write(LogCritical, "icinga", "Configuration error: " + String(ex.what()));
+               return EXIT_FAILURE;
+       }
 
        Application::Ptr app = Application::GetInstance();
 
index 5071be06f8bf3c0ad040c8ed8f92d68f22c84e40..a445a1c207d5d9879c994d05a6172c787a3ba04e 100644 (file)
@@ -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 */
 
index 485d436409d45f0ef75c939d19f8a0f38d905cc7..3d9b644f687525810bab258eb1f4456793c463cb 100644 (file)
@@ -49,7 +49,8 @@ public:
         */
        virtual int Main(const vector<String>& args) = 0;
 
-       static void Shutdown(void);
+       static void RequestShutdown(void);
+       static void Terminate(int exitCode);
 
        static bool IsDebugging(void);