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"
34 #include "configfile.h"
35 #include "types_list.h"
36 #include "filter_chain.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
58 int (*callback) (const char *, const char *);
62 struct cf_callback *next;
65 typedef struct cf_complex_callback_s
68 int (*callback) (oconfig_item_t *);
70 struct cf_complex_callback_s *next;
71 } cf_complex_callback_t;
73 typedef struct cf_value_map_s
76 int (*func) (oconfig_item_t *);
79 typedef struct cf_global_option_s
83 _Bool from_cli; /* value set from CLI */
88 * Prototypes of callback functions
90 static int dispatch_value_typesdb (oconfig_item_t *ci);
91 static int dispatch_value_plugindir (oconfig_item_t *ci);
92 static int dispatch_loadplugin (oconfig_item_t *ci);
93 static int dispatch_block_plugin (oconfig_item_t *ci);
98 static cf_callback_t *first_callback = NULL;
99 static cf_complex_callback_t *complex_callback_head = NULL;
101 static cf_value_map_t cf_value_map[] =
103 {"TypesDB", dispatch_value_typesdb},
104 {"PluginDir", dispatch_value_plugindir},
105 {"LoadPlugin", dispatch_loadplugin},
106 {"Plugin", dispatch_block_plugin}
108 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
110 static cf_global_option_t cf_global_options[] =
112 {"BaseDir", NULL, 0, PKGLOCALSTATEDIR},
113 {"PIDFile", NULL, 0, PIDFILE},
114 {"Hostname", NULL, 0, NULL},
115 {"FQDNLookup", NULL, 0, "true"},
116 {"Interval", NULL, 0, NULL},
117 {"ReadThreads", NULL, 0, "5"},
118 {"WriteThreads", NULL, 0, "5"},
119 {"WriteQueueLimitHigh", NULL, 0, NULL},
120 {"WriteQueueLimitLow", NULL, 0, NULL},
121 {"Timeout", NULL, 0, "2"},
122 {"AutoLoadPlugin", NULL, 0, "false"},
123 {"CollectInternalStats", NULL, 0, "false"},
124 {"PreCacheChain", NULL, 0, "PreCache"},
125 {"PostCacheChain", NULL, 0, "PostCache"},
126 {"MaxReadInterval", NULL, 0, "86400"}
128 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
130 static int cf_default_typesdb = 1;
133 * Functions to handle register/unregister, search, and other plugin related
136 static cf_callback_t *cf_search (const char *type)
138 cf_callback_t *cf_cb;
143 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
144 if (strcasecmp (cf_cb->type, type) == 0)
150 static int cf_dispatch (const char *type, const char *orig_key,
151 const char *orig_value)
153 cf_callback_t *cf_cb;
154 plugin_ctx_t old_ctx;
160 if (orig_key == NULL)
163 DEBUG ("type = %s, key = %s, value = %s",
166 ESCAPE_NULL(orig_value));
168 if ((cf_cb = cf_search (type)) == NULL)
170 WARNING ("Found a configuration for the `%s' plugin, but "
171 "the plugin isn't loaded or didn't register "
172 "a configuration callback.", type);
176 if ((key = strdup (orig_key)) == NULL)
178 if ((value = strdup (orig_value)) == NULL)
186 old_ctx = plugin_set_ctx (cf_cb->ctx);
188 for (i = 0; i < cf_cb->keys_num; i++)
190 if ((cf_cb->keys[i] != NULL)
191 && (strcasecmp (cf_cb->keys[i], key) == 0))
193 ret = (*cf_cb->callback) (key, value);
198 plugin_set_ctx (old_ctx);
200 if (i >= cf_cb->keys_num)
201 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
207 } /* int cf_dispatch */
209 static int dispatch_global_option (const oconfig_item_t *ci)
211 if (ci->values_num != 1)
213 if (ci->values[0].type == OCONFIG_TYPE_STRING)
214 return (global_option_set (ci->key, ci->values[0].value.string, 0));
215 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
218 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
219 return (global_option_set (ci->key, tmp, 0));
221 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
223 if (ci->values[0].value.boolean)
224 return (global_option_set (ci->key, "true", 0));
226 return (global_option_set (ci->key, "false", 0));
230 } /* int dispatch_global_option */
232 static int dispatch_value_typesdb (oconfig_item_t *ci)
234 assert (strcasecmp (ci->key, "TypesDB") == 0);
236 cf_default_typesdb = 0;
238 if (ci->values_num < 1) {
239 ERROR ("configfile: `TypesDB' needs at least one argument.");
243 for (int i = 0; i < ci->values_num; ++i)
245 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
246 WARNING ("configfile: TypesDB: Skipping %i. argument which "
247 "is not a string.", i + 1);
251 read_types_list (ci->values[i].value.string);
254 } /* int dispatch_value_typesdb */
256 static int dispatch_value_plugindir (oconfig_item_t *ci)
258 assert (strcasecmp (ci->key, "PluginDir") == 0);
260 if (ci->values_num != 1)
262 if (ci->values[0].type != OCONFIG_TYPE_STRING)
265 plugin_set_dir (ci->values[0].value.string);
269 static int dispatch_loadplugin (oconfig_item_t *ci)
272 unsigned int flags = 0;
273 plugin_ctx_t ctx = { 0 };
274 plugin_ctx_t old_ctx;
277 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
279 if (ci->values_num != 1)
281 if (ci->values[0].type != OCONFIG_TYPE_STRING)
284 name = ci->values[0].value.string;
285 if (strcmp ("libvirt", name) == 0)
288 /* default to the global interval set before loading this plugin */
289 ctx.interval = cf_get_default_interval ();
290 ctx.flush_interval = 0;
291 ctx.flush_timeout = 0;
293 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_flag (child, &flags, PLUGIN_FLAGS_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\" "
308 child->key, ci->values[0].value.string);
312 old_ctx = plugin_set_ctx (ctx);
313 ret_val = plugin_load (name, (uint32_t) flags);
314 /* reset to the "global" context */
315 plugin_set_ctx (old_ctx);
318 } /* int dispatch_value_loadplugin */
320 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
327 buffer_free = sizeof (buffer);
329 for (int i = 0; i < ci->values_num; i++)
333 if (ci->values[i].type == OCONFIG_TYPE_STRING)
334 status = ssnprintf (buffer_ptr, buffer_free, " %s",
335 ci->values[i].value.string);
336 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
337 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
338 ci->values[i].value.number);
339 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
340 status = ssnprintf (buffer_ptr, buffer_free, " %s",
341 ci->values[i].value.boolean
344 if ((status < 0) || (status >= buffer_free))
346 buffer_free -= status;
347 buffer_ptr += status;
349 /* skip the initial space */
350 buffer_ptr = buffer + 1;
352 return (cf_dispatch (plugin, ci->key, buffer_ptr));
353 } /* int dispatch_value_plugin */
355 static int dispatch_value (oconfig_item_t *ci)
359 for (int i = 0; i < cf_value_map_num; i++)
360 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
362 ret = cf_value_map[i].func (ci);
366 for (int i = 0; i < cf_global_options_num; i++)
367 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
369 ret = dispatch_global_option (ci);
374 } /* int dispatch_value */
376 static int dispatch_block_plugin (oconfig_item_t *ci)
380 if (strcasecmp (ci->key, "Plugin") != 0)
382 if (ci->values_num < 1)
384 if (ci->values[0].type != OCONFIG_TYPE_STRING)
387 name = ci->values[0].value.string;
388 if (strcmp ("libvirt", name) == 0)
390 /* TODO(octo): Remove this legacy. */
391 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
392 "Your configuration is still using the old name. "
393 "Please change it to use \"virt\" as soon as possible. "
394 "This compatibility code will go away eventually.");
398 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
400 plugin_ctx_t ctx = { 0 };
401 plugin_ctx_t old_ctx;
404 /* default to the global interval set before loading this plugin */
405 ctx.interval = cf_get_default_interval ();
407 old_ctx = plugin_set_ctx (ctx);
408 status = plugin_load (name, /* flags = */ 0);
409 /* reset to the "global" context */
410 plugin_set_ctx (old_ctx);
414 ERROR ("Automatically loading plugin \"%s\" failed "
415 "with status %i.", name, status);
420 /* Check for a complex callback first */
421 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL; cb = cb->next)
423 if (strcasecmp (name, cb->type) == 0)
425 plugin_ctx_t old_ctx;
428 old_ctx = plugin_set_ctx (cb->ctx);
429 ret_val = (cb->callback (ci));
430 plugin_set_ctx (old_ctx);
435 /* Hm, no complex plugin found. Dispatch the values one by one */
436 for (int i = 0; i < ci->children_num; i++)
438 if (ci->children[i].children == NULL)
439 dispatch_value_plugin (name, ci->children + i);
442 WARNING ("There is a `%s' block within the "
443 "configuration for the %s plugin. "
444 "The plugin either only expects "
445 "\"simple\" configuration statements "
446 "or wasn't loaded using `LoadPlugin'."
447 " Please check your configuration.",
448 ci->children[i].key, name);
456 static int dispatch_block (oconfig_item_t *ci)
458 if (strcasecmp (ci->key, "LoadPlugin") == 0)
459 return (dispatch_loadplugin (ci));
460 else if (strcasecmp (ci->key, "Plugin") == 0)
461 return (dispatch_block_plugin (ci));
462 else if (strcasecmp (ci->key, "Chain") == 0)
463 return (fc_configure (ci));
468 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
471 oconfig_item_t *temp;
473 assert (offset >= 0);
474 assert (dst->children_num > offset);
476 /* Free the memory used by the replaced child. Usually that's the
477 * `Include "blah"' statement. */
478 temp = dst->children + offset;
479 for (int i = 0; i < temp->values_num; i++)
481 if (temp->values[i].type == OCONFIG_TYPE_STRING)
483 sfree (temp->values[i].value.string);
486 sfree (temp->values);
489 /* If (src->children_num == 0) the array size is decreased. If offset
490 * is _not_ the last element, (offset < (dst->children_num - 1)), then
491 * we need to move the trailing elements before resizing the array. */
492 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
494 int nmemb = dst->children_num - (offset + 1);
495 memmove (dst->children + offset, dst->children + offset + 1,
496 sizeof (oconfig_item_t) * nmemb);
499 /* Resize the memory containing the children to be big enough to hold
501 if (dst->children_num + src->children_num - 1 == 0)
503 dst->children_num = 0;
507 temp = realloc (dst->children,
508 sizeof (oconfig_item_t)
509 * (dst->children_num + src->children_num - 1));
512 ERROR ("configfile: realloc failed.");
515 dst->children = temp;
517 /* If there are children behind the include statement, and they have
518 * not yet been moved because (src->children_num == 0), then move them
519 * to the end of the list, so that the new children have room before
521 if ((src->children_num > 0)
522 && ((dst->children_num - (offset + 1)) > 0))
524 int nmemb = dst->children_num - (offset + 1);
525 int old_offset = offset + 1;
526 int new_offset = offset + src->children_num;
528 memmove (dst->children + new_offset,
529 dst->children + old_offset,
530 sizeof (oconfig_item_t) * nmemb);
533 /* Last but not least: If there are new children, copy them to the
534 * memory reserved for them. */
535 if (src->children_num > 0)
537 memcpy (dst->children + offset,
539 sizeof (oconfig_item_t) * src->children_num);
542 /* Update the number of children. */
543 dst->children_num += (src->children_num - 1);
546 } /* int cf_ci_replace_child */
548 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
550 oconfig_item_t *temp;
552 if ((src == NULL) || (src->children_num == 0))
555 temp = realloc (dst->children,
556 sizeof (oconfig_item_t)
557 * (dst->children_num + src->children_num));
560 ERROR ("configfile: realloc failed.");
563 dst->children = temp;
565 memcpy (dst->children + dst->children_num,
567 sizeof (oconfig_item_t)
568 * src->children_num);
569 dst->children_num += src->children_num;
572 } /* int cf_ci_append_children */
574 #define CF_MAX_DEPTH 8
575 static oconfig_item_t *cf_read_generic (const char *path,
576 const char *pattern, int depth);
578 static int cf_include_all (oconfig_item_t *root, int depth)
580 for (int i = 0; i < root->children_num; i++)
585 char *pattern = NULL;
587 if (strcasecmp (root->children[i].key, "Include") != 0)
590 old = root->children + i;
592 if ((old->values_num != 1)
593 || (old->values[0].type != OCONFIG_TYPE_STRING))
595 ERROR ("configfile: `Include' needs exactly one string argument.");
599 for (int j = 0; j < old->children_num; ++j)
601 oconfig_item_t *child = old->children + j;
603 if (strcasecmp (child->key, "Filter") == 0)
604 cf_util_get_string (child, &pattern);
606 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
610 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
616 /* Now replace the i'th child in `root' with `new'. */
617 if (cf_ci_replace_child (root, new, i) < 0) {
623 /* ... and go back to the new i'th child. */
628 } /* for (i = 0; i < root->children_num; i++) */
631 } /* int cf_include_all */
633 static oconfig_item_t *cf_read_file (const char *file,
634 const char *pattern, int depth)
636 oconfig_item_t *root;
639 assert (depth < CF_MAX_DEPTH);
641 if (pattern != NULL) {
642 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
643 char *tmp = sstrdup (file);
644 char *filename = basename (tmp);
646 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
647 DEBUG ("configfile: Not including `%s' because it "
648 "does not match pattern `%s'.",
656 ERROR ("configfile: Cannot apply pattern filter '%s' "
657 "to file '%s': functions basename() and / or "
658 "fnmatch() not available.", pattern, file);
659 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
662 root = oconfig_parse_file (file);
665 ERROR ("configfile: Cannot read file `%s'.", file);
669 status = cf_include_all (root, depth);
677 } /* oconfig_item_t *cf_read_file */
679 static int cf_compare_string (const void *p1, const void *p2)
681 return strcmp (*(const char **) p1, *(const char **) p2);
684 static oconfig_item_t *cf_read_dir (const char *dir,
685 const char *pattern, int depth)
687 oconfig_item_t *root = NULL;
690 char **filenames = NULL;
691 int filenames_num = 0;
694 assert (depth < CF_MAX_DEPTH);
700 ERROR ("configfile: opendir failed: %s",
701 sstrerror (errno, errbuf, sizeof (errbuf)));
705 root = calloc (1, sizeof (*root));
708 ERROR ("configfile: calloc failed.");
713 while ((de = readdir (dh)) != NULL)
718 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
721 status = ssnprintf (name, sizeof (name), "%s/%s",
723 if ((status < 0) || ((size_t) status >= sizeof (name)))
725 ERROR ("configfile: Not including `%s/%s' because its"
726 " name is too long.",
729 for (int i = 0; i < filenames_num; ++i)
737 tmp = realloc (filenames,
738 filenames_num * sizeof (*filenames));
740 ERROR ("configfile: realloc failed.");
742 for (int i = 0; i < filenames_num - 1; ++i)
750 filenames[filenames_num - 1] = sstrdup (name);
753 if (filenames == NULL)
759 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
762 for (int i = 0; i < filenames_num; ++i)
764 oconfig_item_t *temp;
765 char *name = filenames[i];
767 temp = cf_read_generic (name, pattern, depth);
770 /* An error should already have been reported. */
775 cf_ci_append_children (root, temp);
776 sfree (temp->children);
785 } /* oconfig_item_t *cf_read_dir */
790 * Path is stat'ed and either cf_read_file or cf_read_dir is called
793 * There are two versions of this function: If `wordexp' exists shell wildcards
794 * will be expanded and the function will include all matches found. If
795 * `wordexp' (or, more precisely, its header file) is not available the
796 * simpler function is used which does not do any such expansion.
799 static oconfig_item_t *cf_read_generic (const char *path,
800 const char *pattern, int depth)
802 oconfig_item_t *root = NULL;
804 const char *path_ptr;
807 if (depth >= CF_MAX_DEPTH)
809 ERROR ("configfile: Not including `%s' because the maximum "
810 "nesting depth has been reached.", path);
814 status = wordexp (path, &we, WRDE_NOCMD);
817 ERROR ("configfile: wordexp (%s) failed.", path);
821 root = calloc (1, sizeof (*root));
824 ERROR ("configfile: calloc failed.");
828 /* wordexp() might return a sorted list already. That's not
829 * documented though, so let's make sure we get what we want. */
830 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
833 for (size_t i = 0; i < we.we_wordc; i++)
835 oconfig_item_t *temp;
838 path_ptr = we.we_wordv[i];
840 status = stat (path_ptr, &statbuf);
844 WARNING ("configfile: stat (%s) failed: %s",
846 sstrerror (errno, errbuf, sizeof (errbuf)));
850 if (S_ISREG (statbuf.st_mode))
851 temp = cf_read_file (path_ptr, pattern, depth);
852 else if (S_ISDIR (statbuf.st_mode))
853 temp = cf_read_dir (path_ptr, pattern, depth);
856 WARNING ("configfile: %s is neither a file nor a "
866 cf_ci_append_children (root, temp);
867 sfree (temp->children);
874 } /* oconfig_item_t *cf_read_generic */
875 /* #endif HAVE_WORDEXP_H */
877 #else /* if !HAVE_WORDEXP_H */
878 static oconfig_item_t *cf_read_generic (const char *path,
879 const char *pattern, int depth)
884 if (depth >= CF_MAX_DEPTH)
886 ERROR ("configfile: Not including `%s' because the maximum "
887 "nesting depth has been reached.", path);
891 status = stat (path, &statbuf);
895 ERROR ("configfile: stat (%s) failed: %s",
897 sstrerror (errno, errbuf, sizeof (errbuf)));
901 if (S_ISREG (statbuf.st_mode))
902 return (cf_read_file (path, pattern, depth));
903 else if (S_ISDIR (statbuf.st_mode))
904 return (cf_read_dir (path, pattern, depth));
906 ERROR ("configfile: %s is neither a file nor a directory.", path);
908 } /* oconfig_item_t *cf_read_generic */
909 #endif /* !HAVE_WORDEXP_H */
914 int global_option_set (const char *option, const char *value, _Bool from_cli)
917 DEBUG ("option = %s; value = %s;", option, value);
919 for (i = 0; i < cf_global_options_num; i++)
920 if (strcasecmp (cf_global_options[i].key, option) == 0)
923 if (i >= cf_global_options_num)
925 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
929 if (cf_global_options[i].from_cli && (! from_cli))
931 DEBUG ("configfile: Ignoring %s `%s' option because "
932 "it was overriden by a command-line option.",
937 sfree (cf_global_options[i].value);
940 cf_global_options[i].value = strdup (value);
942 cf_global_options[i].value = NULL;
944 cf_global_options[i].from_cli = from_cli;
949 const char *global_option_get (const char *option)
952 for (i = 0; i < cf_global_options_num; i++)
953 if (strcasecmp (cf_global_options[i].key, option) == 0)
956 if (i >= cf_global_options_num)
958 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
962 return ((cf_global_options[i].value != NULL)
963 ? cf_global_options[i].value
964 : cf_global_options[i].def);
965 } /* char *global_option_get */
967 long global_option_get_long (const char *option, long default_value)
972 str = global_option_get (option);
974 return (default_value);
977 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
979 return (default_value);
982 } /* char *global_option_get_long */
984 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
990 optstr = global_option_get (name);
995 v = strtod (optstr, &endptr);
996 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1001 return (DOUBLE_TO_CDTIME_T (v));
1002 } /* }}} cdtime_t global_option_get_time */
1004 cdtime_t cf_get_default_interval (void)
1006 return (global_option_get_time ("Interval",
1007 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1010 void cf_unregister (const char *type)
1012 for (cf_callback_t *prev = NULL, *this = first_callback;
1014 prev = this, this = this->next)
1015 if (strcasecmp (this->type, type) == 0)
1018 first_callback = this->next;
1020 prev->next = this->next;
1025 } /* void cf_unregister */
1027 void cf_unregister_complex (const char *type)
1029 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
1031 prev = this, this = this->next)
1032 if (strcasecmp (this->type, type) == 0)
1035 complex_callback_head = this->next;
1037 prev->next = this->next;
1043 } /* void cf_unregister */
1045 void cf_register (const char *type,
1046 int (*callback) (const char *, const char *),
1047 const char **keys, int keys_num)
1049 cf_callback_t *cf_cb;
1051 /* Remove this module from the list, if it already exists */
1052 cf_unregister (type);
1054 /* This pointer will be free'd in `cf_unregister' */
1055 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1059 cf_cb->callback = callback;
1061 cf_cb->keys_num = keys_num;
1062 cf_cb->ctx = plugin_get_ctx ();
1064 cf_cb->next = first_callback;
1065 first_callback = cf_cb;
1066 } /* void cf_register */
1068 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1070 cf_complex_callback_t *new;
1072 new = malloc (sizeof (*new));
1076 new->type = strdup (type);
1077 if (new->type == NULL)
1083 new->callback = callback;
1086 new->ctx = plugin_get_ctx ();
1088 if (complex_callback_head == NULL)
1090 complex_callback_head = new;
1094 cf_complex_callback_t *last = complex_callback_head;
1095 while (last->next != NULL)
1101 } /* int cf_register_complex */
1103 int cf_read (const char *filename)
1105 oconfig_item_t *conf;
1108 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1111 ERROR ("Unable to read config file %s.", filename);
1114 else if (conf->children_num == 0)
1116 ERROR ("Configuration file %s is empty.", filename);
1117 oconfig_free (conf);
1121 for (int i = 0; i < conf->children_num; i++)
1123 if (conf->children[i].children == NULL)
1125 if (dispatch_value (conf->children + i) != 0)
1130 if (dispatch_block (conf->children + i) != 0)
1135 oconfig_free (conf);
1137 /* Read the default types.db if no `TypesDB' option was given. */
1138 if (cf_default_typesdb)
1140 if (read_types_list (PKGDATADIR"/types.db") != 0)
1147 /* Assures the config option is a string, duplicates it and returns the copy in
1148 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1150 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1154 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1156 ERROR ("cf_util_get_string: The %s option requires "
1157 "exactly one string argument.", ci->key);
1161 string = strdup (ci->values[0].value.string);
1165 if (*ret_string != NULL)
1166 sfree (*ret_string);
1167 *ret_string = string;
1170 } /* }}} int cf_util_get_string */
1172 /* Assures the config option is a string and copies it to the provided buffer.
1173 * Assures null-termination. */
1174 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1177 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1180 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1182 ERROR ("cf_util_get_string_buffer: The %s option requires "
1183 "exactly one string argument.", ci->key);
1187 strncpy (buffer, ci->values[0].value.string, buffer_size);
1188 buffer[buffer_size - 1] = 0;
1191 } /* }}} int cf_util_get_string_buffer */
1193 /* Assures the config option is a number and returns it as an int. */
1194 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1196 if ((ci == NULL) || (ret_value == NULL))
1199 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1201 ERROR ("cf_util_get_int: The %s option requires "
1202 "exactly one numeric argument.", ci->key);
1206 *ret_value = (int) ci->values[0].value.number;
1209 } /* }}} int cf_util_get_int */
1211 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1213 if ((ci == NULL) || (ret_value == NULL))
1216 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1218 ERROR ("cf_util_get_double: The %s option requires "
1219 "exactly one numeric argument.", ci->key);
1223 *ret_value = ci->values[0].value.number;
1226 } /* }}} int cf_util_get_double */
1228 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1230 if ((ci == NULL) || (ret_bool == NULL))
1233 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1235 ERROR ("cf_util_get_boolean: The %s option requires "
1236 "exactly one boolean argument.", ci->key);
1240 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1243 } /* }}} int cf_util_get_boolean */
1245 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1246 unsigned int *ret_value, unsigned int flag)
1251 if (ret_value == NULL)
1255 status = cf_util_get_boolean (ci, &b);
1265 *ret_value &= ~flag;
1269 } /* }}} int cf_util_get_flag */
1271 /* Assures that the config option is a string or a number if the correct range
1272 * of 1-65535. The string is then converted to a port number using
1273 * `service_name_to_port_number' and returned.
1274 * Returns the port number in the range [1-65535] or less than zero upon
1276 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1280 if ((ci->values_num != 1)
1281 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1282 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1284 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1285 "exactly one string argument.", ci->key);
1289 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1290 return (service_name_to_port_number (ci->values[0].value.string));
1292 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1293 tmp = (int) (ci->values[0].value.number + 0.5);
1294 if ((tmp < 1) || (tmp > 65535))
1296 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1297 "a service name or a port number. The number "
1298 "you specified, %i, is not in the valid "
1299 "range of 1-65535.",
1305 } /* }}} int cf_util_get_port_number */
1307 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1313 if (ci->values_num != 1)
1315 ERROR ("cf_util_get_service: The %s option requires exactly "
1316 "one argument.", ci->key);
1320 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1321 return (cf_util_get_string (ci, ret_string));
1322 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1324 ERROR ("cf_util_get_service: The %s option requires "
1325 "exactly one string or numeric argument.",
1330 status = cf_util_get_int (ci, &port);
1333 else if ((port < 1) || (port > 65535))
1335 ERROR ("cf_util_get_service: The port number given "
1336 "for the %s option is out of "
1337 "range (%i).", ci->key, port);
1341 service = malloc (6);
1342 if (service == NULL)
1344 ERROR ("cf_util_get_service: Out of memory.");
1347 ssnprintf (service, 6, "%i", port);
1349 sfree (*ret_string);
1350 *ret_string = service;
1353 } /* }}} int cf_util_get_service */
1355 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1357 if ((ci == NULL) || (ret_value == NULL))
1360 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1362 ERROR ("cf_util_get_cdtime: The %s option requires "
1363 "exactly one numeric argument.", ci->key);
1367 if (ci->values[0].value.number < 0.0)
1369 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1370 "option must not be negative.", ci->key);
1374 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1377 } /* }}} int cf_util_get_cdtime */