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) {
619 /* ... and go back to the new i'th child. */
624 } /* for (i = 0; i < root->children_num; i++) */
627 } /* int cf_include_all */
629 static oconfig_item_t *cf_read_file (const char *file,
630 const char *pattern, int depth)
632 oconfig_item_t *root;
635 assert (depth < CF_MAX_DEPTH);
637 if (pattern != NULL) {
638 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
639 char *tmp = sstrdup (file);
640 char *filename = basename (tmp);
642 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
643 DEBUG ("configfile: Not including `%s' because it "
644 "does not match pattern `%s'.",
652 ERROR ("configfile: Cannot apply pattern filter '%s' "
653 "to file '%s': functions basename() and / or "
654 "fnmatch() not available.", pattern, file);
655 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
658 root = oconfig_parse_file (file);
661 ERROR ("configfile: Cannot read file `%s'.", file);
665 status = cf_include_all (root, depth);
673 } /* oconfig_item_t *cf_read_file */
675 static int cf_compare_string (const void *p1, const void *p2)
677 return strcmp (*(const char **) p1, *(const char **) p2);
680 static oconfig_item_t *cf_read_dir (const char *dir,
681 const char *pattern, int depth)
683 oconfig_item_t *root = NULL;
686 char **filenames = NULL;
687 int filenames_num = 0;
691 assert (depth < CF_MAX_DEPTH);
697 ERROR ("configfile: opendir failed: %s",
698 sstrerror (errno, errbuf, sizeof (errbuf)));
702 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
705 ERROR ("configfile: malloc failed.");
708 memset (root, 0, sizeof (oconfig_item_t));
710 while ((de = readdir (dh)) != NULL)
715 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
718 status = ssnprintf (name, sizeof (name), "%s/%s",
720 if ((status < 0) || ((size_t) status >= sizeof (name)))
722 ERROR ("configfile: Not including `%s/%s' because its"
723 " name is too long.",
725 for (i = 0; i < filenames_num; ++i)
733 tmp = (char **) realloc (filenames,
734 filenames_num * sizeof (*filenames));
736 ERROR ("configfile: realloc failed.");
737 for (i = 0; i < filenames_num - 1; ++i)
745 filenames[filenames_num - 1] = sstrdup (name);
748 if (filenames == NULL)
751 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
754 for (i = 0; i < filenames_num; ++i)
756 oconfig_item_t *temp;
757 char *name = filenames[i];
759 temp = cf_read_generic (name, pattern, depth);
762 /* An error should already have been reported. */
767 cf_ci_append_children (root, temp);
768 sfree (temp->children);
776 } /* oconfig_item_t *cf_read_dir */
781 * Path is stat'ed and either cf_read_file or cf_read_dir is called
784 * There are two versions of this function: If `wordexp' exists shell wildcards
785 * will be expanded and the function will include all matches found. If
786 * `wordexp' (or, more precisely, it's header file) is not available the
787 * simpler function is used which does not do any such expansion.
790 static oconfig_item_t *cf_read_generic (const char *path,
791 const char *pattern, int depth)
793 oconfig_item_t *root = NULL;
795 const char *path_ptr;
799 if (depth >= CF_MAX_DEPTH)
801 ERROR ("configfile: Not including `%s' because the maximum "
802 "nesting depth has been reached.", path);
806 status = wordexp (path, &we, WRDE_NOCMD);
809 ERROR ("configfile: wordexp (%s) failed.", path);
813 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
816 ERROR ("configfile: malloc failed.");
819 memset (root, '\0', sizeof (oconfig_item_t));
821 /* wordexp() might return a sorted list already. That's not
822 * documented though, so let's make sure we get what we want. */
823 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
826 for (i = 0; i < we.we_wordc; i++)
828 oconfig_item_t *temp;
831 path_ptr = we.we_wordv[i];
833 status = stat (path_ptr, &statbuf);
837 WARNING ("configfile: stat (%s) failed: %s",
839 sstrerror (errno, errbuf, sizeof (errbuf)));
843 if (S_ISREG (statbuf.st_mode))
844 temp = cf_read_file (path_ptr, pattern, depth);
845 else if (S_ISDIR (statbuf.st_mode))
846 temp = cf_read_dir (path_ptr, pattern, depth);
849 WARNING ("configfile: %s is neither a file nor a "
859 cf_ci_append_children (root, temp);
860 sfree (temp->children);
867 } /* oconfig_item_t *cf_read_generic */
868 /* #endif HAVE_WORDEXP_H */
870 #else /* if !HAVE_WORDEXP_H */
871 static oconfig_item_t *cf_read_generic (const char *path,
872 const char *pattern, int depth)
877 if (depth >= CF_MAX_DEPTH)
879 ERROR ("configfile: Not including `%s' because the maximum "
880 "nesting depth has been reached.", path);
884 status = stat (path, &statbuf);
888 ERROR ("configfile: stat (%s) failed: %s",
890 sstrerror (errno, errbuf, sizeof (errbuf)));
894 if (S_ISREG (statbuf.st_mode))
895 return (cf_read_file (path, pattern, depth));
896 else if (S_ISDIR (statbuf.st_mode))
897 return (cf_read_dir (path, pattern, depth));
899 ERROR ("configfile: %s is neither a file nor a directory.", path);
901 } /* oconfig_item_t *cf_read_generic */
902 #endif /* !HAVE_WORDEXP_H */
907 int global_option_set (const char *option, const char *value)
911 DEBUG ("option = %s; value = %s;", option, value);
913 for (i = 0; i < cf_global_options_num; i++)
914 if (strcasecmp (cf_global_options[i].key, option) == 0)
917 if (i >= cf_global_options_num)
920 sfree (cf_global_options[i].value);
923 cf_global_options[i].value = strdup (value);
925 cf_global_options[i].value = NULL;
930 const char *global_option_get (const char *option)
934 for (i = 0; i < cf_global_options_num; i++)
935 if (strcasecmp (cf_global_options[i].key, option) == 0)
938 if (i >= cf_global_options_num)
941 return ((cf_global_options[i].value != NULL)
942 ? cf_global_options[i].value
943 : cf_global_options[i].def);
944 } /* char *global_option_get */
946 long global_option_get_long (const char *option, long default_value)
951 str = global_option_get (option);
953 return (default_value);
956 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
958 return (default_value);
961 } /* char *global_option_get_long */
963 cdtime_t cf_get_default_interval (void)
965 char const *str = global_option_get ("Interval");
966 double interval_double = COLLECTD_DEFAULT_INTERVAL;
971 double tmp = strtod (str, &endptr);
973 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
974 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
977 ERROR ("cf_get_default_interval: Interval must be a positive number. "
978 "The current number is %g.", tmp);
980 interval_double = tmp;
983 return (DOUBLE_TO_CDTIME_T (interval_double));
984 } /* }}} cdtime_t cf_get_default_interval */
986 void cf_unregister (const char *type)
988 cf_callback_t *this, *prev;
990 for (prev = NULL, this = first_callback;
992 prev = this, this = this->next)
993 if (strcasecmp (this->type, type) == 0)
996 first_callback = this->next;
998 prev->next = this->next;
1003 } /* void cf_unregister */
1005 void cf_unregister_complex (const char *type)
1007 cf_complex_callback_t *this, *prev;
1009 for (prev = NULL, this = complex_callback_head;
1011 prev = this, this = this->next)
1012 if (strcasecmp (this->type, type) == 0)
1015 complex_callback_head = this->next;
1017 prev->next = this->next;
1023 } /* void cf_unregister */
1025 void cf_register (const char *type,
1026 int (*callback) (const char *, const char *),
1027 const char **keys, int keys_num)
1029 cf_callback_t *cf_cb;
1031 /* Remove this module from the list, if it already exists */
1032 cf_unregister (type);
1034 /* This pointer will be free'd in `cf_unregister' */
1035 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1039 cf_cb->callback = callback;
1041 cf_cb->keys_num = keys_num;
1042 cf_cb->ctx = plugin_get_ctx ();
1044 cf_cb->next = first_callback;
1045 first_callback = cf_cb;
1046 } /* void cf_register */
1048 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1050 cf_complex_callback_t *new;
1052 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1056 new->type = strdup (type);
1057 if (new->type == NULL)
1063 new->callback = callback;
1066 new->ctx = plugin_get_ctx ();
1068 if (complex_callback_head == NULL)
1070 complex_callback_head = new;
1074 cf_complex_callback_t *last = complex_callback_head;
1075 while (last->next != NULL)
1081 } /* int cf_register_complex */
1083 int cf_read (char *filename)
1085 oconfig_item_t *conf;
1088 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1091 ERROR ("Unable to read config file %s.", filename);
1094 else if (conf->children_num == 0)
1096 ERROR ("Configuration file %s is empty.", filename);
1097 oconfig_free (conf);
1101 for (i = 0; i < conf->children_num; i++)
1103 if (conf->children[i].children == NULL)
1104 dispatch_value (conf->children + i);
1106 dispatch_block (conf->children + i);
1109 oconfig_free (conf);
1111 /* Read the default types.db if no `TypesDB' option was given. */
1112 if (cf_default_typesdb)
1113 read_types_list (PKGDATADIR"/types.db");
1118 /* Assures the config option is a string, duplicates it and returns the copy in
1119 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1121 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1125 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1127 ERROR ("cf_util_get_string: The %s option requires "
1128 "exactly one string argument.", ci->key);
1132 string = strdup (ci->values[0].value.string);
1136 if (*ret_string != NULL)
1137 sfree (*ret_string);
1138 *ret_string = string;
1141 } /* }}} int cf_util_get_string */
1143 /* Assures the config option is a string and copies it to the provided buffer.
1144 * Assures null-termination. */
1145 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1148 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1151 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1153 ERROR ("cf_util_get_string_buffer: The %s option requires "
1154 "exactly one string argument.", ci->key);
1158 strncpy (buffer, ci->values[0].value.string, buffer_size);
1159 buffer[buffer_size - 1] = 0;
1162 } /* }}} int cf_util_get_string_buffer */
1164 /* Assures the config option is a number and returns it as an int. */
1165 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1167 if ((ci == NULL) || (ret_value == NULL))
1170 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1172 ERROR ("cf_util_get_int: The %s option requires "
1173 "exactly one numeric argument.", ci->key);
1177 *ret_value = (int) ci->values[0].value.number;
1180 } /* }}} int cf_util_get_int */
1182 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1184 if ((ci == NULL) || (ret_value == NULL))
1187 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1189 ERROR ("cf_util_get_double: The %s option requires "
1190 "exactly one numeric argument.", ci->key);
1194 *ret_value = ci->values[0].value.number;
1197 } /* }}} int cf_util_get_double */
1199 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1201 if ((ci == NULL) || (ret_bool == NULL))
1204 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1206 ERROR ("cf_util_get_boolean: The %s option requires "
1207 "exactly one boolean argument.", ci->key);
1211 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1214 } /* }}} int cf_util_get_boolean */
1216 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1217 unsigned int *ret_value, unsigned int flag)
1222 if (ret_value == NULL)
1226 status = cf_util_get_boolean (ci, &b);
1236 *ret_value &= ~flag;
1240 } /* }}} int cf_util_get_flag */
1242 /* Assures that the config option is a string or a number if the correct range
1243 * of 1-65535. The string is then converted to a port number using
1244 * `service_name_to_port_number' and returned.
1245 * Returns the port number in the range [1-65535] or less than zero upon
1247 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1251 if ((ci->values_num != 1)
1252 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1253 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1255 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1256 "exactly one string argument.", ci->key);
1260 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1261 return (service_name_to_port_number (ci->values[0].value.string));
1263 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1264 tmp = (int) (ci->values[0].value.number + 0.5);
1265 if ((tmp < 1) || (tmp > 65535))
1267 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1268 "a service name or a port number. The number "
1269 "you specified, %i, is not in the valid "
1270 "range of 1-65535.",
1276 } /* }}} int cf_util_get_port_number */
1278 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1284 if (ci->values_num != 1)
1286 ERROR ("cf_util_get_service: The %s option requires exactly "
1287 "one argument.", ci->key);
1291 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1292 return (cf_util_get_string (ci, ret_string));
1293 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1295 ERROR ("cf_util_get_service: The %s option requires "
1296 "exactly one string or numeric argument.",
1301 status = cf_util_get_int (ci, &port);
1304 else if ((port < 1) || (port > 65535))
1306 ERROR ("cf_util_get_service: The port number given "
1307 "for the %s option is out of "
1308 "range (%i).", ci->key, port);
1312 service = malloc (6);
1313 if (service == NULL)
1315 ERROR ("cf_util_get_service: Out of memory.");
1318 ssnprintf (service, 6, "%i", port);
1320 sfree (*ret_string);
1321 *ret_string = service;
1324 } /* }}} int cf_util_get_service */
1326 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1328 if ((ci == NULL) || (ret_value == NULL))
1331 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1333 ERROR ("cf_util_get_cdtime: The %s option requires "
1334 "exactly one numeric argument.", ci->key);
1338 if (ci->values[0].value.number < 0.0)
1340 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1341 "option must not be negative.", ci->key);
1345 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1348 } /* }}} int cf_util_get_cdtime */