X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fconfigfile.c;h=79ce14b7ab4498356a2710a445f2bf4ac661c9ad;hb=8f6aa6970bf787e6a11e095322af3338ec781d78;hp=2830a86e1ace7a7e869ed33309b9593dfe487dbe;hpb=9b2d6a2792ef579fca5c03a2076c1e05f4b93507;p=collectd.git diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 2830a86e..79ce14b7 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -29,11 +29,11 @@ #include "liboconfig/oconfig.h" -#include "common.h" #include "configfile.h" #include "filter_chain.h" #include "plugin.h" #include "types_list.h" +#include "utils/common/common.h" #if HAVE_WORDEXP_H #include @@ -190,13 +190,17 @@ static int cf_dispatch(const char *type, const char *orig_key, } /* int cf_dispatch */ static int dispatch_global_option(const oconfig_item_t *ci) { - if (ci->values_num != 1) + if (ci->values_num != 1) { + ERROR("configfile: Global option `%s' needs exactly one argument.", + ci->key); return -1; + } + if (ci->values[0].type == OCONFIG_TYPE_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]; - snprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number); + ssnprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number); return global_option_set(ci->key, tmp, 0); } else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) { if (ci->values[0].value.boolean) @@ -205,6 +209,8 @@ static int dispatch_global_option(const oconfig_item_t *ci) { return global_option_set(ci->key, "false", 0); } + ERROR("configfile: Global option `%s' argument has unknown type.", ci->key); + return -1; } /* int dispatch_global_option */ @@ -234,37 +240,38 @@ 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) + if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) { + ERROR("configfile: The `PluginDir' option needs exactly one string " + "argument."); return -1; + } plugin_set_dir(ci->values[0].value.string); return 0; } static int dispatch_loadplugin(oconfig_item_t *ci) { - const char *name; bool global = false; - plugin_ctx_t ctx = {0}; - plugin_ctx_t old_ctx; - int ret_val; assert(strcasecmp(ci->key, "LoadPlugin") == 0); - if (ci->values_num != 1) - return -1; - if (ci->values[0].type != OCONFIG_TYPE_STRING) + if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) { + ERROR("configfile: The `LoadPlugin' block needs exactly one string " + "argument."); return -1; + } - name = ci->values[0].value.string; + const char *name = ci->values[0].value.string; if (strcmp("libvirt", name) == 0) name = "virt"; /* default to the global interval set before loading this plugin */ - ctx.interval = cf_get_default_interval(); - ctx.flush_interval = 0; - ctx.flush_timeout = 0; + plugin_ctx_t ctx = { + .interval = cf_get_default_interval(), + .name = strdup(name), + }; + if (ctx.name == NULL) + return ENOMEM; for (int i = 0; i < ci->children_num; ++i) { oconfig_item_t *child = ci->children + i; @@ -280,12 +287,12 @@ static int dispatch_loadplugin(oconfig_item_t *ci) { else { WARNING("Ignoring unknown LoadPlugin option \"%s\" " "for plugin \"%s\"", - child->key, ci->values[0].value.string); + child->key, name); } } - old_ctx = plugin_set_ctx(ctx); - ret_val = plugin_load(name, global); + plugin_ctx_t old_ctx = plugin_set_ctx(ctx); + int ret_val = plugin_load(name, global); /* reset to the "global" context */ plugin_set_ctx(old_ctx); @@ -305,12 +312,12 @@ static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) { if (ci->values[i].type == OCONFIG_TYPE_STRING) status = - snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string); + ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string); else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) status = - snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number); + ssnprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number); else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) - status = snprintf(buffer_ptr, buffer_free, " %s", + status = ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.boolean ? "true" : "false"); if ((status < 0) || (status >= buffer_free)) @@ -333,6 +340,9 @@ static int dispatch_value(oconfig_item_t *ci) { break; } + if (ret != 0) + return ret; + 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); @@ -343,16 +353,18 @@ static int dispatch_value(oconfig_item_t *ci) { } /* int dispatch_value */ static int dispatch_block_plugin(oconfig_item_t *ci) { - const char *name; + assert(strcasecmp(ci->key, "Plugin") == 0); - if (strcasecmp(ci->key, "Plugin") != 0) - return -1; - if (ci->values_num < 1) + if (ci->values_num < 1) { + ERROR("configfile: The `Plugin' block requires arguments."); return -1; - if (ci->values[0].type != OCONFIG_TYPE_STRING) + } + if (ci->values[0].type != OCONFIG_TYPE_STRING) { + ERROR("configfile: First argument of `Plugin' block should be a string."); return -1; + } - name = ci->values[0].value.string; + const char *name = ci->values[0].value.string; if (strcmp("libvirt", name) == 0) { /* TODO(octo): Remove this legacy. */ WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid " @@ -370,6 +382,7 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { /* default to the global interval set before loading this plugin */ ctx.interval = cf_get_default_interval(); + ctx.name = strdup(name); old_ctx = plugin_set_ctx(ctx); status = plugin_load(name, /* flags = */ false); @@ -399,10 +412,16 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { } /* Hm, no complex plugin found. Dispatch the values one by one */ - for (int i = 0; i < ci->children_num; i++) { - if (ci->children[i].children == NULL) - dispatch_value_plugin(name, ci->children + i); - else { + for (int i = 0, ret = 0; i < ci->children_num; i++) { + if (ci->children[i].children == NULL) { + oconfig_item_t *child = ci->children + i; + ret = dispatch_value_plugin(name, child); + if (ret != 0) { + ERROR("Plugin %s failed to handle option %s, return code: %i", name, + child->key, ret); + return ret; + } + } else { WARNING("There is a `%s' block within the " "configuration for the %s plugin. " "The plugin either only expects " @@ -461,9 +480,9 @@ static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src, return 0; } - temp = realloc(dst->children, - sizeof(oconfig_item_t) * - (dst->children_num + src->children_num - 1)); + temp = + realloc(dst->children, sizeof(oconfig_item_t) * + (dst->children_num + src->children_num - 1)); if (temp == NULL) { ERROR("configfile: realloc failed."); return -1; @@ -502,9 +521,8 @@ static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) { if ((src == NULL) || (src->children_num == 0)) return 0; - temp = - realloc(dst->children, - sizeof(oconfig_item_t) * (dst->children_num + src->children_num)); + temp = realloc(dst->children, sizeof(oconfig_item_t) * + (dst->children_num + src->children_num)); if (temp == NULL) { ERROR("configfile: realloc failed."); return -1; @@ -652,7 +670,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, if ((de->d_name[0] == '.') || (de->d_name[0] == 0)) continue; - status = snprintf(name, sizeof(name), "%s/%s", dir, de->d_name); + status = ssnprintf(name, sizeof(name), "%s/%s", dir, de->d_name); if ((status < 0) || ((size_t)status >= sizeof(name))) { ERROR("configfile: Not including `%s/%s' because its" " name is too long.", @@ -792,7 +810,7 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, return root; } /* oconfig_item_t *cf_read_generic */ -/* #endif HAVE_WORDEXP_H */ + /* #endif HAVE_WORDEXP_H */ #else /* if !HAVE_WORDEXP_H */ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, @@ -1035,16 +1053,12 @@ int cf_read(const char *filename) { * success. */ int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */ { - char *string; - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR("cf_util_get_string: The %s option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } - string = strdup(ci->values[0].value.string); + char *string = strdup(ci->values[0].value.string); if (string == NULL) return -1; @@ -1056,21 +1070,19 @@ int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */ } /* }}} int cf_util_get_string */ /* Assures the config option is a string and copies it to the provided buffer. - * Assures null-termination. */ + * Assures NUL-termination. */ int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */ size_t buffer_size) { if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1)) return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR("cf_util_get_string_buffer: The %s option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } strncpy(buffer, ci->values[0].value.string, buffer_size); - buffer[buffer_size - 1] = 0; + buffer[buffer_size - 1] = '\0'; return 0; } /* }}} int cf_util_get_string_buffer */ @@ -1082,9 +1094,7 @@ int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_int: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } @@ -1099,9 +1109,7 @@ int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_double: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } @@ -1117,9 +1125,7 @@ int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) && (ci->values[0].type != OCONFIG_TYPE_STRING))) { - ERROR("cf_util_get_boolean: The %s option requires " - "exactly one boolean argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key); return -1; } @@ -1128,19 +1134,19 @@ int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ *ret_bool = ci->values[0].value.boolean ? true : false; break; case OCONFIG_TYPE_STRING: - WARNING("cf_util_get_boolean: Using string value `%s' for boolean option " - "`%s' is deprecated and will be removed in future releases. " - "Use unquoted true or false instead.", - ci->values[0].value.string, ci->key); + P_WARNING("Using string value `%s' for boolean option `%s' is deprecated " + "and will be removed in future releases. Use unquoted true or " + "false instead.", + ci->values[0].value.string, ci->key); if (IS_TRUE(ci->values[0].value.string)) *ret_bool = true; else if (IS_FALSE(ci->values[0].value.string)) *ret_bool = false; else { - ERROR("cf_util_get_boolean: Cannot parse string value `%s' of the `%s' " - "option as a boolean value.", - ci->values[0].value.string, ci->key); + P_ERROR("Cannot parse string value `%s' of the `%s' option as a boolean " + "value.", + ci->values[0].value.string, ci->key); return -1; } break; @@ -1181,9 +1187,7 @@ int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */ if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) && (ci->values[0].type != OCONFIG_TYPE_NUMBER))) { - ERROR("cf_util_get_port_number: The \"%s\" option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } @@ -1193,11 +1197,9 @@ int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */ assert(ci->values[0].type == OCONFIG_TYPE_NUMBER); tmp = (int)(ci->values[0].value.number + 0.5); if ((tmp < 1) || (tmp > 65535)) { - ERROR("cf_util_get_port_number: The \"%s\" option requires " - "a service name or a port number. The number " - "you specified, %i, is not in the valid " - "range of 1-65535.", - ci->key, tmp); + P_ERROR("The `%s' option requires a service name or a port number. The " + "number you specified, %i, is not in the valid range of 1-65535.", + ci->key, tmp); return -1; } @@ -1211,18 +1213,15 @@ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ int status; if (ci->values_num != 1) { - ERROR("cf_util_get_service: The %s option requires exactly " - "one argument.", - ci->key); + P_ERROR("The `%s` option requires exactly one argument.", ci->key); return -1; } if (ci->values[0].type == OCONFIG_TYPE_STRING) return cf_util_get_string(ci, ret_string); if (ci->values[0].type != OCONFIG_TYPE_NUMBER) { - ERROR("cf_util_get_service: The %s option requires " - "exactly one string or numeric argument.", - ci->key); + P_ERROR("The `%s` option requires exactly one string or numeric argument.", + ci->key); } port = 0; @@ -1230,19 +1229,17 @@ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ if (status != 0) return status; else if ((port < 1) || (port > 65535)) { - ERROR("cf_util_get_service: The port number given " - "for the %s option is out of " - "range (%i).", - ci->key, port); + P_ERROR("The port number given for the `%s` option is out of range (%i).", + ci->key, port); return -1; } service = malloc(6); if (service == NULL) { - ERROR("cf_util_get_service: Out of memory."); + P_ERROR("cf_util_get_service: Out of memory."); return -1; } - snprintf(service, 6, "%i", port); + ssnprintf(service, 6, "%i", port); sfree(*ret_string); *ret_string = service; @@ -1256,16 +1253,13 @@ int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_cdtime: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } if (ci->values[0].value.number < 0.0) { - ERROR("cf_util_get_cdtime: The numeric argument of the %s " - "option must not be negative.", - ci->key); + P_ERROR("The numeric argument of the `%s' option must not be negative.", + ci->key); return -1; }