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 = NULL;
95 static cf_complex_callback_t *complex_callback_head = NULL;
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)
195 if (ci->values[0].type == OCONFIG_TYPE_STRING)
196 return global_option_set(ci->key, ci->values[0].value.string, 0);
197 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) {
199 snprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number);
200 return global_option_set(ci->key, tmp, 0);
201 } else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) {
202 if (ci->values[0].value.boolean)
203 return global_option_set(ci->key, "true", 0);
205 return global_option_set(ci->key, "false", 0);
209 } /* int dispatch_global_option */
211 static int dispatch_value_typesdb(oconfig_item_t *ci) {
212 assert(strcasecmp(ci->key, "TypesDB") == 0);
214 cf_default_typesdb = 0;
216 if (ci->values_num < 1) {
217 ERROR("configfile: `TypesDB' needs at least one argument.");
221 for (int i = 0; i < ci->values_num; ++i) {
222 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
223 WARNING("configfile: TypesDB: Skipping %i. argument which "
229 read_types_list(ci->values[i].value.string);
232 } /* int dispatch_value_typesdb */
234 static int dispatch_value_plugindir(oconfig_item_t *ci) {
235 assert(strcasecmp(ci->key, "PluginDir") == 0);
237 if (ci->values_num != 1)
239 if (ci->values[0].type != OCONFIG_TYPE_STRING)
242 plugin_set_dir(ci->values[0].value.string);
246 static int dispatch_loadplugin(oconfig_item_t *ci) {
249 plugin_ctx_t ctx = {0};
250 plugin_ctx_t old_ctx;
253 assert(strcasecmp(ci->key, "LoadPlugin") == 0);
255 if (ci->values_num != 1)
257 if (ci->values[0].type != OCONFIG_TYPE_STRING)
260 name = ci->values[0].value.string;
261 if (strcmp("libvirt", name) == 0)
264 /* default to the global interval set before loading this plugin */
265 ctx.interval = cf_get_default_interval();
266 ctx.flush_interval = 0;
267 ctx.flush_timeout = 0;
269 for (int i = 0; i < ci->children_num; ++i) {
270 oconfig_item_t *child = ci->children + i;
272 if (strcasecmp("Globals", child->key) == 0)
273 cf_util_get_boolean(child, &global);
274 else if (strcasecmp("Interval", child->key) == 0)
275 cf_util_get_cdtime(child, &ctx.interval);
276 else if (strcasecmp("FlushInterval", child->key) == 0)
277 cf_util_get_cdtime(child, &ctx.flush_interval);
278 else if (strcasecmp("FlushTimeout", child->key) == 0)
279 cf_util_get_cdtime(child, &ctx.flush_timeout);
281 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
283 child->key, ci->values[0].value.string);
287 old_ctx = plugin_set_ctx(ctx);
288 ret_val = plugin_load(name, global);
289 /* reset to the "global" context */
290 plugin_set_ctx(old_ctx);
293 } /* int dispatch_value_loadplugin */
295 static int dispatch_value_plugin(const char *plugin, oconfig_item_t *ci) {
301 buffer_free = sizeof(buffer);
303 for (int i = 0; i < ci->values_num; i++) {
306 if (ci->values[i].type == OCONFIG_TYPE_STRING)
308 snprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string);
309 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
311 snprintf(buffer_ptr, buffer_free, " %lf", ci->values[i].value.number);
312 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
313 status = snprintf(buffer_ptr, buffer_free, " %s",
314 ci->values[i].value.boolean ? "true" : "false");
316 if ((status < 0) || (status >= buffer_free))
318 buffer_free -= status;
319 buffer_ptr += status;
321 /* skip the initial space */
322 buffer_ptr = buffer + 1;
324 return cf_dispatch(plugin, ci->key, buffer_ptr);
325 } /* int dispatch_value_plugin */
327 static int dispatch_value(oconfig_item_t *ci) {
330 for (int i = 0; i < cf_value_map_num; i++)
331 if (strcasecmp(cf_value_map[i].key, ci->key) == 0) {
332 ret = cf_value_map[i].func(ci);
336 for (int i = 0; i < cf_global_options_num; i++)
337 if (strcasecmp(cf_global_options[i].key, ci->key) == 0) {
338 ret = dispatch_global_option(ci);
343 } /* int dispatch_value */
345 static int dispatch_block_plugin(oconfig_item_t *ci) {
348 if (strcasecmp(ci->key, "Plugin") != 0)
350 if (ci->values_num < 1)
352 if (ci->values[0].type != OCONFIG_TYPE_STRING)
355 name = ci->values[0].value.string;
356 if (strcmp("libvirt", name) == 0) {
357 /* TODO(octo): Remove this legacy. */
358 WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid "
359 "problems with the build system. "
360 "Your configuration is still using the old name. "
361 "Please change it to use \"virt\" as soon as possible. "
362 "This compatibility code will go away eventually.");
366 if (IS_TRUE(global_option_get("AutoLoadPlugin"))) {
367 plugin_ctx_t ctx = {0};
368 plugin_ctx_t old_ctx;
371 /* default to the global interval set before loading this plugin */
372 ctx.interval = cf_get_default_interval();
374 old_ctx = plugin_set_ctx(ctx);
375 status = plugin_load(name, /* flags = */ 0);
376 /* reset to the "global" context */
377 plugin_set_ctx(old_ctx);
380 ERROR("Automatically loading plugin \"%s\" failed "
387 /* Check for a complex callback first */
388 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL;
390 if (strcasecmp(name, cb->type) == 0) {
391 plugin_ctx_t old_ctx;
394 old_ctx = plugin_set_ctx(cb->ctx);
395 ret_val = (cb->callback(ci));
396 plugin_set_ctx(old_ctx);
401 /* Hm, no complex plugin found. Dispatch the values one by one */
402 for (int i = 0; i < ci->children_num; i++) {
403 if (ci->children[i].children == NULL)
404 dispatch_value_plugin(name, ci->children + i);
406 WARNING("There is a `%s' block within the "
407 "configuration for the %s plugin. "
408 "The plugin either only expects "
409 "\"simple\" configuration statements "
410 "or wasn't loaded using `LoadPlugin'."
411 " Please check your configuration.",
412 ci->children[i].key, name);
419 static int dispatch_block(oconfig_item_t *ci) {
420 if (strcasecmp(ci->key, "LoadPlugin") == 0)
421 return dispatch_loadplugin(ci);
422 else if (strcasecmp(ci->key, "Plugin") == 0)
423 return dispatch_block_plugin(ci);
424 else if (strcasecmp(ci->key, "Chain") == 0)
425 return fc_configure(ci);
430 static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src,
432 oconfig_item_t *temp;
435 assert(dst->children_num > offset);
437 /* Free the memory used by the replaced child. Usually that's the
438 * `Include "blah"' statement. */
439 temp = dst->children + offset;
440 for (int i = 0; i < temp->values_num; i++) {
441 if (temp->values[i].type == OCONFIG_TYPE_STRING) {
442 sfree(temp->values[i].value.string);
448 /* If (src->children_num == 0) the array size is decreased. If offset
449 * is _not_ the last element, (offset < (dst->children_num - 1)), then
450 * we need to move the trailing elements before resizing the array. */
451 if ((src->children_num == 0) && (offset < (dst->children_num - 1))) {
452 int nmemb = dst->children_num - (offset + 1);
453 memmove(dst->children + offset, dst->children + offset + 1,
454 sizeof(oconfig_item_t) * nmemb);
457 /* Resize the memory containing the children to be big enough to hold
459 if (dst->children_num + src->children_num - 1 == 0) {
460 dst->children_num = 0;
464 temp = realloc(dst->children,
465 sizeof(oconfig_item_t) *
466 (dst->children_num + src->children_num - 1));
468 ERROR("configfile: realloc failed.");
471 dst->children = temp;
473 /* If there are children behind the include statement, and they have
474 * not yet been moved because (src->children_num == 0), then move them
475 * to the end of the list, so that the new children have room before
477 if ((src->children_num > 0) && ((dst->children_num - (offset + 1)) > 0)) {
478 int nmemb = dst->children_num - (offset + 1);
479 int old_offset = offset + 1;
480 int new_offset = offset + src->children_num;
482 memmove(dst->children + new_offset, dst->children + old_offset,
483 sizeof(oconfig_item_t) * nmemb);
486 /* Last but not least: If there are new children, copy them to the
487 * memory reserved for them. */
488 if (src->children_num > 0) {
489 memcpy(dst->children + offset, src->children,
490 sizeof(oconfig_item_t) * src->children_num);
493 /* Update the number of children. */
494 dst->children_num += (src->children_num - 1);
497 } /* int cf_ci_replace_child */
499 static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) {
500 oconfig_item_t *temp;
502 if ((src == NULL) || (src->children_num == 0))
506 realloc(dst->children,
507 sizeof(oconfig_item_t) * (dst->children_num + src->children_num));
509 ERROR("configfile: realloc failed.");
512 dst->children = temp;
514 memcpy(dst->children + dst->children_num, src->children,
515 sizeof(oconfig_item_t) * src->children_num);
516 dst->children_num += src->children_num;
519 } /* int cf_ci_append_children */
521 #define CF_MAX_DEPTH 8
522 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
525 static int cf_include_all(oconfig_item_t *root, int depth) {
526 for (int i = 0; i < root->children_num; i++) {
530 char *pattern = NULL;
532 if (strcasecmp(root->children[i].key, "Include") != 0)
535 old = root->children + i;
537 if ((old->values_num != 1) ||
538 (old->values[0].type != OCONFIG_TYPE_STRING)) {
539 ERROR("configfile: `Include' needs exactly one string argument.");
543 for (int j = 0; j < old->children_num; ++j) {
544 oconfig_item_t *child = old->children + j;
546 if (strcasecmp(child->key, "Filter") == 0)
547 cf_util_get_string(child, &pattern);
549 ERROR("configfile: Option `%s' not allowed in <Include> block.",
553 new = cf_read_generic(old->values[0].value.string, pattern, depth + 1);
559 /* Now replace the i'th child in `root' with `new'. */
560 if (cf_ci_replace_child(root, new, i) < 0) {
566 /* ... and go back to the new i'th child. */
571 } /* for (i = 0; i < root->children_num; i++) */
574 } /* int cf_include_all */
576 static oconfig_item_t *cf_read_file(const char *file, const char *pattern,
578 oconfig_item_t *root;
581 assert(depth < CF_MAX_DEPTH);
583 if (pattern != NULL) {
584 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
585 char *tmp = sstrdup(file);
586 char *filename = basename(tmp);
588 if ((filename != NULL) && (fnmatch(pattern, filename, 0) != 0)) {
589 DEBUG("configfile: Not including `%s' because it "
590 "does not match pattern `%s'.",
598 ERROR("configfile: Cannot apply pattern filter '%s' "
599 "to file '%s': functions basename() and / or "
600 "fnmatch() not available.",
602 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
605 root = oconfig_parse_file(file);
607 ERROR("configfile: Cannot read file `%s'.", file);
611 status = cf_include_all(root, depth);
618 } /* oconfig_item_t *cf_read_file */
620 static int cf_compare_string(const void *p1, const void *p2) {
621 return strcmp(*(const char **)p1, *(const char **)p2);
624 static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern,
626 oconfig_item_t *root = NULL;
629 char **filenames = NULL;
630 int filenames_num = 0;
633 assert(depth < CF_MAX_DEPTH);
637 ERROR("configfile: opendir failed: %s", STRERRNO);
641 root = calloc(1, sizeof(*root));
643 ERROR("configfile: calloc failed.");
648 while ((de = readdir(dh)) != NULL) {
652 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
655 status = snprintf(name, sizeof(name), "%s/%s", dir, de->d_name);
656 if ((status < 0) || ((size_t)status >= sizeof(name))) {
657 ERROR("configfile: Not including `%s/%s' because its"
658 " name is too long.",
661 for (int i = 0; i < filenames_num; ++i)
669 tmp = realloc(filenames, filenames_num * sizeof(*filenames));
671 ERROR("configfile: realloc failed.");
673 for (int i = 0; i < filenames_num - 1; ++i)
681 filenames[filenames_num - 1] = sstrdup(name);
684 if (filenames == NULL) {
689 qsort((void *)filenames, filenames_num, sizeof(*filenames),
692 for (int i = 0; i < filenames_num; ++i) {
693 oconfig_item_t *temp;
694 char *name = filenames[i];
696 temp = cf_read_generic(name, pattern, depth);
698 /* An error should already have been reported. */
703 cf_ci_append_children(root, temp);
704 sfree(temp->children);
713 } /* oconfig_item_t *cf_read_dir */
718 * Path is stat'ed and either cf_read_file or cf_read_dir is called
721 * There are two versions of this function: If `wordexp' exists shell wildcards
722 * will be expanded and the function will include all matches found. If
723 * `wordexp' (or, more precisely, its header file) is not available the
724 * simpler function is used which does not do any such expansion.
727 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
729 oconfig_item_t *root = NULL;
731 const char *path_ptr;
734 if (depth >= CF_MAX_DEPTH) {
735 ERROR("configfile: Not including `%s' because the maximum "
736 "nesting depth has been reached.",
741 status = wordexp(path, &we, WRDE_NOCMD);
743 ERROR("configfile: wordexp (%s) failed.", path);
747 root = calloc(1, sizeof(*root));
749 ERROR("configfile: calloc failed.");
753 /* wordexp() might return a sorted list already. That's not
754 * documented though, so let's make sure we get what we want. */
755 qsort((void *)we.we_wordv, we.we_wordc, sizeof(*we.we_wordv),
758 for (size_t i = 0; i < we.we_wordc; i++) {
759 oconfig_item_t *temp;
762 path_ptr = we.we_wordv[i];
764 status = stat(path_ptr, &statbuf);
766 WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO);
770 if (S_ISREG(statbuf.st_mode))
771 temp = cf_read_file(path_ptr, pattern, depth);
772 else if (S_ISDIR(statbuf.st_mode))
773 temp = cf_read_dir(path_ptr, pattern, depth);
775 WARNING("configfile: %s is neither a file nor a "
786 cf_ci_append_children(root, temp);
787 sfree(temp->children);
794 } /* oconfig_item_t *cf_read_generic */
795 /* #endif HAVE_WORDEXP_H */
797 #else /* if !HAVE_WORDEXP_H */
798 static oconfig_item_t *cf_read_generic(const char *path, const char *pattern,
803 if (depth >= CF_MAX_DEPTH) {
804 ERROR("configfile: Not including `%s' because the maximum "
805 "nesting depth has been reached.",
810 status = stat(path, &statbuf);
812 ERROR("configfile: stat (%s) failed: %s", path, STRERRNO);
816 if (S_ISREG(statbuf.st_mode))
817 return cf_read_file(path, pattern, depth);
818 else if (S_ISDIR(statbuf.st_mode))
819 return cf_read_dir(path, pattern, depth);
821 ERROR("configfile: %s is neither a file nor a directory.", path);
823 } /* oconfig_item_t *cf_read_generic */
824 #endif /* !HAVE_WORDEXP_H */
829 int global_option_set(const char *option, const char *value, _Bool from_cli) {
831 DEBUG("option = %s; value = %s;", option, value);
833 for (i = 0; i < cf_global_options_num; i++)
834 if (strcasecmp(cf_global_options[i].key, option) == 0)
837 if (i >= cf_global_options_num) {
838 ERROR("configfile: Cannot set unknown global option `%s'.", option);
842 if (cf_global_options[i].from_cli && (!from_cli)) {
843 DEBUG("configfile: Ignoring %s `%s' option because "
844 "it was overriden by a command-line option.",
849 sfree(cf_global_options[i].value);
852 cf_global_options[i].value = strdup(value);
854 cf_global_options[i].value = NULL;
856 cf_global_options[i].from_cli = from_cli;
861 const char *global_option_get(const char *option) {
863 for (i = 0; i < cf_global_options_num; i++)
864 if (strcasecmp(cf_global_options[i].key, option) == 0)
867 if (i >= cf_global_options_num) {
868 ERROR("configfile: Cannot get unknown global option `%s'.", option);
872 return (cf_global_options[i].value != NULL) ? cf_global_options[i].value
873 : cf_global_options[i].def;
874 } /* char *global_option_get */
876 long global_option_get_long(const char *option, long default_value) {
880 str = global_option_get(option);
882 return default_value;
885 value = strtol(str, /* endptr = */ NULL, /* base = */ 0);
887 return default_value;
890 } /* char *global_option_get_long */
892 cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */
898 optstr = global_option_get(name);
903 v = strtod(optstr, &endptr);
904 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
909 return DOUBLE_TO_CDTIME_T(v);
910 } /* }}} cdtime_t global_option_get_time */
912 cdtime_t cf_get_default_interval(void) {
913 return global_option_get_time("Interval",
914 DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL));
917 void cf_unregister(const char *type) {
918 for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL;
919 prev = this, this = this->next)
920 if (strcasecmp(this->type, type) == 0) {
922 first_callback = this->next;
924 prev->next = this->next;
929 } /* void cf_unregister */
931 void cf_unregister_complex(const char *type) {
932 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
933 this != NULL; prev = this, this = this->next)
934 if (strcasecmp(this->type, type) == 0) {
936 complex_callback_head = this->next;
938 prev->next = this->next;
944 } /* void cf_unregister */
946 void cf_register(const char *type, int (*callback)(const char *, const char *),
947 const char **keys, int keys_num) {
948 cf_callback_t *cf_cb;
950 /* Remove this module from the list, if it already exists */
953 /* This pointer will be free'd in `cf_unregister' */
954 if ((cf_cb = malloc(sizeof(*cf_cb))) == NULL)
958 cf_cb->callback = callback;
960 cf_cb->keys_num = keys_num;
961 cf_cb->ctx = plugin_get_ctx();
963 cf_cb->next = first_callback;
964 first_callback = cf_cb;
965 } /* void cf_register */
967 int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) {
968 cf_complex_callback_t *new;
970 new = malloc(sizeof(*new));
974 new->type = strdup(type);
975 if (new->type == NULL) {
980 new->callback = callback;
983 new->ctx = plugin_get_ctx();
985 if (complex_callback_head == NULL) {
986 complex_callback_head = new;
988 cf_complex_callback_t *last = complex_callback_head;
989 while (last->next != NULL)
995 } /* int cf_register_complex */
997 int cf_read(const char *filename) {
998 oconfig_item_t *conf;
1001 conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0);
1003 ERROR("Unable to read config file %s.", filename);
1005 } else if (conf->children_num == 0) {
1006 ERROR("Configuration file %s is empty.", filename);
1011 for (int i = 0; i < conf->children_num; i++) {
1012 if (conf->children[i].children == NULL) {
1013 if (dispatch_value(conf->children + i) != 0)
1016 if (dispatch_block(conf->children + i) != 0)
1023 /* Read the default types.db if no `TypesDB' option was given. */
1024 if (cf_default_typesdb) {
1025 if (read_types_list(PKGDATADIR "/types.db") != 0)
1033 /* Assures the config option is a string, duplicates it and returns the copy in
1034 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1036 int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1040 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1041 ERROR("cf_util_get_string: The %s option requires "
1042 "exactly one string argument.",
1047 string = strdup(ci->values[0].value.string);
1051 if (*ret_string != NULL)
1053 *ret_string = string;
1056 } /* }}} int cf_util_get_string */
1058 /* Assures the config option is a string and copies it to the provided buffer.
1059 * Assures null-termination. */
1060 int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */
1061 size_t buffer_size) {
1062 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1065 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1066 ERROR("cf_util_get_string_buffer: The %s option requires "
1067 "exactly one string argument.",
1072 strncpy(buffer, ci->values[0].value.string, buffer_size);
1073 buffer[buffer_size - 1] = 0;
1076 } /* }}} int cf_util_get_string_buffer */
1078 /* Assures the config option is a number and returns it as an int. */
1079 int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */
1081 if ((ci == NULL) || (ret_value == NULL))
1084 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1085 ERROR("cf_util_get_int: The %s option requires "
1086 "exactly one numeric argument.",
1091 *ret_value = (int)ci->values[0].value.number;
1094 } /* }}} int cf_util_get_int */
1096 int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */
1098 if ((ci == NULL) || (ret_value == NULL))
1101 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1102 ERROR("cf_util_get_double: The %s option requires "
1103 "exactly one numeric argument.",
1108 *ret_value = ci->values[0].value.number;
1111 } /* }}} int cf_util_get_double */
1113 int cf_util_get_boolean(const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1115 if ((ci == NULL) || (ret_bool == NULL))
1118 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) &&
1119 (ci->values[0].type != OCONFIG_TYPE_STRING))) {
1120 ERROR("cf_util_get_boolean: The %s option requires "
1121 "exactly one boolean argument.",
1126 switch (ci->values[0].type) {
1127 case OCONFIG_TYPE_BOOLEAN:
1128 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1130 case OCONFIG_TYPE_STRING:
1131 WARNING("cf_util_get_boolean: Using string value `%s' for boolean option "
1132 "`%s' is deprecated and will be removed in future releases. "
1133 "Use unquoted true or false instead.",
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 ERROR("cf_util_get_boolean: Cannot parse string value `%s' of the `%s' "
1142 "option as a boolean value.",
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) {
1157 if (ret_value == NULL)
1161 status = cf_util_get_boolean(ci, &b);
1168 *ret_value &= ~flag;
1172 } /* }}} int cf_util_get_flag */
1174 /* Assures that the config option is a string or a number if the correct range
1175 * of 1-65535. The string is then converted to a port number using
1176 * `service_name_to_port_number' and returned.
1177 * Returns the port number in the range [1-65535] or less than zero upon
1179 int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */
1183 if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) &&
1184 (ci->values[0].type != OCONFIG_TYPE_NUMBER))) {
1185 ERROR("cf_util_get_port_number: The \"%s\" option requires "
1186 "exactly one string argument.",
1191 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1192 return service_name_to_port_number(ci->values[0].value.string);
1194 assert(ci->values[0].type == OCONFIG_TYPE_NUMBER);
1195 tmp = (int)(ci->values[0].value.number + 0.5);
1196 if ((tmp < 1) || (tmp > 65535)) {
1197 ERROR("cf_util_get_port_number: The \"%s\" option requires "
1198 "a service name or a port number. The number "
1199 "you specified, %i, is not in the valid "
1200 "range of 1-65535.",
1206 } /* }}} int cf_util_get_port_number */
1208 int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */
1214 if (ci->values_num != 1) {
1215 ERROR("cf_util_get_service: The %s option requires exactly "
1221 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1222 return cf_util_get_string(ci, ret_string);
1223 if (ci->values[0].type != OCONFIG_TYPE_NUMBER) {
1224 ERROR("cf_util_get_service: The %s option requires "
1225 "exactly one string or numeric argument.",
1230 status = cf_util_get_int(ci, &port);
1233 else if ((port < 1) || (port > 65535)) {
1234 ERROR("cf_util_get_service: The port number given "
1235 "for the %s option is out of "
1241 service = malloc(6);
1242 if (service == NULL) {
1243 ERROR("cf_util_get_service: Out of memory.");
1246 snprintf(service, 6, "%i", port);
1249 *ret_string = service;
1252 } /* }}} int cf_util_get_service */
1254 int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1256 if ((ci == NULL) || (ret_value == NULL))
1259 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
1260 ERROR("cf_util_get_cdtime: The %s option requires "
1261 "exactly one numeric argument.",
1266 if (ci->values[0].value.number < 0.0) {
1267 ERROR("cf_util_get_cdtime: The numeric argument of the %s "
1268 "option must not be negative.",
1273 *ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
1276 } /* }}} int cf_util_get_cdtime */