X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fconfigfile.c;h=983d99569796fde8b684242663106357e3506d3d;hb=82f6ebad0250c0b8cd0cdf7453fe427fd7b38135;hp=c929d0097aef6279db09c61411442cf3ddef99aa;hpb=a1ae4b5afaf3207a7070b85f802b41b32c0b9a20;p=collectd.git diff --git a/src/configfile.c b/src/configfile.c index c929d009..983d9956 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -75,7 +75,7 @@ typedef struct cf_global_option_s */ static int dispatch_value_typesdb (const oconfig_item_t *ci); static int dispatch_value_plugindir (const oconfig_item_t *ci); -static int dispatch_value_loadplugin (const oconfig_item_t *ci); +static int dispatch_loadplugin (const oconfig_item_t *ci); /* * Private variables @@ -87,7 +87,7 @@ static cf_value_map_t cf_value_map[] = { {"TypesDB", dispatch_value_typesdb}, {"PluginDir", dispatch_value_plugindir}, - {"LoadPlugin", dispatch_value_loadplugin} + {"LoadPlugin", dispatch_loadplugin} }; static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map); @@ -99,6 +99,7 @@ static cf_global_option_t cf_global_options[] = {"FQDNLookup", NULL, "false"}, {"Interval", NULL, "10"}, {"ReadThreads", NULL, "5"}, + {"Timeout", NULL, "2"}, {"PreCacheChain", NULL, "PreCache"}, {"PostCacheChain", NULL, "PostCache"} }; @@ -172,7 +173,7 @@ static int cf_dispatch (const char *type, const char *orig_key, free (key); free (value); - DEBUG ("return (%i)", ret); + DEBUG ("cf_dispatch: return (%i)", ret); return (ret); } /* int cf_dispatch */ @@ -239,8 +240,10 @@ static int dispatch_value_plugindir (const oconfig_item_t *ci) return (0); } -static int dispatch_value_loadplugin (const oconfig_item_t *ci) +static int dispatch_loadplugin (const oconfig_item_t *ci) { + int i; + uint32_t flags = 0; assert (strcasecmp (ci->key, "LoadPlugin") == 0); if (ci->values_num != 1) @@ -248,7 +251,19 @@ static int dispatch_value_loadplugin (const oconfig_item_t *ci) if (ci->values[0].type != OCONFIG_TYPE_STRING) return (-1); - return (plugin_load (ci->values[0].value.string)); + for (i = 0; i < ci->children_num; ++i) { + if (ci->children[i].values_num != 1 || + ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) { + WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string); + continue; + } + if (strcasecmp(ci->children[i].key, "globals") == 0) { + flags |= PLUGIN_FLAGS_GLOBAL; + } else { + WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string); + } + } + return (plugin_load (ci->values[0].value.string, flags)); } /* int dispatch_value_loadplugin */ static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci) @@ -336,7 +351,15 @@ static int dispatch_block_plugin (oconfig_item_t *ci) if (ci->children[i].children == NULL) dispatch_value_plugin (name, ci->children + i); else - {DEBUG ("No nested config blocks allowed for this plugin.");} + { + WARNING ("There is a `%s' block within the " + "configuration for the %s plugin. " + "The plugin either only expects " + "\"simple\" configuration statements " + "or wasn't loaded using `LoadPlugin'." + " Please check your configuration.", + ci->children[i].key, name); + } } return (0); @@ -345,7 +368,9 @@ static int dispatch_block_plugin (oconfig_item_t *ci) static int dispatch_block (oconfig_item_t *ci) { - if (strcasecmp (ci->key, "Plugin") == 0) + if (strcasecmp (ci->key, "LoadPlugin") == 0) + return (dispatch_loadplugin (ci)); + else if (strcasecmp (ci->key, "Plugin") == 0) return (dispatch_block_plugin (ci)); else if (strcasecmp (ci->key, "Threshold") == 0) return (ut_config (ci)); @@ -378,17 +403,23 @@ static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src, temp = NULL; /* If (src->children_num == 0) the array size is decreased. If offset - * is _not_ the last element, (offset < (src->children_num - 1)), then + * is _not_ the last element, (offset < (dst->children_num - 1)), then * we need to move the trailing elements before resizing the array. */ - if ((src->children_num == 0) && (offset < (src->children_num - 1))) + if ((src->children_num == 0) && (offset < (dst->children_num - 1))) { - int nmemb = src->children_num - (offset + 1); - memmove (src->children + offset, src->children + offset + 1, + int nmemb = dst->children_num - (offset + 1); + memmove (dst->children + offset, dst->children + offset + 1, sizeof (oconfig_item_t) * nmemb); } /* Resize the memory containing the children to be big enough to hold * all children. */ + if (dst->children_num + src->children_num - 1 == 0) + { + dst->children_num = 0; + return (0); + } + temp = (oconfig_item_t *) realloc (dst->children, sizeof (oconfig_item_t) * (dst->children_num + src->children_num - 1)); @@ -415,7 +446,7 @@ static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src, sizeof (oconfig_item_t) * nmemb); } - /* Last but not least: If there are new childrem, copy them to the + /* Last but not least: If there are new children, copy them to the * memory reserved for them. */ if (src->children_num > 0) { @@ -489,7 +520,11 @@ static int cf_include_all (oconfig_item_t *root, int depth) continue; /* Now replace the i'th child in `root' with `new'. */ - cf_ci_replace_child (root, new, i); + if (cf_ci_replace_child (root, new, i) < 0) + return (-1); + + /* ... and go back to the new i'th child. */ + --i; sfree (new->values); sfree (new); @@ -548,14 +583,14 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) ERROR ("configfile: malloc failed."); return (NULL); } - memset (root, '\0', sizeof (oconfig_item_t)); + memset (root, 0, sizeof (oconfig_item_t)); while ((de = readdir (dh)) != NULL) { char name[1024]; char **tmp; - if ((de->d_name[0] == '.') || (de->d_name[0] == '\0')) + if ((de->d_name[0] == '.') || (de->d_name[0] == 0)) continue; status = ssnprintf (name, sizeof (name), "%s/%s", @@ -597,13 +632,11 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth) char *name = filenames[i]; temp = cf_read_generic (name, depth); - if (temp == NULL) { - int j; - for (j = i; j < filenames_num; ++j) - free (filenames[j]); - free (filenames); - oconfig_free (root); - return (NULL); + if (temp == NULL) + { + /* An error should already have been reported. */ + sfree (name); + continue; } cf_ci_append_children (root, temp); @@ -675,11 +708,10 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) if (status != 0) { char errbuf[1024]; - ERROR ("configfile: stat (%s) failed: %s", + WARNING ("configfile: stat (%s) failed: %s", path_ptr, sstrerror (errno, errbuf, sizeof (errbuf))); - oconfig_free (root); - return (NULL); + continue; } if (S_ISREG (statbuf.st_mode)) @@ -688,7 +720,7 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) temp = cf_read_dir (path_ptr, depth); else { - ERROR ("configfile: %s is neither a file nor a " + WARNING ("configfile: %s is neither a file nor a " "directory.", path); continue; } @@ -705,6 +737,12 @@ static oconfig_item_t *cf_read_generic (const char *path, int depth) wordfree (&we); + if (root->children == NULL) + { + oconfig_free (root); + return (NULL); + } + return (root); } /* oconfig_item_t *cf_read_generic */ /* #endif HAVE_WORDEXP_H */ @@ -906,3 +944,99 @@ int cf_read (char *filename) return (0); } /* int cf_read */ + +/* Assures the config option is a string, duplicates it and returns the copy in + * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon + * 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); + } + + string = strdup (ci->values[0].value.string); + if (string == NULL) + return (-1); + + if (*ret_string != NULL) + sfree (*ret_string); + *ret_string = string; + + return (0); +} /* }}} int cf_util_get_string */ + +/* Assures the config option is a string and copies it to the provided buffer. + * Assures null-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); + return (-1); + } + + strncpy (buffer, ci->values[0].value.string, buffer_size); + buffer[buffer_size - 1] = 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); + + 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); + } + + *ret_value = (int) ci->values[0].value.number; + + return (0); +} /* }}} int cf_util_get_int */ + +int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */ +{ + if ((ci == NULL) || (ret_bool == NULL)) + return (EINVAL); + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) + { + ERROR ("cf_util_get_boolean: The %s option requires " + "exactly one boolean argument.", ci->key); + return (-1); + } + + *ret_bool = ci->values[0].value.boolean ? true : false; + + return (0); +} /* }}} int cf_util_get_boolean */ + +/* Assures that the config option is a string. The string is then converted to + * a port number using `service_name_to_port_number' and returned. Returns the + * port number in the range [1-65535] or less than zero upon failure. */ +int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */ +{ + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + ERROR ("cf_util_get_port_number: The %s option requires " + "exactly one string argument.", ci->key); + return (-1); + } + + return (service_name_to_port_number (ci->values[0].value.string)); +} /* }}} int cf_util_get_port_number */