From 14f3ee8024f5f91ece36135e15f450a311966d49 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 21 Feb 2017 11:31:07 +0100 Subject: [PATCH] Ensure that only child processes for checks are reniced fixes #4989 --- lib/base/process.cpp | 20 ++++++++++++++++---- lib/base/process.hpp | 4 ++++ lib/icinga/pluginutility.cpp | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/base/process.cpp b/lib/base/process.cpp index 96c8dd3f4..03d217ce8 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -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& arguments, const Dictionary::Ptr& extraEnvironment, int fds[3]) +static pid_t ProcessSpawn(const std::vector& 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& 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") diff --git a/lib/base/process.hpp b/lib/base/process.hpp index 8b7627128..e20311f05 100644 --- a/lib/base/process.hpp +++ b/lib/base/process.hpp @@ -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& callback = boost::function()); 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; diff --git a/lib/icinga/pluginutility.cpp b/lib/icinga/pluginutility.cpp index 772457a6d..835854703 100644 --- a/lib/icinga/pluginutility.cpp +++ b/lib/icinga/pluginutility.cpp @@ -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)); }