2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian tokkee Harl <sh at tokkee.org>
30 #include "liboconfig/oconfig.h"
33 #include "configfile.h"
34 #include "filter_chain.h"
36 #include "types_list.h"
40 #endif /* HAVE_WORDEXP_H */
44 #endif /* HAVE_FNMATCH_H */
48 #endif /* HAVE_LIBGEN_H */
50 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
55 typedef struct cf_callback {
57 int (*callback)(const char *, const char *);
61 struct cf_callback *next;
64 typedef struct cf_complex_callback_s {
66 int (*callback)(oconfig_item_t *);
68 struct cf_complex_callback_s *next;
69 } cf_complex_callback_t;
71 typedef struct cf_value_map_s {
73 int (*func)(oconfig_item_t *);
76 typedef struct cf_global_option_s {
79 bool from_cli; /* value set from CLI */
84 * Prototypes of callback functions
86 static int dispatch_value_typesdb(oconfig_item_t *ci);
87 static int dispatch_value_plugindir(oconfig_item_t *ci);
88 static int dispatch_loadplugin(oconfig_item_t *ci);
89 static int dispatch_block_plugin(oconfig_item_t *ci);
94 static cf_callback_t *first_callback;
95 static cf_complex_callback_t *complex_callback_head;
97 static cf_value_map_t cf_value_map[] = {{"TypesDB", dispatch_value_typesdb},
98 {"PluginDir", dispatch_value_plugindir},
99 {"LoadPlugin", dispatch_loadplugin},
100 {"Plugin", dispatch_block_plugin}};
101 static int cf_value_map_num = STATIC_ARRAY_SIZE(cf_value_map);
103 static cf_global_option_t cf_global_options[] = {
104 {"BaseDir", NULL, 0, PKGLOCALSTATEDIR},
105 {"PIDFile", NULL, 0, PIDFILE},
106 {"Hostname", NULL, 0, NULL},
107 {"FQDNLookup", NULL, 0, "true"},
108 {"Interval", NULL, 0, NULL},
109 {"ReadThreads", NULL, 0, "5"},
110 {"WriteThreads", NULL, 0, "5"},
111 {"WriteQueueLimitHigh", NULL, 0, NULL},
112 {"WriteQueueLimitLow", NULL, 0, NULL},
113 {"Timeout", NULL, 0, "2"},
114 {"AutoLoadPlugin", NULL, 0, "false"},
115 {"CollectInternalStats", NULL, 0, "false"},
116 {"PreCacheChain", NULL, 0, "PreCache"},
117 {"PostCacheChain", NULL, 0, "PostCache"},
118 {"MaxReadInterval", NULL, 0, "86400"}};
119 static int cf_global_options_num = STATIC_ARRAY_SIZE(cf_global_options);
121 static int cf_default_typesdb = 1;
124 * Functions to handle register/unregister, search, and other plugin related
127 static cf_callback_t *cf_search(const char *type) {
128 cf_callback_t *cf_cb;
133 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
134 if (strcasecmp(cf_cb->type, type) == 0)
140 static int cf_dispatch(const char *type, const char *orig_key,
141 const char *orig_value) {
142 cf_callback_t *cf_cb;
143 plugin_ctx_t old_ctx;
149 if (orig_key == NULL)
152 DEBUG("type = %s, key = %s, value = %s", ESCAPE_NULL(type), orig_key,
153 ESCAPE_NULL(orig_value));
155 if ((cf_cb = cf_search(type)) == NULL) {
156 WARNING("Found a configuration for the `%s' plugin, but "
157 "the plugin isn't loaded or didn't register "
158 "a configuration callback.",
163 if ((key = strdup(orig_key)) == NULL)
165 if ((value = strdup(orig_value)) == NULL) {
172 old_ctx = plugin_set_ctx(cf_cb->ctx);
174 for (i = 0; i < cf_cb->keys_num; i++) {
175 if ((cf_cb->keys[i] != NULL) && (strcasecmp(cf_cb->keys[i], key) == 0)) {
176 ret = (*cf_cb->callback)(key, value);
181 plugin_set_ctx(old_ctx);
183 if (i >= cf_cb->keys_num)
184 WARNING("Plugin `%s' did not register for value `%s'.", type, key);
190 } /* int cf_dispatch */
192 static int dispatch_global_option(const oconfig_item_t *ci) {
193 if (ci->values_num != 1) {
194 ERROR("configfile: Global option `%s' needs exactly one argument.",
199 if (ci->values[0].type == OCONFIG_TYPE_STRING)
200 return global_option_set(ci->key, ci->values[0].value.string, 0);
201 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
203 snprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number);
204 return global_option_set(ci->key, tmp, 0);
205 } else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) {
206 if (ci->values[0].value.boolean)
207 return global_option_set(ci->key, "true", 0);
209 return global_option_set(ci->key, "false", 0);
212 ERROR("configfile: Global option `%s' argument has unknown type.", ci->key);
215 } /* int dispatch_global_option */
217 static int dispatch_value_typesdb(oconfig_item_t *ci) {
218 assert(strcasecmp(ci->key, "TypesDB") == 0);
220 cf_default_typesdb = 0;
222 if (ci->values_num < 1) {
223 ERROR("configfile: `TypesDB' needs at least one argument.");
227 for (int i = 0; i < ci->values_num; ++i) {
228 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
229 WARNING("configfile: TypesDB: Skipping %i. argument which "
235 read_types_list(ci->values[i].value.string);
238 } /* int dispatch_value_typesdb */
240 static int dispatch_value_plugindir(oconfig_item_t *ci) {
241 assert(strcasecmp(ci->key, "PluginDir") == 0);
243 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
244 ERROR("configfile: The `PluginDir' option needs exactly one string "
249 plugin_set_dir(ci->values[0].value.string);
253 static int dispatch_loadplugin(oconfig_item_t *ci) {
256 plugin_ctx_t ctx = {0};
257 plugin_ctx_t old_ctx;
260 assert(strcasecmp(ci->key, "LoadPlugin") == 0);
262 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
263 ERROR("configfile: The `LoadPlugin' block needs exactly one string "
268 name = ci->values[0].value.string;
269 if (strcmp("libvirt", name) == 0)
272 /* default to the global interval set before loading this plugin */
273 ctx.interval = cf_get_default_interval();
274 ctx.flush_interval = 0;
275 ctx.flush_timeout = 0;
277 for (int i = 0; i < ci->children_num; ++i) {
278 oconfig_item_t *child = ci->children + i;
280 if (strcasecmp("Globals", child->key) == 0)
281 cf_util_get_boolean(child, &global);
282 else if (strcasecmp("Interval", child->key) == 0)
283 cf_util_get_cdtime(child, &ctx.interval);
284 else if (strcasecmp("FlushInterval", child->key) == 0)
285 cf_util_get_cdtime(child, &ctx.flush_interval);
286 else if (strcasecmp("FlushTimeout", child->key) == 0)
287 cf_util_get_cdtime(child, &ctx.flush_timeout);
289 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
291 child->key, ci->values[0].value.string);
295 old_ctx = plugin_set_ctx(ctx);
296 ret_val = plugin_load(name, global);
297 /* reset to the "global" context */
298 plugin_set_ctx(old_ctx);
301 } /* int dispatch_value_loadplugin */
303 static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) {
309 buffer_free = sizeof(buffer);
311 for (int i = 0; i < ci->values_num; i++) {
314 if (ci->values[i].type == OCONFIG_TYPE_STRING)
316 snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
317 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
319 snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number);
320 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
321 status = snprintf(buffer_ptr, buffer_free, " %s",
322 ci->values[i].value.boolean ? "true" : "false");
324 if ((status < 0) || (status >= buffer_free))
326 buffer_free -= status;
327 buffer_ptr += status;
329 /* skip the initial space */
330 buffer_ptr = buffer + 1;
332 return cf_dispatch(plugin, ci->key, buffer_ptr);
333 } /* int dispatch_value_plugin */
335 static int dispatch_value(oconfig_item_t *ci) {
338 for (int i = 0; i < cf_value_map_num; i++)
339 if (strcasecmp(cf_value_map[i].key, ci->key) == 0) {
340 ret = cf_value_map[i].func(ci);
347 for (int i = 0; i < cf_global_options_num; i++)
348 if (strcasecmp(cf_global_options[i].key, ci->key) == 0) {
349 ret = dispatch_global_option(ci);
354 } /* int dispatch_value */
356 static int dispatch_block_plugin(oconfig_item_t *ci) {
357 assert(strcasecmp(ci->key, "Plugin") == 0);
359 if (ci->values_num < 1) {
360 ERROR("configfile: The `Plugin' block requires arguments.");
363 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
364 ERROR("configfile: First argument of `Plugin' block should be a string.");
368 const char *name = ci->values[0].value.string;
369 if (strcmp("libvirt", name) == 0) {
370 /* TODO(octo): Remove this legacy. */
371 WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid "
372 "problems with the build system. "
373 "Your configuration is still using the old name. "
374 "Please change it to use \"virt\" as soon as possible. "
375 "This compatibility code will go away eventually.");
379 if (IS_TRUE(global_option_get("AutoLoadPlugin"))) {
380 plugin_ctx_t ctx = {0};
381 plugin_ctx_t old_ctx;
384 /* default to the global interval set before loading this plugin */
385 ctx.interval = cf_get_default_interval();
387 old_ctx = plugin_set_ctx(ctx);
388 status = plugin_load(name, /* flags = */ false);
389 /* reset to the "global" context */
390 plugin_set_ctx(old_ctx);
393 ERROR("Automatically loading plugin \"%s\" failed "
400 /* Check for a complex callback first */
401 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL;
403 if (strcasecmp(name, cb->type) == 0) {
404 plugin_ctx_t old_ctx;
407 old_ctx = plugin_set_ctx(cb->ctx);
408 ret_val = (cb->callback(ci));
409 plugin_set_ctx(old_ctx);
414 /* Hm, no complex plugin found. Dispatch the values one by one */
415 for (int i = 0; i < ci->children_num; i++) {
416 if (ci->children[i].children == NULL)
417 dispatch_value_plugin(name, ci->children + i);
419 WARNING("There is a `%s' block within the "
420 "configuration for the %s plugin. "
421 "The plugin either only expects "
422 "\"simple\" configuration statements "
423 "or wasn't loaded using `LoadPlugin'."
424 " Please check your configuration.",
425 ci->children[i].key, name);
432 static int dispatch_block(oconfig_item_t *ci) {
433 if (strcasecmp(ci->key, "LoadPlugin") == 0)
434 return dispatch_loadplugin(ci);
435 else if (strcasecmp(ci->key, "Plugin") == 0)
436 return dispatch_block_plugin(ci);
437 else if (strcasecmp(ci->key, "Chain") == 0)
438 return fc_configure(ci);
443 static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src,
445 oconfig_item_t *temp;
448 assert(dst->children_num > offset);
450 /* Free the memory used by the replaced child. Usually that's the
451 * `Include "blah"' statement. */
452 temp = dst->children + offset;
453 for (int i = 0; i < temp->values_num; i++) {
454 if (temp->values[i].type == OCONFIG_TYPE_STRING) {
455 sfree(temp->values[i].value.string);
461 /* If (src->children_num == 0) the array size is decreased. If offset
462 * is _not_ the last element, (offset < (dst->children_num - 1)), then
463 * we need to move the trailing elements before resizing the array. */
464 if ((src->children_num == 0) && (offset < (dst->children_num - 1))) {
465 int nmemb = dst->children_num - (offset + 1);
466 memmove(dst->children + offset, dst->children + offset + 1,
467 sizeof(oconfig_item_t) * nmemb);
470 /* Resize the memory containing the children to be big enough to hold
472 if (dst->children_num + src->children_num - 1 == 0) {
473 dst->children_num = 0;
477 temp = realloc(dst->children,
478 sizeof(oconfig_item_t) *
479 (dst->children_num + src->children_num - 1));
481 ERROR("configfile: realloc failed.");
484 dst->children = temp;
486 /* If there are children behind the include statement, and they have
487 * not yet been moved because (src->children_num == 0), then move them
488 * to the end of the list, so that the new children have room before
490 if ((src->children_num > 0) && ((dst->children_num - (offset + 1)) > 0)) {
491 int nmemb = dst->children_num - (offset + 1);
492 int old_offset = offset + 1;
493 int new_offset = offset + src->children_num;
495 memmove(dst->children + new_offset, dst->children + old_offset,
496 sizeof(oconfig_item_t) * nmemb);
499 /* Last but not least: If there are new children, copy them to the
500 * memory reserved for them. */
501 if (src->children_num > 0) {
502 memcpy(dst->children + offset, src->children,
503 sizeof(oconfig_item_t) * src->children_num);
506 /* Update the number of children. */
507 dst->children_num += (src->children_num - 1);
510 } /* int cf_ci_replace_child */
512 static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) {
513 oconfig_item_t *temp;
515 if ((src == NULL) || (src->children_num == 0))
519 realloc(dst->children,
520 sizeof(oconfig_item_t) * (dst->children_num + src->children_num));
522 ERROR("configfile: realloc failed.");
525 dst->children = temp;
527 memcpy(dst->children + dst->children_num, src->children,
528 sizeof(oconfig_item_t) * src->children_num);
529 dst->children_num += src->children_num;
532 } /* int cf_ci_append_children */
534 #define CF_MAX_DEPTH 8
535 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
538 static int cf_include_all(oconfig_item_t *root, int depth) {
539 for (int i = 0; i < root->children_num; i++) {
543 char *pattern = NULL;
545 if (strcasecmp(root->children[i].key, "Include") != 0)
548 old = root->children + i;
550 if ((old->values_num != 1) ||
551 (old->values[0].type != OCONFIG_TYPE_STRING)) {
552 ERROR("configfile: `Include' needs exactly one string argument.");
556 for (int j = 0; j < old->children_num; ++j) {
557 oconfig_item_t *child = old->children + j;
559 if (strcasecmp(child->key, "Filter") == 0)
560 cf_util_get_string(child, &pattern);
562 ERROR("configfile: Option `%s' not allowed in <Include> block.",
566 new = cf_read_generic(old->values[0].value.string, pattern, depth + 1);
572 /* Now replace the i'th child in `root' with `new'. */
573 if (cf_ci_replace_child(root, new, i) < 0) {
579 /* ... and go back to the new i'th child. */
584 } /* for (i = 0; i < root->children_num; i++) */
587 } /* int cf_include_all */
589 static oconfig_item_t *cf_read_file(const char *file, const char *pattern,
591 oconfig_item_t *root;
594 assert(depth < CF_MAX_DEPTH);
596 if (pattern != NULL) {
597 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
598 char *tmp = sstrdup(file);
599 char *filename = basename(tmp);
601 if ((filename != NULL) && (fnmatch(pattern, filename, 0) != 0)) {
602 DEBUG("configfile: Not including `%s' because it "
603 "does not match pattern `%s'.",
611 ERROR("configfile: Cannot apply pattern filter '%s' "
612 "to file '%s': functions basename() and / or "
613 "fnmatch() not available.",
615 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
618 root = oconfig_parse_file(file);
620 ERROR("configfile: Cannot read file `%s'.", file);
624 status = cf_include_all(root, depth);
631 } /* oconfig_item_t *cf_read_file */
633 static int cf_compare_string(const void *p1, const void *p2) {
634 return strcmp(*(const char **)p1, *(const char **)p2);
637 static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern,
639 oconfig_item_t *root = NULL;
642 char **filenames = NULL;
643 int filenames_num = 0;
646 assert(depth < CF_MAX_DEPTH);
650 ERROR("configfile: opendir failed: %s", STRERRNO);
654 root = calloc(1, sizeof(*root));
656 ERROR("configfile: calloc failed.");
661 while ((de = readdir(dh)) != NULL) {
665 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
668 status = snprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
669 if ((status < 0) || ((size_t)status >= sizeof(name))) {
670 ERROR("configfile: Not including `%s/%s' because its"
671 " name is too long.",
674 for (int i = 0; i < filenames_num; ++i)
682 tmp = realloc(filenames, filenames_num * sizeof(*filenames));
684 ERROR("configfile: realloc failed.");
686 for (int i = 0; i < filenames_num - 1; ++i)
694 filenames[filenames_num - 1] = sstrdup(name);
697 if (filenames == NULL) {
702 qsort((void *)filenames, filenames_num, sizeof(*filenames),
705 for (int i = 0; i < filenames_num; ++i) {
706 oconfig_item_t *temp;
707 char *name = filenames[i];
709 temp = cf_read_generic(name, pattern, depth);
711 /* An error should already have been reported. */
716 cf_ci_append_children(root, temp);
717 sfree(temp->children);
726 } /* oconfig_item_t *cf_read_dir */
731 * Path is stat'ed and either cf_read_file or cf_read_dir is called
734 * There are two versions of this function: If `wordexp' exists shell wildcards
735 * will be expanded and the function will include all matches found. If
736 * `wordexp' (or, more precisely, its header file) is not available the
737 * simpler function is used which does not do any such expansion.
740 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
742 oconfig_item_t *root = NULL;
744 const char *path_ptr;
747 if (depth >= CF_MAX_DEPTH) {
748 ERROR("configfile: Not including `%s' because the maximum "
749 "nesting depth has been reached.",
754 status = wordexp(path, &we, WRDE_NOCMD);
756 ERROR("configfile: wordexp (%s) failed.", path);
760 root = calloc(1, sizeof(*root));
762 ERROR("configfile: calloc failed.");
766 /* wordexp() might return a sorted list already. That's not
767 * documented though, so let's make sure we get what we want. */
768 qsort((void *)we.we_wordv, we.we_wordc, sizeof(*we.we_wordv),
771 for (size_t i = 0; i < we.we_wordc; i++) {
772 oconfig_item_t *temp;
775 path_ptr = we.we_wordv[i];
777 status = stat(path_ptr, &statbuf);
779 WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO);
783 if (S_ISREG(statbuf.st_mode))
784 temp = cf_read_file(path_ptr, pattern, depth);
785 else if (S_ISDIR(statbuf.st_mode))
786 temp = cf_read_dir(path_ptr, pattern, depth);
788 WARNING("configfile: %s is neither a file nor a "
799 cf_ci_append_children(root, temp);
800 sfree(temp->children);
807 } /* oconfig_item_t *cf_read_generic */
808 /* #endif HAVE_WORDEXP_H */
810 #else /* if !HAVE_WORDEXP_H */
811 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
816 if (depth >= CF_MAX_DEPTH) {
817 ERROR("configfile: Not including `%s' because the maximum "
818 "nesting depth has been reached.",
823 status = stat(path, &statbuf);
825 ERROR("configfile: stat (%s) failed: %s", path, STRERRNO);
829 if (S_ISREG(statbuf.st_mode))
830 return cf_read_file(path, pattern, depth);
831 else if (S_ISDIR(statbuf.st_mode))
832 return cf_read_dir(path, pattern, depth);
834 ERROR("configfile: %s is neither a file nor a directory.", path);
836 } /* oconfig_item_t *cf_read_generic */
837 #endif /* !HAVE_WORDEXP_H */
842 int global_option_set(const char *option, const char *value, bool from_cli) {
844 DEBUG("option = %s; value = %s;", option, value);
846 for (i = 0; i < cf_global_options_num; i++)
847 if (strcasecmp(cf_global_options[i].key, option) == 0)
850 if (i >= cf_global_options_num) {
851 ERROR("configfile: Cannot set unknown global option `%s'.", option);
855 if (cf_global_options[i].from_cli && (!from_cli)) {
856 DEBUG("configfile: Ignoring %s `%s' option because "
857 "it was overriden by a command-line option.",
862 sfree(cf_global_options[i].value);
865 cf_global_options[i].value = strdup(value);
867 cf_global_options[i].value = NULL;
869 cf_global_options[i].from_cli = from_cli;
874 const char *global_option_get(const char *option) {
876 for (i = 0; i < cf_global_options_num; i++)
877 if (strcasecmp(cf_global_options[i].key, option) == 0)
880 if (i >= cf_global_options_num) {
881 ERROR("configfile: Cannot get unknown global option `%s'.", option);
885 return (cf_global_options[i].value != NULL) ? cf_global_options[i].value
886 : cf_global_options[i].def;
887 } /* char *global_option_get */
889 long global_option_get_long(const char *option, long default_value) {
893 str = global_option_get(option);
895 return default_value;
898 value = strtol(str, /* endptr = */ NULL, /* base = */ 0);
900 return default_value;
903 } /* char *global_option_get_long */
905 cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */
911 optstr = global_option_get(name);
916 v = strtod(optstr, &endptr);
917 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
922 return DOUBLE_TO_CDTIME_T(v);
923 } /* }}} cdtime_t global_option_get_time */
925 cdtime_t cf_get_default_interval(void) {
926 return global_option_get_time("Interval",
927 DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL));
930 void cf_unregister(const char *type) {
931 for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL;
932 prev = this, this = this->next)
933 if (strcasecmp(this->type, type) == 0) {
935 first_callback = this->next;
937 prev->next = this->next;
942 } /* void cf_unregister */
944 void cf_unregister_complex(const char *type) {
945 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
946 this != NULL; prev = this, this = this->next)
947 if (strcasecmp(this->type, type) == 0) {
949 complex_callback_head = this->next;
951 prev->next = this->next;
957 } /* void cf_unregister */
959 void cf_register(const char *type, int (*callback)(const char *, const char *),
960 const char **keys, int keys_num) {
961 cf_callback_t *cf_cb;
963 /* Remove this module from the list, if it already exists */
966 /* This pointer will be free'd in `cf_unregister' */
967 if ((cf_cb = malloc(sizeof(*cf_cb))) == NULL)
971 cf_cb->callback = callback;
973 cf_cb->keys_num = keys_num;
974 cf_cb->ctx = plugin_get_ctx();
976 cf_cb->next = first_callback;
977 first_callback = cf_cb;
978 } /* void cf_register */
980 int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) {
981 cf_complex_callback_t *new;
983 new = malloc(sizeof(*new));
987 new->type = strdup(type);
988 if (new->type == NULL) {
993 new->callback = callback;
996 new->ctx = plugin_get_ctx();
998 if (complex_callback_head == NULL) {
999 complex_callback_head = new;
1001 cf_complex_callback_t *last = complex_callback_head;
1002 while (last->next != NULL)
1008 } /* int cf_register_complex */
1010 int cf_read(const char *filename) {
1011 oconfig_item_t *conf;
1014 conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0);
1016 ERROR("Unable to read config file %s.", filename);
1018 } else if (conf->children_num == 0) {
1019 ERROR("Configuration file %s is empty.", filename);
1024 for (int i = 0; i < conf->children_num; i++) {
1025 if (conf->children[i].children == NULL) {
1026 if (dispatch_value(conf->children + i) != 0)
1029 if (dispatch_block(conf->children + i) != 0)
1036 /* Read the default types.db if no `TypesDB' option was given. */
1037 if (cf_default_typesdb) {
1038 if (read_types_list(PKGDATADIR "/types.db") != 0)
1046 /* Assures the config option is a string, duplicates it and returns the copy in
1047 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1049 int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1053 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1054 ERROR("cf_util_get_string: The %s option requires "
1055 "exactly one string argument.",
1060 string = strdup(ci->values[0].value.string);
1064 if (*ret_string != NULL)
1066 *ret_string = string;
1069 } /* }}} int cf_util_get_string */
1071 /* Assures the config option is a string and copies it to the provided buffer.
1072 * Assures null-termination. */
1073 int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */
1074 size_t buffer_size) {
1075 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1078 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1079 ERROR("cf_util_get_string_buffer: The %s option requires "
1080 "exactly one string argument.",
1085 strncpy(buffer, ci->values[0].value.string, buffer_size);
1086 buffer[buffer_size - 1] = 0;
1089 } /* }}} int cf_util_get_string_buffer */
1091 /* Assures the config option is a number and returns it as an int. */
1092 int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */
1094 if ((ci == NULL) || (ret_value == NULL))
1097 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1098 ERROR("cf_util_get_int: The %s option requires "
1099 "exactly one numeric argument.",
1104 *ret_value = (int)ci->values[0].value.number;
1107 } /* }}} int cf_util_get_int */
1109 int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */
1111 if ((ci == NULL) || (ret_value == NULL))
1114 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1115 ERROR("cf_util_get_double: The %s option requires "
1116 "exactly one numeric argument.",
1121 *ret_value = ci->values[0].value.number;
1124 } /* }}} int cf_util_get_double */
1126 int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */
1128 if ((ci == NULL) || (ret_bool == NULL))
1131 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) &&
1132 (ci->values[0].type != OCONFIG_TYPE_STRING))) {
1133 ERROR("cf_util_get_boolean: The %s option requires "
1134 "exactly one boolean argument.",
1139 switch (ci->values[0].type) {
1140 case OCONFIG_TYPE_BOOLEAN:
1141 *ret_bool = ci->values[0].value.boolean ? true : false;
1143 case OCONFIG_TYPE_STRING:
1144 WARNING("cf_util_get_boolean: Using string value `%s' for boolean option "
1145 "`%s' is deprecated and will be removed in future releases. "
1146 "Use unquoted true or false instead.",
1147 ci->values[0].value.string, ci->key);
1149 if (IS_TRUE(ci->values[0].value.string))
1151 else if (IS_FALSE(ci->values[0].value.string))
1154 ERROR("cf_util_get_boolean: Cannot parse string value `%s' of the `%s' "
1155 "option as a boolean value.",
1156 ci->values[0].value.string, ci->key);
1163 } /* }}} int cf_util_get_boolean */
1165 int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */
1166 unsigned int *ret_value, unsigned int flag) {
1169 if (ret_value == NULL)
1173 status = cf_util_get_boolean(ci, &b);
1180 *ret_value &= ~flag;
1184 } /* }}} int cf_util_get_flag */
1186 /* Assures that the config option is a string or a number if the correct range
1187 * of 1-65535. The string is then converted to a port number using
1188 * `service_name_to_port_number' and returned.
1189 * Returns the port number in the range [1-65535] or less than zero upon
1191 int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */
1195 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
1196 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
1197 ERROR("cf_util_get_port_number: The \"%s\" option requires "
1198 "exactly one string argument.",
1203 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1204 return service_name_to_port_number(ci->values[0].value.string);
1206 assert(ci->values[0].type == OCONFIG_TYPE_NUMBER);
1207 tmp = (int)(ci->values[0].value.number + 0.5);
1208 if ((tmp < 1) || (tmp > 65535)) {
1209 ERROR("cf_util_get_port_number: The \"%s\" option requires "
1210 "a service name or a port number. The number "
1211 "you specified, %i, is not in the valid "
1212 "range of 1-65535.",
1218 } /* }}} int cf_util_get_port_number */
1220 int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1226 if (ci->values_num != 1) {
1227 ERROR("cf_util_get_service: The %s option requires exactly "
1233 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1234 return cf_util_get_string(ci, ret_string);
1235 if (ci->values[0].type != OCONFIG_TYPE_NUMBER) {
1236 ERROR("cf_util_get_service: The %s option requires "
1237 "exactly one string or numeric argument.",
1242 status = cf_util_get_int(ci, &port);
1245 else if ((port < 1) || (port > 65535)) {
1246 ERROR("cf_util_get_service: The port number given "
1247 "for the %s option is out of "
1253 service = malloc(6);
1254 if (service == NULL) {
1255 ERROR("cf_util_get_service: Out of memory.");
1258 snprintf(service, 6, "%i", port);
1261 *ret_string = service;
1264 } /* }}} int cf_util_get_service */
1266 int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1268 if ((ci == NULL) || (ret_value == NULL))
1271 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1272 ERROR("cf_util_get_cdtime: The %s option requires "
1273 "exactly one numeric argument.",
1278 if (ci->values[0].value.number < 0.0) {
1279 ERROR("cf_util_get_cdtime: The numeric argument of the %s "
1280 "option must not be negative.",
1285 *ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
1288 } /* }}} int cf_util_get_cdtime */