]> granicus.if.org Git - icinga2/commitdiff
Use threads for external commands
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 20 Aug 2014 11:45:31 +0000 (13:45 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 20 Aug 2014 11:45:31 +0000 (13:45 +0200)
refs #6589
fixes #6942

components/compat/externalcommandlistener.cpp
components/compat/externalcommandlistener.hpp

index a4f76a4d9ae1c929529f8dba5b4a73dffc437f73..f9361095518a2d79ad9773c82e02378625bc227f 100644 (file)
@@ -113,6 +113,8 @@ void ExternalCommandListener::CommandPipeThread(const String& commandPath)
                FILE *fp = fdopen(fd, "r");
 
                if (fp == NULL) {
+                       (void) close(fd);
+
                        std::ostringstream msgbuf;
                        msgbuf << "fdopen() for fifo path '" << commandPath << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
                        Log(LogCritical, "ExternalCommandListener",  msgbuf.str());
@@ -128,21 +130,24 @@ void ExternalCommandListener::CommandPipeThread(const String& commandPath)
                            (line[strlen(line) - 1] == '\r' || line[strlen(line) - 1] == '\n'))
                                line[strlen(line) - 1] = '\0';
 
-                       String command = line;
-
-                       try {
-                               Log(LogInformation, "ExternalCommandListener", "Executing external command: " + command);
-
-                               ExternalCommandProcessor::Execute(command);
-                       } catch (const std::exception&) {
-                               std::ostringstream msgbuf;
-                               msgbuf << "External command failed.";
-                               Log(LogWarning, "ExternalCommandListener", msgbuf.str());
-                       }
+                       Utility::QueueAsyncCallback(boost::bind(&ExternalCommandListener::ExecuteCommand, line));
                }
 
                delete line;
                fclose(fp);
        }
 }
+
+void ExternalCommandListener::ExecuteCommand(const String& command)
+{
+       try {
+               Log(LogInformation, "ExternalCommandListener", "Executing external command: " + command);
+
+               ExternalCommandProcessor::Execute(command);
+       } catch (const std::exception&) {
+               std::ostringstream msgbuf;
+               msgbuf << "External command failed: " << command;
+               Log(LogWarning, "ExternalCommandListener", msgbuf.str());
+       }
+}
 #endif /* _WIN32 */
index 0006aec33567ee64e5f79fb6314b8565cfd50f6c..342a3d2b7ea64160b21c42f415292b9a10bb5df5 100644 (file)
@@ -49,6 +49,7 @@ private:
        boost::thread m_CommandThread;
 
        void CommandPipeThread(const String& commandPath);
+       static void ExecuteCommand(const String& command);
 #endif /* _WIN32 */
 };