@@ -0,0 +1,733 @@
+From 802137b5180acf546fcaed1e65497e9631115eec Mon Sep 17 00:00:00 2001
+From: tildeslash <info@tildeslash.com>
+Date: Thu, 8 May 2014 22:39:25 +0200
+Subject: [PATCH] - MONIT-55 (Issue #34): fix the memory corruption ... if
+ initprocesstree_sysdep() returned error, the ptree/oldptree destroy may be
+ called twice+ as the references were saved via Util_isProcessRunning called
+ from control.c during process (re)start
+
+- if process initialization failed, don't let partial data to be passed to validate
+---
+ src/process.c | 463 ++++++++++++++++++++++-----------------------
+ src/process/sysdep_LINUX.c | 95 +++++-----
+ src/util.c | 10 +-
+ 3 files changed, 278 insertions(+), 290 deletions(-)
+
+diff --git a/src/process.c b/src/process.c
+index 5c5cd4d..7df4824 100644
+--- a/src/process.c
++++ b/src/process.c
+@@ -73,19 +73,18 @@
+ * @return TRUE if succeeded otherwise FALSE.
+ */
+ int init_process_info(void) {
+- memset(&systeminfo, 0, sizeof(SystemInfo_T));
+- gettimeofday(&systeminfo.collected, NULL);
+- if(uname(&systeminfo.uname) < 0) {
+- LogError("'%s' resource monitoring initialization error -- uname failed: %s\n", Run.system->name, STRERROR);
+- return FALSE;
+- }
+-
+- systeminfo.total_cpu_user_percent = -10;
+- systeminfo.total_cpu_syst_percent = -10;
+- systeminfo.total_cpu_wait_percent = -10;
++ memset(&systeminfo, 0, sizeof(SystemInfo_T));
++ gettimeofday(&systeminfo.collected, NULL);
++ if(uname(&systeminfo.uname) < 0) {
++ LogError("'%s' resource monitoring initialization error -- uname failed: %s\n", Run.system->name, STRERROR);
++ return FALSE;
++ }
+
+- return (init_process_info_sysdep());
++ systeminfo.total_cpu_user_percent = -10;
++ systeminfo.total_cpu_syst_percent = -10;
++ systeminfo.total_cpu_wait_percent = -10;
+
++ return (init_process_info_sysdep());
+ }
+
+
+@@ -97,55 +96,50 @@ int init_process_info(void) {
+ * @return TRUE if succeeded otherwise FALSE.
+ */
+ int update_process_data(Service_T s, ProcessTree_T *pt, int treesize, pid_t pid) {
+- int leaf;
+-
+- ASSERT(s);
+- ASSERT(systeminfo.mem_kbyte_max > 0);
+-
+- /* save the previous pid and set actual one */
+- s->inf->priv.process._pid = s->inf->priv.process.pid;
+- s->inf->priv.process.pid = pid;
+-
+- if ((leaf = findprocess(pid, pt, treesize)) != -1) {
+-
+- /* save the previous ppid and set actual one */
+- s->inf->priv.process._ppid = s->inf->priv.process.ppid;
+- s->inf->priv.process.ppid = pt[leaf].ppid;
+- s->inf->priv.process.uid = pt[leaf].uid;
+- s->inf->priv.process.euid = pt[leaf].euid;
+- s->inf->priv.process.gid = pt[leaf].gid;
+- s->inf->priv.process.uptime = time(NULL) - pt[leaf].starttime;
+- s->inf->priv.process.children = pt[leaf].children_sum;
+- s->inf->priv.process.mem_kbyte = pt[leaf].mem_kbyte;
+- s->inf->priv.process.status_flag = pt[leaf].status_flag;
+- s->inf->priv.process.total_mem_kbyte = pt[leaf].mem_kbyte_sum;
+- s->inf->priv.process.cpu_percent = pt[leaf].cpu_percent;
+- s->inf->priv.process.total_cpu_percent = pt[leaf].cpu_percent_sum;
+-
+- if (systeminfo.mem_kbyte_max == 0) {
+- s->inf->priv.process.total_mem_percent = 0;
+- s->inf->priv.process.mem_percent = 0;
+- } else {
+- s->inf->priv.process.total_mem_percent = (int)((double)pt[leaf].mem_kbyte_sum * 1000.0 / systeminfo.mem_kbyte_max);
+- s->inf->priv.process.mem_percent = (int)((double)pt[leaf].mem_kbyte * 1000.0 / systeminfo.mem_kbyte_max);
+- }
+-
+- } else {
+- s->inf->priv.process.ppid = 0;
+- s->inf->priv.process.uid = -1;
+- s->inf->priv.process.euid = -1;
+- s->inf->priv.process.gid = -1;
+- s->inf->priv.process.uptime = 0;
+- s->inf->priv.process.children = 0;
+- s->inf->priv.process.total_mem_kbyte = 0;
+- s->inf->priv.process.total_mem_percent = 0;
+- s->inf->priv.process.mem_kbyte = 0;
+- s->inf->priv.process.mem_percent = 0;
+- s->inf->priv.process.cpu_percent = 0;
+- s->inf->priv.process.total_cpu_percent = 0;
+- }
+-
+- return TRUE;
++ ASSERT(s);
++ ASSERT(systeminfo.mem_kbyte_max > 0);
++
++ /* save the previous pid and set actual one */
++ s->inf->priv.process._pid = s->inf->priv.process.pid;
++ s->inf->priv.process.pid = pid;
++
++ int leaf;
++ if ((leaf = findprocess(pid, pt, treesize)) != -1) {
++ /* save the previous ppid and set actual one */
++ s->inf->priv.process._ppid = s->inf->priv.process.ppid;
++ s->inf->priv.process.ppid = pt[leaf].ppid;
++ s->inf->priv.process.uid = pt[leaf].uid;
++ s->inf->priv.process.euid = pt[leaf].euid;
++ s->inf->priv.process.gid = pt[leaf].gid;
++ s->inf->priv.process.uptime = time(NULL) - pt[leaf].starttime;
++ s->inf->priv.process.children = pt[leaf].children_sum;
++ s->inf->priv.process.mem_kbyte = pt[leaf].mem_kbyte;
++ s->inf->priv.process.status_flag = pt[leaf].status_flag;
++ s->inf->priv.process.total_mem_kbyte = pt[leaf].mem_kbyte_sum;
++ s->inf->priv.process.cpu_percent = pt[leaf].cpu_percent;
++ s->inf->priv.process.total_cpu_percent = pt[leaf].cpu_percent_sum;
++ if (systeminfo.mem_kbyte_max == 0) {
++ s->inf->priv.process.total_mem_percent = 0;
++ s->inf->priv.process.mem_percent = 0;
++ } else {
++ s->inf->priv.process.total_mem_percent = (int)((double)pt[leaf].mem_kbyte_sum * 1000.0 / systeminfo.mem_kbyte_max);
++ s->inf->priv.process.mem_percent = (int)((double)pt[leaf].mem_kbyte * 1000.0 / systeminfo.mem_kbyte_max);
++ }
++ } else {
++ s->inf->priv.process.ppid = 0;
++ s->inf->priv.process.uid = -1;
++ s->inf->priv.process.euid = -1;
++ s->inf->priv.process.gid = -1;
++ s->inf->priv.process.uptime = 0;
++ s->inf->priv.process.children = 0;
++ s->inf->priv.process.total_mem_kbyte = 0;
++ s->inf->priv.process.total_mem_percent = 0;
++ s->inf->priv.process.mem_kbyte = 0;
++ s->inf->priv.process.mem_percent = 0;
++ s->inf->priv.process.cpu_percent = 0;
++ s->inf->priv.process.total_cpu_percent = 0;
++ }
++ return TRUE;
+ }
+
+
+@@ -154,47 +148,45 @@ int update_process_data(Service_T s, ProcessTree_T *pt, int treesize, pid_t pid)
+ * @return TRUE if successful, otherwise FALSE
+ */
+ int update_system_load() {
+-
+- if (Run.doprocess) {
+-
+- ASSERT(systeminfo.mem_kbyte_max > 0);
+-
+- /** Get load average triplet */
+- if (-1 == getloadavg_sysdep(systeminfo.loadavg, 3)) {
+- LogError("'%s' statistic error -- load average gathering failed\n", Run.system->name);
+- goto error1;
+- }
+-
+- /** Get memory usage statistic */
+- if (! used_system_memory_sysdep(&systeminfo)) {
+- LogError("'%s' statistic error -- memory usage gathering failed\n", Run.system->name);
+- goto error2;
+- }
+- systeminfo.total_mem_percent = (int)(1000 * (double)systeminfo.total_mem_kbyte / (double)systeminfo.mem_kbyte_max);
+- systeminfo.total_swap_percent = systeminfo.swap_kbyte_max ? (int)(1000 * (double)systeminfo.total_swap_kbyte / (double)systeminfo.swap_kbyte_max) : 0;
+-
+- /** Get CPU usage statistic */
+- if (! used_system_cpu_sysdep(&systeminfo)) {
+- LogError("'%s' statistic error -- cpu usage gathering failed\n", Run.system->name);
+- goto error3;
+- }
+-
+- return TRUE;
+- }
++ if (Run.doprocess) {
++ ASSERT(systeminfo.mem_kbyte_max > 0);
++
++ /** Get load average triplet */
++ if (-1 == getloadavg_sysdep(systeminfo.loadavg, 3)) {
++ LogError("'%s' statistic error -- load average gathering failed\n", Run.system->name);
++ goto error1;
++ }
++
++ /** Get memory usage statistic */
++ if (! used_system_memory_sysdep(&systeminfo)) {
++ LogError("'%s' statistic error -- memory usage gathering failed\n", Run.system->name);
++ goto error2;
++ }
++ systeminfo.total_mem_percent = (int)(1000 * (double)systeminfo.total_mem_kbyte / (double)systeminfo.mem_kbyte_max);
++ systeminfo.total_swap_percent = systeminfo.swap_kbyte_max ? (int)(1000 * (double)systeminfo.total_swap_kbyte / (double)systeminfo.swap_kbyte_max) : 0;
++
++ /** Get CPU usage statistic */
++ if (! used_system_cpu_sysdep(&systeminfo)) {
|