]> granicus.if.org Git - icinga2/commitdiff
Error messages: Gracefully handle PID file errors.
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 5 Jun 2014 15:44:41 +0000 (17:44 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 5 Jun 2014 15:44:41 +0000 (17:44 +0200)
Refs #6070

lib/base/application.cpp

index ef5fcc3aa4ce4e7b1eccb80db9e29cbc9f02630b..bba8cdb17d98eb06dcf86c4b57f907a45856892e 100644 (file)
@@ -93,7 +93,13 @@ void Application::Stop(void)
        // over. Write the PID of the new process to the pidfile before this
        // process exits to keep systemd happy.
        if (l_Restarting) {
-               UpdatePidFile(GetPidPath(), m_ReloadProcess);
+               try {
+                       UpdatePidFile(GetPidPath(), m_ReloadProcess);
+               } catch (std::exception&) {
+                       /* abort restart */
+                       Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation.");
+                       return;
+               }
                ClosePidFile(false);
        } else
                ClosePidFile(true);
@@ -638,7 +644,12 @@ int Application::Run(void)
        SetConsoleCtrlHandler(&Application::CtrlHandler, TRUE);
 #endif /* _WIN32 */
 
-       UpdatePidFile(GetPidPath());
+       try {
+               UpdatePidFile(GetPidPath());
+       } catch (std::exception&) {
+               Log(LogCritical, "Application", "Cannot update PID file '" + GetPidPath() + "'. Aborting.");
+               return false;
+       }
 
        result = Main();
 
@@ -667,8 +678,10 @@ void Application::UpdatePidFile(const String& filename, pid_t pid)
        if (m_PidFile == NULL)
                m_PidFile = fopen(filename.CStr(), "w");
 
-       if (m_PidFile == NULL)
+       if (m_PidFile == NULL) {
+               Log(LogCritical, "Application", "Could not open PID file '" + filename + "'.");
                BOOST_THROW_EXCEPTION(std::runtime_error("Could not open PID file '" + filename + "'"));
+       }
 
 #ifndef _WIN32
        int fd = fileno(m_PidFile);
@@ -689,6 +702,10 @@ void Application::UpdatePidFile(const String& filename, pid_t pid)
        }
 
        if (ftruncate(fd, 0) < 0) {
+               std::ostringstream msgbuf;
+               msgbuf << "ftruncate() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
+               Log(LogCritical, "Application",  msgbuf.str());
+
                BOOST_THROW_EXCEPTION(posix_error()
                    << boost::errinfo_api_function("ftruncate")
                    << boost::errinfo_errno(errno));