Module: check_mk
Branch: master
Commit: e9b2f05f1a2f5db462d8c21319641f5ec9a021d0
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e9b2f05f1a2f5d…
Author: Sebastian Herbord <sh(a)mathias-kettner.de>
Date: Wed Jun 15 14:42:32 2016 +0200
windows agent: minor fixed to memory management
---
agents/windows/Configuration.cc | 12 +++++++++++-
agents/windows/Configuration.h | 1 +
agents/windows/ExternalCmd.cc | 9 ++++++---
agents/windows/PerfCounter.cc | 2 +-
agents/windows/check_mk_agent.cc | 20 ++++++++++++++++++--
agents/windows/types.h | 11 +++++++++++
6 files changed, 48 insertions(+), 7 deletions(-)
diff --git a/agents/windows/Configuration.cc b/agents/windows/Configuration.cc
index 18de0a8..fd40b98 100644
--- a/agents/windows/Configuration.cc
+++ b/agents/windows/Configuration.cc
@@ -54,7 +54,9 @@ Configuration::Configuration(const Environment &env)
, _encrypted_rt(true)
, _environment(env)
, _ps_use_wmi(false)
- , _ps_full_path(false) {
+ , _ps_full_path(false)
+ , _eventlog_vista_api(false)
+{
_logwatch_globlines.setGroupFunction(&Configuration::addConditionPattern);
CollectorRegistry::instance().startFile();
@@ -69,6 +71,14 @@ Configuration::Configuration(const Environment &env)
postProcessOnlyFrom();
}
+Configuration::~Configuration()
+{
+ for (const auto &textfile : *_logwatch_textfiles) {
+ delete textfile;
+ }
+ _logwatch_textfiles.clear();
+}
+
unsigned long Configuration::enabledSections() const {
return _enabled_sections;
}
diff --git a/agents/windows/Configuration.h b/agents/windows/Configuration.h
index 78561fb..99322e8 100644
--- a/agents/windows/Configuration.h
+++ b/agents/windows/Configuration.h
@@ -61,6 +61,7 @@ check = MEM mrpe/check_mem -w 10 -c 20
class Configuration {
public:
Configuration(const Environment &env);
+ ~Configuration();
unsigned long enabledSections() const;
unsigned long realtimeSections() const;
diff --git a/agents/windows/ExternalCmd.cc b/agents/windows/ExternalCmd.cc
index 7f32358..4239057 100644
--- a/agents/windows/ExternalCmd.cc
+++ b/agents/windows/ExternalCmd.cc
@@ -48,10 +48,10 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
// child process needs to be able to inherit the pipe handles
security_attributes.bInheritHandle = true;
- HANDLE script_stdout;
- HANDLE script_stderr;
+ HANDLE script_stdout { INVALID_HANDLE_VALUE };
+ HANDLE script_stderr { INVALID_HANDLE_VALUE };
- OnScopeExit release_handles([&]() {
+ OnScopeExit release_handles([script_stdout, script_stderr]() {
::CloseHandle(script_stdout);
::CloseHandle(script_stderr);
});
@@ -68,6 +68,8 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
// base new process statup info on current process
STARTUPINFO si;
+ ZeroMemory(&si, sizeof(STARTUPINFO));
+ si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
@@ -75,6 +77,7 @@ ExternalCmd::ExternalCmd(const char *cmdline) {
si.hStdError = with_stderr ? script_stdout : script_stderr;
PROCESS_INFORMATION pi;
+ ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
std::unique_ptr<char[], decltype(free) *> cmdline_buf(strdup(cmdline),
free);
if (!CreateProcess(nullptr, cmdline_buf.get(), nullptr, nullptr, TRUE,
diff --git a/agents/windows/PerfCounter.cc b/agents/windows/PerfCounter.cc
index b2e6c69..b4268eb 100644
--- a/agents/windows/PerfCounter.cc
+++ b/agents/windows/PerfCounter.cc
@@ -172,7 +172,7 @@ std::vector<BYTE> PerfCounterObject::retrieveCounterData(
result.resize(DEFAULT_BUFFER_SIZE);
DWORD buffer_size = result.size();
- DWORD type;
+ DWORD type {0};
DWORD ret;
while ((ret = RegQueryValueExW(HKEY_PERFORMANCE_DATA, counterList, nullptr,
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index a98e9a1..3e9de18 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -202,6 +202,7 @@ bool g_found_crash = false;
std::unique_ptr<OHMMonitor> g_ohmMonitor;
+
class WMILookup {
public:
static wmi::Helper &get(const std::wstring &path = L"Root\\Cimv2")
{
@@ -2075,8 +2076,12 @@ int launch_program(script_container *cont) {
memset(buf, 0, BUFFER_SIZE);
time_t process_start = time(0);
+ if (cont->buffer_work != NULL) {
+ HeapFree(GetProcessHeap(), 0, cont->buffer_work);
+ }
cont->buffer_work = (char *)HeapAlloc(
GetProcessHeap(), HEAP_ZERO_MEMORY, HEAP_BUFFER_DEFAULT);
+
unsigned long current_heap_size =
HeapSize(GetProcessHeap(), 0, cont->buffer_work);
@@ -2347,7 +2352,6 @@ void output_external_programs(OutputProxy &out, script_type
type) {
HeapFree(GetProcessHeap(), 0, cont->buffer_work);
cont->buffer = cache_buffer;
}
-
cont->buffer_work = NULL;
cont->status = SCRIPT_IDLE;
} else if (cont->retry_count < 0 && cont->buffer != NULL) {
@@ -2908,7 +2912,7 @@ void stop_threads() {
// just check the script_container status
int sizedt = script_containers.size();
HANDLE hThreadArray[sizedt];
- int active_thread_count = 0;
+ DWORD active_thread_count = 0;
script_containers_t::iterator it_cont = script_containers.begin();
while (it_cont != script_containers.end()) {
@@ -3505,6 +3509,18 @@ void cleanup() {
cleanup_logwatch();
}
+
+ for (auto ptr : g_eventlog_hints) {
+ free(ptr->name);
+ delete ptr;
+ }
+ g_eventlog_hints.clear();
+
+ for (auto kv : script_containers) {
+ delete kv.second;
+ }
+ script_containers.clear();
+ CloseHandle(g_collection_thread);
}
void show_version() { printf("Check_MK_Agent version %s\n", check_mk_version);
}
diff --git a/agents/windows/types.h b/agents/windows/types.h
index 5b330f3..1c02718 100644
--- a/agents/windows/types.h
+++ b/agents/windows/types.h
@@ -143,6 +143,17 @@ struct runas_include {
};
struct script_container {
+ ~script_container() {
+ if (worker_thread != INVALID_HANDLE_VALUE) {
+ CloseHandle(worker_thread);
+ }
+ // allocated with strdup
+ free(path);
+ free(script_path);
+ // allocated with HeapAlloc (may be null)
+ HeapFree(GetProcessHeap(), 0, buffer);
+ HeapFree(GetProcessHeap(), 0, buffer_work);
+ }
char *path; // full path with interpreter, cscript, etc.
char *script_path; // path of script
int max_age;