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;
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 memset (&ctx, 0, sizeof (ctx));
293 ctx.interval = cf_get_default_interval ();
294 ctx.flush_interval = 0;
295 ctx.flush_timeout = 0;
297 for (i = 0; i < ci->children_num; ++i)
299 oconfig_item_t *child = ci->children + i;
301 if (strcasecmp("Globals", child->key) == 0)
302 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
303 else if (strcasecmp ("Interval", child->key) == 0)
304 cf_util_get_cdtime (child, &ctx.interval);
305 else if (strcasecmp ("FlushInterval", child->key) == 0)
306 cf_util_get_cdtime (child, &ctx.flush_interval);
307 else if (strcasecmp ("FlushTimeout", child->key) == 0)
308 cf_util_get_cdtime (child, &ctx.flush_timeout);
310 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
312 child->key, ci->values[0].value.string);
316 old_ctx = plugin_set_ctx (ctx);
317 ret_val = plugin_load (name, (uint32_t) flags);
318 /* reset to the "global" context */
319 plugin_set_ctx (old_ctx);
322 } /* int dispatch_value_loadplugin */
324 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
332 buffer_free = sizeof (buffer);
334 for (i = 0; i < ci->values_num; i++)
338 if (ci->values[i].type == OCONFIG_TYPE_STRING)
339 status = ssnprintf (buffer_ptr, buffer_free, " %s",
340 ci->values[i].value.string);
341 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
342 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
343 ci->values[i].value.number);
344 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
345 status = ssnprintf (buffer_ptr, buffer_free, " %s",
346 ci->values[i].value.boolean
349 if ((status < 0) || (status >= buffer_free))
351 buffer_free -= status;
352 buffer_ptr += status;
354 /* skip the initial space */
355 buffer_ptr = buffer + 1;
357 return (cf_dispatch (plugin, ci->key, buffer_ptr));
358 } /* int dispatch_value_plugin */
360 static int dispatch_value (oconfig_item_t *ci)
365 for (i = 0; i < cf_value_map_num; i++)
366 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
368 ret = cf_value_map[i].func (ci);
372 for (i = 0; i < cf_global_options_num; i++)
373 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
375 ret = dispatch_global_option (ci);
380 } /* int dispatch_value */
382 static int dispatch_block_plugin (oconfig_item_t *ci)
387 cf_complex_callback_t *cb;
389 if (strcasecmp (ci->key, "Plugin") != 0)
391 if (ci->values_num < 1)
393 if (ci->values[0].type != OCONFIG_TYPE_STRING)
396 name = ci->values[0].value.string;
397 if (strcmp ("libvirt", name) == 0)
399 /* TODO(octo): Remove this legacy. */
400 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
401 "Your configuration is still using the old name. "
402 "Please change it to use \"virt\" as soon as possible. "
403 "This compatibility code will go away eventually.");
407 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
410 plugin_ctx_t old_ctx;
413 /* default to the global interval set before loading this plugin */
414 memset (&ctx, 0, sizeof (ctx));
415 ctx.interval = cf_get_default_interval ();
417 old_ctx = plugin_set_ctx (ctx);
418 status = plugin_load (name, /* flags = */ 0);
419 /* reset to the "global" context */
420 plugin_set_ctx (old_ctx);
424 ERROR ("Automatically loading plugin \"%s\" failed "
425 "with status %i.", name, status);
430 /* Check for a complex callback first */
431 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
433 if (strcasecmp (name, cb->type) == 0)
435 plugin_ctx_t old_ctx;
438 old_ctx = plugin_set_ctx (cb->ctx);
439 ret_val = (cb->callback (ci));
440 plugin_set_ctx (old_ctx);
445 /* Hm, no complex plugin found. Dispatch the values one by one */
446 for (i = 0; i < ci->children_num; i++)
448 if (ci->children[i].children == NULL)
449 dispatch_value_plugin (name, ci->children + i);
452 WARNING ("There is a `%s' block within the "
453 "configuration for the %s plugin. "
454 "The plugin either only expects "
455 "\"simple\" configuration statements "
456 "or wasn't loaded using `LoadPlugin'."
457 " Please check your configuration.",
458 ci->children[i].key, name);
466 static int dispatch_block (oconfig_item_t *ci)
468 if (strcasecmp (ci->key, "LoadPlugin") == 0)
469 return (dispatch_loadplugin (ci));
470 else if (strcasecmp (ci->key, "Plugin") == 0)
471 return (dispatch_block_plugin (ci));
472 else if (strcasecmp (ci->key, "Chain") == 0)
473 return (fc_configure (ci));
478 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
481 oconfig_item_t *temp;
484 assert (offset >= 0);
485 assert (dst->children_num > offset);
487 /* Free the memory used by the replaced child. Usually that's the
488 * `Include "blah"' statement. */
489 temp = dst->children + offset;
490 for (i = 0; i < temp->values_num; i++)
492 if (temp->values[i].type == OCONFIG_TYPE_STRING)
494 sfree (temp->values[i].value.string);
497 sfree (temp->values);
500 /* If (src->children_num == 0) the array size is decreased. If offset
501 * is _not_ the last element, (offset < (dst->children_num - 1)), then
502 * we need to move the trailing elements before resizing the array. */
503 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
505 int nmemb = dst->children_num - (offset + 1);
506 memmove (dst->children + offset, dst->children + offset + 1,
507 sizeof (oconfig_item_t) * nmemb);
510 /* Resize the memory containing the children to be big enough to hold
512 if (dst->children_num + src->children_num - 1 == 0)
514 dst->children_num = 0;
518 temp = realloc (dst->children,
519 sizeof (oconfig_item_t)
520 * (dst->children_num + src->children_num - 1));
523 ERROR ("configfile: realloc failed.");
526 dst->children = temp;
528 /* If there are children behind the include statement, and they have
529 * not yet been moved because (src->children_num == 0), then move them
530 * to the end of the list, so that the new children have room before
532 if ((src->children_num > 0)
533 && ((dst->children_num - (offset + 1)) > 0))
535 int nmemb = dst->children_num - (offset + 1);
536 int old_offset = offset + 1;
537 int new_offset = offset + src->children_num;
539 memmove (dst->children + new_offset,
540 dst->children + old_offset,
541 sizeof (oconfig_item_t) * nmemb);
544 /* Last but not least: If there are new children, copy them to the
545 * memory reserved for them. */
546 if (src->children_num > 0)
548 memcpy (dst->children + offset,
550 sizeof (oconfig_item_t) * src->children_num);
553 /* Update the number of children. */
554 dst->children_num += (src->children_num - 1);
557 } /* int cf_ci_replace_child */
559 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
561 oconfig_item_t *temp;
563 if ((src == NULL) || (src->children_num == 0))
566 temp = realloc (dst->children,
567 sizeof (oconfig_item_t)
568 * (dst->children_num + src->children_num));
571 ERROR ("configfile: realloc failed.");
574 dst->children = temp;
576 memcpy (dst->children + dst->children_num,
578 sizeof (oconfig_item_t)
579 * src->children_num);
580 dst->children_num += src->children_num;
583 } /* int cf_ci_append_children */
585 #define CF_MAX_DEPTH 8
586 static oconfig_item_t *cf_read_generic (const char *path,
587 const char *pattern, int depth);
589 static int cf_include_all (oconfig_item_t *root, int depth)
593 for (i = 0; i < root->children_num; i++)
598 char *pattern = NULL;
602 if (strcasecmp (root->children[i].key, "Include") != 0)
605 old = root->children + i;
607 if ((old->values_num != 1)
608 || (old->values[0].type != OCONFIG_TYPE_STRING))
610 ERROR ("configfile: `Include' needs exactly one string argument.");
614 for (j = 0; j < old->children_num; ++j)
616 oconfig_item_t *child = old->children + j;
618 if (strcasecmp (child->key, "Filter") == 0)
619 cf_util_get_string (child, &pattern);
621 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
625 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
631 /* Now replace the i'th child in `root' with `new'. */
632 if (cf_ci_replace_child (root, new, i) < 0) {
638 /* ... and go back to the new i'th child. */
643 } /* for (i = 0; i < root->children_num; i++) */
646 } /* int cf_include_all */
648 static oconfig_item_t *cf_read_file (const char *file,
649 const char *pattern, int depth)
651 oconfig_item_t *root;
654 assert (depth < CF_MAX_DEPTH);
656 if (pattern != NULL) {
657 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
658 char *tmp = sstrdup (file);
659 char *filename = basename (tmp);
661 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
662 DEBUG ("configfile: Not including `%s' because it "
663 "does not match pattern `%s'.",
671 ERROR ("configfile: Cannot apply pattern filter '%s' "
672 "to file '%s': functions basename() and / or "
673 "fnmatch() not available.", pattern, file);
674 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
677 root = oconfig_parse_file (file);
680 ERROR ("configfile: Cannot read file `%s'.", file);
684 status = cf_include_all (root, depth);
692 } /* oconfig_item_t *cf_read_file */
694 static int cf_compare_string (const void *p1, const void *p2)
696 return strcmp (*(const char **) p1, *(const char **) p2);
699 static oconfig_item_t *cf_read_dir (const char *dir,
700 const char *pattern, int depth)
702 oconfig_item_t *root = NULL;
705 char **filenames = NULL;
706 int filenames_num = 0;
710 assert (depth < CF_MAX_DEPTH);
716 ERROR ("configfile: opendir failed: %s",
717 sstrerror (errno, errbuf, sizeof (errbuf)));
721 root = calloc (1, sizeof (*root));
724 ERROR ("configfile: calloc failed.");
729 while ((de = readdir (dh)) != NULL)
734 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
737 status = ssnprintf (name, sizeof (name), "%s/%s",
739 if ((status < 0) || ((size_t) status >= sizeof (name)))
741 ERROR ("configfile: Not including `%s/%s' because its"
742 " name is too long.",
745 for (i = 0; i < filenames_num; ++i)
753 tmp = realloc (filenames,
754 filenames_num * sizeof (*filenames));
756 ERROR ("configfile: realloc failed.");
758 for (i = 0; i < filenames_num - 1; ++i)
766 filenames[filenames_num - 1] = sstrdup (name);
769 if (filenames == NULL)
775 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
778 for (i = 0; i < filenames_num; ++i)
780 oconfig_item_t *temp;
781 char *name = filenames[i];
783 temp = cf_read_generic (name, pattern, depth);
786 /* An error should already have been reported. */
791 cf_ci_append_children (root, temp);
792 sfree (temp->children);
801 } /* oconfig_item_t *cf_read_dir */
806 * Path is stat'ed and either cf_read_file or cf_read_dir is called
809 * There are two versions of this function: If `wordexp' exists shell wildcards
810 * will be expanded and the function will include all matches found. If
811 * `wordexp' (or, more precisely, it's header file) is not available the
812 * simpler function is used which does not do any such expansion.
815 static oconfig_item_t *cf_read_generic (const char *path,
816 const char *pattern, int depth)
818 oconfig_item_t *root = NULL;
820 const char *path_ptr;
824 if (depth >= CF_MAX_DEPTH)
826 ERROR ("configfile: Not including `%s' because the maximum "
827 "nesting depth has been reached.", path);
831 status = wordexp (path, &we, WRDE_NOCMD);
834 ERROR ("configfile: wordexp (%s) failed.", path);
838 root = calloc (1, sizeof (*root));
841 ERROR ("configfile: calloc failed.");
845 /* wordexp() might return a sorted list already. That's not
846 * documented though, so let's make sure we get what we want. */
847 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
850 for (i = 0; i < we.we_wordc; i++)
852 oconfig_item_t *temp;
855 path_ptr = we.we_wordv[i];
857 status = stat (path_ptr, &statbuf);
861 WARNING ("configfile: stat (%s) failed: %s",
863 sstrerror (errno, errbuf, sizeof (errbuf)));
867 if (S_ISREG (statbuf.st_mode))
868 temp = cf_read_file (path_ptr, pattern, depth);
869 else if (S_ISDIR (statbuf.st_mode))
870 temp = cf_read_dir (path_ptr, pattern, depth);
873 WARNING ("configfile: %s is neither a file nor a "
883 cf_ci_append_children (root, temp);
884 sfree (temp->children);
891 } /* oconfig_item_t *cf_read_generic */
892 /* #endif HAVE_WORDEXP_H */
894 #else /* if !HAVE_WORDEXP_H */
895 static oconfig_item_t *cf_read_generic (const char *path,
896 const char *pattern, int depth)
901 if (depth >= CF_MAX_DEPTH)
903 ERROR ("configfile: Not including `%s' because the maximum "
904 "nesting depth has been reached.", path);
908 status = stat (path, &statbuf);
912 ERROR ("configfile: stat (%s) failed: %s",
914 sstrerror (errno, errbuf, sizeof (errbuf)));
918 if (S_ISREG (statbuf.st_mode))
919 return (cf_read_file (path, pattern, depth));
920 else if (S_ISDIR (statbuf.st_mode))
921 return (cf_read_dir (path, pattern, depth));
923 ERROR ("configfile: %s is neither a file nor a directory.", path);
925 } /* oconfig_item_t *cf_read_generic */
926 #endif /* !HAVE_WORDEXP_H */
931 int global_option_set (const char *option, const char *value, _Bool from_cli)
935 DEBUG ("option = %s; value = %s;", option, value);
937 for (i = 0; i < cf_global_options_num; i++)
938 if (strcasecmp (cf_global_options[i].key, option) == 0)
941 if (i >= cf_global_options_num)
943 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
947 if (cf_global_options[i].from_cli && (! from_cli))
949 DEBUG ("configfile: Ignoring %s `%s' option because "
950 "it was overriden by a command-line option.",
955 sfree (cf_global_options[i].value);
958 cf_global_options[i].value = strdup (value);
960 cf_global_options[i].value = NULL;
962 cf_global_options[i].from_cli = from_cli;
967 const char *global_option_get (const char *option)
971 for (i = 0; i < cf_global_options_num; i++)
972 if (strcasecmp (cf_global_options[i].key, option) == 0)
975 if (i >= cf_global_options_num)
977 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
981 return ((cf_global_options[i].value != NULL)
982 ? cf_global_options[i].value
983 : cf_global_options[i].def);
984 } /* char *global_option_get */
986 long global_option_get_long (const char *option, long default_value)
991 str = global_option_get (option);
993 return (default_value);
996 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
998 return (default_value);
1001 } /* char *global_option_get_long */
1003 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
1006 char *endptr = NULL;
1009 optstr = global_option_get (name);
1014 v = strtod (optstr, &endptr);
1015 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1020 return (DOUBLE_TO_CDTIME_T (v));
1021 } /* }}} cdtime_t global_option_get_time */
1023 cdtime_t cf_get_default_interval (void)
1025 return (global_option_get_time ("Interval",
1026 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1029 void cf_unregister (const char *type)
1031 cf_callback_t *this, *prev;
1033 for (prev = NULL, this = first_callback;
1035 prev = this, this = this->next)
1036 if (strcasecmp (this->type, type) == 0)
1039 first_callback = this->next;
1041 prev->next = this->next;
1046 } /* void cf_unregister */
1048 void cf_unregister_complex (const char *type)
1050 cf_complex_callback_t *this, *prev;
1052 for (prev = NULL, this = complex_callback_head;
1054 prev = this, this = this->next)
1055 if (strcasecmp (this->type, type) == 0)
1058 complex_callback_head = this->next;
1060 prev->next = this->next;
1066 } /* void cf_unregister */
1068 void cf_register (const char *type,
1069 int (*callback) (const char *, const char *),
1070 const char **keys, int keys_num)
1072 cf_callback_t *cf_cb;
1074 /* Remove this module from the list, if it already exists */
1075 cf_unregister (type);
1077 /* This pointer will be free'd in `cf_unregister' */
1078 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1082 cf_cb->callback = callback;
1084 cf_cb->keys_num = keys_num;
1085 cf_cb->ctx = plugin_get_ctx ();
1087 cf_cb->next = first_callback;
1088 first_callback = cf_cb;
1089 } /* void cf_register */
1091 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1093 cf_complex_callback_t *new;
1095 new = malloc (sizeof (*new));
1099 new->type = strdup (type);
1100 if (new->type == NULL)
1106 new->callback = callback;
1109 new->ctx = plugin_get_ctx ();
1111 if (complex_callback_head == NULL)
1113 complex_callback_head = new;
1117 cf_complex_callback_t *last = complex_callback_head;
1118 while (last->next != NULL)
1124 } /* int cf_register_complex */
1126 int cf_read (const char *filename)
1128 oconfig_item_t *conf;
1132 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1135 ERROR ("Unable to read config file %s.", filename);
1138 else if (conf->children_num == 0)
1140 ERROR ("Configuration file %s is empty.", filename);
1141 oconfig_free (conf);
1145 for (i = 0; i < conf->children_num; i++)
1147 if (conf->children[i].children == NULL)
1149 if (dispatch_value (conf->children + i) != 0)
1154 if (dispatch_block (conf->children + i) != 0)
1159 oconfig_free (conf);
1161 /* Read the default types.db if no `TypesDB' option was given. */
1162 if (cf_default_typesdb)
1164 if (read_types_list (PKGDATADIR"/types.db") != 0)
1171 /* Assures the config option is a string, duplicates it and returns the copy in
1172 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1174 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1178 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1180 ERROR ("cf_util_get_string: The %s option requires "
1181 "exactly one string argument.", ci->key);
1185 string = strdup (ci->values[0].value.string);
1189 if (*ret_string != NULL)
1190 sfree (*ret_string);
1191 *ret_string = string;
1194 } /* }}} int cf_util_get_string */
1196 /* Assures the config option is a string and copies it to the provided buffer.
1197 * Assures null-termination. */
1198 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1201 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1204 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1206 ERROR ("cf_util_get_string_buffer: The %s option requires "
1207 "exactly one string argument.", ci->key);
1211 strncpy (buffer, ci->values[0].value.string, buffer_size);
1212 buffer[buffer_size - 1] = 0;
1215 } /* }}} int cf_util_get_string_buffer */
1217 /* Assures the config option is a number and returns it as an int. */
1218 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1220 if ((ci == NULL) || (ret_value == NULL))
1223 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1225 ERROR ("cf_util_get_int: The %s option requires "
1226 "exactly one numeric argument.", ci->key);
1230 *ret_value = (int) ci->values[0].value.number;
1233 } /* }}} int cf_util_get_int */
1235 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1237 if ((ci == NULL) || (ret_value == NULL))
1240 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1242 ERROR ("cf_util_get_double: The %s option requires "
1243 "exactly one numeric argument.", ci->key);
1247 *ret_value = ci->values[0].value.number;
1250 } /* }}} int cf_util_get_double */
1252 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1254 if ((ci == NULL) || (ret_bool == NULL))
1257 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1259 ERROR ("cf_util_get_boolean: The %s option requires "
1260 "exactly one boolean argument.", ci->key);
1264 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1267 } /* }}} int cf_util_get_boolean */
1269 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1270 unsigned int *ret_value, unsigned int flag)
1275 if (ret_value == NULL)
1279 status = cf_util_get_boolean (ci, &b);
1289 *ret_value &= ~flag;
1293 } /* }}} int cf_util_get_flag */
1295 /* Assures that the config option is a string or a number if the correct range
1296 * of 1-65535. The string is then converted to a port number using
1297 * `service_name_to_port_number' and returned.
1298 * Returns the port number in the range [1-65535] or less than zero upon
1300 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1304 if ((ci->values_num != 1)
1305 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1306 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1308 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1309 "exactly one string argument.", ci->key);
1313 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1314 return (service_name_to_port_number (ci->values[0].value.string));
1316 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1317 tmp = (int) (ci->values[0].value.number + 0.5);
1318 if ((tmp < 1) || (tmp > 65535))
1320 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1321 "a service name or a port number. The number "
1322 "you specified, %i, is not in the valid "
1323 "range of 1-65535.",
1329 } /* }}} int cf_util_get_port_number */
1331 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1337 if (ci->values_num != 1)
1339 ERROR ("cf_util_get_service: The %s option requires exactly "
1340 "one argument.", ci->key);
1344 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1345 return (cf_util_get_string (ci, ret_string));
1346 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1348 ERROR ("cf_util_get_service: The %s option requires "
1349 "exactly one string or numeric argument.",
1354 status = cf_util_get_int (ci, &port);
1357 else if ((port < 1) || (port > 65535))
1359 ERROR ("cf_util_get_service: The port number given "
1360 "for the %s option is out of "
1361 "range (%i).", ci->key, port);
1365 service = malloc (6);
1366 if (service == NULL)
1368 ERROR ("cf_util_get_service: Out of memory.");
1371 ssnprintf (service, 6, "%i", port);
1373 sfree (*ret_string);
1374 *ret_string = service;
1377 } /* }}} int cf_util_get_service */
1379 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1381 if ((ci == NULL) || (ret_value == NULL))
1384 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1386 ERROR ("cf_util_get_cdtime: The %s option requires "
1387 "exactly one numeric argument.", ci->key);
1391 if (ci->values[0].value.number < 0.0)
1393 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1394 "option must not be negative.", ci->key);
1398 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1401 } /* }}} int cf_util_get_cdtime */