Modify the -T command line option so it watches for more errors. (#1642)
authorcoreykosak <coreykosak@users.noreply.github.com>
Mon, 2 May 2016 13:48:09 +0000 (09:48 -0400)
committerMarc Fournier <marc.fournier@camptocamp.com>
Mon, 2 May 2016 13:48:09 +0000 (15:48 +0200)
After this change, the following kinds of errors will cause collectd -T to
exit with an abnormal status:
- errors reading the config file
- errors reading types.db
- errors on plugin_init
- errors on plugin_shutdown

src/daemon/collectd.c
src/daemon/configfile.c
src/daemon/plugin.c
src/daemon/plugin.h

index 2576789..a72ff35 100644 (file)
@@ -332,9 +332,7 @@ static int do_init (void)
        }
 #endif
 
-       plugin_init_all ();
-
-       return (0);
+       return plugin_init_all ();
 } /* int do_init () */
 
 
@@ -388,8 +386,7 @@ static int do_loop (void)
 
 static int do_shutdown (void)
 {
-       plugin_shutdown_all ();
-       return (0);
+       return plugin_shutdown_all ();
 } /* int do_shutdown */
 
 #if COLLECT_DAEMON
@@ -729,12 +726,19 @@ int main (int argc, char **argv)
        /*
         * run the actual loops
         */
-       do_init ();
+       if (do_init () != 0)
+       {
+               ERROR ("Error: one or more plugin init callbacks failed.");
+               exit_status = 1;
+       }
 
        if (test_readall)
        {
                if (plugin_read_all_once () != 0)
+               {
+                       ERROR ("Error: one or more plugin read callbacks failed.");
                        exit_status = 1;
+               }
        }
        else
        {
@@ -745,7 +749,11 @@ int main (int argc, char **argv)
        /* close syslog */
        INFO ("Exiting normally.");
 
-       do_shutdown ();
+       if (do_shutdown () != 0)
+       {
+               ERROR ("Error: one or more plugin shutdown callbacks failed.");
+               exit_status = 1;
+       }
 
 #if COLLECT_DAEMON
        if (daemonize)
index 70ada80..34c23cd 100644 (file)
@@ -358,7 +358,7 @@ static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
 
 static int dispatch_value (oconfig_item_t *ci)
 {
-       int ret = -2;
+       int ret = 0;
        int i;
 
        for (i = 0; i < cf_value_map_num; i++)
@@ -1117,6 +1117,7 @@ int cf_read (const char *filename)
 {
        oconfig_item_t *conf;
        int i;
+       int ret = 0;
 
        conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
        if (conf == NULL)
@@ -1134,18 +1135,27 @@ int cf_read (const char *filename)
        for (i = 0; i < conf->children_num; i++)
        {
                if (conf->children[i].children == NULL)
-                       dispatch_value (conf->children + i);
+               {
+                       if (dispatch_value (conf->children + i) != 0)
+                               ret = -1;
+               }
                else
-                       dispatch_block (conf->children + i);
+               {
+                       if (dispatch_block (conf->children + i) != 0)
+                               ret = -1;
+               }
        }
 
        oconfig_free (conf);
 
        /* Read the default types.db if no `TypesDB' option was given. */
        if (cf_default_typesdb)
-               read_types_list (PKGDATADIR"/types.db");
+       {
+               if (read_types_list (PKGDATADIR"/types.db") != 0)
+                       ret = -1;
+       }
 
-       return (0);
+       return ret;
 } /* int cf_read */
 
 /* Assures the config option is a string, duplicates it and returns the copy in
index fa78fa0..3a2e287 100644 (file)
@@ -1690,11 +1690,12 @@ int plugin_unregister_notification (const char *name)
        return (plugin_unregister (list_notification, name));
 }
 
-void plugin_init_all (void)
+int plugin_init_all (void)
 {
        char const *chain_name;
        llentry_t *le;
        int status;
+       int ret = 0;
 
        /* Init the value cache */
        uc_init ();
@@ -1739,7 +1740,7 @@ void plugin_init_all (void)
        }
 
        if ((list_init == NULL) && (read_heap == NULL))
-               return;
+               return ret;
 
        /* Calling all init callbacks before checking if read callbacks
         * are available allows the init callbacks to register the read
@@ -1768,6 +1769,7 @@ void plugin_init_all (void)
                         * handling themselves. */
                        /* FIXME: Unload _all_ functions */
                        plugin_unregister_read (le->key);
+                       ret = -1;
                }
 
                le = le->next;
@@ -1789,6 +1791,7 @@ void plugin_init_all (void)
                if (num != -1)
                        start_read_threads ((num > 0) ? num : 5);
        }
+       return ret;
 } /* void plugin_init_all */
 
 /* TODO: Rename this function. */
@@ -1972,9 +1975,10 @@ int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
   return (0);
 } /* int plugin_flush */
 
-void plugin_shutdown_all (void)
+int plugin_shutdown_all (void)
 {
        llentry_t *le;
+       int ret = 0;  // Assume success.
 
        stop_read_threads ();
 
@@ -2011,7 +2015,8 @@ void plugin_shutdown_all (void)
                 * after callback returns. */
                le = le->next;
 
-               (*callback) ();
+               if ((*callback) () != 0)
+                       ret = -1;
 
                plugin_set_ctx (old_ctx);
        }
@@ -2033,6 +2038,7 @@ void plugin_shutdown_all (void)
 
        plugin_free_loaded ();
        plugin_free_data_sets ();
+       return (ret);
 } /* void plugin_shutdown_all */
 
 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
index a63f5cd..1e4b3d1 100644 (file)
@@ -240,10 +240,10 @@ void plugin_set_dir (const char *dir);
  */
 int plugin_load (const char *name, uint32_t flags);
 
-void plugin_init_all (void);
+int plugin_init_all (void);
 void plugin_read_all (void);
 int plugin_read_all_once (void);
-void plugin_shutdown_all (void);
+int plugin_shutdown_all (void);
 
 /*
  * NAME