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"
32 #include "configfile.h"
33 #include "filter_chain.h"
35 #include "types_list.h"
36 #include "utils/common/common.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_option(const cf_callback_t *cf_cb, oconfig_item_t *ci) {
142 const char *plugin = cf_cb->type;
143 const char *orig_key = ci->key;
144 if (orig_key == NULL)
147 /* (Re)construct string value for option */
149 int buffer_free = sizeof(buffer);
150 char *buffer_ptr = buffer;
152 for (int i = 0; i < ci->values_num; i++) {
155 if (ci->values[i].type == OCONFIG_TYPE_STRING)
157 ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
158 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
159 status = ssnprintf(buffer_ptr, buffer_free, " %lf",
160 ci->values[i].value.number);
161 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
162 status = ssnprintf(buffer_ptr, buffer_free, " %s",
163 ci->values[i].value.boolean ? "true" : "false");
165 if ((status < 0) || (status >= buffer_free))
167 buffer_free -= status;
168 buffer_ptr += status;
171 /* skip the initial space */
172 const char *orig_value = buffer + 1;
174 DEBUG("plugin = %s, key = %s, value = %s", ESCAPE_NULL(plugin), orig_key,
175 ESCAPE_NULL(orig_value));
177 char *key = strdup(orig_key);
181 char *value = strdup(orig_value);
189 plugin_ctx_t old_ctx = plugin_set_ctx(cf_cb->ctx);
192 for (i = 0; i < cf_cb->keys_num; i++) {
193 if ((cf_cb->keys[i] != NULL) && (strcasecmp(cf_cb->keys[i], key) == 0)) {
194 ret = (*cf_cb->callback)(key, value);
199 plugin_set_ctx(old_ctx);
201 if (i >= cf_cb->keys_num)
202 WARNING("Plugin `%s' did not register for value `%s'.", plugin, key);
208 } /* int cf_dispatch_option */
210 static int dispatch_global_option(const oconfig_item_t *ci) {
211 if (ci->values_num != 1) {
212 ERROR("configfile: Global option `%s' needs exactly one argument.",
217 if (ci->values[0].type == OCONFIG_TYPE_STRING)
218 return global_option_set(ci->key, ci->values[0].value.string, 0);
219 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
221 ssnprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number);
222 return global_option_set(ci->key, tmp, 0);
223 } else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) {
224 if (ci->values[0].value.boolean)
225 return global_option_set(ci->key, "true", 0);
227 return global_option_set(ci->key, "false", 0);
230 ERROR("configfile: Global option `%s' argument has unknown type.", ci->key);
233 } /* int dispatch_global_option */
235 static int dispatch_value_typesdb(oconfig_item_t *ci) {
236 assert(strcasecmp(ci->key, "TypesDB") == 0);
238 cf_default_typesdb = 0;
240 if (ci->values_num < 1) {
241 ERROR("configfile: `TypesDB' needs at least one argument.");
245 for (int i = 0; i < ci->values_num; ++i) {
246 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
247 WARNING("configfile: TypesDB: Skipping %i. argument which "
253 read_types_list(ci->values[i].value.string);
256 } /* int dispatch_value_typesdb */
258 static int dispatch_value_plugindir(oconfig_item_t *ci) {
259 assert(strcasecmp(ci->key, "PluginDir") == 0);
261 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
262 ERROR("configfile: The `PluginDir' option needs exactly one string "
267 plugin_set_dir(ci->values[0].value.string);
271 static int dispatch_loadplugin(oconfig_item_t *ci) {
274 assert(strcasecmp(ci->key, "LoadPlugin") == 0);
276 if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
277 ERROR("configfile: The `LoadPlugin' block needs exactly one string "
282 const char *name = ci->values[0].value.string;
283 if (strcmp("libvirt", name) == 0)
286 /* default to the global interval set before loading this plugin */
288 .interval = cf_get_default_interval(),
289 .name = strdup(name),
291 if (ctx.name == NULL)
294 for (int i = 0; i < ci->children_num; ++i) {
295 oconfig_item_t *child = ci->children + i;
297 if (strcasecmp("Globals", child->key) == 0)
298 cf_util_get_boolean(child, &global);
299 else if (strcasecmp("Interval", child->key) == 0)
300 cf_util_get_cdtime(child, &ctx.interval);
301 else if (strcasecmp("FlushInterval", child->key) == 0)
302 cf_util_get_cdtime(child, &ctx.flush_interval);
303 else if (strcasecmp("FlushTimeout", child->key) == 0)
304 cf_util_get_cdtime(child, &ctx.flush_timeout);
306 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
312 plugin_ctx_t old_ctx = plugin_set_ctx(ctx);
313 int ret_val = plugin_load(name, global);
314 /* reset to the "global" context */
315 plugin_set_ctx(old_ctx);
318 } /* int dispatch_value_loadplugin */
320 static int dispatch_value(oconfig_item_t *ci) {
323 for (int i = 0; i < cf_value_map_num; i++)
324 if (strcasecmp(cf_value_map[i].key, ci->key) == 0) {
325 ret = cf_value_map[i].func(ci);
332 for (int i = 0; i < cf_global_options_num; i++)
333 if (strcasecmp(cf_global_options[i].key, ci->key) == 0) {
334 ret = dispatch_global_option(ci);
339 } /* int dispatch_value */
341 static int dispatch_block_plugin(oconfig_item_t *ci) {
342 assert(strcasecmp(ci->key, "Plugin") == 0);
344 if (ci->values_num < 1) {
345 ERROR("configfile: The `Plugin' block requires arguments.");
348 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
349 ERROR("configfile: First argument of `Plugin' block should be a string.");
353 const char *plugin_name = ci->values[0].value.string;
354 if (strcmp("libvirt", plugin_name) == 0) {
355 /* TODO(octo): Remove this legacy. */
356 WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid "
357 "problems with the build system. "
358 "Your configuration is still using the old name. "
359 "Please change it to use \"virt\" as soon as possible. "
360 "This compatibility code will go away eventually.");
361 plugin_name = "virt";
364 bool plugin_loaded = plugin_is_loaded(plugin_name);
366 if (!plugin_loaded && IS_TRUE(global_option_get("AutoLoadPlugin"))) {
367 plugin_ctx_t ctx = {0};
369 /* default to the global interval set before loading this plugin */
370 ctx.interval = cf_get_default_interval();
371 ctx.name = strdup(plugin_name);
373 plugin_ctx_t old_ctx = plugin_set_ctx(ctx);
374 int status = plugin_load(plugin_name, /* flags = */ false);
375 /* reset to the "global" context */
376 plugin_set_ctx(old_ctx);
379 ERROR("Automatically loading plugin `%s' failed "
381 plugin_name, status);
384 plugin_loaded = true;
387 if (!plugin_loaded) {
388 WARNING("There is configuration for the `%s' plugin, but the plugin isn't "
389 "loaded. Please check your configuration.",
392 /* Try to be backward-compatible with previous versions */
396 /* Check for a complex callback first */
397 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL;
399 if (strcasecmp(plugin_name, cb->type) == 0) {
400 plugin_ctx_t old_ctx = plugin_set_ctx(cb->ctx);
401 int ret_val = (cb->callback(ci));
402 plugin_set_ctx(old_ctx);
407 /* Try to be backward-compatible with previous versions */
408 if (ci->children_num == 0)
411 /* Hm, no complex plugin found. Dispatch the values one by one */
412 cf_callback_t *cf_cb = cf_search(plugin_name);
414 WARNING("Found a configuration for the `%s' plugin, but "
415 "the plugin didn't register a configuration callback.",
420 for (int i = 0; i < ci->children_num; i++) {
421 if (ci->children[i].children == NULL) {
422 oconfig_item_t *child = ci->children + i;
423 int ret = cf_dispatch_option(cf_cb, child);
425 ERROR("Plugin `%s' failed to handle option `%s', return code: %i",
426 plugin_name, child->key, ret);
430 WARNING("There is a `%s' block within the "
431 "configuration for the `%s' plugin. "
432 "The plugin only expects \"simple\" configuration options. "
433 "Blocks are not supported. Please check your configuration.",
434 ci->children[i].key, plugin_name);
441 static int dispatch_block(oconfig_item_t *ci) {
442 if (strcasecmp(ci->key, "LoadPlugin") == 0)
443 return dispatch_loadplugin(ci);
444 else if (strcasecmp(ci->key, "Plugin") == 0)
445 return dispatch_block_plugin(ci);
446 else if (strcasecmp(ci->key, "Chain") == 0)
447 return fc_configure(ci);
452 static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src,
454 oconfig_item_t *temp;
457 assert(dst->children_num > offset);
459 /* Free the memory used by the replaced child. Usually that's the
460 * `Include "blah"' statement. */
461 temp = dst->children + offset;
462 for (int i = 0; i < temp->values_num; i++) {
463 if (temp->values[i].type == OCONFIG_TYPE_STRING) {
464 sfree(temp->values[i].value.string);
470 /* If (src->children_num == 0) the array size is decreased. If offset
471 * is _not_ the last element, (offset < (dst->children_num - 1)), then
472 * we need to move the trailing elements before resizing the array. */
473 if ((src->children_num == 0) && (offset < (dst->children_num - 1))) {
474 int nmemb = dst->children_num - (offset + 1);
475 memmove(dst->children + offset, dst->children + offset + 1,
476 sizeof(oconfig_item_t) * nmemb);
479 /* Resize the memory containing the children to be big enough to hold
481 if (dst->children_num + src->children_num - 1 == 0) {
482 dst->children_num = 0;
487 realloc(dst->children, sizeof(oconfig_item_t) *
488 (dst->children_num + src->children_num - 1));
490 ERROR("configfile: realloc failed.");
493 dst->children = temp;
495 /* If there are children behind the include statement, and they have
496 * not yet been moved because (src->children_num == 0), then move them
497 * to the end of the list, so that the new children have room before
499 if ((src->children_num > 0) && ((dst->children_num - (offset + 1)) > 0)) {
500 int nmemb = dst->children_num - (offset + 1);
501 int old_offset = offset + 1;
502 int new_offset = offset + src->children_num;
504 memmove(dst->children + new_offset, dst->children + old_offset,
505 sizeof(oconfig_item_t) * nmemb);
508 /* Last but not least: If there are new children, copy them to the
509 * memory reserved for them. */
510 if (src->children_num > 0) {
511 memcpy(dst->children + offset, src->children,
512 sizeof(oconfig_item_t) * src->children_num);
515 /* Update the number of children. */
516 dst->children_num += (src->children_num - 1);
519 } /* int cf_ci_replace_child */
521 static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) {
522 oconfig_item_t *temp;
524 if ((src == NULL) || (src->children_num == 0))
527 temp = realloc(dst->children, sizeof(oconfig_item_t) *
528 (dst->children_num + src->children_num));
530 ERROR("configfile: realloc failed.");
533 dst->children = temp;
535 memcpy(dst->children + dst->children_num, src->children,
536 sizeof(oconfig_item_t) * src->children_num);
537 dst->children_num += src->children_num;
540 } /* int cf_ci_append_children */
542 #define CF_MAX_DEPTH 8
543 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
546 static int cf_include_all(oconfig_item_t *root, int depth) {
547 for (int i = 0; i < root->children_num; i++) {
551 char *pattern = NULL;
553 if (strcasecmp(root->children[i].key, "Include") != 0)
556 old = root->children + i;
558 if ((old->values_num != 1) ||
559 (old->values[0].type != OCONFIG_TYPE_STRING)) {
560 ERROR("configfile: `Include' needs exactly one string argument.");
564 for (int j = 0; j < old->children_num; ++j) {
565 oconfig_item_t *child = old->children + j;
567 if (strcasecmp(child->key, "Filter") == 0)
568 cf_util_get_string(child, &pattern);
570 ERROR("configfile: Option `%s' not allowed in <Include> block.",
574 new = cf_read_generic(old->values[0].value.string, pattern, depth + 1);
580 /* Now replace the i'th child in `root' with `new'. */
581 if (cf_ci_replace_child(root, new, i) < 0) {
587 /* ... and go back to the new i'th child. */
592 } /* for (i = 0; i < root->children_num; i++) */
595 } /* int cf_include_all */
597 static oconfig_item_t *cf_read_file(const char *file, const char *pattern,
599 oconfig_item_t *root;
602 assert(depth < CF_MAX_DEPTH);
604 if (pattern != NULL) {
605 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
606 char *tmp = sstrdup(file);
607 char *filename = basename(tmp);
609 if ((filename != NULL) && (fnmatch(pattern, filename, 0) != 0)) {
610 DEBUG("configfile: Not including `%s' because it "
611 "does not match pattern `%s'.",
619 ERROR("configfile: Cannot apply pattern filter '%s' "
620 "to file '%s': functions basename() and / or "
621 "fnmatch() not available.",
623 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
626 root = oconfig_parse_file(file);
628 ERROR("configfile: Cannot read file `%s'.", file);
632 status = cf_include_all(root, depth);
639 } /* oconfig_item_t *cf_read_file */
641 static int cf_compare_string(const void *p1, const void *p2) {
642 return strcmp(*(const char **)p1, *(const char **)p2);
645 static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern,
647 oconfig_item_t *root = NULL;
650 char **filenames = NULL;
651 int filenames_num = 0;
654 assert(depth < CF_MAX_DEPTH);
658 ERROR("configfile: opendir failed: %s", STRERRNO);
662 root = calloc(1, sizeof(*root));
664 ERROR("configfile: calloc failed.");
669 while ((de = readdir(dh)) != NULL) {
673 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
676 status = ssnprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
677 if ((status < 0) || ((size_t)status >= sizeof(name))) {
678 ERROR("configfile: Not including `%s/%s' because its"
679 " name is too long.",
682 for (int i = 0; i < filenames_num; ++i)
690 tmp = realloc(filenames, filenames_num * sizeof(*filenames));
692 ERROR("configfile: realloc failed.");
694 for (int i = 0; i < filenames_num - 1; ++i)
702 filenames[filenames_num - 1] = sstrdup(name);
705 if (filenames == NULL) {
710 qsort((void *)filenames, filenames_num, sizeof(*filenames),
713 for (int i = 0; i < filenames_num; ++i) {
714 oconfig_item_t *temp;
715 char *name = filenames[i];
717 temp = cf_read_generic(name, pattern, depth);
719 /* An error should already have been reported. */
724 cf_ci_append_children(root, temp);
725 sfree(temp->children);
734 } /* oconfig_item_t *cf_read_dir */
739 * Path is stat'ed and either cf_read_file or cf_read_dir is called
742 * There are two versions of this function: If `wordexp' exists shell wildcards
743 * will be expanded and the function will include all matches found. If
744 * `wordexp' (or, more precisely, its header file) is not available the
745 * simpler function is used which does not do any such expansion.
748 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
750 oconfig_item_t *root = NULL;
752 const char *path_ptr;
755 if (depth >= CF_MAX_DEPTH) {
756 ERROR("configfile: Not including `%s' because the maximum "
757 "nesting depth has been reached.",
762 status = wordexp(path, &we, WRDE_NOCMD);
764 ERROR("configfile: wordexp (%s) failed.", path);
768 root = calloc(1, sizeof(*root));
770 ERROR("configfile: calloc failed.");
774 /* wordexp() might return a sorted list already. That's not
775 * documented though, so let's make sure we get what we want. */
776 qsort((void *)we.we_wordv, we.we_wordc, sizeof(*we.we_wordv),
779 for (size_t i = 0; i < we.we_wordc; i++) {
780 oconfig_item_t *temp;
783 path_ptr = we.we_wordv[i];
785 status = stat(path_ptr, &statbuf);
787 WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO);
791 if (S_ISREG(statbuf.st_mode))
792 temp = cf_read_file(path_ptr, pattern, depth);
793 else if (S_ISDIR(statbuf.st_mode))
794 temp = cf_read_dir(path_ptr, pattern, depth);
796 WARNING("configfile: %s is neither a file nor a "
807 cf_ci_append_children(root, temp);
808 sfree(temp->children);
815 } /* oconfig_item_t *cf_read_generic */
816 /* #endif HAVE_WORDEXP_H */
818 #else /* if !HAVE_WORDEXP_H */
819 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
824 if (depth >= CF_MAX_DEPTH) {
825 ERROR("configfile: Not including `%s' because the maximum "
826 "nesting depth has been reached.",
831 status = stat(path, &statbuf);
833 ERROR("configfile: stat (%s) failed: %s", path, STRERRNO);
837 if (S_ISREG(statbuf.st_mode))
838 return cf_read_file(path, pattern, depth);
839 else if (S_ISDIR(statbuf.st_mode))
840 return cf_read_dir(path, pattern, depth);
842 ERROR("configfile: %s is neither a file nor a directory.", path);
844 } /* oconfig_item_t *cf_read_generic */
845 #endif /* !HAVE_WORDEXP_H */
850 int global_option_set(const char *option, const char *value, bool from_cli) {
852 DEBUG("option = %s; value = %s;", option, value);
854 for (i = 0; i < cf_global_options_num; i++)
855 if (strcasecmp(cf_global_options[i].key, option) == 0)
858 if (i >= cf_global_options_num) {
859 ERROR("configfile: Cannot set unknown global option `%s'.", option);
863 if (cf_global_options[i].from_cli && (!from_cli)) {
864 DEBUG("configfile: Ignoring %s `%s' option because "
865 "it was overriden by a command-line option.",
870 sfree(cf_global_options[i].value);
873 cf_global_options[i].value = strdup(value);
875 cf_global_options[i].value = NULL;
877 cf_global_options[i].from_cli = from_cli;
882 const char *global_option_get(const char *option) {
884 for (i = 0; i < cf_global_options_num; i++)
885 if (strcasecmp(cf_global_options[i].key, option) == 0)
888 if (i >= cf_global_options_num) {
889 ERROR("configfile: Cannot get unknown global option `%s'.", option);
893 return (cf_global_options[i].value != NULL) ? cf_global_options[i].value
894 : cf_global_options[i].def;
895 } /* char *global_option_get */
897 long global_option_get_long(const char *option, long default_value) {
901 str = global_option_get(option);
903 return default_value;
906 value = strtol(str, /* endptr = */ NULL, /* base = */ 0);
908 return default_value;
911 } /* char *global_option_get_long */
913 cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */
919 optstr = global_option_get(name);
924 v = strtod(optstr, &endptr);
925 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
930 return DOUBLE_TO_CDTIME_T(v);
931 } /* }}} cdtime_t global_option_get_time */
933 cdtime_t cf_get_default_interval(void) {
934 return global_option_get_time("Interval",
935 DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL));
938 void cf_unregister(const char *type) {
939 for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL;
940 prev = this, this = this->next)
941 if (strcasecmp(this->type, type) == 0) {
943 first_callback = this->next;
945 prev->next = this->next;
950 } /* void cf_unregister */
952 void cf_unregister_complex(const char *type) {
953 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
954 this != NULL; prev = this, this = this->next)
955 if (strcasecmp(this->type, type) == 0) {
957 complex_callback_head = this->next;
959 prev->next = this->next;
965 } /* void cf_unregister */
967 void cf_register(const char *type, int (*callback)(const char *, const char *),
968 const char **keys, int keys_num) {
969 cf_callback_t *cf_cb;
971 /* Remove this module from the list, if it already exists */
974 /* This pointer will be free'd in `cf_unregister' */
975 if ((cf_cb = malloc(sizeof(*cf_cb))) == NULL)
979 cf_cb->callback = callback;
981 cf_cb->keys_num = keys_num;
982 cf_cb->ctx = plugin_get_ctx();
984 cf_cb->next = first_callback;
985 first_callback = cf_cb;
986 } /* void cf_register */
988 int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) {
989 cf_complex_callback_t *new;
991 new = malloc(sizeof(*new));
995 new->type = strdup(type);
996 if (new->type == NULL) {
1001 new->callback = callback;
1004 new->ctx = plugin_get_ctx();
1006 if (complex_callback_head == NULL) {
1007 complex_callback_head = new;
1009 cf_complex_callback_t *last = complex_callback_head;
1010 while (last->next != NULL)
1016 } /* int cf_register_complex */
1018 int cf_read(const char *filename) {
1019 oconfig_item_t *conf;
1022 conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0);
1024 ERROR("Unable to read config file %s.", filename);
1026 } else if (conf->children_num == 0) {
1027 ERROR("Configuration file %s is empty.", filename);
1032 for (int i = 0; i < conf->children_num; i++) {
1033 if (conf->children[i].children == NULL) {
1034 if (dispatch_value(conf->children + i) != 0)
1037 if (dispatch_block(conf->children + i) != 0)
1044 /* Read the default types.db if no `TypesDB' option was given. */
1045 if (cf_default_typesdb) {
1046 if (read_types_list(PKGDATADIR "/types.db") != 0)
1054 /* Assures the config option is a string, duplicates it and returns the copy in
1055 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1057 int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1059 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1060 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1064 char *string = strdup(ci->values[0].value.string);
1068 if (*ret_string != NULL)
1070 *ret_string = string;
1073 } /* }}} int cf_util_get_string */
1075 /* Assures the config option is a string and copies it to the provided buffer.
1076 * Assures NUL-termination. */
1077 int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */
1078 size_t buffer_size) {
1079 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1082 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1083 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1087 strncpy(buffer, ci->values[0].value.string, buffer_size);
1088 buffer[buffer_size - 1] = '\0';
1091 } /* }}} int cf_util_get_string_buffer */
1093 /* Assures the config option is a number and returns it as an int. */
1094 int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */
1096 if ((ci == NULL) || (ret_value == NULL))
1099 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1100 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
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 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
1119 *ret_value = ci->values[0].value.number;
1122 } /* }}} int cf_util_get_double */
1124 int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */
1126 if ((ci == NULL) || (ret_bool == NULL))
1129 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) &&
1130 (ci->values[0].type != OCONFIG_TYPE_STRING))) {
1131 P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key);
1135 switch (ci->values[0].type) {
1136 case OCONFIG_TYPE_BOOLEAN:
1137 *ret_bool = ci->values[0].value.boolean ? true : false;
1139 case OCONFIG_TYPE_STRING:
1140 P_WARNING("Using string value `%s' for boolean option `%s' is deprecated "
1141 "and will be removed in future releases. Use unquoted true or "
1143 ci->values[0].value.string, ci->key);
1145 if (IS_TRUE(ci->values[0].value.string))
1147 else if (IS_FALSE(ci->values[0].value.string))
1150 P_ERROR("Cannot parse string value `%s' of the `%s' option as a boolean "
1152 ci->values[0].value.string, ci->key);
1159 } /* }}} int cf_util_get_boolean */
1161 int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */
1162 unsigned int *ret_value, unsigned int flag) {
1165 if (ret_value == NULL)
1169 status = cf_util_get_boolean(ci, &b);
1176 *ret_value &= ~flag;
1180 } /* }}} int cf_util_get_flag */
1182 /* Assures that the config option is a string or a number if the correct range
1183 * of 1-65535. The string is then converted to a port number using
1184 * `service_name_to_port_number' and returned.
1185 * Returns the port number in the range [1-65535] or less than zero upon
1187 int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */
1191 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
1192 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
1193 P_ERROR("The `%s' option requires exactly one string argument.", ci->key);
1197 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1198 return service_name_to_port_number(ci->values[0].value.string);
1200 assert(ci->values[0].type == OCONFIG_TYPE_NUMBER);
1201 tmp = (int)(ci->values[0].value.number + 0.5);
1202 if ((tmp < 1) || (tmp > 65535)) {
1203 P_ERROR("The `%s' option requires a service name or a port number. The "
1204 "number you specified, %i, is not in the valid range of 1-65535.",
1210 } /* }}} int cf_util_get_port_number */
1212 int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1218 if (ci->values_num != 1) {
1219 P_ERROR("The `%s` option requires exactly one argument.", ci->key);
1223 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1224 return cf_util_get_string(ci, ret_string);
1225 if (ci->values[0].type != OCONFIG_TYPE_NUMBER) {
1226 P_ERROR("The `%s` option requires exactly one string or numeric argument.",
1231 status = cf_util_get_int(ci, &port);
1234 else if ((port < 1) || (port > 65535)) {
1235 P_ERROR("The port number given for the `%s` option is out of range (%i).",
1240 service = malloc(6);
1241 if (service == NULL) {
1242 P_ERROR("cf_util_get_service: Out of memory.");
1245 ssnprintf(service, 6, "%i", port);
1248 *ret_string = service;
1251 } /* }}} int cf_util_get_service */
1253 int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1255 if ((ci == NULL) || (ret_value == NULL))
1258 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1259 P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key);
1263 if (ci->values[0].value.number < 0.0) {
1264 P_ERROR("The numeric argument of the `%s' option must not be negative.",
1269 *ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
1272 } /* }}} int cf_util_get_cdtime */