]> granicus.if.org Git - icinga2/commitdiff
Use RequestShutdown with exitcode instead of Application::Exit
authorJean-Marcel Flach <jean-marcel.flach@netways.de>
Thu, 26 Feb 2015 16:09:45 +0000 (17:09 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 27 Feb 2015 13:43:30 +0000 (14:43 +0100)
fixes #8436

icinga-app/icinga.cpp
lib/base/application.cpp
lib/base/application.hpp
lib/cli/daemoncommand.cpp
lib/db_ido_mysql/idomysqlconnection.cpp
lib/db_ido_pgsql/idopgsqlconnection.cpp
lib/remote/apilistener.cpp

index 6efdc157382960c4f61a72373874b078d726cf6e..3ca2ce933f0c754faf01ecb768c52c986b4e60a7 100644 (file)
@@ -653,7 +653,7 @@ int main(int argc, char **argv)
                };
 
                StartServiceCtrlDispatcher(dispatchTable);
-               Application::Exit(1);
+               Application::Exit(EXIT_FAILURE);
        }
 #endif /* _WIN32 */
 
index 205f175a4922d41e233bb08bb9c1fadfa69aacdf..b979924eafd9d132363a6506fe346369eeddb787 100644 (file)
@@ -54,6 +54,7 @@ static bool l_InExceptionHandler = false;
 int Application::m_ArgC;
 char **Application::m_ArgV;
 double Application::m_StartTime;
+int Application::m_ExitStatus = 0;
 
 /**
  * Constructor for the Application class.
@@ -110,7 +111,6 @@ void Application::Exit(int rc)
        }
 
        UninitializeBase();
-
 #ifdef I2_DEBUG
        exit(rc);
 #else /* I2_DEBUG */
@@ -308,7 +308,11 @@ mainloop:
                goto mainloop;
        }
 
-       Log(LogInformation, "Application", "Shutting down Icinga...");
+       if (m_ExitStatus)
+               Log(LogInformation, "Application", "Shutting down Icinga after a fatal error.");
+       else
+               Log(LogInformation, "Application", "Shutting down Icinga...");
+
        DynamicObject::StopObjects();
        Application::GetInstance()->OnShutdown();
 
@@ -355,8 +359,9 @@ pid_t Application::StartReloadProcess(void)
  * Signals the application to shut down during the next
  * execution of the event loop.
  */
-void Application::RequestShutdown(void)
+void Application::RequestShutdown(int rc)
 {
+       m_ExitStatus = rc > m_ExitStatus ? rc : m_ExitStatus;
        m_ShuttingDown = true;
 }
 
index f00831b5887c936f8e317e4bb75cd506d28e6ea3..6ed9c648f745109d1beae99657f309643e6c4b83 100644 (file)
@@ -69,7 +69,7 @@ public:
 
        static void InstallExceptionHandlers(void);
 
-       static void RequestShutdown(void);
+       static void RequestShutdown(int rc = 0);
        static void RequestRestart(void);
        static void RequestReopenLogs(void);
 
@@ -162,6 +162,7 @@ private:
        static bool m_Debugging; /**< Whether debugging is enabled. */
        static LogSeverity m_DebuggingSeverity; /**< Whether debugging severity is set. */
        static double m_StartTime;
+       static int m_ExitStatus;
 
 #ifndef _WIN32
        static void SigIntTermHandler(int signum);
index bfd860deee9d5565c7771e84873745e020a49eb5..4d564b94a8cc388e2176401e45520891588609bb 100644 (file)
@@ -93,14 +93,14 @@ static bool Daemonize(void)
 
                if (ret == pid) {
                        Log(LogCritical, "cli", "The daemon could not be started. See log output for details.");
-                       Application::Exit(EXIT_FAILURE);
+                       _exit(EXIT_FAILURE);
                } else if (ret == -1) {
                        Log(LogCritical, "cli")
                            << "waitpid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
-                       Application::Exit(EXIT_FAILURE);
+                       _exit(EXIT_FAILURE);
                }
 
-               Application::Exit(0);
+               _exit(EXIT_SUCCESS);
        }
 
        Application::GetTP().Start();
index 560f8f8a0aa0d9380c6f1cb27a1643fb5336878c..aa7a7bb8cf598b79f15c96efe2b3bd324bd94039 100644 (file)
@@ -225,7 +225,7 @@ void IdoMysqlConnection::Reconnect(void)
                if (!row) {
                        Log(LogCritical, "IdoMysqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
 
-                       Application::Exit(EXIT_FAILURE);
+                       Application::RequestShutdown(EXIT_FAILURE);
                }
 
                DiscardRows(result);
@@ -237,7 +237,7 @@ void IdoMysqlConnection::Reconnect(void)
                            << "Schema version '" << version << "' does not match the required version '"
                            << SCHEMA_VERSION << "'! Please check the upgrade documentation.";
 
-                       Application::Exit(EXIT_FAILURE);
+                       Application::RequestShutdown(EXIT_FAILURE);
                }
 
                String instanceName = GetInstanceName();
index e401b75cde5e219ae6f789aca54b1039a8825149..a8debf876f00970424e5ac916d534aa17260ec6f 100644 (file)
@@ -225,7 +225,7 @@ void IdoPgsqlConnection::Reconnect(void)
                if (!row) {
                        Log(LogCritical, "IdoPgsqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
 
-                       Application::Exit(EXIT_FAILURE);
+                       Application::RequestShutdown(EXIT_FAILURE);
                }
 
                String version = row->Get("version");
@@ -235,7 +235,7 @@ void IdoPgsqlConnection::Reconnect(void)
                            << "Schema version '" << version << "' does not match the required version '"
                            << SCHEMA_VERSION << "'! Please check the upgrade documentation.";
 
-                       Application::Exit(EXIT_FAILURE);
+                       Application::RequestShutdown(EXIT_FAILURE);
                }
 
                String instanceName = GetInstanceName();
index d4c7284976b258cd6c376f755bc31c9a21e5b94e..04874b8312d88b816e012369480688bba9c17ba6 100644 (file)
@@ -49,17 +49,13 @@ void ApiListener::OnConfigLoaded(void)
        try {
                cert = GetX509Certificate(GetCertPath());
        } catch (const std::exception&) {
-               Log(LogCritical, "ApiListener")
-                   << "Cannot get certificate from cert path: '" << GetCertPath() << "'.";
-               Application::Exit(EXIT_FAILURE);
+               BOOST_THROW_EXCEPTION(ScriptError("Cannot get certificate from cert path: '" + GetCertPath() + "'.", GetDebugInfo()));
        }
 
        try {
                SetIdentity(GetCertificateCN(cert));
        } catch (const std::exception&) {
-               Log(LogCritical, "ApiListener")
-                   << "Cannot get certificate common name from cert path: '" << GetCertPath() << "'.";
-               Application::Exit(EXIT_FAILURE);
+               BOOST_THROW_EXCEPTION(ScriptError("Cannot get certificate common name from cert path: '" + GetCertPath() + "'.", GetDebugInfo()));
        }
 
        Log(LogInformation, "ApiListener")
@@ -68,29 +64,22 @@ void ApiListener::OnConfigLoaded(void)
        try {
                m_SSLContext = MakeSSLContext(GetCertPath(), GetKeyPath(), GetCaPath());
        } catch (const std::exception&) {
-               Log(LogCritical, "ApiListener")
-                   << "Cannot make SSL context for cert path: '" << GetCertPath() << "' key path: '" << GetKeyPath() << "' ca path: '" << GetCaPath() << "'.";
-               Application::Exit(EXIT_FAILURE);
+               BOOST_THROW_EXCEPTION(ScriptError("Cannot make SSL context for cert path: '" + GetCertPath() + "' key path: '" + GetKeyPath() + "' ca path: '" + GetCaPath() + "'.", GetDebugInfo()));
        }
 
        if (!GetCrlPath().IsEmpty()) {
                try {
                        AddCRLToSSLContext(m_SSLContext, GetCrlPath());
                } catch (const std::exception&) {
-                       Log(LogCritical, "ApiListener")
-                           << "Cannot add certificate revocation list to SSL context for crl path: '" << GetCrlPath() << "'.";
-                       Application::Exit(EXIT_FAILURE);
+                       BOOST_THROW_EXCEPTION(ScriptError("Cannot add certificate revocation list to SSL context for crl path: '" + GetCrlPath() + "'.", GetDebugInfo()));
                }
        }
 }
 
 void ApiListener::OnAllConfigLoaded(void)
 {
-       if (!Endpoint::GetByName(GetIdentity())) {
-               Log(LogCritical, "ApiListener")
-                   << "Endpoint object for '" << GetIdentity() << "' is missing.";
-               Application::Exit(EXIT_FAILURE);
-       }
+       if (!Endpoint::GetByName(GetIdentity()))
+               BOOST_THROW_EXCEPTION(ScriptError("Endpoint object for '" + GetIdentity() + "' is missing.", GetDebugInfo()));
 }
 
 /**
@@ -117,7 +106,7 @@ void ApiListener::Start(void)
        if (!AddListener(GetBindHost(), GetBindPort())) {
                Log(LogCritical, "ApiListener")
                     << "Cannot add listener on host '" << GetBindHost() << "' for port '" << GetBindPort() << "'.";
-               Application::Exit(EXIT_FAILURE);
+               Application::RequestShutdown(EXIT_FAILURE);
        }
 
        m_Timer = new Timer();