]> granicus.if.org Git - icinga2/commitdiff
Ensure that only child processes for checks are reniced
authorGunnar Beutner <gunnar.beutner@icinga.com>
Tue, 21 Feb 2017 10:31:07 +0000 (11:31 +0100)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Tue, 21 Feb 2017 10:31:07 +0000 (11:31 +0100)
fixes #4989

lib/base/process.cpp
lib/base/process.hpp
lib/icinga/pluginutility.cpp

index 96c8dd3f4cb0484abee8c769d2bcdf594f43fc08..03d217ce862680e0ea0de36ab6d71211078ba91b 100644 (file)
@@ -63,7 +63,7 @@ static boost::once_flag l_ProcessOnceFlag = BOOST_ONCE_INIT;
 static boost::once_flag l_SpawnHelperOnceFlag = BOOST_ONCE_INIT;
 
 Process::Process(const Process::Arguments& arguments, const Dictionary::Ptr& extraEnvironment)
-       : m_Arguments(arguments), m_ExtraEnvironment(extraEnvironment), m_Timeout(600)
+       : m_Arguments(arguments), m_ExtraEnvironment(extraEnvironment), m_Timeout(600), m_AdjustPriority(false)
 #ifdef _WIN32
        , m_ReadPending(false), m_ReadFailed(false), m_Overlapped()
 #endif /* _WIN32 */
@@ -94,6 +94,7 @@ static Value ProcessSpawnImpl(struct msghdr *msgh, const Dictionary::Ptr& reques
 
        Array::Ptr arguments = request->Get("arguments");
        Dictionary::Ptr extraEnvironment = request->Get("extraEnvironment");
+       bool adjustPriority = request->Get("adjustPriority");
 
        // build argv
        char **argv = new char *[arguments->GetLength() + 1];
@@ -161,7 +162,7 @@ static Value ProcessSpawnImpl(struct msghdr *msgh, const Dictionary::Ptr& reques
                (void)close(fds[2]);
 
 #ifdef HAVE_NICE
-               if (nice(5) < 0)
+               if (adjustPriority && nice(5) < 0)
                        Log(LogWarning, "base", "Failed to renice child process.");
 #endif /* HAVE_NICE */
 
@@ -366,12 +367,13 @@ static void StartSpawnProcessHelper(void)
        l_ProcessControlPID = pid;
 }
 
-static pid_t ProcessSpawn(const std::vector<String>& arguments, const Dictionary::Ptr& extraEnvironment, int fds[3])
+static pid_t ProcessSpawn(const std::vector<String>& arguments, const Dictionary::Ptr& extraEnvironment, bool adjustPriority, int fds[3])
 {
        Dictionary::Ptr request = new Dictionary();
        request->Set("command", "spawn");
        request->Set("arguments", Array::FromVector(arguments));
        request->Set("extraEnvironment", extraEnvironment);
+       request->Set("adjustPriority", adjustPriority);
 
        String jrequest = JsonEncode(request);
        size_t length = jrequest.GetLength();
@@ -579,6 +581,16 @@ double Process::GetTimeout(void) const
        return m_Timeout;
 }
 
+void Process::SetAdjustPriority(bool adjust)
+{
+       m_AdjustPriority = adjust;
+}
+
+bool Process::GetAdjustPriority(void) const
+{
+       return m_AdjustPriority;
+}
+
 void Process::IOThreadProc(int tid)
 {
 #ifdef _WIN32
@@ -959,7 +971,7 @@ void Process::Run(const boost::function<void(const ProcessResult&)>& callback)
        fds[1] = outfds[1];
        fds[2] = outfds[1];
 
-       m_Process = ProcessSpawn(m_Arguments, m_ExtraEnvironment, fds);
+       m_Process = ProcessSpawn(m_Arguments, m_ExtraEnvironment, m_AdjustPriority, fds);
        m_PID = m_Process;
 
        Log(LogNotice, "Process")
index 8b7627128dd31233f0898efc66bc0bc9c25311ec..e20311f05eede4f43fd9cc20714a75119fb30306 100644 (file)
@@ -73,6 +73,9 @@ public:
        void SetTimeout(double timeout);
        double GetTimeout(void) const;
 
+       void SetAdjustPriority(bool adjust);
+       bool GetAdjustPriority(void) const;
+
        void Run(const boost::function<void (const ProcessResult&)>& callback = boost::function<void (const ProcessResult&)>());
 
        pid_t GetPID(void) const;
@@ -92,6 +95,7 @@ private:
        Dictionary::Ptr m_ExtraEnvironment;
 
        double m_Timeout;
+       bool m_AdjustPriority;
 
        ProcessHandle m_Process;
        pid_t m_PID;
index 772457a6dbc6a0c4270602768e7dcafa2a88e23c..8358547030474188d261c120b778a8bf952172f7 100644 (file)
@@ -93,6 +93,8 @@ void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkab
        else
                process->SetTimeout(checkable->GetCheckTimeout());
 
+       process->SetAdjustPriority(true);
+
        process->Run(boost::bind(callback, command, _1));
 }