X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fconfigfile.c;h=a31fb64d17781854d015236155c6a833eae8c186;hb=e1bfa71aca1f37c2f293dc9adb44065c6e7a9ad9;hp=16e07247b3cf56d07937d677c1eddf861c5c2655;hpb=092e0c8763ef268918fb8117be9579d375ac8cfc;p=collectd.git diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 16e07247..a31fb64d 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -72,15 +72,16 @@ typedef struct cf_complex_callback_s typedef struct cf_value_map_s { - char *key; + const char *key; int (*func) (oconfig_item_t *); } cf_value_map_t; typedef struct cf_global_option_s { - char *key; + const char *key; char *value; - char *def; + _Bool from_cli; /* value set from CLI */ + const char *def; } cf_global_option_t; /* @@ -108,21 +109,21 @@ static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map); static cf_global_option_t cf_global_options[] = { - {"BaseDir", NULL, PKGLOCALSTATEDIR}, - {"PIDFile", NULL, PIDFILE}, - {"Hostname", NULL, NULL}, - {"FQDNLookup", NULL, "true"}, - {"Interval", NULL, NULL}, - {"ReadThreads", NULL, "5"}, - {"WriteThreads", NULL, "5"}, - {"WriteQueueLimitHigh", NULL, NULL}, - {"WriteQueueLimitLow", NULL, NULL}, - {"Timeout", NULL, "2"}, - {"AutoLoadPlugin", NULL, "false"}, - {"CollectInternalStats", NULL, "false"}, - {"PreCacheChain", NULL, "PreCache"}, - {"PostCacheChain", NULL, "PostCache"}, - {"MaxReadInterval", NULL, "86400"} + {"BaseDir", NULL, 0, PKGLOCALSTATEDIR}, + {"PIDFile", NULL, 0, PIDFILE}, + {"Hostname", NULL, 0, NULL}, + {"FQDNLookup", NULL, 0, "true"}, + {"Interval", NULL, 0, NULL}, + {"ReadThreads", NULL, 0, "5"}, + {"WriteThreads", NULL, 0, "5"}, + {"WriteQueueLimitHigh", NULL, 0, NULL}, + {"WriteQueueLimitLow", NULL, 0, NULL}, + {"Timeout", NULL, 0, "2"}, + {"AutoLoadPlugin", NULL, 0, "false"}, + {"CollectInternalStats", NULL, 0, "false"}, + {"PreCacheChain", NULL, 0, "PreCache"}, + {"PostCacheChain", NULL, 0, "PostCache"}, + {"MaxReadInterval", NULL, 0, "86400"} }; static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options); @@ -154,7 +155,7 @@ static int cf_dispatch (const char *type, const char *orig_key, char *key; char *value; int ret; - int i; + int i = 0; if (orig_key == NULL) return (EINVAL); @@ -210,19 +211,19 @@ static int dispatch_global_option (const oconfig_item_t *ci) if (ci->values_num != 1) return (-1); if (ci->values[0].type == OCONFIG_TYPE_STRING) - return (global_option_set (ci->key, ci->values[0].value.string)); + return (global_option_set (ci->key, ci->values[0].value.string, 0)); else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) { char tmp[128]; ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number); - return (global_option_set (ci->key, tmp)); + return (global_option_set (ci->key, tmp, 0)); } else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) { if (ci->values[0].value.boolean) - return (global_option_set (ci->key, "true")); + return (global_option_set (ci->key, "true", 0)); else - return (global_option_set (ci->key, "false")); + return (global_option_set (ci->key, "false", 0)); } return (-1); @@ -230,8 +231,6 @@ static int dispatch_global_option (const oconfig_item_t *ci) static int dispatch_value_typesdb (oconfig_item_t *ci) { - int i = 0; - assert (strcasecmp (ci->key, "TypesDB") == 0); cf_default_typesdb = 0; @@ -241,7 +240,7 @@ static int dispatch_value_typesdb (oconfig_item_t *ci) return (-1); } - for (i = 0; i < ci->values_num; ++i) + for (int i = 0; i < ci->values_num; ++i) { if (OCONFIG_TYPE_STRING != ci->values[i].type) { WARNING ("configfile: TypesDB: Skipping %i. argument which " @@ -257,7 +256,7 @@ static int dispatch_value_typesdb (oconfig_item_t *ci) static int dispatch_value_plugindir (oconfig_item_t *ci) { assert (strcasecmp (ci->key, "PluginDir") == 0); - + if (ci->values_num != 1) return (-1); if (ci->values[0].type != OCONFIG_TYPE_STRING) @@ -269,10 +268,9 @@ static int dispatch_value_plugindir (oconfig_item_t *ci) static int dispatch_loadplugin (oconfig_item_t *ci) { - int i; const char *name; unsigned int flags = 0; - plugin_ctx_t ctx; + plugin_ctx_t ctx = { 0 }; plugin_ctx_t old_ctx; int ret_val; @@ -288,22 +286,26 @@ static int dispatch_loadplugin (oconfig_item_t *ci) name = "virt"; /* default to the global interval set before loading this plugin */ - memset (&ctx, 0, sizeof (ctx)); ctx.interval = cf_get_default_interval (); + ctx.flush_interval = 0; + ctx.flush_timeout = 0; - for (i = 0; i < ci->children_num; ++i) { - if (strcasecmp("Globals", ci->children[i].key) == 0) - cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL); - else if (strcasecmp ("Interval", ci->children[i].key) == 0) { - if (cf_util_get_cdtime (ci->children + i, &ctx.interval) != 0) { - /* cf_util_get_cdtime will log an error */ - continue; - } - } + for (int i = 0; i < ci->children_num; ++i) + { + oconfig_item_t *child = ci->children + i; + + if (strcasecmp("Globals", child->key) == 0) + cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL); + else if (strcasecmp ("Interval", child->key) == 0) + cf_util_get_cdtime (child, &ctx.interval); + else if (strcasecmp ("FlushInterval", child->key) == 0) + cf_util_get_cdtime (child, &ctx.flush_interval); + else if (strcasecmp ("FlushTimeout", child->key) == 0) + cf_util_get_cdtime (child, &ctx.flush_timeout); else { WARNING("Ignoring unknown LoadPlugin option \"%s\" " "for plugin \"%s\"", - ci->children[i].key, ci->values[0].value.string); + child->key, ci->values[0].value.string); } } @@ -320,12 +322,11 @@ static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci) char buffer[4096]; char *buffer_ptr; int buffer_free; - int i; buffer_ptr = buffer; buffer_free = sizeof (buffer); - for (i = 0; i < ci->values_num; i++) + for (int i = 0; i < ci->values_num; i++) { int status = -1; @@ -353,17 +354,16 @@ static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci) static int dispatch_value (oconfig_item_t *ci) { - int ret = -2; - int i; + int ret = 0; - for (i = 0; i < cf_value_map_num; i++) + for (int i = 0; i < cf_value_map_num; i++) if (strcasecmp (cf_value_map[i].key, ci->key) == 0) { ret = cf_value_map[i].func (ci); break; } - for (i = 0; i < cf_global_options_num; i++) + for (int i = 0; i < cf_global_options_num; i++) if (strcasecmp (cf_global_options[i].key, ci->key) == 0) { ret = dispatch_global_option (ci); @@ -375,10 +375,7 @@ static int dispatch_value (oconfig_item_t *ci) static int dispatch_block_plugin (oconfig_item_t *ci) { - int i; - char *name; - - cf_complex_callback_t *cb; + const char *name; if (strcasecmp (ci->key, "Plugin") != 0) return (-1); @@ -400,12 +397,11 @@ static int dispatch_block_plugin (oconfig_item_t *ci) if (IS_TRUE (global_option_get ("AutoLoadPlugin"))) { - plugin_ctx_t ctx; + plugin_ctx_t ctx = { 0 }; plugin_ctx_t old_ctx; int status; /* default to the global interval set before loading this plugin */ - memset (&ctx, 0, sizeof (ctx)); ctx.interval = cf_get_default_interval (); old_ctx = plugin_set_ctx (ctx); @@ -422,7 +418,7 @@ static int dispatch_block_plugin (oconfig_item_t *ci) } /* Check for a complex callback first */ - for (cb = complex_callback_head; cb != NULL; cb = cb->next) + for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL; cb = cb->next) { if (strcasecmp (name, cb->type) == 0) { @@ -437,7 +433,7 @@ static int dispatch_block_plugin (oconfig_item_t *ci) } /* Hm, no complex plugin found. Dispatch the values one by one */ - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { if (ci->children[i].children == NULL) dispatch_value_plugin (name, ci->children + i); @@ -473,7 +469,6 @@ static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src, int offset) { oconfig_item_t *temp; - int i; assert (offset >= 0); assert (dst->children_num > offset); @@ -481,7 +476,7 @@ static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src, /* Free the memory used by the replaced child. Usually that's the * `Include "blah"' statement. */ temp = dst->children + offset; - for (i = 0; i < temp->values_num; i++) + for (int i = 0; i < temp->values_num; i++) { if (temp->values[i].type == OCONFIG_TYPE_STRING) { @@ -509,7 +504,7 @@ static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src, return (0); } - temp = (oconfig_item_t *) realloc (dst->children, + temp = realloc (dst->children, sizeof (oconfig_item_t) * (dst->children_num + src->children_num - 1)); if (temp == NULL) @@ -557,7 +552,7 @@ static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src) if ((src == NULL) || (src->children_num == 0)) return (0); - temp = (oconfig_item_t *) realloc (dst->children, + temp = realloc (dst->children, sizeof (oconfig_item_t) * (dst->children_num + src->children_num)); if (temp == NULL) @@ -582,17 +577,13 @@ static oconfig_item_t *cf_read_generic (const char *path, static int cf_include_all (oconfig_item_t *root, int depth) { - int i; - - for (i = 0; i < root->children_num; i++) + for (int i = 0; i < root->children_num; i++) { oconfig_item_t *new; oconfig_item_t *old; char *pattern = NULL; - int j; - if (strcasecmp (root->children[i].key, "Include") != 0) continue; @@ -605,7 +596,7 @@ static int cf_include_all (oconfig_item_t *root, int depth) continue; } - for (j = 0; j < old->children_num; ++j) + for (int j = 0; j < old->children_num; ++j) { oconfig_item_t *child = old->children + j; @@ -699,7 +690,6 @@ static oconfig_item_t *cf_read_dir (const char *dir, char **filenames = NULL; int filenames_num = 0; int status; - int i; assert (depth < CF_MAX_DEPTH); @@ -712,14 +702,13 @@ static oconfig_item_t *cf_read_dir (const char *dir, return (NULL); } - root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t)); + root = calloc (1, sizeof (*root)); if (root == NULL) { - ERROR ("configfile: malloc failed."); + ERROR ("configfile: calloc failed."); closedir (dh); return (NULL); } - memset (root, 0, sizeof (oconfig_item_t)); while ((de = readdir (dh)) != NULL) { @@ -737,7 +726,7 @@ static oconfig_item_t *cf_read_dir (const char *dir, " name is too long.", dir, de->d_name); closedir (dh); - for (i = 0; i < filenames_num; ++i) + for (int i = 0; i < filenames_num; ++i) free (filenames[i]); free (filenames); free (root); @@ -745,12 +734,12 @@ static oconfig_item_t *cf_read_dir (const char *dir, } ++filenames_num; - tmp = (char **) realloc (filenames, + tmp = realloc (filenames, filenames_num * sizeof (*filenames)); if (tmp == NULL) { ERROR ("configfile: realloc failed."); closedir (dh); - for (i = 0; i < filenames_num - 1; ++i) + for (int i = 0; i < filenames_num - 1; ++i) free (filenames[i]); free (filenames); free (root); @@ -770,7 +759,7 @@ static oconfig_item_t *cf_read_dir (const char *dir, qsort ((void *) filenames, filenames_num, sizeof (*filenames), cf_compare_string); - for (i = 0; i < filenames_num; ++i) + for (int i = 0; i < filenames_num; ++i) { oconfig_item_t *temp; char *name = filenames[i]; @@ -803,7 +792,7 @@ static oconfig_item_t *cf_read_dir (const char *dir, * * There are two versions of this function: If `wordexp' exists shell wildcards * will be expanded and the function will include all matches found. If - * `wordexp' (or, more precisely, it's header file) is not available the + * `wordexp' (or, more precisely, its header file) is not available the * simpler function is used which does not do any such expansion. */ #if HAVE_WORDEXP_H @@ -814,7 +803,6 @@ static oconfig_item_t *cf_read_generic (const char *path, int status; const char *path_ptr; wordexp_t we; - size_t i; if (depth >= CF_MAX_DEPTH) { @@ -830,20 +818,19 @@ static oconfig_item_t *cf_read_generic (const char *path, return (NULL); } - root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t)); + root = calloc (1, sizeof (*root)); if (root == NULL) { - ERROR ("configfile: malloc failed."); + ERROR ("configfile: calloc failed."); return (NULL); } - memset (root, '\0', sizeof (oconfig_item_t)); /* wordexp() might return a sorted list already. That's not * documented though, so let's make sure we get what we want. */ qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv), cf_compare_string); - for (i = 0; i < we.we_wordc; i++) + for (size_t i = 0; i < we.we_wordc; i++) { oconfig_item_t *temp; struct stat statbuf; @@ -921,13 +908,12 @@ static oconfig_item_t *cf_read_generic (const char *path, } /* oconfig_item_t *cf_read_generic */ #endif /* !HAVE_WORDEXP_H */ -/* +/* * Public functions */ -int global_option_set (const char *option, const char *value) +int global_option_set (const char *option, const char *value, _Bool from_cli) { int i; - DEBUG ("option = %s; value = %s;", option, value); for (i = 0; i < cf_global_options_num; i++) @@ -935,12 +921,16 @@ int global_option_set (const char *option, const char *value) break; if (i >= cf_global_options_num) + { + ERROR ("configfile: Cannot set unknown global option `%s'.", option); return (-1); + } - if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1) + if (cf_global_options[i].from_cli && (! from_cli)) { - DEBUG ("Configfile: Ignoring `PIDFILE' option because " - "command-line option `-P' take precedence."); + DEBUG ("configfile: Ignoring %s `%s' option because " + "it was overriden by a command-line option.", + option, value); return (0); } @@ -951,20 +941,24 @@ int global_option_set (const char *option, const char *value) else cf_global_options[i].value = NULL; + cf_global_options[i].from_cli = from_cli; + return (0); } const char *global_option_get (const char *option) { int i; - for (i = 0; i < cf_global_options_num; i++) if (strcasecmp (cf_global_options[i].key, option) == 0) break; if (i >= cf_global_options_num) + { + ERROR ("configfile: Cannot get unknown global option `%s'.", option); return (NULL); - + } + return ((cf_global_options[i].value != NULL) ? cf_global_options[i].value : cf_global_options[i].def); @@ -1015,9 +1009,7 @@ cdtime_t cf_get_default_interval (void) void cf_unregister (const char *type) { - cf_callback_t *this, *prev; - - for (prev = NULL, this = first_callback; + for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL; prev = this, this = this->next) if (strcasecmp (this->type, type) == 0) @@ -1034,9 +1026,7 @@ void cf_unregister (const char *type) void cf_unregister_complex (const char *type) { - cf_complex_callback_t *this, *prev; - - for (prev = NULL, this = complex_callback_head; + for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head; this != NULL; prev = this, this = this->next) if (strcasecmp (this->type, type) == 0) @@ -1062,7 +1052,7 @@ void cf_register (const char *type, cf_unregister (type); /* This pointer will be free'd in `cf_unregister' */ - if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL) + if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL) return; cf_cb->type = type; @@ -1079,7 +1069,7 @@ int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *)) { cf_complex_callback_t *new; - new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t)); + new = malloc (sizeof (*new)); if (new == NULL) return (-1); @@ -1110,10 +1100,10 @@ int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *)) return (0); } /* int cf_register_complex */ -int cf_read (char *filename) +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) @@ -1128,21 +1118,30 @@ int cf_read (char *filename) return (-1); } - for (i = 0; i < conf->children_num; i++) + for (int 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