Code

processes plugin: Let `ps_list_register' fail if any part of the regex stuff fails.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 27 May 2008 15:13:35 +0000 (17:13 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 27 May 2008 15:13:35 +0000 (17:13 +0200)
src/processes.c

index 177a9fbb81971714a9b2a4a4462542c734d91951..b022b461e84d07280666ae6020b93b5073dc84ff 100644 (file)
@@ -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)