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) (const oconfig_item_t *);
75 typedef struct cf_global_option_s
83 * Prototypes of callback functions
85 static int dispatch_value_typesdb (const oconfig_item_t *ci);
86 static int dispatch_value_plugindir (const oconfig_item_t *ci);
87 static int dispatch_loadplugin (const oconfig_item_t *ci);
92 static cf_callback_t *first_callback = NULL;
93 static cf_complex_callback_t *complex_callback_head = NULL;
95 static cf_value_map_t cf_value_map[] =
97 {"TypesDB", dispatch_value_typesdb},
98 {"PluginDir", dispatch_value_plugindir},
99 {"LoadPlugin", dispatch_loadplugin}
101 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
103 static cf_global_option_t cf_global_options[] =
105 {"BaseDir", NULL, PKGLOCALSTATEDIR},
106 {"PIDFile", NULL, PIDFILE},
107 {"Hostname", NULL, NULL},
108 {"FQDNLookup", NULL, "true"},
109 {"Interval", NULL, NULL},
110 {"ReadThreads", NULL, "5"},
111 {"WriteThreads", NULL, "5"},
112 {"WriteQueueLimitHigh", NULL, NULL},
113 {"WriteQueueLimitLow", NULL, NULL},
114 {"Timeout", NULL, "2"},
115 {"AutoLoadPlugin", NULL, "false"},
116 {"PreCacheChain", NULL, "PreCache"},
117 {"PostCacheChain", NULL, "PostCache"}
119 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
121 static int cf_default_typesdb = 1;
124 * Functions to handle register/unregister, search, and other plugin related
127 static cf_callback_t *cf_search (const char *type)
129 cf_callback_t *cf_cb;
134 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
135 if (strcasecmp (cf_cb->type, type) == 0)
141 static int cf_dispatch (const char *type, const char *orig_key,
142 const char *orig_value)
144 cf_callback_t *cf_cb;
145 plugin_ctx_t old_ctx;
151 DEBUG ("type = %s, key = %s, value = %s",
153 ESCAPE_NULL(orig_key),
154 ESCAPE_NULL(orig_value));
156 if ((cf_cb = cf_search (type)) == NULL)
158 WARNING ("Found a configuration for the `%s' plugin, but "
159 "the plugin isn't loaded or didn't register "
160 "a configuration callback.", type);
164 if ((key = strdup (orig_key)) == NULL)
166 if ((value = strdup (orig_value)) == NULL)
174 old_ctx = plugin_set_ctx (cf_cb->ctx);
176 for (i = 0; i < cf_cb->keys_num; i++)
178 if ((cf_cb->keys[i] != NULL)
179 && (strcasecmp (cf_cb->keys[i], key) == 0))
181 ret = (*cf_cb->callback) (key, value);
186 plugin_set_ctx (old_ctx);
188 if (i >= cf_cb->keys_num)
189 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
194 DEBUG ("cf_dispatch: return (%i)", ret);
197 } /* int cf_dispatch */
199 static int dispatch_global_option (const oconfig_item_t *ci)
201 if (ci->values_num != 1)
203 if (ci->values[0].type == OCONFIG_TYPE_STRING)
204 return (global_option_set (ci->key, ci->values[0].value.string));
205 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
208 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
209 return (global_option_set (ci->key, tmp));
211 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
213 if (ci->values[0].value.boolean)
214 return (global_option_set (ci->key, "true"));
216 return (global_option_set (ci->key, "false"));
220 } /* int dispatch_global_option */
222 static int dispatch_value_typesdb (const oconfig_item_t *ci)
226 assert (strcasecmp (ci->key, "TypesDB") == 0);
228 cf_default_typesdb = 0;
230 if (ci->values_num < 1) {
231 ERROR ("configfile: `TypesDB' needs at least one argument.");
235 for (i = 0; i < ci->values_num; ++i)
237 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
238 WARNING ("configfile: TypesDB: Skipping %i. argument which "
239 "is not a string.", i + 1);
243 read_types_list (ci->values[i].value.string);
246 } /* int dispatch_value_typesdb */
248 static int dispatch_value_plugindir (const oconfig_item_t *ci)
250 assert (strcasecmp (ci->key, "PluginDir") == 0);
252 if (ci->values_num != 1)
254 if (ci->values[0].type != OCONFIG_TYPE_STRING)
257 plugin_set_dir (ci->values[0].value.string);
261 static int dispatch_loadplugin (const oconfig_item_t *ci)
265 unsigned int flags = 0;
267 plugin_ctx_t old_ctx;
270 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
272 if (ci->values_num != 1)
274 if (ci->values[0].type != OCONFIG_TYPE_STRING)
277 name = ci->values[0].value.string;
279 /* default to the global interval set before loading this plugin */
280 memset (&ctx, 0, sizeof (ctx));
281 ctx.interval = cf_get_default_interval ();
283 for (i = 0; i < ci->children_num; ++i) {
284 if (strcasecmp("Globals", ci->children[i].key) == 0)
285 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
286 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
287 double interval = 0.0;
289 if (cf_util_get_double (ci->children + i, &interval) != 0) {
290 /* cf_util_get_double will log an error */
294 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
297 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
299 ci->children[i].key, ci->values[0].value.string);
303 old_ctx = plugin_set_ctx (ctx);
304 ret_val = plugin_load (name, (uint32_t) flags);
305 /* reset to the "global" context */
306 plugin_set_ctx (old_ctx);
309 } /* int dispatch_value_loadplugin */
311 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
319 buffer_free = sizeof (buffer);
321 for (i = 0; i < ci->values_num; i++)
325 if (ci->values[i].type == OCONFIG_TYPE_STRING)
326 status = ssnprintf (buffer_ptr, buffer_free, " %s",
327 ci->values[i].value.string);
328 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
329 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
330 ci->values[i].value.number);
331 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
332 status = ssnprintf (buffer_ptr, buffer_free, " %s",
333 ci->values[i].value.boolean
336 if ((status < 0) || (status >= buffer_free))
338 buffer_free -= status;
339 buffer_ptr += status;
341 /* skip the initial space */
342 buffer_ptr = buffer + 1;
344 return (cf_dispatch (plugin, ci->key, buffer_ptr));
345 } /* int dispatch_value_plugin */
347 static int dispatch_value (const oconfig_item_t *ci)
352 for (i = 0; i < cf_value_map_num; i++)
353 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
355 ret = cf_value_map[i].func (ci);
359 for (i = 0; i < cf_global_options_num; i++)
360 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
362 ret = dispatch_global_option (ci);
367 } /* int dispatch_value */
369 static int dispatch_block_plugin (oconfig_item_t *ci)
374 cf_complex_callback_t *cb;
376 if (strcasecmp (ci->key, "Plugin") != 0)
378 if (ci->values_num < 1)
380 if (ci->values[0].type != OCONFIG_TYPE_STRING)
383 name = ci->values[0].value.string;
385 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
388 plugin_ctx_t old_ctx;
391 /* default to the global interval set before loading this plugin */
392 memset (&ctx, 0, sizeof (ctx));
393 ctx.interval = cf_get_default_interval ();
395 old_ctx = plugin_set_ctx (ctx);
396 status = plugin_load (name, /* flags = */ 0);
397 /* reset to the "global" context */
398 plugin_set_ctx (old_ctx);
402 ERROR ("Automatically loading plugin \"%s\" failed "
403 "with status %i.", name, status);
408 /* Check for a complex callback first */
409 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
411 if (strcasecmp (name, cb->type) == 0)
413 plugin_ctx_t old_ctx;
416 old_ctx = plugin_set_ctx (cb->ctx);
417 ret_val = (cb->callback (ci));
418 plugin_set_ctx (old_ctx);
423 /* Hm, no complex plugin found. Dispatch the values one by one */
424 for (i = 0; i < ci->children_num; i++)
426 if (ci->children[i].children == NULL)
427 dispatch_value_plugin (name, ci->children + i);
430 WARNING ("There is a `%s' block within the "
431 "configuration for the %s plugin. "
432 "The plugin either only expects "
433 "\"simple\" configuration statements "
434 "or wasn't loaded using `LoadPlugin'."
435 " Please check your configuration.",
436 ci->children[i].key, name);
444 static int dispatch_block (oconfig_item_t *ci)
446 if (strcasecmp (ci->key, "LoadPlugin") == 0)
447 return (dispatch_loadplugin (ci));
448 else if (strcasecmp (ci->key, "Plugin") == 0)
449 return (dispatch_block_plugin (ci));
450 else if (strcasecmp (ci->key, "Chain") == 0)
451 return (fc_configure (ci));
456 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
459 oconfig_item_t *temp;
462 assert (offset >= 0);
463 assert (dst->children_num > offset);
465 /* Free the memory used by the replaced child. Usually that's the
466 * `Include "blah"' statement. */
467 temp = dst->children + offset;
468 for (i = 0; i < temp->values_num; i++)
470 if (temp->values[i].type == OCONFIG_TYPE_STRING)
472 sfree (temp->values[i].value.string);
475 sfree (temp->values);
478 /* If (src->children_num == 0) the array size is decreased. If offset
479 * is _not_ the last element, (offset < (dst->children_num - 1)), then
480 * we need to move the trailing elements before resizing the array. */
481 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
483 int nmemb = dst->children_num - (offset + 1);
484 memmove (dst->children + offset, dst->children + offset + 1,
485 sizeof (oconfig_item_t) * nmemb);
488 /* Resize the memory containing the children to be big enough to hold
490 if (dst->children_num + src->children_num - 1 == 0)
492 dst->children_num = 0;
496 temp = (oconfig_item_t *) realloc (dst->children,
497 sizeof (oconfig_item_t)
498 * (dst->children_num + src->children_num - 1));
501 ERROR ("configfile: realloc failed.");
504 dst->children = temp;
506 /* If there are children behind the include statement, and they have
507 * not yet been moved because (src->children_num == 0), then move them
508 * to the end of the list, so that the new children have room before
510 if ((src->children_num > 0)
511 && ((dst->children_num - (offset + 1)) > 0))
513 int nmemb = dst->children_num - (offset + 1);
514 int old_offset = offset + 1;
515 int new_offset = offset + src->children_num;
517 memmove (dst->children + new_offset,
518 dst->children + old_offset,
519 sizeof (oconfig_item_t) * nmemb);
522 /* Last but not least: If there are new children, copy them to the
523 * memory reserved for them. */
524 if (src->children_num > 0)
526 memcpy (dst->children + offset,
528 sizeof (oconfig_item_t) * src->children_num);
531 /* Update the number of children. */
532 dst->children_num += (src->children_num - 1);
535 } /* int cf_ci_replace_child */
537 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
539 oconfig_item_t *temp;
541 if ((src == NULL) || (src->children_num == 0))
544 temp = (oconfig_item_t *) realloc (dst->children,
545 sizeof (oconfig_item_t)
546 * (dst->children_num + src->children_num));
549 ERROR ("configfile: realloc failed.");
552 dst->children = temp;
554 memcpy (dst->children + dst->children_num,
556 sizeof (oconfig_item_t)
557 * src->children_num);
558 dst->children_num += src->children_num;
561 } /* int cf_ci_append_children */
563 #define CF_MAX_DEPTH 8
564 static oconfig_item_t *cf_read_generic (const char *path,
565 const char *pattern, int depth);
567 static int cf_include_all (oconfig_item_t *root, int depth)
571 for (i = 0; i < root->children_num; i++)
576 char *pattern = NULL;
580 if (strcasecmp (root->children[i].key, "Include") != 0)
583 old = root->children + i;
585 if ((old->values_num != 1)
586 || (old->values[0].type != OCONFIG_TYPE_STRING))
588 ERROR ("configfile: `Include' needs exactly one string argument.");
592 for (j = 0; j < old->children_num; ++j)
594 oconfig_item_t *child = old->children + j;
596 if (strcasecmp (child->key, "Filter") == 0)
597 cf_util_get_string (child, &pattern);
599 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
603 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
609 /* Now replace the i'th child in `root' with `new'. */
610 if (cf_ci_replace_child (root, new, i) < 0)
613 /* ... and go back to the new i'th child. */
618 } /* for (i = 0; i < root->children_num; i++) */
621 } /* int cf_include_all */
623 static oconfig_item_t *cf_read_file (const char *file,
624 const char *pattern, int depth)
626 oconfig_item_t *root;
629 assert (depth < CF_MAX_DEPTH);
631 if (pattern != NULL) {
632 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
633 char *tmp = sstrdup (file);
634 char *filename = basename (tmp);
636 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
637 DEBUG ("configfile: Not including `%s' because it "
638 "does not match pattern `%s'.",
646 ERROR ("configfile: Cannot apply pattern filter '%s' "
647 "to file '%s': functions basename() and / or "
648 "fnmatch() not available.", pattern, file);
649 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
652 root = oconfig_parse_file (file);
655 ERROR ("configfile: Cannot read file `%s'.", file);
659 status = cf_include_all (root, depth);
667 } /* oconfig_item_t *cf_read_file */
669 static int cf_compare_string (const void *p1, const void *p2)
671 return strcmp (*(const char **) p1, *(const char **) p2);
674 static oconfig_item_t *cf_read_dir (const char *dir,
675 const char *pattern, int depth)
677 oconfig_item_t *root = NULL;
680 char **filenames = NULL;
681 int filenames_num = 0;
685 assert (depth < CF_MAX_DEPTH);
691 ERROR ("configfile: opendir failed: %s",
692 sstrerror (errno, errbuf, sizeof (errbuf)));
696 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
699 ERROR ("configfile: malloc failed.");
702 memset (root, 0, sizeof (oconfig_item_t));
704 while ((de = readdir (dh)) != NULL)
709 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
712 status = ssnprintf (name, sizeof (name), "%s/%s",
714 if ((status < 0) || ((size_t) status >= sizeof (name)))
716 ERROR ("configfile: Not including `%s/%s' because its"
717 " name is too long.",
719 for (i = 0; i < filenames_num; ++i)
727 tmp = (char **) realloc (filenames,
728 filenames_num * sizeof (*filenames));
730 ERROR ("configfile: realloc failed.");
731 for (i = 0; i < filenames_num - 1; ++i)
739 filenames[filenames_num - 1] = sstrdup (name);
742 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
745 for (i = 0; i < filenames_num; ++i)
747 oconfig_item_t *temp;
748 char *name = filenames[i];
750 temp = cf_read_generic (name, pattern, depth);
753 /* An error should already have been reported. */
758 cf_ci_append_children (root, temp);
759 sfree (temp->children);
767 } /* oconfig_item_t *cf_read_dir */
772 * Path is stat'ed and either cf_read_file or cf_read_dir is called
775 * There are two versions of this function: If `wordexp' exists shell wildcards
776 * will be expanded and the function will include all matches found. If
777 * `wordexp' (or, more precisely, it's header file) is not available the
778 * simpler function is used which does not do any such expansion.
781 static oconfig_item_t *cf_read_generic (const char *path,
782 const char *pattern, int depth)
784 oconfig_item_t *root = NULL;
786 const char *path_ptr;
790 if (depth >= CF_MAX_DEPTH)
792 ERROR ("configfile: Not including `%s' because the maximum "
793 "nesting depth has been reached.", path);
797 status = wordexp (path, &we, WRDE_NOCMD);
800 ERROR ("configfile: wordexp (%s) failed.", path);
804 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
807 ERROR ("configfile: malloc failed.");
810 memset (root, '\0', sizeof (oconfig_item_t));
812 /* wordexp() might return a sorted list already. That's not
813 * documented though, so let's make sure we get what we want. */
814 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
817 for (i = 0; i < we.we_wordc; i++)
819 oconfig_item_t *temp;
822 path_ptr = we.we_wordv[i];
824 status = stat (path_ptr, &statbuf);
828 WARNING ("configfile: stat (%s) failed: %s",
830 sstrerror (errno, errbuf, sizeof (errbuf)));
834 if (S_ISREG (statbuf.st_mode))
835 temp = cf_read_file (path_ptr, pattern, depth);
836 else if (S_ISDIR (statbuf.st_mode))
837 temp = cf_read_dir (path_ptr, pattern, depth);
840 WARNING ("configfile: %s is neither a file nor a "
850 cf_ci_append_children (root, temp);
851 sfree (temp->children);
858 } /* oconfig_item_t *cf_read_generic */
859 /* #endif HAVE_WORDEXP_H */
861 #else /* if !HAVE_WORDEXP_H */
862 static oconfig_item_t *cf_read_generic (const char *path,
863 const char *pattern, int depth)
868 if (depth >= CF_MAX_DEPTH)
870 ERROR ("configfile: Not including `%s' because the maximum "
871 "nesting depth has been reached.", path);
875 status = stat (path, &statbuf);
879 ERROR ("configfile: stat (%s) failed: %s",
881 sstrerror (errno, errbuf, sizeof (errbuf)));
885 if (S_ISREG (statbuf.st_mode))
886 return (cf_read_file (path, pattern, depth));
887 else if (S_ISDIR (statbuf.st_mode))
888 return (cf_read_dir (path, pattern, depth));
890 ERROR ("configfile: %s is neither a file nor a directory.", path);
892 } /* oconfig_item_t *cf_read_generic */
893 #endif /* !HAVE_WORDEXP_H */
898 int global_option_set (const char *option, const char *value)
902 DEBUG ("option = %s; value = %s;", option, value);
904 for (i = 0; i < cf_global_options_num; i++)
905 if (strcasecmp (cf_global_options[i].key, option) == 0)
908 if (i >= cf_global_options_num)
911 sfree (cf_global_options[i].value);
914 cf_global_options[i].value = strdup (value);
916 cf_global_options[i].value = NULL;
921 const char *global_option_get (const char *option)
925 for (i = 0; i < cf_global_options_num; i++)
926 if (strcasecmp (cf_global_options[i].key, option) == 0)
929 if (i >= cf_global_options_num)
932 return ((cf_global_options[i].value != NULL)
933 ? cf_global_options[i].value
934 : cf_global_options[i].def);
935 } /* char *global_option_get */
937 long global_option_get_long (const char *option, long default_value)
942 str = global_option_get (option);
944 return (default_value);
947 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
949 return (default_value);
952 } /* char *global_option_get_long */
954 cdtime_t cf_get_default_interval (void)
956 char const *str = global_option_get ("Interval");
957 double interval_double = COLLECTD_DEFAULT_INTERVAL;
962 double tmp = strtod (str, &endptr);
964 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
965 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
968 ERROR ("cf_get_default_interval: Interval must be a positive number. "
969 "The current number is %g.", tmp);
971 interval_double = tmp;
974 return (DOUBLE_TO_CDTIME_T (interval_double));
975 } /* }}} cdtime_t cf_get_default_interval */
977 void cf_unregister (const char *type)
979 cf_callback_t *this, *prev;
981 for (prev = NULL, this = first_callback;
983 prev = this, this = this->next)
984 if (strcasecmp (this->type, type) == 0)
987 first_callback = this->next;
989 prev->next = this->next;
994 } /* void cf_unregister */
996 void cf_unregister_complex (const char *type)
998 cf_complex_callback_t *this, *prev;
1000 for (prev = NULL, this = complex_callback_head;
1002 prev = this, this = this->next)
1003 if (strcasecmp (this->type, type) == 0)
1006 complex_callback_head = this->next;
1008 prev->next = this->next;
1014 } /* void cf_unregister */
1016 void cf_register (const char *type,
1017 int (*callback) (const char *, const char *),
1018 const char **keys, int keys_num)
1020 cf_callback_t *cf_cb;
1022 /* Remove this module from the list, if it already exists */
1023 cf_unregister (type);
1025 /* This pointer will be free'd in `cf_unregister' */
1026 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1030 cf_cb->callback = callback;
1032 cf_cb->keys_num = keys_num;
1033 cf_cb->ctx = plugin_get_ctx ();
1035 cf_cb->next = first_callback;
1036 first_callback = cf_cb;
1037 } /* void cf_register */
1039 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1041 cf_complex_callback_t *new;
1043 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1047 new->type = strdup (type);
1048 if (new->type == NULL)
1054 new->callback = callback;
1057 new->ctx = plugin_get_ctx ();
1059 if (complex_callback_head == NULL)
1061 complex_callback_head = new;
1065 cf_complex_callback_t *last = complex_callback_head;
1066 while (last->next != NULL)
1072 } /* int cf_register_complex */
1074 int cf_read (char *filename)
1076 oconfig_item_t *conf;
1079 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1082 ERROR ("Unable to read config file %s.", filename);
1085 else if (conf->children_num == 0)
1087 ERROR ("Configuration file %s is empty.", filename);
1088 oconfig_free (conf);
1092 for (i = 0; i < conf->children_num; i++)
1094 if (conf->children[i].children == NULL)
1095 dispatch_value (conf->children + i);
1097 dispatch_block (conf->children + i);
1100 oconfig_free (conf);
1102 /* Read the default types.db if no `TypesDB' option was given. */
1103 if (cf_default_typesdb)
1104 read_types_list (PKGDATADIR"/types.db");
1109 /* Assures the config option is a string, duplicates it and returns the copy in
1110 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1112 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1116 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1118 ERROR ("cf_util_get_string: The %s option requires "
1119 "exactly one string argument.", ci->key);
1123 string = strdup (ci->values[0].value.string);
1127 if (*ret_string != NULL)
1128 sfree (*ret_string);
1129 *ret_string = string;
1132 } /* }}} int cf_util_get_string */
1134 /* Assures the config option is a string and copies it to the provided buffer.
1135 * Assures null-termination. */
1136 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1139 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1142 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1144 ERROR ("cf_util_get_string_buffer: The %s option requires "
1145 "exactly one string argument.", ci->key);
1149 strncpy (buffer, ci->values[0].value.string, buffer_size);
1150 buffer[buffer_size - 1] = 0;
1153 } /* }}} int cf_util_get_string_buffer */
1155 /* Assures the config option is a number and returns it as an int. */
1156 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1158 if ((ci == NULL) || (ret_value == NULL))
1161 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1163 ERROR ("cf_util_get_int: The %s option requires "
1164 "exactly one numeric argument.", ci->key);
1168 *ret_value = (int) ci->values[0].value.number;
1171 } /* }}} int cf_util_get_int */
1173 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1175 if ((ci == NULL) || (ret_value == NULL))
1178 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1180 ERROR ("cf_util_get_double: The %s option requires "
1181 "exactly one numeric argument.", ci->key);
1185 *ret_value = ci->values[0].value.number;
1188 } /* }}} int cf_util_get_double */
1190 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1192 if ((ci == NULL) || (ret_bool == NULL))
1195 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1197 ERROR ("cf_util_get_boolean: The %s option requires "
1198 "exactly one boolean argument.", ci->key);
1202 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1205 } /* }}} int cf_util_get_boolean */
1207 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1208 unsigned int *ret_value, unsigned int flag)
1213 if (ret_value == NULL)
1217 status = cf_util_get_boolean (ci, &b);
1227 *ret_value &= ~flag;
1231 } /* }}} int cf_util_get_flag */
1233 /* Assures that the config option is a string or a number if the correct range
1234 * of 1-65535. The string is then converted to a port number using
1235 * `service_name_to_port_number' and returned.
1236 * Returns the port number in the range [1-65535] or less than zero upon
1238 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1242 if ((ci->values_num != 1)
1243 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1244 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1246 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1247 "exactly one string argument.", ci->key);
1251 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1252 return (service_name_to_port_number (ci->values[0].value.string));
1254 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1255 tmp = (int) (ci->values[0].value.number + 0.5);
1256 if ((tmp < 1) || (tmp > 65535))
1258 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1259 "a service name or a port number. The number "
1260 "you specified, %i, is not in the valid "
1261 "range of 1-65535.",
1267 } /* }}} int cf_util_get_port_number */
1269 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1275 if (ci->values_num != 1)
1277 ERROR ("cf_util_get_service: The %s option requires exactly "
1278 "one argument.", ci->key);
1282 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1283 return (cf_util_get_string (ci, ret_string));
1284 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1286 ERROR ("cf_util_get_service: The %s option requires "
1287 "exactly one string or numeric argument.",
1292 status = cf_util_get_int (ci, &port);
1295 else if ((port < 1) || (port > 65535))
1297 ERROR ("cf_util_get_service: The port number given "
1298 "for the %s option is out of "
1299 "range (%i).", ci->key, port);
1303 service = malloc (6);
1304 if (service == NULL)
1306 ERROR ("cf_util_get_service: Out of memory.");
1309 ssnprintf (service, 6, "%i", port);
1311 sfree (*ret_string);
1312 *ret_string = service;
1315 } /* }}} int cf_util_get_service */
1317 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1319 if ((ci == NULL) || (ret_value == NULL))
1322 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1324 ERROR ("cf_util_get_cdtime: The %s option requires "
1325 "exactly one numeric argument.", ci->key);
1329 if (ci->values[0].value.number < 0.0)
1331 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1332 "option must not be negative.", ci->key);
1336 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1339 } /* }}} int cf_util_get_cdtime */