Fix memleak
[monky] / src / proc.c
index 7f64cdc..218393e 100644 (file)
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *   (see AUTHORS)
  * All rights reserved.
  *
@@ -115,18 +115,22 @@ void print_pid_cmdline(struct text_object *obj, char *p, int p_max_size)
        char* buf;
        int i, bytes_read;
 
-       asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               for(i = 0; i < bytes_read-1; i++) {
-                       if(buf[i] == 0) {
-                               buf[i] = ' ';
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/cmdline", obj->data.s);
+               strcpy(obj->data.s, buf);
+               free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       for(i = 0; i < bytes_read-1; i++) {
+                               if(buf[i] == 0) {
+                                       buf[i] = ' ';
+                               }
                        }
+                       snprintf(p, p_max_size, "%s", buf);
+                       free(buf);
                }
-               snprintf(p, p_max_size, "%s", buf);
-               free(buf);
+       } else {
+               NORM_ERR("$pid_cmdline didn't receive a argument");
        }
 }
 
@@ -213,14 +217,18 @@ void print_pid_nice(struct text_object *obj, char *p, int p_max_size) {
        int bytes_read;
        long int nice_value;
 
-       asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
-               snprintf(p, p_max_size, "%ld", nice_value);
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
+               strcpy(obj->data.s, buf);
                free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %ld", &nice_value);
+                       snprintf(p, p_max_size, "%ld", nice_value);
+                       free(buf);
+               }
+       } else {
+               NORM_ERR("$pid_nice didn't receive a argument");
        }
 }
 
@@ -288,14 +296,18 @@ void print_pid_priority(struct text_object *obj, char *p, int p_max_size) {
        int bytes_read;
        long int priority;
 
-       asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
-               snprintf(p, p_max_size, "%ld", priority);
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
+               strcpy(obj->data.s, buf);
                free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %ld", &priority);
+                       snprintf(p, p_max_size, "%ld", priority);
+                       free(buf);
+               }
+       } else {
+               NORM_ERR("$pid_priority didn't receive a argument");
        }
 }
 
@@ -472,14 +484,18 @@ void print_pid_time_kernelmode(struct text_object *obj, char *p, int p_max_size)
        int bytes_read;
        unsigned long int umtime;
 
-       asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
-               snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
+               strcpy(obj->data.s, buf);
                free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &umtime);
+                       snprintf(p, p_max_size, "%.2f", (float) umtime / 100);
+                       free(buf);
+               }
+       } else {
+               NORM_ERR("$pid_time_kernelmode didn't receive a argument");
        }
 }
 
@@ -488,14 +504,18 @@ void print_pid_time_usermode(struct text_object *obj, char *p, int p_max_size) {
        int bytes_read;
        unsigned long int kmtime;
 
-       asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
-               snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
+               strcpy(obj->data.s, buf);
                free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu", &kmtime);
+                       snprintf(p, p_max_size, "%.2f", (float) kmtime / 100);
+                       free(buf);
+               }
+       } else {
+               NORM_ERR("$pid_time_usermode didn't receive a argument");
        }
 }
 
@@ -504,14 +524,18 @@ void print_pid_time(struct text_object *obj, char *p, int p_max_size) {
        int bytes_read;
        unsigned long int umtime, kmtime;
 
-       asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
-       strcpy(obj->data.s, buf);
-       free(buf);
-       buf = readfile(obj->data.s, &bytes_read, 1);
-       if(buf != NULL) {
-               sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
-               snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
+       if(*(obj->data.s) != 0) {
+               asprintf(&buf, PROCDIR "/%s/stat", obj->data.s);
+               strcpy(obj->data.s, buf);
                free(buf);
+               buf = readfile(obj->data.s, &bytes_read, 1);
+               if(buf != NULL) {
+                       sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &umtime, &kmtime);
+                       snprintf(p, p_max_size, "%.2f", (float) (umtime + kmtime) / 100);
+                       free(buf);
+               }
+       } else {
+               NORM_ERR("$pid_time didn't receive a argument");
        }
 }
 
@@ -795,3 +819,53 @@ void print_pid_vmlib(struct text_object *obj, char *p, int p_max_size) {
 void print_pid_vmpte(struct text_object *obj, char *p, int p_max_size) {
        internal_print_pid_vm(obj->data.s, p, p_max_size, "VmPTE:\t", "Can't find the process page table entries size in '%s'");
 }
+
+#define READ_ENTRY "read_bytes: "
+#define READNOTFOUND   "Can't find the amount of bytes read in '%s'"
+void print_pid_read(struct text_object *obj, char *p, int p_max_size) {
+       char *begin, *end, *buf = NULL;
+       int bytes_read;
+
+       asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
+       strcpy(obj->data.s, buf);
+       free(buf);
+       buf = readfile(obj->data.s, &bytes_read, 1);
+       if(buf != NULL) {
+               begin = strstr(buf, READ_ENTRY);
+               if(begin != NULL) {
+                       end = strchr(begin, '\n');
+                       if(end != NULL) {
+                               *(end) = 0;
+                       }
+                       snprintf(p, p_max_size, "%s", begin);
+               } else {
+                       NORM_ERR(READNOTFOUND, obj->data.s);
+               }
+               free(buf);
+       }
+}
+
+#define WRITE_ENTRY "write_bytes: "
+#define WRITENOTFOUND  "Can't find the amount of bytes written in '%s'"
+void print_pid_write(struct text_object *obj, char *p, int p_max_size) {
+       char *begin, *end, *buf = NULL;
+       int bytes_read;
+
+       asprintf(&buf, PROCDIR "/%s/io", obj->data.s);
+       strcpy(obj->data.s, buf);
+       free(buf);
+       buf = readfile(obj->data.s, &bytes_read, 1);
+       if(buf != NULL) {
+               begin = strstr(buf, WRITE_ENTRY);
+               if(begin != NULL) {
+                       end = strchr(begin, '\n');
+                       if(end != NULL) {
+                               *(end) = 0;
+                       }
+                       snprintf(p, p_max_size, "%s", begin);
+               } else {
+                       NORM_ERR(WRITENOTFOUND, obj->data.s);
+               }
+               free(buf);
+       }
+}