X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fprocesses.c;h=03b44b58d740b0359a8e9dfb876cd6095117dcf1;hb=a8c59c6d75efe3656b2f019ddefb2ce6cb2cbb2e;hp=d0d736154281f7f9819825d346c1a16b1e44b45d;hpb=daebbbee0ac53d39a0486aeb8c41fd358f46ed82;p=collectd.git diff --git a/src/processes.c b/src/processes.c index d0d73615..03b44b58 100644 --- a/src/processes.c +++ b/src/processes.c @@ -262,20 +262,19 @@ static void ps_list_register (const char *name, const char *regexp) procstat_t *ptr; int status; - new = (procstat_t *) malloc (sizeof (procstat_t)); + new = calloc (1, sizeof (*new)); if (new == NULL) { - ERROR ("processes plugin: ps_list_register: malloc failed."); + ERROR ("processes plugin: ps_list_register: calloc failed."); return; } - memset (new, 0, sizeof (procstat_t)); sstrncpy (new->name, name, sizeof (new->name)); #if HAVE_REGEX_H if (regexp != NULL) { DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name); - new->re = (regex_t *) malloc (sizeof (regex_t)); + new->re = malloc (sizeof (*new->re)); if (new->re == NULL) { ERROR ("processes plugin: ps_list_register: malloc failed."); @@ -410,10 +409,9 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t { procstat_entry_t *new; - new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t)); + new = calloc (1, sizeof (*new)); if (new == NULL) return; - memset (new, 0, sizeof (procstat_entry_t)); new->id = entry->id; if (pse == NULL) @@ -1040,13 +1038,11 @@ static int ps_read_process (long pid, procstat_t *ps, char *state) * strchr(3) and strrchr(3) to avoid pointer arithmetic which would * otherwise be required to determine name_len. */ name_start_pos = 0; - while ((buffer[name_start_pos] != '(') - && (name_start_pos < buffer_len)) + while (name_start_pos < buffer_len && buffer[name_start_pos] != '(') name_start_pos++; name_end_pos = buffer_len; - while ((buffer[name_end_pos] != ')') - && (name_end_pos > 0)) + while (name_end_pos > 0 && buffer[name_end_pos] != ')') name_end_pos--; /* Either '(' or ')' is not found or they are in the wrong order. @@ -1094,7 +1090,7 @@ static int ps_read_process (long pid, procstat_t *ps, char *state) ps->vmem_code = -1; DEBUG("ps_read_process: did not get vmem data for pid %li", pid); } - if (ps->num_lwp <= 0) + if (ps->num_lwp == 0) ps->num_lwp = 1; ps->num_proc = 1; } @@ -1218,7 +1214,7 @@ static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len) buf_ptr += status; len -= status; - if (len <= 0) + if (len == 0) break; } @@ -1348,18 +1344,15 @@ static int ps_read_process(long pid, procstat_t *ps, char *state) snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid); - buffer = malloc(sizeof (pstatus_t)); - memset(buffer, 0, sizeof (pstatus_t)); + buffer = calloc(1, sizeof (pstatus_t)); read_file_contents(filename, buffer, sizeof (pstatus_t)); myStatus = (pstatus_t *) buffer; - buffer = malloc(sizeof (psinfo_t)); - memset(buffer, 0, sizeof(psinfo_t)); + buffer = calloc(1, sizeof (psinfo_t)); read_file_contents(f_psinfo, buffer, sizeof (psinfo_t)); myInfo = (psinfo_t *) buffer; - buffer = malloc(sizeof (prusage_t)); - memset(buffer, 0, sizeof(prusage_t)); + buffer = calloc(1, sizeof (prusage_t)); read_file_contents(f_usage, buffer, sizeof (prusage_t)); myUsage = (prusage_t *) buffer; @@ -1369,6 +1362,10 @@ static int ps_read_process(long pid, procstat_t *ps, char *state) ps->num_proc = 0; ps->num_lwp = 0; *state = (char) 'Z'; + + sfree(myStatus); + sfree(myInfo); + sfree(myUsage); return (0); } else { ps->num_proc = 1;