From 4cc3fe8eb3a74650a01a5464ddb9f8d67e4e17dd Mon Sep 17 00:00:00 2001 From: coreykosak Date: Mon, 2 May 2016 09:48:09 -0400 Subject: [PATCH] Modify the -T command line option so it watches for more errors. (#1642) 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 | 22 +++++++++++++++------- src/daemon/configfile.c | 20 +++++++++++++++----- src/daemon/plugin.c | 14 ++++++++++---- src/daemon/plugin.h | 4 ++-- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/daemon/collectd.c b/src/daemon/collectd.c index 2576789c..a72ff35f 100644 --- a/src/daemon/collectd.c +++ b/src/daemon/collectd.c @@ -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) diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 70ada80a..34c23cdf 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -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 diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index fa78fa02..3a2e2878 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -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) /* {{{ */ diff --git a/src/daemon/plugin.h b/src/daemon/plugin.h index a63f5cdc..1e4b3d18 100644 --- a/src/daemon/plugin.h +++ b/src/daemon/plugin.h @@ -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 -- 2.30.2