X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fprocesses.c;h=5a795b5aefd915a5e4c1faeb072c63ebc3e2dfa0;hb=d0540d2ebaead6661de0ef29c0435c046c7ff4f7;hp=8e2443bca4414be83166f06da96771a500560d9f;hpb=6286127470b83578c2889db546cfa32cb0fe0147;p=collectd.git diff --git a/src/processes.c b/src/processes.c index 8e2443bc..5a795b5a 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) @@ -1094,7 +1092,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 +1216,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; } @@ -1255,7 +1253,7 @@ static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len) return buf; } /* char *ps_get_cmdline (...) */ -static int read_fork_rate () +static int read_fork_rate (void) { FILE *proc_stat; char buffer[1024]; @@ -1348,18 +1346,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 +1364,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; @@ -1450,7 +1449,7 @@ static int ps_read_process(long pid, procstat_t *ps, char *state) * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads). * The result is the sum for all the threads created on each cpu */ -static int read_fork_rate() +static int read_fork_rate (void) { extern kstat_ctl_t *kc; kstat_t *ksp_chain = NULL;