2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
26 #include "liboconfig/oconfig.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
36 #endif /* HAVE_WORDEXP_H */
40 #endif /* HAVE_FNMATCH_H */
44 #endif /* HAVE_LIBGEN_H */
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
51 typedef struct cf_callback
54 int (*callback) (const char *, const char *);
58 struct cf_callback *next;
61 typedef struct cf_complex_callback_s
64 int (*callback) (oconfig_item_t *);
66 struct cf_complex_callback_s *next;
67 } cf_complex_callback_t;
69 typedef struct cf_value_map_s
72 int (*func) (oconfig_item_t *);
75 typedef struct cf_global_option_s
83 * Prototypes of callback functions
85 static int dispatch_value_typesdb (oconfig_item_t *ci);
86 static int dispatch_value_plugindir (oconfig_item_t *ci);
87 static int dispatch_loadplugin (oconfig_item_t *ci);
88 static int dispatch_block_plugin (oconfig_item_t *ci);
93 static cf_callback_t *first_callback = NULL;
94 static cf_complex_callback_t *complex_callback_head = NULL;
96 static cf_value_map_t cf_value_map[] =
98 {"TypesDB", dispatch_value_typesdb},
99 {"PluginDir", dispatch_value_plugindir},
100 {"LoadPlugin", dispatch_loadplugin},
101 {"Plugin", dispatch_block_plugin}
103 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
105 static cf_global_option_t cf_global_options[] =
107 {"BaseDir", NULL, PKGLOCALSTATEDIR},
108 {"PIDFile", NULL, PIDFILE},
109 {"Hostname", NULL, NULL},
110 {"FQDNLookup", NULL, "true"},
111 {"Interval", NULL, NULL},
112 {"ReadThreads", NULL, "5"},
113 {"WriteThreads", NULL, "5"},
114 {"WriteQueueLimitHigh", NULL, NULL},
115 {"WriteQueueLimitLow", NULL, NULL},
116 {"Timeout", NULL, "2"},
117 {"AutoLoadPlugin", NULL, "false"},
118 {"PreCacheChain", NULL, "PreCache"},
119 {"PostCacheChain", NULL, "PostCache"}
121 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
123 static int cf_default_typesdb = 1;
126 * Functions to handle register/unregister, search, and other plugin related
129 static cf_callback_t *cf_search (const char *type)
131 cf_callback_t *cf_cb;
136 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
137 if (strcasecmp (cf_cb->type, type) == 0)
143 static int cf_dispatch (const char *type, const char *orig_key,
144 const char *orig_value)
146 cf_callback_t *cf_cb;
147 plugin_ctx_t old_ctx;
153 if (orig_key == NULL)
156 DEBUG ("type = %s, key = %s, value = %s",
159 ESCAPE_NULL(orig_value));
161 if ((cf_cb = cf_search (type)) == NULL)
163 WARNING ("Found a configuration for the `%s' plugin, but "
164 "the plugin isn't loaded or didn't register "
165 "a configuration callback.", type);
169 if ((key = strdup (orig_key)) == NULL)
171 if ((value = strdup (orig_value)) == NULL)
179 old_ctx = plugin_set_ctx (cf_cb->ctx);
181 for (i = 0; i < cf_cb->keys_num; i++)
183 if ((cf_cb->keys[i] != NULL)
184 && (strcasecmp (cf_cb->keys[i], key) == 0))
186 ret = (*cf_cb->callback) (key, value);
191 plugin_set_ctx (old_ctx);
193 if (i >= cf_cb->keys_num)
194 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
200 } /* int cf_dispatch */
202 static int dispatch_global_option (const oconfig_item_t *ci)
204 if (ci->values_num != 1)
206 if (ci->values[0].type == OCONFIG_TYPE_STRING)
207 return (global_option_set (ci->key, ci->values[0].value.string));
208 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
211 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
212 return (global_option_set (ci->key, tmp));
214 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
216 if (ci->values[0].value.boolean)
217 return (global_option_set (ci->key, "true"));
219 return (global_option_set (ci->key, "false"));
223 } /* int dispatch_global_option */
225 static int dispatch_value_typesdb (oconfig_item_t *ci)
229 assert (strcasecmp (ci->key, "TypesDB") == 0);
231 cf_default_typesdb = 0;
233 if (ci->values_num < 1) {
234 ERROR ("configfile: `TypesDB' needs at least one argument.");
238 for (i = 0; i < ci->values_num; ++i)
240 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
241 WARNING ("configfile: TypesDB: Skipping %i. argument which "
242 "is not a string.", i + 1);
246 read_types_list (ci->values[i].value.string);
249 } /* int dispatch_value_typesdb */
251 static int dispatch_value_plugindir (oconfig_item_t *ci)
253 assert (strcasecmp (ci->key, "PluginDir") == 0);
255 if (ci->values_num != 1)
257 if (ci->values[0].type != OCONFIG_TYPE_STRING)
260 plugin_set_dir (ci->values[0].value.string);
264 static int dispatch_loadplugin (oconfig_item_t *ci)
268 unsigned int flags = 0;
270 plugin_ctx_t old_ctx;
273 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
275 if (ci->values_num != 1)
277 if (ci->values[0].type != OCONFIG_TYPE_STRING)
280 name = ci->values[0].value.string;
282 /* default to the global interval set before loading this plugin */
283 memset (&ctx, 0, sizeof (ctx));
284 ctx.interval = cf_get_default_interval ();
286 for (i = 0; i < ci->children_num; ++i) {
287 if (strcasecmp("Globals", ci->children[i].key) == 0)
288 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
289 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
290 double interval = 0.0;
292 if (cf_util_get_double (ci->children + i, &interval) != 0) {
293 /* cf_util_get_double will log an error */
297 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
300 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
302 ci->children[i].key, ci->values[0].value.string);
306 old_ctx = plugin_set_ctx (ctx);
307 ret_val = plugin_load (name, (uint32_t) flags);
308 /* reset to the "global" context */
309 plugin_set_ctx (old_ctx);
312 } /* int dispatch_value_loadplugin */
314 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
322 buffer_free = sizeof (buffer);
324 for (i = 0; i < ci->values_num; i++)
328 if (ci->values[i].type == OCONFIG_TYPE_STRING)
329 status = ssnprintf (buffer_ptr, buffer_free, " %s",
330 ci->values[i].value.string);
331 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
332 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
333 ci->values[i].value.number);
334 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
335 status = ssnprintf (buffer_ptr, buffer_free, " %s",
336 ci->values[i].value.boolean
339 if ((status < 0) || (status >= buffer_free))
341 buffer_free -= status;
342 buffer_ptr += status;
344 /* skip the initial space */
345 buffer_ptr = buffer + 1;
347 return (cf_dispatch (plugin, ci->key, buffer_ptr));
348 } /* int dispatch_value_plugin */
350 static int dispatch_value (oconfig_item_t *ci)
355 for (i = 0; i < cf_value_map_num; i++)
356 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
358 ret = cf_value_map[i].func (ci);
362 for (i = 0; i < cf_global_options_num; i++)
363 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
365 ret = dispatch_global_option (ci);
370 } /* int dispatch_value */
372 static int dispatch_block_plugin (oconfig_item_t *ci)
377 cf_complex_callback_t *cb;
379 if (strcasecmp (ci->key, "Plugin") != 0)
381 if (ci->values_num < 1)
383 if (ci->values[0].type != OCONFIG_TYPE_STRING)
386 name = ci->values[0].value.string;
388 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
391 plugin_ctx_t old_ctx;
394 /* default to the global interval set before loading this plugin */
395 memset (&ctx, 0, sizeof (ctx));
396 ctx.interval = cf_get_default_interval ();
398 old_ctx = plugin_set_ctx (ctx);
399 status = plugin_load (name, /* flags = */ 0);
400 /* reset to the "global" context */
401 plugin_set_ctx (old_ctx);
405 ERROR ("Automatically loading plugin \"%s\" failed "
406 "with status %i.", name, status);
411 /* Check for a complex callback first */
412 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
414 if (strcasecmp (name, cb->type) == 0)
416 plugin_ctx_t old_ctx;
419 old_ctx = plugin_set_ctx (cb->ctx);
420 ret_val = (cb->callback (ci));
421 plugin_set_ctx (old_ctx);
426 /* Hm, no complex plugin found. Dispatch the values one by one */
427 for (i = 0; i < ci->children_num; i++)
429 if (ci->children[i].children == NULL)
430 dispatch_value_plugin (name, ci->children + i);
433 WARNING ("There is a `%s' block within the "
434 "configuration for the %s plugin. "
435 "The plugin either only expects "
436 "\"simple\" configuration statements "
437 "or wasn't loaded using `LoadPlugin'."
438 " Please check your configuration.",
439 ci->children[i].key, name);
447 static int dispatch_block (oconfig_item_t *ci)
449 if (strcasecmp (ci->key, "LoadPlugin") == 0)
450 return (dispatch_loadplugin (ci));
451 else if (strcasecmp (ci->key, "Plugin") == 0)
452 return (dispatch_block_plugin (ci));
453 else if (strcasecmp (ci->key, "Chain") == 0)
454 return (fc_configure (ci));
459 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
462 oconfig_item_t *temp;
465 assert (offset >= 0);
466 assert (dst->children_num > offset);
468 /* Free the memory used by the replaced child. Usually that's the
469 * `Include "blah"' statement. */
470 temp = dst->children + offset;
471 for (i = 0; i < temp->values_num; i++)
473 if (temp->values[i].type == OCONFIG_TYPE_STRING)
475 sfree (temp->values[i].value.string);
478 sfree (temp->values);
481 /* If (src->children_num == 0) the array size is decreased. If offset
482 * is _not_ the last element, (offset < (dst->children_num - 1)), then
483 * we need to move the trailing elements before resizing the array. */
484 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
486 int nmemb = dst->children_num - (offset + 1);
487 memmove (dst->children + offset, dst->children + offset + 1,
488 sizeof (oconfig_item_t) * nmemb);
491 /* Resize the memory containing the children to be big enough to hold
493 if (dst->children_num + src->children_num - 1 == 0)
495 dst->children_num = 0;
499 temp = (oconfig_item_t *) realloc (dst->children,
500 sizeof (oconfig_item_t)
501 * (dst->children_num + src->children_num - 1));
504 ERROR ("configfile: realloc failed.");
507 dst->children = temp;
509 /* If there are children behind the include statement, and they have
510 * not yet been moved because (src->children_num == 0), then move them
511 * to the end of the list, so that the new children have room before
513 if ((src->children_num > 0)
514 && ((dst->children_num - (offset + 1)) > 0))
516 int nmemb = dst->children_num - (offset + 1);
517 int old_offset = offset + 1;
518 int new_offset = offset + src->children_num;
520 memmove (dst->children + new_offset,
521 dst->children + old_offset,
522 sizeof (oconfig_item_t) * nmemb);
525 /* Last but not least: If there are new children, copy them to the
526 * memory reserved for them. */
527 if (src->children_num > 0)
529 memcpy (dst->children + offset,
531 sizeof (oconfig_item_t) * src->children_num);
534 /* Update the number of children. */
535 dst->children_num += (src->children_num - 1);
538 } /* int cf_ci_replace_child */
540 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
542 oconfig_item_t *temp;
544 if ((src == NULL) || (src->children_num == 0))
547 temp = (oconfig_item_t *) realloc (dst->children,
548 sizeof (oconfig_item_t)
549 * (dst->children_num + src->children_num));
552 ERROR ("configfile: realloc failed.");
555 dst->children = temp;
557 memcpy (dst->children + dst->children_num,
559 sizeof (oconfig_item_t)
560 * src->children_num);
561 dst->children_num += src->children_num;
564 } /* int cf_ci_append_children */
566 #define CF_MAX_DEPTH 8
567 static oconfig_item_t *cf_read_generic (const char *path,
568 const char *pattern, int depth);
570 static int cf_include_all (oconfig_item_t *root, int depth)
574 for (i = 0; i < root->children_num; i++)
579 char *pattern = NULL;
583 if (strcasecmp (root->children[i].key, "Include") != 0)
586 old = root->children + i;
588 if ((old->values_num != 1)
589 || (old->values[0].type != OCONFIG_TYPE_STRING))
591 ERROR ("configfile: `Include' needs exactly one string argument.");
595 for (j = 0; j < old->children_num; ++j)
597 oconfig_item_t *child = old->children + j;
599 if (strcasecmp (child->key, "Filter") == 0)
600 cf_util_get_string (child, &pattern);
602 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
606 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
612 /* Now replace the i'th child in `root' with `new'. */
613 if (cf_ci_replace_child (root, new, i) < 0)
616 /* ... and go back to the new i'th child. */
621 } /* for (i = 0; i < root->children_num; i++) */
624 } /* int cf_include_all */
626 static oconfig_item_t *cf_read_file (const char *file,
627 const char *pattern, int depth)
629 oconfig_item_t *root;
632 assert (depth < CF_MAX_DEPTH);
634 if (pattern != NULL) {
635 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
636 char *tmp = sstrdup (file);
637 char *filename = basename (tmp);
639 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
640 DEBUG ("configfile: Not including `%s' because it "
641 "does not match pattern `%s'.",
649 ERROR ("configfile: Cannot apply pattern filter '%s' "
650 "to file '%s': functions basename() and / or "
651 "fnmatch() not available.", pattern, file);
652 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
655 root = oconfig_parse_file (file);
658 ERROR ("configfile: Cannot read file `%s'.", file);
662 status = cf_include_all (root, depth);
670 } /* oconfig_item_t *cf_read_file */
672 static int cf_compare_string (const void *p1, const void *p2)
674 return strcmp (*(const char **) p1, *(const char **) p2);
677 static oconfig_item_t *cf_read_dir (const char *dir,
678 const char *pattern, int depth)
680 oconfig_item_t *root = NULL;
683 char **filenames = NULL;
684 int filenames_num = 0;
688 assert (depth < CF_MAX_DEPTH);
694 ERROR ("configfile: opendir failed: %s",
695 sstrerror (errno, errbuf, sizeof (errbuf)));
699 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
702 ERROR ("configfile: malloc failed.");
705 memset (root, 0, sizeof (oconfig_item_t));
707 while ((de = readdir (dh)) != NULL)
712 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
715 status = ssnprintf (name, sizeof (name), "%s/%s",
717 if ((status < 0) || ((size_t) status >= sizeof (name)))
719 ERROR ("configfile: Not including `%s/%s' because its"
720 " name is too long.",
722 for (i = 0; i < filenames_num; ++i)
730 tmp = (char **) realloc (filenames,
731 filenames_num * sizeof (*filenames));
733 ERROR ("configfile: realloc failed.");
734 for (i = 0; i < filenames_num - 1; ++i)
742 filenames[filenames_num - 1] = sstrdup (name);
745 if (filenames == NULL)
748 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
751 for (i = 0; i < filenames_num; ++i)
753 oconfig_item_t *temp;
754 char *name = filenames[i];
756 temp = cf_read_generic (name, pattern, depth);
759 /* An error should already have been reported. */
764 cf_ci_append_children (root, temp);
765 sfree (temp->children);
773 } /* oconfig_item_t *cf_read_dir */
778 * Path is stat'ed and either cf_read_file or cf_read_dir is called
781 * There are two versions of this function: If `wordexp' exists shell wildcards
782 * will be expanded and the function will include all matches found. If
783 * `wordexp' (or, more precisely, it's header file) is not available the
784 * simpler function is used which does not do any such expansion.
787 static oconfig_item_t *cf_read_generic (const char *path,
788 const char *pattern, int depth)
790 oconfig_item_t *root = NULL;
792 const char *path_ptr;
796 if (depth >= CF_MAX_DEPTH)
798 ERROR ("configfile: Not including `%s' because the maximum "
799 "nesting depth has been reached.", path);
803 status = wordexp (path, &we, WRDE_NOCMD);
806 ERROR ("configfile: wordexp (%s) failed.", path);
810 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
813 ERROR ("configfile: malloc failed.");
816 memset (root, '\0', sizeof (oconfig_item_t));
818 /* wordexp() might return a sorted list already. That's not
819 * documented though, so let's make sure we get what we want. */
820 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
823 for (i = 0; i < we.we_wordc; i++)
825 oconfig_item_t *temp;
828 path_ptr = we.we_wordv[i];
830 status = stat (path_ptr, &statbuf);
834 WARNING ("configfile: stat (%s) failed: %s",
836 sstrerror (errno, errbuf, sizeof (errbuf)));
840 if (S_ISREG (statbuf.st_mode))
841 temp = cf_read_file (path_ptr, pattern, depth);
842 else if (S_ISDIR (statbuf.st_mode))
843 temp = cf_read_dir (path_ptr, pattern, depth);
846 WARNING ("configfile: %s is neither a file nor a "
856 cf_ci_append_children (root, temp);
857 sfree (temp->children);
864 } /* oconfig_item_t *cf_read_generic */
865 /* #endif HAVE_WORDEXP_H */
867 #else /* if !HAVE_WORDEXP_H */
868 static oconfig_item_t *cf_read_generic (const char *path,
869 const char *pattern, int depth)
874 if (depth >= CF_MAX_DEPTH)
876 ERROR ("configfile: Not including `%s' because the maximum "
877 "nesting depth has been reached.", path);
881 status = stat (path, &statbuf);
885 ERROR ("configfile: stat (%s) failed: %s",
887 sstrerror (errno, errbuf, sizeof (errbuf)));
891 if (S_ISREG (statbuf.st_mode))
892 return (cf_read_file (path, pattern, depth));
893 else if (S_ISDIR (statbuf.st_mode))
894 return (cf_read_dir (path, pattern, depth));
896 ERROR ("configfile: %s is neither a file nor a directory.", path);
898 } /* oconfig_item_t *cf_read_generic */
899 #endif /* !HAVE_WORDEXP_H */
904 int global_option_set (const char *option, const char *value)
908 DEBUG ("option = %s; value = %s;", option, value);
910 for (i = 0; i < cf_global_options_num; i++)
911 if (strcasecmp (cf_global_options[i].key, option) == 0)
914 if (i >= cf_global_options_num)
917 sfree (cf_global_options[i].value);
920 cf_global_options[i].value = strdup (value);
922 cf_global_options[i].value = NULL;
927 const char *global_option_get (const char *option)
931 for (i = 0; i < cf_global_options_num; i++)
932 if (strcasecmp (cf_global_options[i].key, option) == 0)
935 if (i >= cf_global_options_num)
938 return ((cf_global_options[i].value != NULL)
939 ? cf_global_options[i].value
940 : cf_global_options[i].def);
941 } /* char *global_option_get */
943 long global_option_get_long (const char *option, long default_value)
948 str = global_option_get (option);
950 return (default_value);
953 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
955 return (default_value);
958 } /* char *global_option_get_long */
960 cdtime_t cf_get_default_interval (void)
962 char const *str = global_option_get ("Interval");
963 double interval_double = COLLECTD_DEFAULT_INTERVAL;
968 double tmp = strtod (str, &endptr);
970 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
971 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
974 ERROR ("cf_get_default_interval: Interval must be a positive number. "
975 "The current number is %g.", tmp);
977 interval_double = tmp;
980 return (DOUBLE_TO_CDTIME_T (interval_double));
981 } /* }}} cdtime_t cf_get_default_interval */
983 void cf_unregister (const char *type)
985 cf_callback_t *this, *prev;
987 for (prev = NULL, this = first_callback;
989 prev = this, this = this->next)
990 if (strcasecmp (this->type, type) == 0)
993 first_callback = this->next;
995 prev->next = this->next;
1000 } /* void cf_unregister */
1002 void cf_unregister_complex (const char *type)
1004 cf_complex_callback_t *this, *prev;
1006 for (prev = NULL, this = complex_callback_head;
1008 prev = this, this = this->next)
1009 if (strcasecmp (this->type, type) == 0)
1012 complex_callback_head = this->next;
1014 prev->next = this->next;
1020 } /* void cf_unregister */
1022 void cf_register (const char *type,
1023 int (*callback) (const char *, const char *),
1024 const char **keys, int keys_num)
1026 cf_callback_t *cf_cb;
1028 /* Remove this module from the list, if it already exists */
1029 cf_unregister (type);
1031 /* This pointer will be free'd in `cf_unregister' */
1032 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1036 cf_cb->callback = callback;
1038 cf_cb->keys_num = keys_num;
1039 cf_cb->ctx = plugin_get_ctx ();
1041 cf_cb->next = first_callback;
1042 first_callback = cf_cb;
1043 } /* void cf_register */
1045 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1047 cf_complex_callback_t *new;
1049 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1053 new->type = strdup (type);
1054 if (new->type == NULL)
1060 new->callback = callback;
1063 new->ctx = plugin_get_ctx ();
1065 if (complex_callback_head == NULL)
1067 complex_callback_head = new;
1071 cf_complex_callback_t *last = complex_callback_head;
1072 while (last->next != NULL)
1078 } /* int cf_register_complex */
1080 int cf_read (char *filename)
1082 oconfig_item_t *conf;
1085 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1088 ERROR ("Unable to read config file %s.", filename);
1091 else if (conf->children_num == 0)
1093 ERROR ("Configuration file %s is empty.", filename);
1094 oconfig_free (conf);
1098 for (i = 0; i < conf->children_num; i++)
1100 if (conf->children[i].children == NULL)
1101 dispatch_value (conf->children + i);
1103 dispatch_block (conf->children + i);
1106 oconfig_free (conf);
1108 /* Read the default types.db if no `TypesDB' option was given. */
1109 if (cf_default_typesdb)
1110 read_types_list (PKGDATADIR"/types.db");
1115 /* Assures the config option is a string, duplicates it and returns the copy in
1116 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1118 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1122 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1124 ERROR ("cf_util_get_string: The %s option requires "
1125 "exactly one string argument.", ci->key);
1129 string = strdup (ci->values[0].value.string);
1133 if (*ret_string != NULL)
1134 sfree (*ret_string);
1135 *ret_string = string;
1138 } /* }}} int cf_util_get_string */
1140 /* Assures the config option is a string and copies it to the provided buffer.
1141 * Assures null-termination. */
1142 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1145 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1148 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1150 ERROR ("cf_util_get_string_buffer: The %s option requires "
1151 "exactly one string argument.", ci->key);
1155 strncpy (buffer, ci->values[0].value.string, buffer_size);
1156 buffer[buffer_size - 1] = 0;
1159 } /* }}} int cf_util_get_string_buffer */
1161 /* Assures the config option is a number and returns it as an int. */
1162 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1164 if ((ci == NULL) || (ret_value == NULL))
1167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1169 ERROR ("cf_util_get_int: The %s option requires "
1170 "exactly one numeric argument.", ci->key);
1174 *ret_value = (int) ci->values[0].value.number;
1177 } /* }}} int cf_util_get_int */
1179 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1181 if ((ci == NULL) || (ret_value == NULL))
1184 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1186 ERROR ("cf_util_get_double: The %s option requires "
1187 "exactly one numeric argument.", ci->key);
1191 *ret_value = ci->values[0].value.number;
1194 } /* }}} int cf_util_get_double */
1196 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1198 if ((ci == NULL) || (ret_bool == NULL))
1201 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1203 ERROR ("cf_util_get_boolean: The %s option requires "
1204 "exactly one boolean argument.", ci->key);
1208 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1211 } /* }}} int cf_util_get_boolean */
1213 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1214 unsigned int *ret_value, unsigned int flag)
1219 if (ret_value == NULL)
1223 status = cf_util_get_boolean (ci, &b);
1233 *ret_value &= ~flag;
1237 } /* }}} int cf_util_get_flag */
1239 /* Assures that the config option is a string or a number if the correct range
1240 * of 1-65535. The string is then converted to a port number using
1241 * `service_name_to_port_number' and returned.
1242 * Returns the port number in the range [1-65535] or less than zero upon
1244 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1248 if ((ci->values_num != 1)
1249 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1250 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1252 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1253 "exactly one string argument.", ci->key);
1257 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1258 return (service_name_to_port_number (ci->values[0].value.string));
1260 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1261 tmp = (int) (ci->values[0].value.number + 0.5);
1262 if ((tmp < 1) || (tmp > 65535))
1264 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1265 "a service name or a port number. The number "
1266 "you specified, %i, is not in the valid "
1267 "range of 1-65535.",
1273 } /* }}} int cf_util_get_port_number */
1275 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1281 if (ci->values_num != 1)
1283 ERROR ("cf_util_get_service: The %s option requires exactly "
1284 "one argument.", ci->key);
1288 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1289 return (cf_util_get_string (ci, ret_string));
1290 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1292 ERROR ("cf_util_get_service: The %s option requires "
1293 "exactly one string or numeric argument.",
1298 status = cf_util_get_int (ci, &port);
1301 else if ((port < 1) || (port > 65535))
1303 ERROR ("cf_util_get_service: The port number given "
1304 "for the %s option is out of "
1305 "range (%i).", ci->key, port);
1309 service = malloc (6);
1310 if (service == NULL)
1312 ERROR ("cf_util_get_service: Out of memory.");
1315 ssnprintf (service, 6, "%i", port);
1317 sfree (*ret_string);
1318 *ret_string = service;
1321 } /* }}} int cf_util_get_service */
1323 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1325 if ((ci == NULL) || (ret_value == NULL))
1328 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1330 ERROR ("cf_util_get_cdtime: The %s option requires "
1331 "exactly one numeric argument.", ci->key);
1335 if (ci->values[0].value.number < 0.0)
1337 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1338 "option must not be negative.", ci->key);
1342 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1345 } /* }}} int cf_util_get_cdtime */