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)
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 (i = 0; i < ci->values_num; ++i)
247 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
248 WARNING ("configfile: TypesDB: Skipping %i. argument which "
249 "is not a string.", i + 1);
253 read_types_list (ci->values[i].value.string);
256 } /* int dispatch_value_typesdb */
258 static int dispatch_value_plugindir (oconfig_item_t *ci)
260 assert (strcasecmp (ci->key, "PluginDir") == 0);
262 if (ci->values_num != 1)
264 if (ci->values[0].type != OCONFIG_TYPE_STRING)
267 plugin_set_dir (ci->values[0].value.string);
271 static int dispatch_loadplugin (oconfig_item_t *ci)
275 unsigned int flags = 0;
276 plugin_ctx_t ctx = { 0 };
277 plugin_ctx_t old_ctx;
280 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
282 if (ci->values_num != 1)
284 if (ci->values[0].type != OCONFIG_TYPE_STRING)
287 name = ci->values[0].value.string;
288 if (strcmp ("libvirt", name) == 0)
291 /* default to the global interval set before loading this plugin */
292 ctx.interval = cf_get_default_interval ();
293 ctx.flush_interval = 0;
294 ctx.flush_timeout = 0;
296 for (i = 0; i < ci->children_num; ++i)
298 oconfig_item_t *child = ci->children + i;
300 if (strcasecmp("Globals", child->key) == 0)
301 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
302 else if (strcasecmp ("Interval", child->key) == 0)
303 cf_util_get_cdtime (child, &ctx.interval);
304 else if (strcasecmp ("FlushInterval", child->key) == 0)
305 cf_util_get_cdtime (child, &ctx.flush_interval);
306 else if (strcasecmp ("FlushTimeout", child->key) == 0)
307 cf_util_get_cdtime (child, &ctx.flush_timeout);
309 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
311 child->key, ci->values[0].value.string);
315 old_ctx = plugin_set_ctx (ctx);
316 ret_val = plugin_load (name, (uint32_t) flags);
317 /* reset to the "global" context */
318 plugin_set_ctx (old_ctx);
321 } /* int dispatch_value_loadplugin */
323 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
331 buffer_free = sizeof (buffer);
333 for (i = 0; i < ci->values_num; i++)
337 if (ci->values[i].type == OCONFIG_TYPE_STRING)
338 status = ssnprintf (buffer_ptr, buffer_free, " %s",
339 ci->values[i].value.string);
340 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
341 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
342 ci->values[i].value.number);
343 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
344 status = ssnprintf (buffer_ptr, buffer_free, " %s",
345 ci->values[i].value.boolean
348 if ((status < 0) || (status >= buffer_free))
350 buffer_free -= status;
351 buffer_ptr += status;
353 /* skip the initial space */
354 buffer_ptr = buffer + 1;
356 return (cf_dispatch (plugin, ci->key, buffer_ptr));
357 } /* int dispatch_value_plugin */
359 static int dispatch_value (oconfig_item_t *ci)
364 for (i = 0; i < cf_value_map_num; i++)
365 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
367 ret = cf_value_map[i].func (ci);
371 for (i = 0; i < cf_global_options_num; i++)
372 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
374 ret = dispatch_global_option (ci);
379 } /* int dispatch_value */
381 static int dispatch_block_plugin (oconfig_item_t *ci)
386 cf_complex_callback_t *cb;
388 if (strcasecmp (ci->key, "Plugin") != 0)
390 if (ci->values_num < 1)
392 if (ci->values[0].type != OCONFIG_TYPE_STRING)
395 name = ci->values[0].value.string;
396 if (strcmp ("libvirt", name) == 0)
398 /* TODO(octo): Remove this legacy. */
399 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
400 "Your configuration is still using the old name. "
401 "Please change it to use \"virt\" as soon as possible. "
402 "This compatibility code will go away eventually.");
406 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
408 plugin_ctx_t ctx = { 0 };
409 plugin_ctx_t old_ctx;
412 /* default to the global interval set before loading this plugin */
413 ctx.interval = cf_get_default_interval ();
415 old_ctx = plugin_set_ctx (ctx);
416 status = plugin_load (name, /* flags = */ 0);
417 /* reset to the "global" context */
418 plugin_set_ctx (old_ctx);
422 ERROR ("Automatically loading plugin \"%s\" failed "
423 "with status %i.", name, status);
428 /* Check for a complex callback first */
429 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
431 if (strcasecmp (name, cb->type) == 0)
433 plugin_ctx_t old_ctx;
436 old_ctx = plugin_set_ctx (cb->ctx);
437 ret_val = (cb->callback (ci));
438 plugin_set_ctx (old_ctx);
443 /* Hm, no complex plugin found. Dispatch the values one by one */
444 for (i = 0; i < ci->children_num; i++)
446 if (ci->children[i].children == NULL)
447 dispatch_value_plugin (name, ci->children + i);
450 WARNING ("There is a `%s' block within the "
451 "configuration for the %s plugin. "
452 "The plugin either only expects "
453 "\"simple\" configuration statements "
454 "or wasn't loaded using `LoadPlugin'."
455 " Please check your configuration.",
456 ci->children[i].key, name);
464 static int dispatch_block (oconfig_item_t *ci)
466 if (strcasecmp (ci->key, "LoadPlugin") == 0)
467 return (dispatch_loadplugin (ci));
468 else if (strcasecmp (ci->key, "Plugin") == 0)
469 return (dispatch_block_plugin (ci));
470 else if (strcasecmp (ci->key, "Chain") == 0)
471 return (fc_configure (ci));
476 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
479 oconfig_item_t *temp;
482 assert (offset >= 0);
483 assert (dst->children_num > offset);
485 /* Free the memory used by the replaced child. Usually that's the
486 * `Include "blah"' statement. */
487 temp = dst->children + offset;
488 for (i = 0; i < temp->values_num; i++)
490 if (temp->values[i].type == OCONFIG_TYPE_STRING)
492 sfree (temp->values[i].value.string);
495 sfree (temp->values);
498 /* If (src->children_num == 0) the array size is decreased. If offset
499 * is _not_ the last element, (offset < (dst->children_num - 1)), then
500 * we need to move the trailing elements before resizing the array. */
501 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
503 int nmemb = dst->children_num - (offset + 1);
504 memmove (dst->children + offset, dst->children + offset + 1,
505 sizeof (oconfig_item_t) * nmemb);
508 /* Resize the memory containing the children to be big enough to hold
510 if (dst->children_num + src->children_num - 1 == 0)
512 dst->children_num = 0;
516 temp = realloc (dst->children,
517 sizeof (oconfig_item_t)
518 * (dst->children_num + src->children_num - 1));
521 ERROR ("configfile: realloc failed.");
524 dst->children = temp;
526 /* If there are children behind the include statement, and they have
527 * not yet been moved because (src->children_num == 0), then move them
528 * to the end of the list, so that the new children have room before
530 if ((src->children_num > 0)
531 && ((dst->children_num - (offset + 1)) > 0))
533 int nmemb = dst->children_num - (offset + 1);
534 int old_offset = offset + 1;
535 int new_offset = offset + src->children_num;
537 memmove (dst->children + new_offset,
538 dst->children + old_offset,
539 sizeof (oconfig_item_t) * nmemb);
542 /* Last but not least: If there are new children, copy them to the
543 * memory reserved for them. */
544 if (src->children_num > 0)
546 memcpy (dst->children + offset,
548 sizeof (oconfig_item_t) * src->children_num);
551 /* Update the number of children. */
552 dst->children_num += (src->children_num - 1);
555 } /* int cf_ci_replace_child */
557 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
559 oconfig_item_t *temp;
561 if ((src == NULL) || (src->children_num == 0))
564 temp = realloc (dst->children,
565 sizeof (oconfig_item_t)
566 * (dst->children_num + src->children_num));
569 ERROR ("configfile: realloc failed.");
572 dst->children = temp;
574 memcpy (dst->children + dst->children_num,
576 sizeof (oconfig_item_t)
577 * src->children_num);
578 dst->children_num += src->children_num;
581 } /* int cf_ci_append_children */
583 #define CF_MAX_DEPTH 8
584 static oconfig_item_t *cf_read_generic (const char *path,
585 const char *pattern, int depth);
587 static int cf_include_all (oconfig_item_t *root, int depth)
591 for (i = 0; i < root->children_num; i++)
596 char *pattern = NULL;
600 if (strcasecmp (root->children[i].key, "Include") != 0)
603 old = root->children + i;
605 if ((old->values_num != 1)
606 || (old->values[0].type != OCONFIG_TYPE_STRING))
608 ERROR ("configfile: `Include' needs exactly one string argument.");
612 for (j = 0; j < old->children_num; ++j)
614 oconfig_item_t *child = old->children + j;
616 if (strcasecmp (child->key, "Filter") == 0)
617 cf_util_get_string (child, &pattern);
619 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
623 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
629 /* Now replace the i'th child in `root' with `new'. */
630 if (cf_ci_replace_child (root, new, i) < 0) {
636 /* ... and go back to the new i'th child. */
641 } /* for (i = 0; i < root->children_num; i++) */
644 } /* int cf_include_all */
646 static oconfig_item_t *cf_read_file (const char *file,
647 const char *pattern, int depth)
649 oconfig_item_t *root;
652 assert (depth < CF_MAX_DEPTH);
654 if (pattern != NULL) {
655 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
656 char *tmp = sstrdup (file);
657 char *filename = basename (tmp);
659 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
660 DEBUG ("configfile: Not including `%s' because it "
661 "does not match pattern `%s'.",
669 ERROR ("configfile: Cannot apply pattern filter '%s' "
670 "to file '%s': functions basename() and / or "
671 "fnmatch() not available.", pattern, file);
672 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
675 root = oconfig_parse_file (file);
678 ERROR ("configfile: Cannot read file `%s'.", file);
682 status = cf_include_all (root, depth);
690 } /* oconfig_item_t *cf_read_file */
692 static int cf_compare_string (const void *p1, const void *p2)
694 return strcmp (*(const char **) p1, *(const char **) p2);
697 static oconfig_item_t *cf_read_dir (const char *dir,
698 const char *pattern, int depth)
700 oconfig_item_t *root = NULL;
703 char **filenames = NULL;
704 int filenames_num = 0;
708 assert (depth < CF_MAX_DEPTH);
714 ERROR ("configfile: opendir failed: %s",
715 sstrerror (errno, errbuf, sizeof (errbuf)));
719 root = calloc (1, sizeof (*root));
722 ERROR ("configfile: calloc failed.");
727 while ((de = readdir (dh)) != NULL)
732 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
735 status = ssnprintf (name, sizeof (name), "%s/%s",
737 if ((status < 0) || ((size_t) status >= sizeof (name)))
739 ERROR ("configfile: Not including `%s/%s' because its"
740 " name is too long.",
743 for (i = 0; i < filenames_num; ++i)
751 tmp = realloc (filenames,
752 filenames_num * sizeof (*filenames));
754 ERROR ("configfile: realloc failed.");
756 for (i = 0; i < filenames_num - 1; ++i)
764 filenames[filenames_num - 1] = sstrdup (name);
767 if (filenames == NULL)
773 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
776 for (i = 0; i < filenames_num; ++i)
778 oconfig_item_t *temp;
779 char *name = filenames[i];
781 temp = cf_read_generic (name, pattern, depth);
784 /* An error should already have been reported. */
789 cf_ci_append_children (root, temp);
790 sfree (temp->children);
799 } /* oconfig_item_t *cf_read_dir */
804 * Path is stat'ed and either cf_read_file or cf_read_dir is called
807 * There are two versions of this function: If `wordexp' exists shell wildcards
808 * will be expanded and the function will include all matches found. If
809 * `wordexp' (or, more precisely, it's header file) is not available the
810 * simpler function is used which does not do any such expansion.
813 static oconfig_item_t *cf_read_generic (const char *path,
814 const char *pattern, int depth)
816 oconfig_item_t *root = NULL;
818 const char *path_ptr;
822 if (depth >= CF_MAX_DEPTH)
824 ERROR ("configfile: Not including `%s' because the maximum "
825 "nesting depth has been reached.", path);
829 status = wordexp (path, &we, WRDE_NOCMD);
832 ERROR ("configfile: wordexp (%s) failed.", path);
836 root = calloc (1, sizeof (*root));
839 ERROR ("configfile: calloc failed.");
843 /* wordexp() might return a sorted list already. That's not
844 * documented though, so let's make sure we get what we want. */
845 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
848 for (i = 0; i < we.we_wordc; i++)
850 oconfig_item_t *temp;
853 path_ptr = we.we_wordv[i];
855 status = stat (path_ptr, &statbuf);
859 WARNING ("configfile: stat (%s) failed: %s",
861 sstrerror (errno, errbuf, sizeof (errbuf)));
865 if (S_ISREG (statbuf.st_mode))
866 temp = cf_read_file (path_ptr, pattern, depth);
867 else if (S_ISDIR (statbuf.st_mode))
868 temp = cf_read_dir (path_ptr, pattern, depth);
871 WARNING ("configfile: %s is neither a file nor a "
881 cf_ci_append_children (root, temp);
882 sfree (temp->children);
889 } /* oconfig_item_t *cf_read_generic */
890 /* #endif HAVE_WORDEXP_H */
892 #else /* if !HAVE_WORDEXP_H */
893 static oconfig_item_t *cf_read_generic (const char *path,
894 const char *pattern, int depth)
899 if (depth >= CF_MAX_DEPTH)
901 ERROR ("configfile: Not including `%s' because the maximum "
902 "nesting depth has been reached.", path);
906 status = stat (path, &statbuf);
910 ERROR ("configfile: stat (%s) failed: %s",
912 sstrerror (errno, errbuf, sizeof (errbuf)));
916 if (S_ISREG (statbuf.st_mode))
917 return (cf_read_file (path, pattern, depth));
918 else if (S_ISDIR (statbuf.st_mode))
919 return (cf_read_dir (path, pattern, depth));
921 ERROR ("configfile: %s is neither a file nor a directory.", path);
923 } /* oconfig_item_t *cf_read_generic */
924 #endif /* !HAVE_WORDEXP_H */
929 int global_option_set (const char *option, const char *value, _Bool from_cli)
933 DEBUG ("option = %s; value = %s;", option, value);
935 for (i = 0; i < cf_global_options_num; i++)
936 if (strcasecmp (cf_global_options[i].key, option) == 0)
939 if (i >= cf_global_options_num)
941 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
945 if (cf_global_options[i].from_cli && (! from_cli))
947 DEBUG ("configfile: Ignoring %s `%s' option because "
948 "it was overriden by a command-line option.",
953 sfree (cf_global_options[i].value);
956 cf_global_options[i].value = strdup (value);
958 cf_global_options[i].value = NULL;
960 cf_global_options[i].from_cli = from_cli;
965 const char *global_option_get (const char *option)
969 for (i = 0; i < cf_global_options_num; i++)
970 if (strcasecmp (cf_global_options[i].key, option) == 0)
973 if (i >= cf_global_options_num)
975 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
979 return ((cf_global_options[i].value != NULL)
980 ? cf_global_options[i].value
981 : cf_global_options[i].def);
982 } /* char *global_option_get */
984 long global_option_get_long (const char *option, long default_value)
989 str = global_option_get (option);
991 return (default_value);
994 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
996 return (default_value);
999 } /* char *global_option_get_long */
1001 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
1004 char *endptr = NULL;
1007 optstr = global_option_get (name);
1012 v = strtod (optstr, &endptr);
1013 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1018 return (DOUBLE_TO_CDTIME_T (v));
1019 } /* }}} cdtime_t global_option_get_time */
1021 cdtime_t cf_get_default_interval (void)
1023 return (global_option_get_time ("Interval",
1024 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1027 void cf_unregister (const char *type)
1029 cf_callback_t *this, *prev;
1031 for (prev = NULL, this = first_callback;
1033 prev = this, this = this->next)
1034 if (strcasecmp (this->type, type) == 0)
1037 first_callback = this->next;
1039 prev->next = this->next;
1044 } /* void cf_unregister */
1046 void cf_unregister_complex (const char *type)
1048 cf_complex_callback_t *this, *prev;
1050 for (prev = NULL, this = complex_callback_head;
1052 prev = this, this = this->next)
1053 if (strcasecmp (this->type, type) == 0)
1056 complex_callback_head = this->next;
1058 prev->next = this->next;
1064 } /* void cf_unregister */
1066 void cf_register (const char *type,
1067 int (*callback) (const char *, const char *),
1068 const char **keys, int keys_num)
1070 cf_callback_t *cf_cb;
1072 /* Remove this module from the list, if it already exists */
1073 cf_unregister (type);
1075 /* This pointer will be free'd in `cf_unregister' */
1076 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1080 cf_cb->callback = callback;
1082 cf_cb->keys_num = keys_num;
1083 cf_cb->ctx = plugin_get_ctx ();
1085 cf_cb->next = first_callback;
1086 first_callback = cf_cb;
1087 } /* void cf_register */
1089 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1091 cf_complex_callback_t *new;
1093 new = malloc (sizeof (*new));
1097 new->type = strdup (type);
1098 if (new->type == NULL)
1104 new->callback = callback;
1107 new->ctx = plugin_get_ctx ();
1109 if (complex_callback_head == NULL)
1111 complex_callback_head = new;
1115 cf_complex_callback_t *last = complex_callback_head;
1116 while (last->next != NULL)
1122 } /* int cf_register_complex */
1124 int cf_read (const char *filename)
1126 oconfig_item_t *conf;
1130 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1133 ERROR ("Unable to read config file %s.", filename);
1136 else if (conf->children_num == 0)
1138 ERROR ("Configuration file %s is empty.", filename);
1139 oconfig_free (conf);
1143 for (i = 0; i < conf->children_num; i++)
1145 if (conf->children[i].children == NULL)
1147 if (dispatch_value (conf->children + i) != 0)
1152 if (dispatch_block (conf->children + i) != 0)
1157 oconfig_free (conf);
1159 /* Read the default types.db if no `TypesDB' option was given. */
1160 if (cf_default_typesdb)
1162 if (read_types_list (PKGDATADIR"/types.db") != 0)
1169 /* Assures the config option is a string, duplicates it and returns the copy in
1170 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1172 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1176 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1178 ERROR ("cf_util_get_string: The %s option requires "
1179 "exactly one string argument.", ci->key);
1183 string = strdup (ci->values[0].value.string);
1187 if (*ret_string != NULL)
1188 sfree (*ret_string);
1189 *ret_string = string;
1192 } /* }}} int cf_util_get_string */
1194 /* Assures the config option is a string and copies it to the provided buffer.
1195 * Assures null-termination. */
1196 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1199 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1202 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1204 ERROR ("cf_util_get_string_buffer: The %s option requires "
1205 "exactly one string argument.", ci->key);
1209 strncpy (buffer, ci->values[0].value.string, buffer_size);
1210 buffer[buffer_size - 1] = 0;
1213 } /* }}} int cf_util_get_string_buffer */
1215 /* Assures the config option is a number and returns it as an int. */
1216 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1218 if ((ci == NULL) || (ret_value == NULL))
1221 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1223 ERROR ("cf_util_get_int: The %s option requires "
1224 "exactly one numeric argument.", ci->key);
1228 *ret_value = (int) ci->values[0].value.number;
1231 } /* }}} int cf_util_get_int */
1233 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1235 if ((ci == NULL) || (ret_value == NULL))
1238 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1240 ERROR ("cf_util_get_double: The %s option requires "
1241 "exactly one numeric argument.", ci->key);
1245 *ret_value = ci->values[0].value.number;
1248 } /* }}} int cf_util_get_double */
1250 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1252 if ((ci == NULL) || (ret_bool == NULL))
1255 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1257 ERROR ("cf_util_get_boolean: The %s option requires "
1258 "exactly one boolean argument.", ci->key);
1262 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1265 } /* }}} int cf_util_get_boolean */
1267 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1268 unsigned int *ret_value, unsigned int flag)
1273 if (ret_value == NULL)
1277 status = cf_util_get_boolean (ci, &b);
1287 *ret_value &= ~flag;
1291 } /* }}} int cf_util_get_flag */
1293 /* Assures that the config option is a string or a number if the correct range
1294 * of 1-65535. The string is then converted to a port number using
1295 * `service_name_to_port_number' and returned.
1296 * Returns the port number in the range [1-65535] or less than zero upon
1298 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1302 if ((ci->values_num != 1)
1303 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1304 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1306 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1307 "exactly one string argument.", ci->key);
1311 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1312 return (service_name_to_port_number (ci->values[0].value.string));
1314 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1315 tmp = (int) (ci->values[0].value.number + 0.5);
1316 if ((tmp < 1) || (tmp > 65535))
1318 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1319 "a service name or a port number. The number "
1320 "you specified, %i, is not in the valid "
1321 "range of 1-65535.",
1327 } /* }}} int cf_util_get_port_number */
1329 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1335 if (ci->values_num != 1)
1337 ERROR ("cf_util_get_service: The %s option requires exactly "
1338 "one argument.", ci->key);
1342 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1343 return (cf_util_get_string (ci, ret_string));
1344 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1346 ERROR ("cf_util_get_service: The %s option requires "
1347 "exactly one string or numeric argument.",
1352 status = cf_util_get_int (ci, &port);
1355 else if ((port < 1) || (port > 65535))
1357 ERROR ("cf_util_get_service: The port number given "
1358 "for the %s option is out of "
1359 "range (%i).", ci->key, port);
1363 service = malloc (6);
1364 if (service == NULL)
1366 ERROR ("cf_util_get_service: Out of memory.");
1369 ssnprintf (service, 6, "%i", port);
1371 sfree (*ret_string);
1372 *ret_string = service;
1375 } /* }}} int cf_util_get_service */
1377 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1379 if ((ci == NULL) || (ret_value == NULL))
1382 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1384 ERROR ("cf_util_get_cdtime: The %s option requires "
1385 "exactly one numeric argument.", ci->key);
1389 if (ci->values[0].value.number < 0.0)
1391 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1392 "option must not be negative.", ci->key);
1396 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1399 } /* }}} int cf_util_get_cdtime */