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 */
38 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
43 typedef struct cf_callback
46 int (*callback) (const char *, const char *);
50 struct cf_callback *next;
53 typedef struct cf_complex_callback_s
56 int (*callback) (oconfig_item_t *);
58 struct cf_complex_callback_s *next;
59 } cf_complex_callback_t;
61 typedef struct cf_value_map_s
64 int (*func) (const oconfig_item_t *);
67 typedef struct cf_global_option_s
75 * Prototypes of callback functions
77 static int dispatch_value_typesdb (const oconfig_item_t *ci);
78 static int dispatch_value_plugindir (const oconfig_item_t *ci);
79 static int dispatch_loadplugin (const oconfig_item_t *ci);
84 static cf_callback_t *first_callback = NULL;
85 static cf_complex_callback_t *complex_callback_head = NULL;
87 static cf_value_map_t cf_value_map[] =
89 {"TypesDB", dispatch_value_typesdb},
90 {"PluginDir", dispatch_value_plugindir},
91 {"LoadPlugin", dispatch_loadplugin}
93 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
95 static cf_global_option_t cf_global_options[] =
97 {"BaseDir", NULL, PKGLOCALSTATEDIR},
98 {"PIDFile", NULL, PIDFILE},
99 {"Hostname", NULL, NULL},
100 {"FQDNLookup", NULL, "true"},
101 {"Interval", NULL, "10"},
102 {"ReadThreads", NULL, "5"},
103 {"Timeout", NULL, "2"},
104 {"PreCacheChain", NULL, "PreCache"},
105 {"PostCacheChain", NULL, "PostCache"}
107 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
109 static int cf_default_typesdb = 1;
112 * Functions to handle register/unregister, search, and other plugin related
115 static cf_callback_t *cf_search (const char *type)
117 cf_callback_t *cf_cb;
122 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
123 if (strcasecmp (cf_cb->type, type) == 0)
129 static int cf_dispatch (const char *type, const char *orig_key,
130 const char *orig_value)
132 cf_callback_t *cf_cb;
133 plugin_ctx_t old_ctx;
139 DEBUG ("type = %s, key = %s, value = %s",
141 ESCAPE_NULL(orig_key),
142 ESCAPE_NULL(orig_value));
144 if ((cf_cb = cf_search (type)) == NULL)
146 WARNING ("Found a configuration for the `%s' plugin, but "
147 "the plugin isn't loaded or didn't register "
148 "a configuration callback.", type);
152 if ((key = strdup (orig_key)) == NULL)
154 if ((value = strdup (orig_value)) == NULL)
162 old_ctx = plugin_set_ctx (cf_cb->ctx);
164 for (i = 0; i < cf_cb->keys_num; i++)
166 if ((cf_cb->keys[i] != NULL)
167 && (strcasecmp (cf_cb->keys[i], key) == 0))
169 ret = (*cf_cb->callback) (key, value);
174 plugin_set_ctx (old_ctx);
176 if (i >= cf_cb->keys_num)
177 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
182 DEBUG ("cf_dispatch: return (%i)", ret);
185 } /* int cf_dispatch */
187 static int dispatch_global_option (const oconfig_item_t *ci)
189 if (ci->values_num != 1)
191 if (ci->values[0].type == OCONFIG_TYPE_STRING)
192 return (global_option_set (ci->key, ci->values[0].value.string));
193 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
196 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
197 return (global_option_set (ci->key, tmp));
199 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
201 if (ci->values[0].value.boolean)
202 return (global_option_set (ci->key, "true"));
204 return (global_option_set (ci->key, "false"));
208 } /* int dispatch_global_option */
210 static int dispatch_value_typesdb (const oconfig_item_t *ci)
214 assert (strcasecmp (ci->key, "TypesDB") == 0);
216 cf_default_typesdb = 0;
218 if (ci->values_num < 1) {
219 ERROR ("configfile: `TypesDB' needs at least one argument.");
223 for (i = 0; i < ci->values_num; ++i)
225 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
226 WARNING ("configfile: TypesDB: Skipping %i. argument which "
227 "is not a string.", i + 1);
231 read_types_list (ci->values[i].value.string);
234 } /* int dispatch_value_typesdb */
236 static int dispatch_value_plugindir (const oconfig_item_t *ci)
238 assert (strcasecmp (ci->key, "PluginDir") == 0);
240 if (ci->values_num != 1)
242 if (ci->values[0].type != OCONFIG_TYPE_STRING)
245 plugin_set_dir (ci->values[0].value.string);
249 static int dispatch_loadplugin (const oconfig_item_t *ci)
253 unsigned int flags = 0;
255 plugin_ctx_t old_ctx;
258 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
260 if (ci->values_num != 1)
262 if (ci->values[0].type != OCONFIG_TYPE_STRING)
265 name = ci->values[0].value.string;
267 /* default to the global interval set before loading this plugin */
268 ctx.interval = plugin_get_interval ();
271 * XXX: Magic at work:
273 * Some of the language bindings, for example the Python and Perl
274 * plugins, need to be able to export symbols to the scripts they run.
275 * For this to happen, the "Globals" flag needs to be set.
276 * Unfortunately, this technical detail is hard to explain to the
277 * average user and she shouldn't have to worry about this, ideally.
278 * So in order to save everyone's sanity use a different default for a
279 * handful of special plugins. --octo
281 if ((strcasecmp ("Perl", name) == 0)
282 || (strcasecmp ("Python", name) == 0))
283 flags |= PLUGIN_FLAGS_GLOBAL;
285 for (i = 0; i < ci->children_num; ++i) {
286 if (strcasecmp("Globals", ci->children[i].key) == 0)
287 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
288 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
289 double interval = 0.0;
291 if (cf_util_get_double (ci->children + i, &interval) != 0) {
292 /* cf_util_get_double will log an error */
296 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
299 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
301 ci->children[i].key, ci->values[0].value.string);
305 old_ctx = plugin_set_ctx (ctx);
306 ret_val = plugin_load (name, (uint32_t) flags);
307 /* reset to the "global" context */
308 plugin_set_ctx (old_ctx);
311 } /* int dispatch_value_loadplugin */
313 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
321 buffer_free = sizeof (buffer);
323 for (i = 0; i < ci->values_num; i++)
327 if (ci->values[i].type == OCONFIG_TYPE_STRING)
328 status = ssnprintf (buffer_ptr, buffer_free, " %s",
329 ci->values[i].value.string);
330 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
331 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
332 ci->values[i].value.number);
333 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
334 status = ssnprintf (buffer_ptr, buffer_free, " %s",
335 ci->values[i].value.boolean
338 if ((status < 0) || (status >= buffer_free))
340 buffer_free -= status;
341 buffer_ptr += status;
343 /* skip the initial space */
344 buffer_ptr = buffer + 1;
346 return (cf_dispatch (plugin, ci->key, buffer_ptr));
347 } /* int dispatch_value_plugin */
349 static int dispatch_value (const oconfig_item_t *ci)
354 for (i = 0; i < cf_value_map_num; i++)
355 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
357 ret = cf_value_map[i].func (ci);
361 for (i = 0; i < cf_global_options_num; i++)
362 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
364 ret = dispatch_global_option (ci);
369 } /* int dispatch_value */
371 static int dispatch_block_plugin (oconfig_item_t *ci)
376 cf_complex_callback_t *cb;
378 if (strcasecmp (ci->key, "Plugin") != 0)
380 if (ci->values_num < 1)
382 if (ci->values[0].type != OCONFIG_TYPE_STRING)
385 name = ci->values[0].value.string;
387 /* Check for a complex callback first */
388 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
390 if (strcasecmp (name, cb->type) == 0)
392 plugin_ctx_t old_ctx;
395 old_ctx = plugin_set_ctx (cb->ctx);
396 ret_val = (cb->callback (ci));
397 plugin_set_ctx (old_ctx);
402 /* Hm, no complex plugin found. Dispatch the values one by one */
403 for (i = 0; i < ci->children_num; i++)
405 if (ci->children[i].children == NULL)
406 dispatch_value_plugin (name, ci->children + i);
409 WARNING ("There is a `%s' block within the "
410 "configuration for the %s plugin. "
411 "The plugin either only expects "
412 "\"simple\" configuration statements "
413 "or wasn't loaded using `LoadPlugin'."
414 " Please check your configuration.",
415 ci->children[i].key, name);
423 static int dispatch_block (oconfig_item_t *ci)
425 if (strcasecmp (ci->key, "LoadPlugin") == 0)
426 return (dispatch_loadplugin (ci));
427 else if (strcasecmp (ci->key, "Plugin") == 0)
428 return (dispatch_block_plugin (ci));
429 else if (strcasecmp (ci->key, "Chain") == 0)
430 return (fc_configure (ci));
435 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
438 oconfig_item_t *temp;
441 assert (offset >= 0);
442 assert (dst->children_num > offset);
444 /* Free the memory used by the replaced child. Usually that's the
445 * `Include "blah"' statement. */
446 temp = dst->children + offset;
447 for (i = 0; i < temp->values_num; i++)
449 if (temp->values[i].type == OCONFIG_TYPE_STRING)
451 sfree (temp->values[i].value.string);
454 sfree (temp->values);
457 /* If (src->children_num == 0) the array size is decreased. If offset
458 * is _not_ the last element, (offset < (dst->children_num - 1)), then
459 * we need to move the trailing elements before resizing the array. */
460 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
462 int nmemb = dst->children_num - (offset + 1);
463 memmove (dst->children + offset, dst->children + offset + 1,
464 sizeof (oconfig_item_t) * nmemb);
467 /* Resize the memory containing the children to be big enough to hold
469 temp = (oconfig_item_t *) realloc (dst->children,
470 sizeof (oconfig_item_t)
471 * (dst->children_num + src->children_num - 1));
474 ERROR ("configfile: realloc failed.");
477 dst->children = temp;
479 /* If there are children behind the include statement, and they have
480 * not yet been moved because (src->children_num == 0), then move them
481 * to the end of the list, so that the new children have room before
483 if ((src->children_num > 0)
484 && ((dst->children_num - (offset + 1)) > 0))
486 int nmemb = dst->children_num - (offset + 1);
487 int old_offset = offset + 1;
488 int new_offset = offset + src->children_num;
490 memmove (dst->children + new_offset,
491 dst->children + old_offset,
492 sizeof (oconfig_item_t) * nmemb);
495 /* Last but not least: If there are new children, copy them to the
496 * memory reserved for them. */
497 if (src->children_num > 0)
499 memcpy (dst->children + offset,
501 sizeof (oconfig_item_t) * src->children_num);
504 /* Update the number of children. */
505 dst->children_num += (src->children_num - 1);
508 } /* int cf_ci_replace_child */
510 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
512 oconfig_item_t *temp;
514 if ((src == NULL) || (src->children_num == 0))
517 temp = (oconfig_item_t *) realloc (dst->children,
518 sizeof (oconfig_item_t)
519 * (dst->children_num + src->children_num));
522 ERROR ("configfile: realloc failed.");
525 dst->children = temp;
527 memcpy (dst->children + dst->children_num,
529 sizeof (oconfig_item_t)
530 * src->children_num);
531 dst->children_num += src->children_num;
534 } /* int cf_ci_append_children */
536 #define CF_MAX_DEPTH 8
537 static oconfig_item_t *cf_read_generic (const char *path, int depth);
539 static int cf_include_all (oconfig_item_t *root, int depth)
543 for (i = 0; i < root->children_num; i++)
548 /* Ignore all blocks, including `Include' blocks. */
549 if (root->children[i].children_num != 0)
552 if (strcasecmp (root->children[i].key, "Include") != 0)
555 old = root->children + i;
557 if ((old->values_num != 1)
558 || (old->values[0].type != OCONFIG_TYPE_STRING))
560 ERROR ("configfile: `Include' needs exactly one string argument.");
564 new = cf_read_generic (old->values[0].value.string, depth + 1);
568 /* Now replace the i'th child in `root' with `new'. */
569 cf_ci_replace_child (root, new, i);
571 /* ... and go back to the new i'th child. */
576 } /* for (i = 0; i < root->children_num; i++) */
579 } /* int cf_include_all */
581 static oconfig_item_t *cf_read_file (const char *file, int depth)
583 oconfig_item_t *root;
585 assert (depth < CF_MAX_DEPTH);
587 root = oconfig_parse_file (file);
590 ERROR ("configfile: Cannot read file `%s'.", file);
594 cf_include_all (root, depth);
597 } /* oconfig_item_t *cf_read_file */
599 static int cf_compare_string (const void *p1, const void *p2)
601 return strcmp (*(const char **) p1, *(const char **) p2);
604 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
606 oconfig_item_t *root = NULL;
609 char **filenames = NULL;
610 int filenames_num = 0;
614 assert (depth < CF_MAX_DEPTH);
620 ERROR ("configfile: opendir failed: %s",
621 sstrerror (errno, errbuf, sizeof (errbuf)));
625 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
628 ERROR ("configfile: malloc failed.");
631 memset (root, 0, sizeof (oconfig_item_t));
633 while ((de = readdir (dh)) != NULL)
638 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
641 status = ssnprintf (name, sizeof (name), "%s/%s",
643 if ((status < 0) || ((size_t) status >= sizeof (name)))
645 ERROR ("configfile: Not including `%s/%s' because its"
646 " name is too long.",
648 for (i = 0; i < filenames_num; ++i)
656 tmp = (char **) realloc (filenames,
657 filenames_num * sizeof (*filenames));
659 ERROR ("configfile: realloc failed.");
660 for (i = 0; i < filenames_num - 1; ++i)
668 filenames[filenames_num - 1] = sstrdup (name);
671 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
674 for (i = 0; i < filenames_num; ++i)
676 oconfig_item_t *temp;
677 char *name = filenames[i];
679 temp = cf_read_generic (name, depth);
682 /* An error should already have been reported. */
687 cf_ci_append_children (root, temp);
688 sfree (temp->children);
696 } /* oconfig_item_t *cf_read_dir */
701 * Path is stat'ed and either cf_read_file or cf_read_dir is called
704 * There are two versions of this function: If `wordexp' exists shell wildcards
705 * will be expanded and the function will include all matches found. If
706 * `wordexp' (or, more precisely, it's header file) is not available the
707 * simpler function is used which does not do any such expansion.
710 static oconfig_item_t *cf_read_generic (const char *path, int depth)
712 oconfig_item_t *root = NULL;
714 const char *path_ptr;
718 if (depth >= CF_MAX_DEPTH)
720 ERROR ("configfile: Not including `%s' because the maximum "
721 "nesting depth has been reached.", path);
725 status = wordexp (path, &we, WRDE_NOCMD);
728 ERROR ("configfile: wordexp (%s) failed.", path);
732 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
735 ERROR ("configfile: malloc failed.");
738 memset (root, '\0', sizeof (oconfig_item_t));
740 /* wordexp() might return a sorted list already. That's not
741 * documented though, so let's make sure we get what we want. */
742 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
745 for (i = 0; i < we.we_wordc; i++)
747 oconfig_item_t *temp;
750 path_ptr = we.we_wordv[i];
752 status = stat (path_ptr, &statbuf);
756 WARNING ("configfile: stat (%s) failed: %s",
758 sstrerror (errno, errbuf, sizeof (errbuf)));
762 if (S_ISREG (statbuf.st_mode))
763 temp = cf_read_file (path_ptr, depth);
764 else if (S_ISDIR (statbuf.st_mode))
765 temp = cf_read_dir (path_ptr, depth);
768 WARNING ("configfile: %s is neither a file nor a "
778 cf_ci_append_children (root, temp);
779 sfree (temp->children);
785 if (root->children == NULL)
792 } /* oconfig_item_t *cf_read_generic */
793 /* #endif HAVE_WORDEXP_H */
795 #else /* if !HAVE_WORDEXP_H */
796 static oconfig_item_t *cf_read_generic (const char *path, int depth)
801 if (depth >= CF_MAX_DEPTH)
803 ERROR ("configfile: Not including `%s' because the maximum "
804 "nesting depth has been reached.", path);
808 status = stat (path, &statbuf);
812 ERROR ("configfile: stat (%s) failed: %s",
814 sstrerror (errno, errbuf, sizeof (errbuf)));
818 if (S_ISREG (statbuf.st_mode))
819 return (cf_read_file (path, depth));
820 else if (S_ISDIR (statbuf.st_mode))
821 return (cf_read_dir (path, depth));
823 ERROR ("configfile: %s is neither a file nor a directory.", path);
825 } /* oconfig_item_t *cf_read_generic */
826 #endif /* !HAVE_WORDEXP_H */
831 int global_option_set (const char *option, const char *value)
835 DEBUG ("option = %s; value = %s;", option, value);
837 for (i = 0; i < cf_global_options_num; i++)
838 if (strcasecmp (cf_global_options[i].key, option) == 0)
841 if (i >= cf_global_options_num)
844 sfree (cf_global_options[i].value);
847 cf_global_options[i].value = strdup (value);
849 cf_global_options[i].value = NULL;
854 const char *global_option_get (const char *option)
858 for (i = 0; i < cf_global_options_num; i++)
859 if (strcasecmp (cf_global_options[i].key, option) == 0)
862 if (i >= cf_global_options_num)
865 return ((cf_global_options[i].value != NULL)
866 ? cf_global_options[i].value
867 : cf_global_options[i].def);
868 } /* char *global_option_get */
870 void cf_unregister (const char *type)
872 cf_callback_t *this, *prev;
874 for (prev = NULL, this = first_callback;
876 prev = this, this = this->next)
877 if (strcasecmp (this->type, type) == 0)
880 first_callback = this->next;
882 prev->next = this->next;
887 } /* void cf_unregister */
889 void cf_unregister_complex (const char *type)
891 cf_complex_callback_t *this, *prev;
893 for (prev = NULL, this = complex_callback_head;
895 prev = this, this = this->next)
896 if (strcasecmp (this->type, type) == 0)
899 complex_callback_head = this->next;
901 prev->next = this->next;
907 } /* void cf_unregister */
909 void cf_register (const char *type,
910 int (*callback) (const char *, const char *),
911 const char **keys, int keys_num)
913 cf_callback_t *cf_cb;
915 /* Remove this module from the list, if it already exists */
916 cf_unregister (type);
918 /* This pointer will be free'd in `cf_unregister' */
919 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
923 cf_cb->callback = callback;
925 cf_cb->keys_num = keys_num;
926 cf_cb->ctx = plugin_get_ctx ();
928 cf_cb->next = first_callback;
929 first_callback = cf_cb;
930 } /* void cf_register */
932 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
934 cf_complex_callback_t *new;
936 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
940 new->type = strdup (type);
941 if (new->type == NULL)
947 new->callback = callback;
950 new->ctx = plugin_get_ctx ();
952 if (complex_callback_head == NULL)
954 complex_callback_head = new;
958 cf_complex_callback_t *last = complex_callback_head;
959 while (last->next != NULL)
965 } /* int cf_register_complex */
967 int cf_read (char *filename)
969 oconfig_item_t *conf;
972 conf = cf_read_generic (filename, 0 /* depth */);
975 ERROR ("Unable to read config file %s.", filename);
979 for (i = 0; i < conf->children_num; i++)
981 if (conf->children[i].children == NULL)
982 dispatch_value (conf->children + i);
984 dispatch_block (conf->children + i);
989 /* Read the default types.db if no `TypesDB' option was given. */
990 if (cf_default_typesdb)
991 read_types_list (PKGDATADIR"/types.db");
996 /* Assures the config option is a string, duplicates it and returns the copy in
997 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
999 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1003 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1005 ERROR ("cf_util_get_string: The %s option requires "
1006 "exactly one string argument.", ci->key);
1010 string = strdup (ci->values[0].value.string);
1014 if (*ret_string != NULL)
1015 sfree (*ret_string);
1016 *ret_string = string;
1019 } /* }}} int cf_util_get_string */
1021 /* Assures the config option is a string and copies it to the provided buffer.
1022 * Assures null-termination. */
1023 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1026 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1029 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1031 ERROR ("cf_util_get_string_buffer: The %s option requires "
1032 "exactly one string argument.", ci->key);
1036 strncpy (buffer, ci->values[0].value.string, buffer_size);
1037 buffer[buffer_size - 1] = 0;
1040 } /* }}} int cf_util_get_string_buffer */
1042 /* Assures the config option is a number and returns it as an int. */
1043 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1045 if ((ci == NULL) || (ret_value == NULL))
1048 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1050 ERROR ("cf_util_get_int: The %s option requires "
1051 "exactly one numeric argument.", ci->key);
1055 *ret_value = (int) ci->values[0].value.number;
1058 } /* }}} int cf_util_get_int */
1060 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1062 if ((ci == NULL) || (ret_value == NULL))
1065 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1067 ERROR ("cf_util_get_double: The %s option requires "
1068 "exactly one numeric argument.", ci->key);
1072 *ret_value = ci->values[0].value.number;
1075 } /* }}} int cf_util_get_double */
1077 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1079 if ((ci == NULL) || (ret_bool == NULL))
1082 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1084 ERROR ("cf_util_get_boolean: The %s option requires "
1085 "exactly one boolean argument.", ci->key);
1089 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1092 } /* }}} int cf_util_get_boolean */
1094 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1095 unsigned int *ret_value, unsigned int flag)
1100 if (ret_value == NULL)
1104 status = cf_util_get_boolean (ci, &b);
1114 *ret_value &= ~flag;
1118 } /* }}} int cf_util_get_flag */
1120 /* Assures that the config option is a string or a number if the correct range
1121 * of 1-65535. The string is then converted to a port number using
1122 * `service_name_to_port_number' and returned.
1123 * Returns the port number in the range [1-65535] or less than zero upon
1125 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1129 if ((ci->values_num != 1)
1130 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1131 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1133 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1134 "exactly one string argument.", ci->key);
1138 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1139 return (service_name_to_port_number (ci->values[0].value.string));
1141 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1142 tmp = (int) (ci->values[0].value.number + 0.5);
1143 if ((tmp < 1) || (tmp > 65535))
1145 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1146 "a service name or a port number. The number "
1147 "you specified, %i, is not in the valid "
1148 "range of 1-65535.",
1154 } /* }}} int cf_util_get_port_number */
1156 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *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_cdtime: The %s option requires "
1164 "exactly one numeric argument.", ci->key);
1168 if (ci->values[0].value.number < 0.0)
1170 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1171 "option must not be negative.", ci->key);
1175 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1178 } /* }}} int cf_util_get_cdtime */