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,
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));
"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;
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);
+ 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);
+ return -1;
} /* int dispatch_global_option */
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) {
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);
+ return -1;
if (ci->values[0].type != OCONFIG_TYPE_STRING)
- return (-1);
+ return -1;
plugin_set_dir(ci->values[0].value.string);
- return (0);
+ return 0;
}
static int dispatch_loadplugin(oconfig_item_t *ci) {
assert(strcasecmp(ci->key, "LoadPlugin") == 0);
if (ci->values_num != 1)
- return (-1);
+ return -1;
if (ci->values[0].type != OCONFIG_TYPE_STRING)
- return (-1);
+ return -1;
name = ci->values[0].value.string;
if (strcmp("libvirt", name) == 0)
/* 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) {
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) {
break;
}
- return (ret);
+ return ret;
} /* int dispatch_value */
static int dispatch_block_plugin(oconfig_item_t *ci) {
const char *name;
if (strcasecmp(ci->key, "Plugin") != 0)
- return (-1);
+ return -1;
if (ci->values_num < 1)
- return (-1);
+ return -1;
if (ci->values[0].type != OCONFIG_TYPE_STRING)
- return (-1);
+ return -1;
name = ci->values[0].value.string;
if (strcmp("libvirt", name) == 0) {
ERROR("Automatically loading plugin \"%s\" failed "
"with status %i.",
name, status);
- return (status);
+ return status;
}
}
old_ctx = plugin_set_ctx(cb->ctx);
ret_val = (cb->callback(ci));
plugin_set_ctx(old_ctx);
- return (ret_val);
+ return ret_val;
}
}
}
}
- 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,
* 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;
/* 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;
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
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. */
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,
"does not match pattern `%s'.",
filename, pattern);
free(tmp);
- return (NULL);
+ return NULL;
}
free(tmp);
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) {
char errbuf[1024];
ERROR("configfile: opendir failed: %s",
sstrerror(errno, errbuf, sizeof(errbuf)));
- return (NULL);
+ 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) {
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.",
free(filenames[i]);
free(filenames);
free(root);
- return (NULL);
+ return NULL;
}
++filenames_num;
free(filenames[i]);
free(filenames);
free(root);
- return (NULL);
+ return NULL;
}
filenames = tmp;
if (filenames == NULL) {
closedir(dh);
- return (root);
+ return root;
}
qsort((void *)filenames, filenames_num, sizeof(*filenames),
closedir(dh);
free(filenames);
- return (root);
+ return root;
} /* oconfig_item_t *cf_read_dir */
/*
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
if (temp == NULL) {
oconfig_free(root);
- return (NULL);
+ return NULL;
}
cf_ci_append_children(root, temp);
wordfree(&we);
- return (root);
+ return root;
} /* oconfig_item_t *cf_read_generic */
/* #endif HAVE_WORDEXP_H */
ERROR("configfile: Not including `%s' because the maximum "
"nesting depth has been reached.",
path);
- return (NULL);
+ return NULL;
}
status = stat(path, &statbuf);
char errbuf[1024];
ERROR("configfile: stat (%s) failed: %s", path,
sstrerror(errno, errbuf, sizeof(errbuf)));
- return (NULL);
+ 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 */
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);
cf_global_options[i].from_cli = from_cli;
- return (0);
+ return 0;
}
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) {
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) /* {{{ */
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) {
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;
last->next = new;
}
- return (0);
+ return 0;
} /* int cf_register_complex */
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++) {
}
return ret;
+
} /* int cf_read */
/* Assures the config option is a string, duplicates it and returns the copy in
ERROR("cf_util_get_string: The %s option requires "
"exactly one string argument.",
ci->key);
- return (-1);
+ return -1;
}
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.
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);
+ return -1;
}
strncpy(buffer, ci->values[0].value.string, buffer_size);
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);
+ 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);
+ 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) /* {{{ */
{
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);
+ return -1;
}
switch (ci->values[0].type) {
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);
+ return -1;
}
break;
}
- return (0);
+ return 0;
} /* }}} int cf_util_get_boolean */
int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */
_Bool b;
if (ret_value == NULL)
- return (EINVAL);
+ return EINVAL;
b = 0;
status = cf_util_get_boolean(ci, &b);
if (status != 0)
- return (status);
+ return status;
if (b) {
*ret_value |= flag;
*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
ERROR("cf_util_get_port_number: The \"%s\" option requires "
"exactly one string argument.",
ci->key);
- return (-1);
+ 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);
"you specified, %i, is not in the valid "
"range of 1-65535.",
ci->key, tmp);
- return (-1);
+ 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) /* {{{ */
ERROR("cf_util_get_service: The %s option requires exactly "
"one argument.",
ci->key);
- return (-1);
+ 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.",
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);
+ return -1;
}
service = malloc(6);
if (service == NULL) {
ERROR("cf_util_get_service: Out of memory.");
- return (-1);
+ 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);
+ 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);
+ return -1;
}
*ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
- return (0);
+ return 0;
} /* }}} int cf_util_get_cdtime */