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 {"Timeout", NULL, "2"},
113 {"PreCacheChain", NULL, "PreCache"},
114 {"PostCacheChain", NULL, "PostCache"}
116 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
118 static int cf_default_typesdb = 1;
121 * Functions to handle register/unregister, search, and other plugin related
124 static cf_callback_t *cf_search (const char *type)
126 cf_callback_t *cf_cb;
131 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
132 if (strcasecmp (cf_cb->type, type) == 0)
138 static int cf_dispatch (const char *type, const char *orig_key,
139 const char *orig_value)
141 cf_callback_t *cf_cb;
142 plugin_ctx_t old_ctx;
148 DEBUG ("type = %s, key = %s, value = %s",
150 ESCAPE_NULL(orig_key),
151 ESCAPE_NULL(orig_value));
153 if ((cf_cb = cf_search (type)) == NULL)
155 WARNING ("Found a configuration for the `%s' plugin, but "
156 "the plugin isn't loaded or didn't register "
157 "a configuration callback.", type);
161 if ((key = strdup (orig_key)) == NULL)
163 if ((value = strdup (orig_value)) == NULL)
171 old_ctx = plugin_set_ctx (cf_cb->ctx);
173 for (i = 0; i < cf_cb->keys_num; i++)
175 if ((cf_cb->keys[i] != NULL)
176 && (strcasecmp (cf_cb->keys[i], key) == 0))
178 ret = (*cf_cb->callback) (key, value);
183 plugin_set_ctx (old_ctx);
185 if (i >= cf_cb->keys_num)
186 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
191 DEBUG ("cf_dispatch: return (%i)", ret);
194 } /* int cf_dispatch */
196 static int dispatch_global_option (const oconfig_item_t *ci)
198 if (ci->values_num != 1)
200 if (ci->values[0].type == OCONFIG_TYPE_STRING)
201 return (global_option_set (ci->key, ci->values[0].value.string));
202 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
205 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
206 return (global_option_set (ci->key, tmp));
208 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
210 if (ci->values[0].value.boolean)
211 return (global_option_set (ci->key, "true"));
213 return (global_option_set (ci->key, "false"));
217 } /* int dispatch_global_option */
219 static int dispatch_value_typesdb (const oconfig_item_t *ci)
223 assert (strcasecmp (ci->key, "TypesDB") == 0);
225 cf_default_typesdb = 0;
227 if (ci->values_num < 1) {
228 ERROR ("configfile: `TypesDB' needs at least one argument.");
232 for (i = 0; i < ci->values_num; ++i)
234 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
235 WARNING ("configfile: TypesDB: Skipping %i. argument which "
236 "is not a string.", i + 1);
240 read_types_list (ci->values[i].value.string);
243 } /* int dispatch_value_typesdb */
245 static int dispatch_value_plugindir (const oconfig_item_t *ci)
247 assert (strcasecmp (ci->key, "PluginDir") == 0);
249 if (ci->values_num != 1)
251 if (ci->values[0].type != OCONFIG_TYPE_STRING)
254 plugin_set_dir (ci->values[0].value.string);
258 static int dispatch_loadplugin (const oconfig_item_t *ci)
262 unsigned int flags = 0;
264 plugin_ctx_t old_ctx;
267 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
269 if (ci->values_num != 1)
271 if (ci->values[0].type != OCONFIG_TYPE_STRING)
274 name = ci->values[0].value.string;
276 /* default to the global interval set before loading this plugin */
277 memset (&ctx, 0, sizeof (ctx));
278 ctx.interval = cf_get_default_interval ();
281 * XXX: Magic at work:
283 * Some of the language bindings, for example the Python and Perl
284 * plugins, need to be able to export symbols to the scripts they run.
285 * For this to happen, the "Globals" flag needs to be set.
286 * Unfortunately, this technical detail is hard to explain to the
287 * average user and she shouldn't have to worry about this, ideally.
288 * So in order to save everyone's sanity use a different default for a
289 * handful of special plugins. --octo
291 if ((strcasecmp ("Perl", name) == 0)
292 || (strcasecmp ("Python", name) == 0))
293 flags |= PLUGIN_FLAGS_GLOBAL;
295 for (i = 0; i < ci->children_num; ++i) {
296 if (strcasecmp("Globals", ci->children[i].key) == 0)
297 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
298 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
299 double interval = 0.0;
301 if (cf_util_get_double (ci->children + i, &interval) != 0) {
302 /* cf_util_get_double will log an error */
306 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
309 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
311 ci->children[i].key, ci->values[0].value.string);
315 old_ctx = plugin_set_ctx (ctx);
316 ret_val = plugin_load (name, (uint32_t) flags);
317 /* reset to the "global" context */
318 plugin_set_ctx (old_ctx);
321 } /* int dispatch_value_loadplugin */
323 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
331 buffer_free = sizeof (buffer);
333 for (i = 0; i < ci->values_num; i++)
337 if (ci->values[i].type == OCONFIG_TYPE_STRING)
338 status = ssnprintf (buffer_ptr, buffer_free, " %s",
339 ci->values[i].value.string);
340 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
341 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
342 ci->values[i].value.number);
343 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
344 status = ssnprintf (buffer_ptr, buffer_free, " %s",
345 ci->values[i].value.boolean
348 if ((status < 0) || (status >= buffer_free))
350 buffer_free -= status;
351 buffer_ptr += status;
353 /* skip the initial space */
354 buffer_ptr = buffer + 1;
356 return (cf_dispatch (plugin, ci->key, buffer_ptr));
357 } /* int dispatch_value_plugin */
359 static int dispatch_value (const oconfig_item_t *ci)
364 for (i = 0; i < cf_value_map_num; i++)
365 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
367 ret = cf_value_map[i].func (ci);
371 for (i = 0; i < cf_global_options_num; i++)
372 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
374 ret = dispatch_global_option (ci);
379 } /* int dispatch_value */
381 static int dispatch_block_plugin (oconfig_item_t *ci)
386 cf_complex_callback_t *cb;
388 if (strcasecmp (ci->key, "Plugin") != 0)
390 if (ci->values_num < 1)
392 if (ci->values[0].type != OCONFIG_TYPE_STRING)
395 name = ci->values[0].value.string;
397 /* Check for a complex callback first */
398 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
400 if (strcasecmp (name, cb->type) == 0)
402 plugin_ctx_t old_ctx;
405 old_ctx = plugin_set_ctx (cb->ctx);
406 ret_val = (cb->callback (ci));
407 plugin_set_ctx (old_ctx);
412 /* Hm, no complex plugin found. Dispatch the values one by one */
413 for (i = 0; i < ci->children_num; i++)
415 if (ci->children[i].children == NULL)
416 dispatch_value_plugin (name, ci->children + i);
419 WARNING ("There is a `%s' block within the "
420 "configuration for the %s plugin. "
421 "The plugin either only expects "
422 "\"simple\" configuration statements "
423 "or wasn't loaded using `LoadPlugin'."
424 " Please check your configuration.",
425 ci->children[i].key, name);
433 static int dispatch_block (oconfig_item_t *ci)
435 if (strcasecmp (ci->key, "LoadPlugin") == 0)
436 return (dispatch_loadplugin (ci));
437 else if (strcasecmp (ci->key, "Plugin") == 0)
438 return (dispatch_block_plugin (ci));
439 else if (strcasecmp (ci->key, "Chain") == 0)
440 return (fc_configure (ci));
445 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
448 oconfig_item_t *temp;
451 assert (offset >= 0);
452 assert (dst->children_num > offset);
454 /* Free the memory used by the replaced child. Usually that's the
455 * `Include "blah"' statement. */
456 temp = dst->children + offset;
457 for (i = 0; i < temp->values_num; i++)
459 if (temp->values[i].type == OCONFIG_TYPE_STRING)
461 sfree (temp->values[i].value.string);
464 sfree (temp->values);
467 /* If (src->children_num == 0) the array size is decreased. If offset
468 * is _not_ the last element, (offset < (dst->children_num - 1)), then
469 * we need to move the trailing elements before resizing the array. */
470 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
472 int nmemb = dst->children_num - (offset + 1);
473 memmove (dst->children + offset, dst->children + offset + 1,
474 sizeof (oconfig_item_t) * nmemb);
477 /* Resize the memory containing the children to be big enough to hold
479 if (dst->children_num + src->children_num - 1 == 0)
481 dst->children_num = 0;
485 temp = (oconfig_item_t *) realloc (dst->children,
486 sizeof (oconfig_item_t)
487 * (dst->children_num + src->children_num - 1));
490 ERROR ("configfile: realloc failed.");
493 dst->children = temp;
495 /* If there are children behind the include statement, and they have
496 * not yet been moved because (src->children_num == 0), then move them
497 * to the end of the list, so that the new children have room before
499 if ((src->children_num > 0)
500 && ((dst->children_num - (offset + 1)) > 0))
502 int nmemb = dst->children_num - (offset + 1);
503 int old_offset = offset + 1;
504 int new_offset = offset + src->children_num;
506 memmove (dst->children + new_offset,
507 dst->children + old_offset,
508 sizeof (oconfig_item_t) * nmemb);
511 /* Last but not least: If there are new children, copy them to the
512 * memory reserved for them. */
513 if (src->children_num > 0)
515 memcpy (dst->children + offset,
517 sizeof (oconfig_item_t) * src->children_num);
520 /* Update the number of children. */
521 dst->children_num += (src->children_num - 1);
524 } /* int cf_ci_replace_child */
526 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
528 oconfig_item_t *temp;
530 if ((src == NULL) || (src->children_num == 0))
533 temp = (oconfig_item_t *) realloc (dst->children,
534 sizeof (oconfig_item_t)
535 * (dst->children_num + src->children_num));
538 ERROR ("configfile: realloc failed.");
541 dst->children = temp;
543 memcpy (dst->children + dst->children_num,
545 sizeof (oconfig_item_t)
546 * src->children_num);
547 dst->children_num += src->children_num;
550 } /* int cf_ci_append_children */
552 #define CF_MAX_DEPTH 8
553 static oconfig_item_t *cf_read_generic (const char *path,
554 const char *pattern, int depth);
556 static int cf_include_all (oconfig_item_t *root, int depth)
560 for (i = 0; i < root->children_num; i++)
565 char *pattern = NULL;
569 if (strcasecmp (root->children[i].key, "Include") != 0)
572 old = root->children + i;
574 if ((old->values_num != 1)
575 || (old->values[0].type != OCONFIG_TYPE_STRING))
577 ERROR ("configfile: `Include' needs exactly one string argument.");
581 for (j = 0; j < old->children_num; ++j)
583 oconfig_item_t *child = old->children + j;
585 if (strcasecmp (child->key, "Filter") == 0)
586 cf_util_get_string (child, &pattern);
588 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
592 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
598 /* Now replace the i'th child in `root' with `new'. */
599 if (cf_ci_replace_child (root, new, i) < 0)
602 /* ... and go back to the new i'th child. */
607 } /* for (i = 0; i < root->children_num; i++) */
610 } /* int cf_include_all */
612 static oconfig_item_t *cf_read_file (const char *file,
613 const char *pattern, int depth)
615 oconfig_item_t *root;
618 assert (depth < CF_MAX_DEPTH);
620 if (pattern != NULL) {
621 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
622 char *tmp = sstrdup (file);
623 char *filename = basename (tmp);
625 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
626 DEBUG ("configfile: Not including `%s' because it "
627 "does not match pattern `%s'.",
635 ERROR ("configfile: Cannot apply pattern filter '%s' "
636 "to file '%s': functions basename() and / or "
637 "fnmatch() not available.", pattern, file);
638 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
641 root = oconfig_parse_file (file);
644 ERROR ("configfile: Cannot read file `%s'.", file);
648 status = cf_include_all (root, depth);
656 } /* oconfig_item_t *cf_read_file */
658 static int cf_compare_string (const void *p1, const void *p2)
660 return strcmp (*(const char **) p1, *(const char **) p2);
663 static oconfig_item_t *cf_read_dir (const char *dir,
664 const char *pattern, int depth)
666 oconfig_item_t *root = NULL;
669 char **filenames = NULL;
670 int filenames_num = 0;
674 assert (depth < CF_MAX_DEPTH);
680 ERROR ("configfile: opendir failed: %s",
681 sstrerror (errno, errbuf, sizeof (errbuf)));
685 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
688 ERROR ("configfile: malloc failed.");
691 memset (root, 0, sizeof (oconfig_item_t));
693 while ((de = readdir (dh)) != NULL)
698 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
701 status = ssnprintf (name, sizeof (name), "%s/%s",
703 if ((status < 0) || ((size_t) status >= sizeof (name)))
705 ERROR ("configfile: Not including `%s/%s' because its"
706 " name is too long.",
708 for (i = 0; i < filenames_num; ++i)
716 tmp = (char **) realloc (filenames,
717 filenames_num * sizeof (*filenames));
719 ERROR ("configfile: realloc failed.");
720 for (i = 0; i < filenames_num - 1; ++i)
728 filenames[filenames_num - 1] = sstrdup (name);
731 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
734 for (i = 0; i < filenames_num; ++i)
736 oconfig_item_t *temp;
737 char *name = filenames[i];
739 temp = cf_read_generic (name, pattern, depth);
742 /* An error should already have been reported. */
747 cf_ci_append_children (root, temp);
748 sfree (temp->children);
756 } /* oconfig_item_t *cf_read_dir */
761 * Path is stat'ed and either cf_read_file or cf_read_dir is called
764 * There are two versions of this function: If `wordexp' exists shell wildcards
765 * will be expanded and the function will include all matches found. If
766 * `wordexp' (or, more precisely, it's header file) is not available the
767 * simpler function is used which does not do any such expansion.
770 static oconfig_item_t *cf_read_generic (const char *path,
771 const char *pattern, int depth)
773 oconfig_item_t *root = NULL;
775 const char *path_ptr;
779 if (depth >= CF_MAX_DEPTH)
781 ERROR ("configfile: Not including `%s' because the maximum "
782 "nesting depth has been reached.", path);
786 status = wordexp (path, &we, WRDE_NOCMD);
789 ERROR ("configfile: wordexp (%s) failed.", path);
793 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
796 ERROR ("configfile: malloc failed.");
799 memset (root, '\0', sizeof (oconfig_item_t));
801 /* wordexp() might return a sorted list already. That's not
802 * documented though, so let's make sure we get what we want. */
803 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
806 for (i = 0; i < we.we_wordc; i++)
808 oconfig_item_t *temp;
811 path_ptr = we.we_wordv[i];
813 status = stat (path_ptr, &statbuf);
817 WARNING ("configfile: stat (%s) failed: %s",
819 sstrerror (errno, errbuf, sizeof (errbuf)));
823 if (S_ISREG (statbuf.st_mode))
824 temp = cf_read_file (path_ptr, pattern, depth);
825 else if (S_ISDIR (statbuf.st_mode))
826 temp = cf_read_dir (path_ptr, pattern, depth);
829 WARNING ("configfile: %s is neither a file nor a "
839 cf_ci_append_children (root, temp);
840 sfree (temp->children);
847 } /* oconfig_item_t *cf_read_generic */
848 /* #endif HAVE_WORDEXP_H */
850 #else /* if !HAVE_WORDEXP_H */
851 static oconfig_item_t *cf_read_generic (const char *path,
852 const char *pattern, int depth)
857 if (depth >= CF_MAX_DEPTH)
859 ERROR ("configfile: Not including `%s' because the maximum "
860 "nesting depth has been reached.", path);
864 status = stat (path, &statbuf);
868 ERROR ("configfile: stat (%s) failed: %s",
870 sstrerror (errno, errbuf, sizeof (errbuf)));
874 if (S_ISREG (statbuf.st_mode))
875 return (cf_read_file (path, pattern, depth));
876 else if (S_ISDIR (statbuf.st_mode))
877 return (cf_read_dir (path, pattern, depth));
879 ERROR ("configfile: %s is neither a file nor a directory.", path);
881 } /* oconfig_item_t *cf_read_generic */
882 #endif /* !HAVE_WORDEXP_H */
887 int global_option_set (const char *option, const char *value)
891 DEBUG ("option = %s; value = %s;", option, value);
893 for (i = 0; i < cf_global_options_num; i++)
894 if (strcasecmp (cf_global_options[i].key, option) == 0)
897 if (i >= cf_global_options_num)
900 sfree (cf_global_options[i].value);
903 cf_global_options[i].value = strdup (value);
905 cf_global_options[i].value = NULL;
910 const char *global_option_get (const char *option)
914 for (i = 0; i < cf_global_options_num; i++)
915 if (strcasecmp (cf_global_options[i].key, option) == 0)
918 if (i >= cf_global_options_num)
921 return ((cf_global_options[i].value != NULL)
922 ? cf_global_options[i].value
923 : cf_global_options[i].def);
924 } /* char *global_option_get */
926 cdtime_t cf_get_default_interval (void)
928 char const *str = global_option_get ("Interval");
929 double interval_double = COLLECTD_DEFAULT_INTERVAL;
934 double tmp = strtod (str, &endptr);
936 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
937 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
940 ERROR ("cf_get_default_interval: Interval must be a positive number. "
941 "The current number is %g.", tmp);
943 interval_double = tmp;
946 return (DOUBLE_TO_CDTIME_T (interval_double));
947 } /* }}} cdtime_t cf_get_default_interval */
949 void cf_unregister (const char *type)
951 cf_callback_t *this, *prev;
953 for (prev = NULL, this = first_callback;
955 prev = this, this = this->next)
956 if (strcasecmp (this->type, type) == 0)
959 first_callback = this->next;
961 prev->next = this->next;
966 } /* void cf_unregister */
968 void cf_unregister_complex (const char *type)
970 cf_complex_callback_t *this, *prev;
972 for (prev = NULL, this = complex_callback_head;
974 prev = this, this = this->next)
975 if (strcasecmp (this->type, type) == 0)
978 complex_callback_head = this->next;
980 prev->next = this->next;
986 } /* void cf_unregister */
988 void cf_register (const char *type,
989 int (*callback) (const char *, const char *),
990 const char **keys, int keys_num)
992 cf_callback_t *cf_cb;
994 /* Remove this module from the list, if it already exists */
995 cf_unregister (type);
997 /* This pointer will be free'd in `cf_unregister' */
998 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1002 cf_cb->callback = callback;
1004 cf_cb->keys_num = keys_num;
1005 cf_cb->ctx = plugin_get_ctx ();
1007 cf_cb->next = first_callback;
1008 first_callback = cf_cb;
1009 } /* void cf_register */
1011 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1013 cf_complex_callback_t *new;
1015 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1019 new->type = strdup (type);
1020 if (new->type == NULL)
1026 new->callback = callback;
1029 new->ctx = plugin_get_ctx ();
1031 if (complex_callback_head == NULL)
1033 complex_callback_head = new;
1037 cf_complex_callback_t *last = complex_callback_head;
1038 while (last->next != NULL)
1044 } /* int cf_register_complex */
1046 int cf_read (char *filename)
1048 oconfig_item_t *conf;
1051 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1054 ERROR ("Unable to read config file %s.", filename);
1057 else if (conf->children_num == 0)
1059 ERROR ("Configuration file %s is empty.", filename);
1060 oconfig_free (conf);
1064 for (i = 0; i < conf->children_num; i++)
1066 if (conf->children[i].children == NULL)
1067 dispatch_value (conf->children + i);
1069 dispatch_block (conf->children + i);
1072 oconfig_free (conf);
1074 /* Read the default types.db if no `TypesDB' option was given. */
1075 if (cf_default_typesdb)
1076 read_types_list (PKGDATADIR"/types.db");
1081 /* Assures the config option is a string, duplicates it and returns the copy in
1082 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1084 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1088 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1090 ERROR ("cf_util_get_string: The %s option requires "
1091 "exactly one string argument.", ci->key);
1095 string = strdup (ci->values[0].value.string);
1099 if (*ret_string != NULL)
1100 sfree (*ret_string);
1101 *ret_string = string;
1104 } /* }}} int cf_util_get_string */
1106 /* Assures the config option is a string and copies it to the provided buffer.
1107 * Assures null-termination. */
1108 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1111 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1114 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1116 ERROR ("cf_util_get_string_buffer: The %s option requires "
1117 "exactly one string argument.", ci->key);
1121 strncpy (buffer, ci->values[0].value.string, buffer_size);
1122 buffer[buffer_size - 1] = 0;
1125 } /* }}} int cf_util_get_string_buffer */
1127 /* Assures the config option is a number and returns it as an int. */
1128 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1130 if ((ci == NULL) || (ret_value == NULL))
1133 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1135 ERROR ("cf_util_get_int: The %s option requires "
1136 "exactly one numeric argument.", ci->key);
1140 *ret_value = (int) ci->values[0].value.number;
1143 } /* }}} int cf_util_get_int */
1145 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1147 if ((ci == NULL) || (ret_value == NULL))
1150 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1152 ERROR ("cf_util_get_double: The %s option requires "
1153 "exactly one numeric argument.", ci->key);
1157 *ret_value = ci->values[0].value.number;
1160 } /* }}} int cf_util_get_double */
1162 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1164 if ((ci == NULL) || (ret_bool == NULL))
1167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1169 ERROR ("cf_util_get_boolean: The %s option requires "
1170 "exactly one boolean argument.", ci->key);
1174 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1177 } /* }}} int cf_util_get_boolean */
1179 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1180 unsigned int *ret_value, unsigned int flag)
1185 if (ret_value == NULL)
1189 status = cf_util_get_boolean (ci, &b);
1199 *ret_value &= ~flag;
1203 } /* }}} int cf_util_get_flag */
1205 /* Assures that the config option is a string or a number if the correct range
1206 * of 1-65535. The string is then converted to a port number using
1207 * `service_name_to_port_number' and returned.
1208 * Returns the port number in the range [1-65535] or less than zero upon
1210 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1214 if ((ci->values_num != 1)
1215 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1216 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1218 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1219 "exactly one string argument.", ci->key);
1223 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1224 return (service_name_to_port_number (ci->values[0].value.string));
1226 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1227 tmp = (int) (ci->values[0].value.number + 0.5);
1228 if ((tmp < 1) || (tmp > 65535))
1230 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1231 "a service name or a port number. The number "
1232 "you specified, %i, is not in the valid "
1233 "range of 1-65535.",
1239 } /* }}} int cf_util_get_port_number */
1241 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1247 if (ci->values_num != 1)
1249 ERROR ("cf_util_get_service: The %s option requires exactly "
1250 "one argument.", ci->key);
1254 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1255 return (cf_util_get_string (ci, ret_string));
1256 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1258 ERROR ("cf_util_get_service: The %s option requires "
1259 "exactly one string or numeric argument.",
1264 status = cf_util_get_int (ci, &port);
1267 else if ((port < 1) || (port > 65535))
1269 ERROR ("cf_util_get_service: The port number given "
1270 "for the %s option is out of "
1271 "range (%i).", ci->key, port);
1275 service = malloc (6);
1276 if (service == NULL)
1278 ERROR ("cf_util_get_service: Out of memory.");
1281 ssnprintf (service, 6, "%i", port);
1283 sfree (*ret_string);
1284 *ret_string = service;
1287 } /* }}} int cf_util_get_service */
1289 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1291 if ((ci == NULL) || (ret_value == NULL))
1294 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1296 ERROR ("cf_util_get_cdtime: The %s option requires "
1297 "exactly one numeric argument.", ci->key);
1301 if (ci->values[0].value.number < 0.0)
1303 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1304 "option must not be negative.", ci->key);
1308 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1311 } /* }}} int cf_util_get_cdtime */