mrpe scripts raised an error
Message-ID: <5947cd76.6z8aW3ZDZpsgsLjL%ab(a)mathias-kettner.de>
User-Agent: Heirloom mailx 12.5 6/20/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Module: check_mk
Branch: master
Commit: 7945c19a03a79a6d7887c3aad8437f9bef0d1f8c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=7945c19a03a79a…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Mon Jun 19 14:44:15 2017 +0200
4833 FIX Windows Agent: Fixed pipe handle leak whenever plugin/mrpe scripts raised an
error
Change-Id: I28275c83b153aeef9e355123238388d880c9a088
---
.werks/4833 | 10 +++++++++
agents/windows/ExternalCmd.cc | 27 +++++++++--------------
agents/windows/ExternalCmd.h | 9 ++++----
agents/windows/build_version | 2 +-
agents/windows/check_mk_agent-64.exe | Bin 1339392 -> 1339392 bytes
agents/windows/check_mk_agent-64.unstripped.exe | Bin 15811578 -> 15811521 bytes
agents/windows/check_mk_agent-64.unversioned.exe | Bin 1339392 -> 1339392 bytes
agents/windows/check_mk_agent.exe | Bin 1316864 -> 1316864 bytes
agents/windows/check_mk_agent.msi | Bin 2926080 -> 2926080 bytes
agents/windows/check_mk_agent.unstripped.exe | Bin 13671970 -> 13670888 bytes
agents/windows/check_mk_agent.unversioned.exe | Bin 1316864 -> 1316864 bytes
agents/windows/crash.exe | Bin 101624 -> 101624 bytes
agents/windows/install_agent-64.exe | Bin 591609 -> 591635 bytes
agents/windows/install_agent.exe | Bin 589028 -> 589053 bytes
agents/windows/nowin.exe | Bin 103760 -> 103760 bytes
agents/windows/plugins/mk_logwatch.exe | Bin 4555443 -> 4563981 bytes
agents/windows/sections/SectionMRPE.cc | 1 -
agents/windows/sections/SectionPluginGroup.cc | 2 --
agents/windows/types.h | 1 +
19 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/.werks/4833 b/.werks/4833
new file mode 100644
index 0000000..3adf166
--- /dev/null
+++ b/.werks/4833
@@ -0,0 +1,10 @@
+Title: Windows Agent: Fixed pipe handle leak whenever plugin/mrpe scripts raised an
error
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1497876183
+Class: fix
+
+
diff --git a/agents/windows/ExternalCmd.cc b/agents/windows/ExternalCmd.cc
index 8287536..cbc4663 100644
--- a/agents/windows/ExternalCmd.cc
+++ b/agents/windows/ExternalCmd.cc
@@ -48,12 +48,13 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
// child process needs to be able to inherit the pipe handles
security_attributes.bInheritHandle = true;
- if (!CreatePipe(&_stdout, &_script_stdout, &security_attributes, 0)) {
+ if (!CreatePipe(_stdout.ptr(), _script_stdout.ptr(), &security_attributes, 0)) {
throw win_exception("failed to create pipe");
}
+
if (with_stderr) {
- if (!CreatePipe(&_stderr, &_script_stderr, &security_attributes, 0))
{
+ if (!CreatePipe(_stderr.ptr(), _script_stderr.ptr(), &security_attributes,
0)) {
throw win_exception("failed to create pipe");
}
}
@@ -65,8 +66,8 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
- si.hStdOutput = _script_stdout;
- si.hStdError = with_stderr ? _script_stdout : _script_stderr;
+ si.hStdOutput = (HANDLE)_script_stdout;
+ si.hStdError = with_stderr ? (HANDLE)_script_stdout : (HANDLE)_script_stderr;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
@@ -74,7 +75,7 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
free);
if (!CreateProcess(nullptr, cmdline_buf.get(), nullptr, nullptr, TRUE,
CREATE_NEW_CONSOLE, nullptr, nullptr, &si, &pi)) {
- throw win_exception(std::string("failed to spawn process ") +
cmdline);
+ throw win_exception(std::string("failed to spawn process ") + cmdline);
}
_process = pi.hProcess;
@@ -88,8 +89,6 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
}
ExternalCmd::~ExternalCmd() {
- ::CloseHandle(_stdout);
- ::CloseHandle(_stderr);
if (_job_object != INVALID_HANDLE_VALUE) {
::TerminateJobObject(_job_object, 1);
::CloseHandle(_job_object);
@@ -97,17 +96,13 @@ ExternalCmd::~ExternalCmd() {
::CloseHandle(_process);
}
+
void ExternalCmd::terminateJob(DWORD exit_code) {
::TerminateJobObject(_job_object, exit_code);
::CloseHandle(_job_object);
_job_object = INVALID_HANDLE_VALUE;
}
-void ExternalCmd::closeScriptHandles() {
- ::CloseHandle(_script_stderr);
- ::CloseHandle(_script_stdout);
-}
-
DWORD ExternalCmd::exitCode() {
DWORD res;
GetExitCodeProcess(_process, &res);
@@ -116,23 +111,23 @@ DWORD ExternalCmd::exitCode() {
DWORD ExternalCmd::stdoutAvailable() {
DWORD available;
- PeekNamedPipe(_stdout, nullptr, 0, nullptr, &available, nullptr);
+ PeekNamedPipe((HANDLE)_stdout, nullptr, 0, nullptr, &available, nullptr);
return available;
}
DWORD ExternalCmd::stderrAvailable() {
DWORD available;
- PeekNamedPipe(_stderr, nullptr, 0, nullptr, &available, nullptr);
+ PeekNamedPipe((HANDLE)_stderr, nullptr, 0, nullptr, &available, nullptr);
return available;
}
DWORD ExternalCmd::readStdout(char *buffer, size_t buffer_size, bool block) {
- return readPipe(_stdout, buffer, buffer_size, block);
+ return readPipe((HANDLE)_stdout, buffer, buffer_size, block);
}
DWORD ExternalCmd::readStderr(char *buffer, size_t buffer_size, bool block) {
if (!with_stderr) {
- return readPipe(_stderr, buffer, buffer_size, block);
+ return readPipe((HANDLE)_stderr, buffer, buffer_size, block);
} else {
return 0;
}
diff --git a/agents/windows/ExternalCmd.h b/agents/windows/ExternalCmd.h
index 89939c3..6593022 100644
--- a/agents/windows/ExternalCmd.h
+++ b/agents/windows/ExternalCmd.h
@@ -26,6 +26,7 @@
#define ExternalCmd_h
#include <windows.h>
+#include "types.h"
class ExternalCmd {
public:
@@ -51,12 +52,12 @@ private:
DWORD readPipe(HANDLE pipe, char *buffer, size_t buffer_size, bool block);
private:
- HANDLE _script_stderr{INVALID_HANDLE_VALUE};
- HANDLE _script_stdout{INVALID_HANDLE_VALUE};
+ WinHandle _script_stderr{INVALID_HANDLE_VALUE};
+ WinHandle _script_stdout{INVALID_HANDLE_VALUE};
HANDLE _process{INVALID_HANDLE_VALUE};
HANDLE _job_object{INVALID_HANDLE_VALUE};
- HANDLE _stdout{INVALID_HANDLE_VALUE};
- HANDLE _stderr{INVALID_HANDLE_VALUE};
+ WinHandle _stdout{INVALID_HANDLE_VALUE};
+ WinHandle _stderr{INVALID_HANDLE_VALUE};
};
#endif // ExternalCmd_h
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 4633262..dc1d7d0 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-2663
+2711
diff --git a/agents/windows/check_mk_agent-64.exe b/agents/windows/check_mk_agent-64.exe
index d3a9573..fcff221 100755
Binary files a/agents/windows/check_mk_agent-64.exe and
b/agents/windows/check_mk_agent-64.exe differ
diff --git a/agents/windows/check_mk_agent-64.unstripped.exe
b/agents/windows/check_mk_agent-64.unstripped.exe
index 0891779..95096dd 100755
Binary files a/agents/windows/check_mk_agent-64.unstripped.exe and
b/agents/windows/check_mk_agent-64.unstripped.exe differ
diff --git a/agents/windows/check_mk_agent-64.unversioned.exe
b/agents/windows/check_mk_agent-64.unversioned.exe
index 0a9551c..86953d6 100755
Binary files a/agents/windows/check_mk_agent-64.unversioned.exe and
b/agents/windows/check_mk_agent-64.unversioned.exe differ
diff --git a/agents/windows/check_mk_agent.exe b/agents/windows/check_mk_agent.exe
index 1f699ac..2b15763 100755
Binary files a/agents/windows/check_mk_agent.exe and b/agents/windows/check_mk_agent.exe
differ
diff --git a/agents/windows/check_mk_agent.msi b/agents/windows/check_mk_agent.msi
index a7a3f27..398eabf 100755
Binary files a/agents/windows/check_mk_agent.msi and b/agents/windows/check_mk_agent.msi
differ
diff --git a/agents/windows/check_mk_agent.unstripped.exe
b/agents/windows/check_mk_agent.unstripped.exe
index b0da6dc..69c4e60 100755
Binary files a/agents/windows/check_mk_agent.unstripped.exe and
b/agents/windows/check_mk_agent.unstripped.exe differ
diff --git a/agents/windows/check_mk_agent.unversioned.exe
b/agents/windows/check_mk_agent.unversioned.exe
index 10251ff..235143b 100755
Binary files a/agents/windows/check_mk_agent.unversioned.exe and
b/agents/windows/check_mk_agent.unversioned.exe differ
diff --git a/agents/windows/crash.exe b/agents/windows/crash.exe
index c40e5e5..7a9e64a 100755
Binary files a/agents/windows/crash.exe and b/agents/windows/crash.exe differ
diff --git a/agents/windows/install_agent-64.exe b/agents/windows/install_agent-64.exe
index 41eb2d7..8f55da0 100755
Binary files a/agents/windows/install_agent-64.exe and
b/agents/windows/install_agent-64.exe differ
diff --git a/agents/windows/install_agent.exe b/agents/windows/install_agent.exe
index 030b11e..1be05ce 100755
Binary files a/agents/windows/install_agent.exe and b/agents/windows/install_agent.exe
differ
diff --git a/agents/windows/nowin.exe b/agents/windows/nowin.exe
index 7d41981..856afb5 100755
Binary files a/agents/windows/nowin.exe and b/agents/windows/nowin.exe differ
diff --git a/agents/windows/plugins/mk_logwatch.exe
b/agents/windows/plugins/mk_logwatch.exe
index 3407f7b..3acbda1 100755
Binary files a/agents/windows/plugins/mk_logwatch.exe and
b/agents/windows/plugins/mk_logwatch.exe differ
diff --git a/agents/windows/sections/SectionMRPE.cc
b/agents/windows/sections/SectionMRPE.cc
index 6db231a..f05b87c 100644
--- a/agents/windows/sections/SectionMRPE.cc
+++ b/agents/windows/sections/SectionMRPE.cc
@@ -163,7 +163,6 @@ bool SectionMRPE::produceOutputInner(std::ostream &out,
int nagios_code = command.exitCode();
out << nagios_code << " " << plugin_output
<< "\n";
crash_log("Script finished");
- command.closeScriptHandles();
} catch (const std::exception &e) {
crash_log("mrpe failed: %s", e.what());
out << "3 Unable to execute - plugin may be missing.\n";
diff --git a/agents/windows/sections/SectionPluginGroup.cc
b/agents/windows/sections/SectionPluginGroup.cc
index 4166f4b..30e52a4 100644
--- a/agents/windows/sections/SectionPluginGroup.cc
+++ b/agents/windows/sections/SectionPluginGroup.cc
@@ -145,8 +145,6 @@ static int launch_program(script_container *cont) {
(char *)HeapAlloc(GetProcessHeap(), 0, buffer_u8.size() + 1);
memcpy(cont->buffer_work, buffer_u8.c_str(), buffer_u8.size() + 1);
}
-
- command.closeScriptHandles();
} catch (const std::exception &e) {
crash_log("%s", e.what());
result = CANCELED;
diff --git a/agents/windows/types.h b/agents/windows/types.h
index 084f69a..5386bcd 100644
--- a/agents/windows/types.h
+++ b/agents/windows/types.h
@@ -359,6 +359,7 @@ public:
// wrapper for windows handles that automatically closes the
// handle on leaving scope
+// FIXME: duplicate of ManagedHandle?
class WinHandle {
public:
WinHandle(HANDLE hdl = INVALID_HANDLE_VALUE) : _handle(hdl) {}