X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fconfigfile.c;h=c500d3eeb1ffecd46c03056075b807967aa5a0b9;hb=42a30efe33c4d16276589e0dd1f9e135b38e054d;hp=3934e1f9dba546d2c8a2ed0f1a85ccac730048f8;hpb=35a6c9c5fcd87cb78451a974c4d5b5707926845c;p=collectd.git diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 3934e1f9..c500d3ee 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 @@ -76,7 +76,7 @@ typedef struct cf_value_map_s { typedef struct cf_global_option_s { const char *key; char *value; - _Bool from_cli; /* value set from CLI */ + bool from_cli; /* value set from CLI */ const char *def; } cf_global_option_t; @@ -91,8 +91,8 @@ static int dispatch_block_plugin(oconfig_item_t *ci); /* * Private variables */ -static cf_callback_t *first_callback = NULL; -static cf_complex_callback_t *complex_callback_head = NULL; +static cf_callback_t *first_callback; +static cf_complex_callback_t *complex_callback_head; static cf_value_map_t cf_value_map[] = {{"TypesDB", dispatch_value_typesdb}, {"PluginDir", dispatch_value_plugindir}, @@ -128,13 +128,13 @@ static cf_callback_t *cf_search(const char *type) { cf_callback_t *cf_cb; if (type == NULL) - return (NULL); + return NULL; for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next) if (strcasecmp(cf_cb->type, type) == 0) break; - return (cf_cb); + return cf_cb; } static int cf_dispatch(const char *type, const char *orig_key, @@ -147,7 +147,7 @@ static int cf_dispatch(const char *type, const char *orig_key, int i = 0; if (orig_key == NULL) - return (EINVAL); + return EINVAL; DEBUG("type = %s, key = %s, value = %s", ESCAPE_NULL(type), orig_key, ESCAPE_NULL(orig_value)); @@ -157,14 +157,14 @@ static int cf_dispatch(const char *type, const char *orig_key, "the plugin isn't loaded or didn't register " "a configuration callback.", type); - return (-1); + return -1; } if ((key = strdup(orig_key)) == NULL) - return (1); + return 1; if ((value = strdup(orig_value)) == NULL) { free(key); - return (2); + return 2; } ret = -1; @@ -186,26 +186,32 @@ static int cf_dispatch(const char *type, const char *orig_key, free(key); free(value); - return (ret); + return ret; } /* int cf_dispatch */ static int dispatch_global_option(const oconfig_item_t *ci) { - if (ci->values_num != 1) - return (-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)); + 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, 0)); + snprintf(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) - return (global_option_set(ci->key, "true", 0)); + return global_option_set(ci->key, "true", 0); else - return (global_option_set(ci->key, "false", 0)); + return global_option_set(ci->key, "false", 0); } - return (-1); + ERROR("configfile: Global option `%s' argument has unknown type.", ci->key); + + return -1; } /* int dispatch_global_option */ static int dispatch_value_typesdb(oconfig_item_t *ci) { @@ -215,7 +221,7 @@ static int dispatch_value_typesdb(oconfig_item_t *ci) { if (ci->values_num < 1) { ERROR("configfile: `TypesDB' needs at least one argument."); - return (-1); + return -1; } for (int i = 0; i < ci->values_num; ++i) { @@ -228,49 +234,49 @@ static int dispatch_value_typesdb(oconfig_item_t *ci) { read_types_list(ci->values[i].value.string); } - return (0); + return 0; } /* int dispatch_value_typesdb */ 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) - return (-1); + 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); + return 0; } static int dispatch_loadplugin(oconfig_item_t *ci) { - const char *name; - unsigned int flags = 0; - plugin_ctx_t ctx = {0}; - plugin_ctx_t old_ctx; - int ret_val; + bool global = false; assert(strcasecmp(ci->key, "LoadPlugin") == 0); - if (ci->values_num != 1) - return (-1); - if (ci->values[0].type != OCONFIG_TYPE_STRING) - return (-1); + 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; if (strcasecmp("Globals", child->key) == 0) - cf_util_get_flag(child, &flags, PLUGIN_FLAGS_GLOBAL); + cf_util_get_boolean(child, &global); else if (strcasecmp("Interval", child->key) == 0) cf_util_get_cdtime(child, &ctx.interval); else if (strcasecmp("FlushInterval", child->key) == 0) @@ -280,16 +286,16 @@ 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, (uint32_t)flags); + 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); - return (ret_val); + return ret_val; } /* int dispatch_value_loadplugin */ static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) { @@ -305,23 +311,23 @@ static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) { if (ci->values[i].type == OCONFIG_TYPE_STRING) status = - ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string); + snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string); else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) - status = ssnprintf(buffer_ptr, buffer_free, " %lf", - ci->values[i].value.number); + status = + snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number); else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) - status = ssnprintf(buffer_ptr, buffer_free, " %s", - ci->values[i].value.boolean ? "true" : "false"); + status = snprintf(buffer_ptr, buffer_free, " %s", + ci->values[i].value.boolean ? "true" : "false"); if ((status < 0) || (status >= buffer_free)) - return (-1); + return -1; buffer_free -= status; buffer_ptr += status; } /* skip the initial space */ buffer_ptr = buffer + 1; - return (cf_dispatch(plugin, ci->key, buffer_ptr)); + return cf_dispatch(plugin, ci->key, buffer_ptr); } /* int dispatch_value_plugin */ static int dispatch_value(oconfig_item_t *ci) { @@ -333,26 +339,31 @@ 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); break; } - return (ret); + return ret; } /* 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) - return (-1); - if (ci->values[0].type != OCONFIG_TYPE_STRING) - return (-1); + if (ci->values_num < 1) { + ERROR("configfile: The `Plugin' block requires arguments."); + return -1; + } + 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,9 +381,10 @@ 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 = */ 0); + status = plugin_load(name, /* flags = */ false); /* reset to the "global" context */ plugin_set_ctx(old_ctx); @@ -380,7 +392,7 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { ERROR("Automatically loading plugin \"%s\" failed " "with status %i.", name, status); - return (status); + return status; } } @@ -394,15 +406,19 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { old_ctx = plugin_set_ctx(cb->ctx); ret_val = (cb->callback(ci)); plugin_set_ctx(old_ctx); - return (ret_val); + return ret_val; } } /* 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) { + ret = dispatch_value_plugin(name, ci->children + i); + if (ret != 0) { + ERROR("dispatch for plugin %s returned non-zero code : %i", name, ret); + return ret; + } + } else { WARNING("There is a `%s' block within the " "configuration for the %s plugin. " "The plugin either only expects " @@ -413,18 +429,18 @@ static int dispatch_block_plugin(oconfig_item_t *ci) { } } - return (0); + return 0; } static int dispatch_block(oconfig_item_t *ci) { if (strcasecmp(ci->key, "LoadPlugin") == 0) - return (dispatch_loadplugin(ci)); + return dispatch_loadplugin(ci); else if (strcasecmp(ci->key, "Plugin") == 0) - return (dispatch_block_plugin(ci)); + return dispatch_block_plugin(ci); else if (strcasecmp(ci->key, "Chain") == 0) - return (fc_configure(ci)); + return fc_configure(ci); - return (0); + return 0; } static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src, @@ -458,15 +474,15 @@ static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src, * all children. */ if (dst->children_num + src->children_num - 1 == 0) { dst->children_num = 0; - return (0); + 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); + return -1; } dst->children = temp; @@ -493,20 +509,21 @@ static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src, /* Update the number of children. */ dst->children_num += (src->children_num - 1); - return (0); + return 0; } /* int cf_ci_replace_child */ static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) { oconfig_item_t *temp; if ((src == NULL) || (src->children_num == 0)) - return (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); + return -1; } dst->children = temp; @@ -514,7 +531,7 @@ static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) { sizeof(oconfig_item_t) * src->children_num); dst->children_num += src->children_num; - return (0); + return 0; } /* int cf_ci_append_children */ #define CF_MAX_DEPTH 8 @@ -553,13 +570,13 @@ static int cf_include_all(oconfig_item_t *root, int depth) { sfree(pattern); if (new == NULL) - return (-1); + return -1; /* Now replace the i'th child in `root' with `new'. */ if (cf_ci_replace_child(root, new, i) < 0) { sfree(new->values); sfree(new); - return (-1); + return -1; } /* ... and go back to the new i'th child. */ @@ -569,7 +586,7 @@ static int cf_include_all(oconfig_item_t *root, int depth) { sfree(new); } /* for (i = 0; i < root->children_num; i++) */ - return (0); + return 0; } /* int cf_include_all */ static oconfig_item_t *cf_read_file(const char *file, const char *pattern, @@ -589,7 +606,7 @@ static oconfig_item_t *cf_read_file(const char *file, const char *pattern, "does not match pattern `%s'.", filename, pattern); free(tmp); - return (NULL); + return NULL; } free(tmp); @@ -604,16 +621,16 @@ static oconfig_item_t *cf_read_file(const char *file, const char *pattern, root = oconfig_parse_file(file); if (root == NULL) { ERROR("configfile: Cannot read file `%s'.", file); - return (NULL); + return NULL; } status = cf_include_all(root, depth); if (status != 0) { oconfig_free(root); - return (NULL); + return NULL; } - return (root); + return root; } /* oconfig_item_t *cf_read_file */ static int cf_compare_string(const void *p1, const void *p2) { @@ -633,17 +650,15 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, dh = opendir(dir); if (dh == NULL) { - char errbuf[1024]; - ERROR("configfile: opendir failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (NULL); + ERROR("configfile: opendir failed: %s", STRERRNO); + return NULL; } root = calloc(1, sizeof(*root)); if (root == NULL) { ERROR("configfile: calloc failed."); closedir(dh); - return (NULL); + return NULL; } while ((de = readdir(dh)) != NULL) { @@ -653,7 +668,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 = ssnprintf(name, sizeof(name), "%s/%s", dir, de->d_name); + status = snprintf(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.", @@ -663,7 +678,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, free(filenames[i]); free(filenames); free(root); - return (NULL); + return NULL; } ++filenames_num; @@ -675,7 +690,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, free(filenames[i]); free(filenames); free(root); - return (NULL); + return NULL; } filenames = tmp; @@ -684,7 +699,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, if (filenames == NULL) { closedir(dh); - return (root); + return root; } qsort((void *)filenames, filenames_num, sizeof(*filenames), @@ -710,7 +725,7 @@ static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, closedir(dh); free(filenames); - return (root); + return root; } /* oconfig_item_t *cf_read_dir */ /* @@ -736,19 +751,19 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, ERROR("configfile: Not including `%s' because the maximum " "nesting depth has been reached.", path); - return (NULL); + return NULL; } status = wordexp(path, &we, WRDE_NOCMD); if (status != 0) { ERROR("configfile: wordexp (%s) failed.", path); - return (NULL); + return NULL; } root = calloc(1, sizeof(*root)); if (root == NULL) { ERROR("configfile: calloc failed."); - return (NULL); + return NULL; } /* wordexp() might return a sorted list already. That's not @@ -764,9 +779,7 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, status = stat(path_ptr, &statbuf); if (status != 0) { - char errbuf[1024]; - WARNING("configfile: stat (%s) failed: %s", path_ptr, - sstrerror(errno, errbuf, sizeof(errbuf))); + WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO); continue; } @@ -783,7 +796,7 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, if (temp == NULL) { oconfig_free(root); - return (NULL); + return NULL; } cf_ci_append_children(root, temp); @@ -793,7 +806,7 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, wordfree(&we); - return (root); + return root; } /* oconfig_item_t *cf_read_generic */ /* #endif HAVE_WORDEXP_H */ @@ -807,31 +820,29 @@ static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, ERROR("configfile: Not including `%s' because the maximum " "nesting depth has been reached.", path); - return (NULL); + return NULL; } status = stat(path, &statbuf); if (status != 0) { - char errbuf[1024]; - ERROR("configfile: stat (%s) failed: %s", path, - sstrerror(errno, errbuf, sizeof(errbuf))); - return (NULL); + ERROR("configfile: stat (%s) failed: %s", path, STRERRNO); + return NULL; } if (S_ISREG(statbuf.st_mode)) - return (cf_read_file(path, pattern, depth)); + return cf_read_file(path, pattern, depth); else if (S_ISDIR(statbuf.st_mode)) - return (cf_read_dir(path, pattern, depth)); + return cf_read_dir(path, pattern, depth); ERROR("configfile: %s is neither a file nor a directory.", path); - return (NULL); + return NULL; } /* oconfig_item_t *cf_read_generic */ #endif /* !HAVE_WORDEXP_H */ /* * Public functions */ -int global_option_set(const char *option, const char *value, _Bool from_cli) { +int global_option_set(const char *option, const char *value, bool from_cli) { int i; DEBUG("option = %s; value = %s;", option, value); @@ -841,14 +852,14 @@ int global_option_set(const char *option, const char *value, _Bool from_cli) { if (i >= cf_global_options_num) { ERROR("configfile: Cannot set unknown global option `%s'.", option); - return (-1); + return -1; } if (cf_global_options[i].from_cli && (!from_cli)) { DEBUG("configfile: Ignoring %s `%s' option because " "it was overriden by a command-line option.", option, value); - return (0); + return 0; } sfree(cf_global_options[i].value); @@ -860,7 +871,7 @@ int global_option_set(const char *option, const char *value, _Bool from_cli) { cf_global_options[i].from_cli = from_cli; - return (0); + return 0; } const char *global_option_get(const char *option) { @@ -871,11 +882,11 @@ const char *global_option_get(const char *option) { if (i >= cf_global_options_num) { ERROR("configfile: Cannot get unknown global option `%s'.", option); - return (NULL); + return NULL; } - return ((cf_global_options[i].value != NULL) ? cf_global_options[i].value - : cf_global_options[i].def); + return (cf_global_options[i].value != NULL) ? cf_global_options[i].value + : cf_global_options[i].def; } /* char *global_option_get */ long global_option_get_long(const char *option, long default_value) { @@ -884,14 +895,14 @@ long global_option_get_long(const char *option, long default_value) { str = global_option_get(option); if (NULL == str) - return (default_value); + return default_value; errno = 0; value = strtol(str, /* endptr = */ NULL, /* base = */ 0); if (errno != 0) - return (default_value); + return default_value; - return (value); + return value; } /* char *global_option_get_long */ cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */ @@ -902,21 +913,21 @@ cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */ optstr = global_option_get(name); if (optstr == NULL) - return (def); + return def; errno = 0; v = strtod(optstr, &endptr); if ((endptr == NULL) || (*endptr != 0) || (errno != 0)) - return (def); + return def; else if (v <= 0.0) - return (def); + return def; - return (DOUBLE_TO_CDTIME_T(v)); + return DOUBLE_TO_CDTIME_T(v); } /* }}} cdtime_t global_option_get_time */ cdtime_t cf_get_default_interval(void) { - return (global_option_get_time( - "Interval", DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL))); + return global_option_get_time("Interval", + DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL)); } void cf_unregister(const char *type) { @@ -974,12 +985,12 @@ int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) { new = malloc(sizeof(*new)); if (new == NULL) - return (-1); + return -1; new->type = strdup(type); if (new->type == NULL) { sfree(new); - return (-1); + return -1; } new->callback = callback; @@ -996,7 +1007,7 @@ int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) { last->next = new; } - return (0); + return 0; } /* int cf_register_complex */ int cf_read(const char *filename) { @@ -1006,11 +1017,11 @@ int cf_read(const char *filename) { conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0); if (conf == NULL) { ERROR("Unable to read config file %s.", filename); - return (-1); + return -1; } else if (conf->children_num == 0) { ERROR("Configuration file %s is empty.", filename); oconfig_free(conf); - return (-1); + return -1; } for (int i = 0; i < conf->children_num; i++) { @@ -1032,6 +1043,7 @@ int cf_read(const char *filename) { } return ret; + } /* int cf_read */ /* Assures the config option is a string, duplicates it and returns the copy in @@ -1039,132 +1051,119 @@ 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); - return (-1); + 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); + return -1; if (*ret_string != NULL) sfree(*ret_string); *ret_string = string; - return (0); + return 0; } /* }}} 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); + 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); - return (-1); + 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); + return 0; } /* }}} int cf_util_get_string_buffer */ /* Assures the config option is a number and returns it as an int. */ int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */ { if ((ci == NULL) || (ret_value == NULL)) - return (EINVAL); + 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); - return (-1); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); + return -1; } *ret_value = (int)ci->values[0].value.number; - return (0); + return 0; } /* }}} int cf_util_get_int */ int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */ { if ((ci == NULL) || (ret_value == NULL)) - return (EINVAL); + 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); - return (-1); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); + return -1; } *ret_value = ci->values[0].value.number; - return (0); + return 0; } /* }}} int cf_util_get_double */ -int cf_util_get_boolean(const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */ +int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ { if ((ci == NULL) || (ret_bool == NULL)) - return (EINVAL); + return EINVAL; 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); - return (-1); + P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key); + return -1; } switch (ci->values[0].type) { case OCONFIG_TYPE_BOOLEAN: - *ret_bool = ci->values[0].value.boolean ? 1 : 0; + *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 = 1; + *ret_bool = true; else if (IS_FALSE(ci->values[0].value.string)) - *ret_bool = 0; + *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); - return (-1); + 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; } - return (0); + return 0; } /* }}} int cf_util_get_boolean */ int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */ unsigned int *ret_value, unsigned int flag) { int status; - _Bool b; if (ret_value == NULL) - return (EINVAL); + return EINVAL; - b = 0; + bool b = false; status = cf_util_get_boolean(ci, &b); if (status != 0) - return (status); + return status; if (b) { *ret_value |= flag; @@ -1172,7 +1171,7 @@ int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */ *ret_value &= ~flag; } - return (0); + return 0; } /* }}} int cf_util_get_flag */ /* Assures that the config option is a string or a number if the correct range @@ -1186,27 +1185,23 @@ 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); - return (-1); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); + return -1; } if (ci->values[0].type == OCONFIG_TYPE_STRING) - return (service_name_to_port_number(ci->values[0].value.string)); + return service_name_to_port_number(ci->values[0].value.string); 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); - return (-1); + 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; } - return (tmp); + return tmp; } /* }}} int cf_util_get_port_number */ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ @@ -1216,65 +1211,57 @@ 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); - return (-1); + 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)); + 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; status = cf_util_get_int(ci, &port); if (status != 0) - return (status); + 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); - return (-1); + 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."); - return (-1); + P_ERROR("cf_util_get_service: Out of memory."); + return -1; } - ssnprintf(service, 6, "%i", port); + snprintf(service, 6, "%i", port); sfree(*ret_string); *ret_string = service; - return (0); + return 0; } /* }}} int cf_util_get_service */ int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ { if ((ci == NULL) || (ret_value == NULL)) - return (EINVAL); + 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); - return (-1); + 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); - return (-1); + P_ERROR("The numeric argument of the `%s' option must not be negative.", + ci->key); + return -1; } *ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number); - return (0); + return 0; } /* }}} int cf_util_get_cdtime */