From: Florian Forster Date: Tue, 27 May 2008 15:13:35 +0000 (+0200) Subject: processes plugin: Let `ps_list_register' fail if any part of the regex stuff fails. X-Git-Tag: collectd-4.5.0~139 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=4dc2a589190ec985184fc7718fada38ffaf2743c;p=collectd.git processes plugin: Let `ps_list_register' fail if any part of the regex stuff fails. --- diff --git a/src/processes.c b/src/processes.c index 177a9fbb..b022b461 100644 --- a/src/processes.c +++ b/src/processes.c @@ -174,35 +174,64 @@ static void ps_list_register (const char *name, const char *regexp) { procstat_t *new; procstat_t *ptr; + int status; - if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL) + new = (procstat_t *) malloc (sizeof (procstat_t)); + if (new == NULL) + { + ERROR ("processes plugin: ps_list_register: malloc failed."); return; + } memset (new, 0, sizeof (procstat_t)); sstrncpy (new->name, name, sizeof (new->name)); +#if HAVE_REGEX_H if (regexp != NULL) { -#if HAVE_REGEX_H DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name); - if ((new->re = (regex_t *) malloc (sizeof (regex_t))) != NULL) + new->re = (regex_t *) malloc (sizeof (regex_t)); + if (new->re == NULL) { - if (regcomp(new->re, regexp, REG_EXTENDED|REG_NOSUB) != 0) - { - DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp); - sfree(new->re); - } - } else { - DEBUG("ProcessMatch: malloc failed when allocating memory for regexp!"); + ERROR ("processes plugin: ps_list_register: malloc failed."); + sfree (new); + return; + } + + status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB); + if (status != 0) + { + DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp); + sfree(new->re); + return; } + } #else - DEBUG("ProcessMatch: regexp '%s' met in config file, but regexps are not supported!", regexp); -#endif + if (regexp != NULL) + { + ERROR ("processes plugin: ps_list_register: " + "Regular expression \"%s\" found in config " + "file, but support for regular expressions " + "has been dispabled at compile time.", + regexp); + sfree (new); + return; } +#endif for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) { if (strcmp (ptr->name, name) == 0) + { + WARNING ("processes plugin: You have configured more " + "than one `Process' or " + "`ProcessMatch' with the same name. " + "All but the first setting will be " + "ignored."); + sfree (new->re); + sfree (new); return; + } + if (ptr->next == NULL) break; } @@ -211,7 +240,7 @@ static void ps_list_register (const char *name, const char *regexp) list_head_g = new; else ptr->next = new; -} +} /* void ps_list_register */ /* try to match name against entry, returns 1 if success */ static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)