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 assert(strcasecmp(ci->key, "LoadPlugin") == 0);
258 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
259 ERROR("configfile: The `LoadPlugin' block needs exactly one string "
264 const char *name = ci->values[0].value.string;
265 if (strcmp("libvirt", name) == 0)
268 /* default to the global interval set before loading this plugin */
270 .interval = cf_get_default_interval(), .name = strdup(name),
272 if (ctx.name == NULL)
275 for (int i = 0; i < ci->children_num; ++i) {
276 oconfig_item_t *child = ci->children + i;
278 if (strcasecmp("Globals", child->key) == 0)
279 cf_util_get_boolean(child, &global);
280 else if (strcasecmp("Interval", child->key) == 0)
281 cf_util_get_cdtime(child, &ctx.interval);
282 else if (strcasecmp("FlushInterval", child->key) == 0)
283 cf_util_get_cdtime(child, &ctx.flush_interval);
284 else if (strcasecmp("FlushTimeout", child->key) == 0)
285 cf_util_get_cdtime(child, &ctx.flush_timeout);
287 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
293 plugin_ctx_t old_ctx = plugin_set_ctx(ctx);
294 int ret_val = plugin_load(name, global);
295 /* reset to the "global" context */
296 plugin_set_ctx(old_ctx);
299 } /* int dispatch_value_loadplugin */
301 static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) {
307 buffer_free = sizeof(buffer);
309 for (int i = 0; i < ci->values_num; i++) {
312 if (ci->values[i].type == OCONFIG_TYPE_STRING)
314 snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
315 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
317 snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number);
318 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
319 status = snprintf(buffer_ptr, buffer_free, " %s",
320 ci->values[i].value.boolean ? "true" : "false");
322 if ((status < 0) || (status >= buffer_free))
324 buffer_free -= status;
325 buffer_ptr += status;
327 /* skip the initial space */
328 buffer_ptr = buffer + 1;
330 return cf_dispatch(plugin, ci->key, buffer_ptr);
331 } /* int dispatch_value_plugin */
333 static int dispatch_value(oconfig_item_t *ci) {
336 for (int i = 0; i < cf_value_map_num; i++)
337 if (strcasecmp(cf_value_map[i].key, ci->key) == 0) {
338 ret = cf_value_map[i].func(ci);
345 for (int i = 0; i < cf_global_options_num; i++)
346 if (strcasecmp(cf_global_options[i].key, ci->key) == 0) {
347 ret = dispatch_global_option(ci);
352 } /* int dispatch_value */
354 static int dispatch_block_plugin(oconfig_item_t *ci) {
355 assert(strcasecmp(ci->key, "Plugin") == 0);
357 if (ci->values_num < 1) {
358 ERROR("configfile: The `Plugin' block requires arguments.");
361 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
362 ERROR("configfile: First argument of `Plugin' block should be a string.");
366 const char *name = ci->values[0].value.string;
367 if (strcmp("libvirt", name) == 0) {
368 /* TODO(octo): Remove this legacy. */
369 WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid "
370 "problems with the build system. "
371 "Your configuration is still using the old name. "
372 "Please change it to use \"virt\" as soon as possible. "
373 "This compatibility code will go away eventually.");
377 if (IS_TRUE(global_option_get("AutoLoadPlugin"))) {
378 plugin_ctx_t ctx = {0};
379 plugin_ctx_t old_ctx;
382 /* default to the global interval set before loading this plugin */
383 ctx.interval = cf_get_default_interval();
384 ctx.name = strdup(name);
386 old_ctx = plugin_set_ctx(ctx);
387 status = plugin_load(name, /* flags = */ false);
388 /* reset to the "global" context */
389 plugin_set_ctx(old_ctx);
392 ERROR("Automatically loading plugin \"%s\" failed "
399 /* Check for a complex callback first */
400 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL;
402 if (strcasecmp(name, cb->type) == 0) {
403 plugin_ctx_t old_ctx;
406 old_ctx = plugin_set_ctx(cb->ctx);
407 ret_val = (cb->callback(ci));
408 plugin_set_ctx(old_ctx);
413 /* Hm, no complex plugin found. Dispatch the values one by one */
414 for (int i = 0; i < ci->children_num; i++) {
415 if (ci->children[i].children == NULL)
416 dispatch_value_plugin(name, ci->children + i);
418 WARNING("There is a `%s' block within the "
419 "configuration for the %s plugin. "
420 "The plugin either only expects "
421 "\"simple\" configuration statements "
422 "or wasn't loaded using `LoadPlugin'."
423 " Please check your configuration.",
424 ci->children[i].key, name);
431 static int dispatch_block(oconfig_item_t *ci) {
432 if (strcasecmp(ci->key, "LoadPlugin") == 0)
433 return dispatch_loadplugin(ci);
434 else if (strcasecmp(ci->key, "Plugin") == 0)
435 return dispatch_block_plugin(ci);
436 else if (strcasecmp(ci->key, "Chain") == 0)
437 return fc_configure(ci);
442 static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src,
444 oconfig_item_t *temp;
447 assert(dst->children_num > offset);
449 /* Free the memory used by the replaced child. Usually that's the
450 * `Include "blah"' statement. */
451 temp = dst->children + offset;
452 for (int i = 0; i < temp->values_num; i++) {
453 if (temp->values[i].type == OCONFIG_TYPE_STRING) {
454 sfree(temp->values[i].value.string);
460 /* If (src->children_num == 0) the array size is decreased. If offset
461 * is _not_ the last element, (offset < (dst->children_num - 1)), then
462 * we need to move the trailing elements before resizing the array. */
463 if ((src->children_num == 0) && (offset < (dst->children_num - 1))) {
464 int nmemb = dst->children_num - (offset + 1);
465 memmove(dst->children + offset, dst->children + offset + 1,
466 sizeof(oconfig_item_t) * nmemb);
469 /* Resize the memory containing the children to be big enough to hold
471 if (dst->children_num + src->children_num - 1 == 0) {
472 dst->children_num = 0;
476 temp = realloc(dst->children,
477 sizeof(oconfig_item_t) *
478 (dst->children_num + src->children_num - 1));
480 ERROR("configfile: realloc failed.");
483 dst->children = temp;
485 /* If there are children behind the include statement, and they have
486 * not yet been moved because (src->children_num == 0), then move them
487 * to the end of the list, so that the new children have room before
489 if ((src->children_num > 0) && ((dst->children_num - (offset + 1)) > 0)) {
490 int nmemb = dst->children_num - (offset + 1);
491 int old_offset = offset + 1;
492 int new_offset = offset + src->children_num;
494 memmove(dst->children + new_offset, dst->children + old_offset,
495 sizeof(oconfig_item_t) * nmemb);
498 /* Last but not least: If there are new children, copy them to the
499 * memory reserved for them. */
500 if (src->children_num > 0) {
501 memcpy(dst->children + offset, src->children,
502 sizeof(oconfig_item_t) * src->children_num);
505 /* Update the number of children. */
506 dst->children_num += (src->children_num - 1);
509 } /* int cf_ci_replace_child */
511 static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) {
512 oconfig_item_t *temp;
514 if ((src == NULL) || (src->children_num == 0))
518 realloc(dst->children,
519 sizeof(oconfig_item_t) * (dst->children_num + src->children_num));
521 ERROR("configfile: realloc failed.");
524 dst->children = temp;
526 memcpy(dst->children + dst->children_num, src->children,
527 sizeof(oconfig_item_t) * src->children_num);
528 dst->children_num += src->children_num;
531 } /* int cf_ci_append_children */
533 #define CF_MAX_DEPTH 8
534 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
537 static int cf_include_all(oconfig_item_t *root, int depth) {
538 for (int i = 0; i < root->children_num; i++) {
542 char *pattern = NULL;
544 if (strcasecmp(root->children[i].key, "Include") != 0)
547 old = root->children + i;
549 if ((old->values_num != 1) ||
550 (old->values[0].type != OCONFIG_TYPE_STRING)) {
551 ERROR("configfile: `Include' needs exactly one string argument.");
555 for (int j = 0; j < old->children_num; ++j) {
556 oconfig_item_t *child = old->children + j;
558 if (strcasecmp(child->key, "Filter") == 0)
559 cf_util_get_string(child, &pattern);
561 ERROR("configfile: Option `%s' not allowed in <Include> block.",
565 new = cf_read_generic(old->values[0].value.string, pattern, depth + 1);
571 /* Now replace the i'th child in `root' with `new'. */
572 if (cf_ci_replace_child(root, new, i) < 0) {
578 /* ... and go back to the new i'th child. */
583 } /* for (i = 0; i < root->children_num; i++) */
586 } /* int cf_include_all */
588 static oconfig_item_t *cf_read_file(const char *file, const char *pattern,
590 oconfig_item_t *root;
593 assert(depth < CF_MAX_DEPTH);
595 if (pattern != NULL) {
596 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
597 char *tmp = sstrdup(file);
598 char *filename = basename(tmp);
600 if ((filename != NULL) && (fnmatch(pattern, filename, 0) != 0)) {
601 DEBUG("configfile: Not including `%s' because it "
602 "does not match pattern `%s'.",
610 ERROR("configfile: Cannot apply pattern filter '%s' "
611 "to file '%s': functions basename() and / or "
612 "fnmatch() not available.",
614 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
617 root = oconfig_parse_file(file);
619 ERROR("configfile: Cannot read file `%s'.", file);
623 status = cf_include_all(root, depth);
630 } /* oconfig_item_t *cf_read_file */
632 static int cf_compare_string(const void *p1, const void *p2) {
633 return strcmp(*(const char **)p1, *(const char **)p2);
636 static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern,
638 oconfig_item_t *root = NULL;
641 char **filenames = NULL;
642 int filenames_num = 0;
645 assert(depth < CF_MAX_DEPTH);
649 ERROR("configfile: opendir failed: %s", STRERRNO);
653 root = calloc(1, sizeof(*root));
655 ERROR("configfile: calloc failed.");
660 while ((de = readdir(dh)) != NULL) {
664 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
667 status = snprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
668 if ((status < 0) || ((size_t)status >= sizeof(name))) {
669 ERROR("configfile: Not including `%s/%s' because its"
670 " name is too long.",
673 for (int i = 0; i < filenames_num; ++i)
681 tmp = realloc(filenames, filenames_num * sizeof(*filenames));
683 ERROR("configfile: realloc failed.");
685 for (int i = 0; i < filenames_num - 1; ++i)
693 filenames[filenames_num - 1] = sstrdup(name);
696 if (filenames == NULL) {
701 qsort((void *)filenames, filenames_num, sizeof(*filenames),
704 for (int i = 0; i < filenames_num; ++i) {
705 oconfig_item_t *temp;
706 char *name = filenames[i];
708 temp = cf_read_generic(name, pattern, depth);
710 /* An error should already have been reported. */
715 cf_ci_append_children(root, temp);
716 sfree(temp->children);
725 } /* oconfig_item_t *cf_read_dir */
730 * Path is stat'ed and either cf_read_file or cf_read_dir is called
733 * There are two versions of this function: If `wordexp' exists shell wildcards
734 * will be expanded and the function will include all matches found. If
735 * `wordexp' (or, more precisely, its header file) is not available the
736 * simpler function is used which does not do any such expansion.
739 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
741 oconfig_item_t *root = NULL;
743 const char *path_ptr;
746 if (depth >= CF_MAX_DEPTH) {
747 ERROR("configfile: Not including `%s' because the maximum "
748 "nesting depth has been reached.",
753 status = wordexp(path, &we, WRDE_NOCMD);
755 ERROR("configfile: wordexp (%s) failed.", path);
759 root = calloc(1, sizeof(*root));
761 ERROR("configfile: calloc failed.");
765 /* wordexp() might return a sorted list already. That's not
766 * documented though, so let's make sure we get what we want. */
767 qsort((void *)we.we_wordv, we.we_wordc, sizeof(*we.we_wordv),
770 for (size_t i = 0; i < we.we_wordc; i++) {
771 oconfig_item_t *temp;
774 path_ptr = we.we_wordv[i];
776 status = stat(path_ptr, &statbuf);
778 WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO);
782 if (S_ISREG(statbuf.st_mode))
783 temp = cf_read_file(path_ptr, pattern, depth);
784 else if (S_ISDIR(statbuf.st_mode))
785 temp = cf_read_dir(path_ptr, pattern, depth);
787 WARNING("configfile: %s is neither a file nor a "
798 cf_ci_append_children(root, temp);
799 sfree(temp->children);
806 } /* oconfig_item_t *cf_read_generic */
807 /* #endif HAVE_WORDEXP_H */
809 #else /* if !HAVE_WORDEXP_H */
810 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
815 if (depth >= CF_MAX_DEPTH) {
816 ERROR("configfile: Not including `%s' because the maximum "
817 "nesting depth has been reached.",
822 status = stat(path, &statbuf);
824 ERROR("configfile: stat (%s) failed: %s", path, STRERRNO);
828 if (S_ISREG(statbuf.st_mode))
829 return cf_read_file(path, pattern, depth);
830 else if (S_ISDIR(statbuf.st_mode))
831 return cf_read_dir(path, pattern, depth);
833 ERROR("configfile: %s is neither a file nor a directory.", path);
835 } /* oconfig_item_t *cf_read_generic */
836 #endif /* !HAVE_WORDEXP_H */
841 int global_option_set(const char *option, const char *value, bool from_cli) {
843 DEBUG("option = %s; value = %s;", option, value);
845 for (i = 0; i < cf_global_options_num; i++)
846 if (strcasecmp(cf_global_options[i].key, option) == 0)
849 if (i >= cf_global_options_num) {
850 ERROR("configfile: Cannot set unknown global option `%s'.", option);
854 if (cf_global_options[i].from_cli && (!from_cli)) {
855 DEBUG("configfile: Ignoring %s `%s' option because "
856 "it was overriden by a command-line option.",
861 sfree(cf_global_options[i].value);
864 cf_global_options[i].value = strdup(value);
866 cf_global_options[i].value = NULL;
868 cf_global_options[i].from_cli = from_cli;
873 const char *global_option_get(const char *option) {
875 for (i = 0; i < cf_global_options_num; i++)
876 if (strcasecmp(cf_global_options[i].key, option) == 0)
879 if (i >= cf_global_options_num) {
880 ERROR("configfile: Cannot get unknown global option `%s'.", option);
884 return (cf_global_options[i].value != NULL) ? cf_global_options[i].value
885 : cf_global_options[i].def;
886 } /* char *global_option_get */
888 long global_option_get_long(const char *option, long default_value) {
892 str = global_option_get(option);
894 return default_value;
897 value = strtol(str, /* endptr = */ NULL, /* base = */ 0);
899 return default_value;
902 } /* char *global_option_get_long */
904 cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */
910 optstr = global_option_get(name);
915 v = strtod(optstr, &endptr);
916 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
921 return DOUBLE_TO_CDTIME_T(v);
922 } /* }}} cdtime_t global_option_get_time */
924 cdtime_t cf_get_default_interval(void) {
925 return global_option_get_time("Interval",
926 DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL));
929 void cf_unregister(const char *type) {
930 for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL;
931 prev = this, this = this->next)
932 if (strcasecmp(this->type, type) == 0) {
934 first_callback = this->next;
936 prev->next = this->next;
941 } /* void cf_unregister */
943 void cf_unregister_complex(const char *type) {
944 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
945 this != NULL; prev = this, this = this->next)
946 if (strcasecmp(this->type, type) == 0) {
948 complex_callback_head = this->next;
950 prev->next = this->next;
956 } /* void cf_unregister */
958 void cf_register(const char *type, int (*callback)(const char *, const char *),
959 const char **keys, int keys_num) {
960 cf_callback_t *cf_cb;
962 /* Remove this module from the list, if it already exists */
965 /* This pointer will be free'd in `cf_unregister' */
966 if ((cf_cb = malloc(sizeof(*cf_cb))) == NULL)
970 cf_cb->callback = callback;
972 cf_cb->keys_num = keys_num;
973 cf_cb->ctx = plugin_get_ctx();
975 cf_cb->next = first_callback;
976 first_callback = cf_cb;
977 } /* void cf_register */
979 int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) {
980 cf_complex_callback_t *new;
982 new = malloc(sizeof(*new));
986 new->type = strdup(type);
987 if (new->type == NULL) {
992 new->callback = callback;
995 new->ctx = plugin_get_ctx();
997 if (complex_callback_head == NULL) {
998 complex_callback_head = new;
1000 cf_complex_callback_t *last = complex_callback_head;
1001 while (last->next != NULL)
1007 } /* int cf_register_complex */
1009 int cf_read(const char *filename) {
1010 oconfig_item_t *conf;
1013 conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0);
1015 ERROR("Unable to read config file %s.", filename);
1017 } else if (conf->children_num == 0) {
1018 ERROR("Configuration file %s is empty.", filename);
1023 for (int i = 0; i < conf->children_num; i++) {
1024 if (conf->children[i].children == NULL) {
1025 if (dispatch_value(conf->children + i) != 0)
1028 if (dispatch_block(conf->children + i) != 0)
1035 /* Read the default types.db if no `TypesDB' option was given. */
1036 if (cf_default_typesdb) {
1037 if (read_types_list(PKGDATADIR "/types.db") != 0)
1045 /* Assures the config option is a string, duplicates it and returns the copy in
1046 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1048 int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1050 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1051 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1055 char *string = strdup(ci->values[0].value.string);
1059 if (*ret_string != NULL)
1061 *ret_string = string;
1064 } /* }}} int cf_util_get_string */
1066 /* Assures the config option is a string and copies it to the provided buffer.
1067 * Assures null-termination. */
1068 int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */
1069 size_t buffer_size) {
1070 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1073 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1074 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1078 strncpy(buffer, ci->values[0].value.string, buffer_size);
1079 buffer[buffer_size - 1] = 0;
1082 } /* }}} int cf_util_get_string_buffer */
1084 /* Assures the config option is a number and returns it as an int. */
1085 int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */
1087 if ((ci == NULL) || (ret_value == NULL))
1090 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1091 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
1095 *ret_value = (int)ci->values[0].value.number;
1098 } /* }}} int cf_util_get_int */
1100 int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */
1102 if ((ci == NULL) || (ret_value == NULL))
1105 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1106 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
1110 *ret_value = ci->values[0].value.number;
1113 } /* }}} int cf_util_get_double */
1115 int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */
1117 if ((ci == NULL) || (ret_bool == NULL))
1120 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) &&
1121 (ci->values[0].type != OCONFIG_TYPE_STRING))) {
1122 P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key);
1126 switch (ci->values[0].type) {
1127 case OCONFIG_TYPE_BOOLEAN:
1128 *ret_bool = ci->values[0].value.boolean ? true : false;
1130 case OCONFIG_TYPE_STRING:
1131 P_WARNING("Using string value `%s' for boolean option `%s' is deprecated "
1132 "and will be removed in future releases. Use unquoted true or "
1134 ci->values[0].value.string, ci->key);
1136 if (IS_TRUE(ci->values[0].value.string))
1138 else if (IS_FALSE(ci->values[0].value.string))
1141 P_ERROR("Cannot parse string value `%s' of the `%s' option as a boolean "
1143 ci->values[0].value.string, ci->key);
1150 } /* }}} int cf_util_get_boolean */
1152 int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */
1153 unsigned int *ret_value, unsigned int flag) {
1156 if (ret_value == NULL)
1160 status = cf_util_get_boolean(ci, &b);
1167 *ret_value &= ~flag;
1171 } /* }}} int cf_util_get_flag */
1173 /* Assures that the config option is a string or a number if the correct range
1174 * of 1-65535. The string is then converted to a port number using
1175 * `service_name_to_port_number' and returned.
1176 * Returns the port number in the range [1-65535] or less than zero upon
1178 int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */
1182 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
1183 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
1184 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1188 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1189 return service_name_to_port_number(ci->values[0].value.string);
1191 assert(ci->values[0].type == OCONFIG_TYPE_NUMBER);
1192 tmp = (int)(ci->values[0].value.number + 0.5);
1193 if ((tmp < 1) || (tmp > 65535)) {
1194 P_ERROR("The `%s' option requires a service name or a port number. The "
1195 "number you specified, %i, is not in the valid range of 1-65535.",
1201 } /* }}} int cf_util_get_port_number */
1203 int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1209 if (ci->values_num != 1) {
1210 P_ERROR("The `%s` option requires exactly one argument.", ci->key);
1214 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1215 return cf_util_get_string(ci, ret_string);
1216 if (ci->values[0].type != OCONFIG_TYPE_NUMBER) {
1217 P_ERROR("The `%s` option requires exactly one string or numeric argument.",
1222 status = cf_util_get_int(ci, &port);
1225 else if ((port < 1) || (port > 65535)) {
1226 P_ERROR("The port number given for the `%s` option is out of range (%i).",
1231 service = malloc(6);
1232 if (service == NULL) {
1233 P_ERROR("cf_util_get_service: Out of memory.");
1236 snprintf(service, 6, "%i", port);
1239 *ret_string = service;
1242 } /* }}} int cf_util_get_service */
1244 int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1246 if ((ci == NULL) || (ret_value == NULL))
1249 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1250 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
1254 if (ci->values[0].value.number < 0.0) {
1255 P_ERROR("The numeric argument of the `%s' option must not be negative.",
1260 *ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
1263 } /* }}} int cf_util_get_cdtime */