2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian tokkee Harl <sh at tokkee.org>
30 #include "liboconfig/oconfig.h"
34 #include "configfile.h"
35 #include "types_list.h"
36 #include "filter_chain.h"
40 #endif /* HAVE_WORDEXP_H */
44 #endif /* HAVE_FNMATCH_H */
48 #endif /* HAVE_LIBGEN_H */
50 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
55 typedef struct cf_callback
58 int (*callback) (const char *, const char *);
62 struct cf_callback *next;
65 typedef struct cf_complex_callback_s
68 int (*callback) (oconfig_item_t *);
70 struct cf_complex_callback_s *next;
71 } cf_complex_callback_t;
73 typedef struct cf_value_map_s
76 int (*func) (const oconfig_item_t *);
79 typedef struct cf_global_option_s
87 * Prototypes of callback functions
89 static int dispatch_value_typesdb (const oconfig_item_t *ci);
90 static int dispatch_value_plugindir (const oconfig_item_t *ci);
91 static int dispatch_loadplugin (const oconfig_item_t *ci);
96 static cf_callback_t *first_callback = NULL;
97 static cf_complex_callback_t *complex_callback_head = NULL;
99 static cf_value_map_t cf_value_map[] =
101 {"TypesDB", dispatch_value_typesdb},
102 {"PluginDir", dispatch_value_plugindir},
103 {"LoadPlugin", dispatch_loadplugin}
105 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
107 static cf_global_option_t cf_global_options[] =
109 {"BaseDir", NULL, PKGLOCALSTATEDIR},
110 {"PIDFile", NULL, PIDFILE},
111 {"Hostname", NULL, NULL},
112 {"FQDNLookup", NULL, "true"},
113 {"Interval", NULL, NULL},
114 {"ReadThreads", NULL, "5"},
115 {"WriteThreads", NULL, "5"},
116 {"WriteQueueLimitHigh", NULL, NULL},
117 {"WriteQueueLimitLow", NULL, NULL},
118 {"Timeout", NULL, "2"},
119 {"AutoLoadPlugin", NULL, "false"},
120 {"PreCacheChain", NULL, "PreCache"},
121 {"PostCacheChain", NULL, "PostCache"}
123 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
125 static int cf_default_typesdb = 1;
128 * Functions to handle register/unregister, search, and other plugin related
131 static cf_callback_t *cf_search (const char *type)
133 cf_callback_t *cf_cb;
138 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
139 if (strcasecmp (cf_cb->type, type) == 0)
145 static int cf_dispatch (const char *type, const char *orig_key,
146 const char *orig_value)
148 cf_callback_t *cf_cb;
149 plugin_ctx_t old_ctx;
155 DEBUG ("type = %s, key = %s, value = %s",
157 ESCAPE_NULL(orig_key),
158 ESCAPE_NULL(orig_value));
160 if ((cf_cb = cf_search (type)) == NULL)
162 WARNING ("Found a configuration for the `%s' plugin, but "
163 "the plugin isn't loaded or didn't register "
164 "a configuration callback.", type);
168 if ((key = strdup (orig_key)) == NULL)
170 if ((value = strdup (orig_value)) == NULL)
178 old_ctx = plugin_set_ctx (cf_cb->ctx);
180 for (i = 0; i < cf_cb->keys_num; i++)
182 if ((cf_cb->keys[i] != NULL)
183 && (strcasecmp (cf_cb->keys[i], key) == 0))
185 ret = (*cf_cb->callback) (key, value);
190 plugin_set_ctx (old_ctx);
192 if (i >= cf_cb->keys_num)
193 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
198 DEBUG ("cf_dispatch: return (%i)", ret);
201 } /* int cf_dispatch */
203 static int dispatch_global_option (const oconfig_item_t *ci)
205 if (ci->values_num != 1)
207 if (ci->values[0].type == OCONFIG_TYPE_STRING)
208 return (global_option_set (ci->key, ci->values[0].value.string));
209 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
212 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
213 return (global_option_set (ci->key, tmp));
215 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
217 if (ci->values[0].value.boolean)
218 return (global_option_set (ci->key, "true"));
220 return (global_option_set (ci->key, "false"));
224 } /* int dispatch_global_option */
226 static int dispatch_value_typesdb (const oconfig_item_t *ci)
230 assert (strcasecmp (ci->key, "TypesDB") == 0);
232 cf_default_typesdb = 0;
234 if (ci->values_num < 1) {
235 ERROR ("configfile: `TypesDB' needs at least one argument.");
239 for (i = 0; i < ci->values_num; ++i)
241 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
242 WARNING ("configfile: TypesDB: Skipping %i. argument which "
243 "is not a string.", i + 1);
247 read_types_list (ci->values[i].value.string);
250 } /* int dispatch_value_typesdb */
252 static int dispatch_value_plugindir (const oconfig_item_t *ci)
254 assert (strcasecmp (ci->key, "PluginDir") == 0);
256 if (ci->values_num != 1)
258 if (ci->values[0].type != OCONFIG_TYPE_STRING)
261 plugin_set_dir (ci->values[0].value.string);
265 static int dispatch_loadplugin (const oconfig_item_t *ci)
269 unsigned int flags = 0;
271 plugin_ctx_t old_ctx;
274 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
276 if (ci->values_num != 1)
278 if (ci->values[0].type != OCONFIG_TYPE_STRING)
281 name = ci->values[0].value.string;
283 /* default to the global interval set before loading this plugin */
284 memset (&ctx, 0, sizeof (ctx));
285 ctx.interval = cf_get_default_interval ();
287 for (i = 0; i < ci->children_num; ++i) {
288 if (strcasecmp("Globals", ci->children[i].key) == 0)
289 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
290 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
291 double interval = 0.0;
293 if (cf_util_get_double (ci->children + i, &interval) != 0) {
294 /* cf_util_get_double will log an error */
298 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
301 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
303 ci->children[i].key, ci->values[0].value.string);
307 old_ctx = plugin_set_ctx (ctx);
308 ret_val = plugin_load (name, (uint32_t) flags);
309 /* reset to the "global" context */
310 plugin_set_ctx (old_ctx);
313 } /* int dispatch_value_loadplugin */
315 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
323 buffer_free = sizeof (buffer);
325 for (i = 0; i < ci->values_num; i++)
329 if (ci->values[i].type == OCONFIG_TYPE_STRING)
330 status = ssnprintf (buffer_ptr, buffer_free, " %s",
331 ci->values[i].value.string);
332 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
333 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
334 ci->values[i].value.number);
335 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
336 status = ssnprintf (buffer_ptr, buffer_free, " %s",
337 ci->values[i].value.boolean
340 if ((status < 0) || (status >= buffer_free))
342 buffer_free -= status;
343 buffer_ptr += status;
345 /* skip the initial space */
346 buffer_ptr = buffer + 1;
348 return (cf_dispatch (plugin, ci->key, buffer_ptr));
349 } /* int dispatch_value_plugin */
351 static int dispatch_value (const oconfig_item_t *ci)
356 for (i = 0; i < cf_value_map_num; i++)
357 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
359 ret = cf_value_map[i].func (ci);
363 for (i = 0; i < cf_global_options_num; i++)
364 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
366 ret = dispatch_global_option (ci);
371 } /* int dispatch_value */
373 static int dispatch_block_plugin (oconfig_item_t *ci)
378 cf_complex_callback_t *cb;
380 if (strcasecmp (ci->key, "Plugin") != 0)
382 if (ci->values_num < 1)
384 if (ci->values[0].type != OCONFIG_TYPE_STRING)
387 name = ci->values[0].value.string;
389 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
393 status = plugin_load (name, /* flags = */ 0);
396 ERROR ("Automatically loading plugin \"%s\" failed "
397 "with status %i.", name, status);
402 /* Check for a complex callback first */
403 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
405 if (strcasecmp (name, cb->type) == 0)
407 plugin_ctx_t old_ctx;
410 old_ctx = plugin_set_ctx (cb->ctx);
411 ret_val = (cb->callback (ci));
412 plugin_set_ctx (old_ctx);
417 /* Hm, no complex plugin found. Dispatch the values one by one */
418 for (i = 0; i < ci->children_num; i++)
420 if (ci->children[i].children == NULL)
421 dispatch_value_plugin (name, ci->children + i);
424 WARNING ("There is a `%s' block within the "
425 "configuration for the %s plugin. "
426 "The plugin either only expects "
427 "\"simple\" configuration statements "
428 "or wasn't loaded using `LoadPlugin'."
429 " Please check your configuration.",
430 ci->children[i].key, name);
438 static int dispatch_block (oconfig_item_t *ci)
440 if (strcasecmp (ci->key, "LoadPlugin") == 0)
441 return (dispatch_loadplugin (ci));
442 else if (strcasecmp (ci->key, "Plugin") == 0)
443 return (dispatch_block_plugin (ci));
444 else if (strcasecmp (ci->key, "Chain") == 0)
445 return (fc_configure (ci));
450 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
453 oconfig_item_t *temp;
456 assert (offset >= 0);
457 assert (dst->children_num > offset);
459 /* Free the memory used by the replaced child. Usually that's the
460 * `Include "blah"' statement. */
461 temp = dst->children + offset;
462 for (i = 0; i < temp->values_num; i++)
464 if (temp->values[i].type == OCONFIG_TYPE_STRING)
466 sfree (temp->values[i].value.string);
469 sfree (temp->values);
472 /* If (src->children_num == 0) the array size is decreased. If offset
473 * is _not_ the last element, (offset < (dst->children_num - 1)), then
474 * we need to move the trailing elements before resizing the array. */
475 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
477 int nmemb = dst->children_num - (offset + 1);
478 memmove (dst->children + offset, dst->children + offset + 1,
479 sizeof (oconfig_item_t) * nmemb);
482 /* Resize the memory containing the children to be big enough to hold
484 if (dst->children_num + src->children_num - 1 == 0)
486 dst->children_num = 0;
490 temp = (oconfig_item_t *) realloc (dst->children,
491 sizeof (oconfig_item_t)
492 * (dst->children_num + src->children_num - 1));
495 ERROR ("configfile: realloc failed.");
498 dst->children = temp;
500 /* If there are children behind the include statement, and they have
501 * not yet been moved because (src->children_num == 0), then move them
502 * to the end of the list, so that the new children have room before
504 if ((src->children_num > 0)
505 && ((dst->children_num - (offset + 1)) > 0))
507 int nmemb = dst->children_num - (offset + 1);
508 int old_offset = offset + 1;
509 int new_offset = offset + src->children_num;
511 memmove (dst->children + new_offset,
512 dst->children + old_offset,
513 sizeof (oconfig_item_t) * nmemb);
516 /* Last but not least: If there are new children, copy them to the
517 * memory reserved for them. */
518 if (src->children_num > 0)
520 memcpy (dst->children + offset,
522 sizeof (oconfig_item_t) * src->children_num);
525 /* Update the number of children. */
526 dst->children_num += (src->children_num - 1);
529 } /* int cf_ci_replace_child */
531 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
533 oconfig_item_t *temp;
535 if ((src == NULL) || (src->children_num == 0))
538 temp = (oconfig_item_t *) realloc (dst->children,
539 sizeof (oconfig_item_t)
540 * (dst->children_num + src->children_num));
543 ERROR ("configfile: realloc failed.");
546 dst->children = temp;
548 memcpy (dst->children + dst->children_num,
550 sizeof (oconfig_item_t)
551 * src->children_num);
552 dst->children_num += src->children_num;
555 } /* int cf_ci_append_children */
557 #define CF_MAX_DEPTH 8
558 static oconfig_item_t *cf_read_generic (const char *path,
559 const char *pattern, int depth);
561 static int cf_include_all (oconfig_item_t *root, int depth)
565 for (i = 0; i < root->children_num; i++)
570 char *pattern = NULL;
574 if (strcasecmp (root->children[i].key, "Include") != 0)
577 old = root->children + i;
579 if ((old->values_num != 1)
580 || (old->values[0].type != OCONFIG_TYPE_STRING))
582 ERROR ("configfile: `Include' needs exactly one string argument.");
586 for (j = 0; j < old->children_num; ++j)
588 oconfig_item_t *child = old->children + j;
590 if (strcasecmp (child->key, "Filter") == 0)
591 cf_util_get_string (child, &pattern);
593 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
597 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
603 /* Now replace the i'th child in `root' with `new'. */
604 if (cf_ci_replace_child (root, new, i) < 0)
607 /* ... and go back to the new i'th child. */
612 } /* for (i = 0; i < root->children_num; i++) */
615 } /* int cf_include_all */
617 static oconfig_item_t *cf_read_file (const char *file,
618 const char *pattern, int depth)
620 oconfig_item_t *root;
623 assert (depth < CF_MAX_DEPTH);
625 if (pattern != NULL) {
626 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
627 char *tmp = sstrdup (file);
628 char *filename = basename (tmp);
630 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
631 DEBUG ("configfile: Not including `%s' because it "
632 "does not match pattern `%s'.",
640 ERROR ("configfile: Cannot apply pattern filter '%s' "
641 "to file '%s': functions basename() and / or "
642 "fnmatch() not available.", pattern, file);
643 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
646 root = oconfig_parse_file (file);
649 ERROR ("configfile: Cannot read file `%s'.", file);
653 status = cf_include_all (root, depth);
661 } /* oconfig_item_t *cf_read_file */
663 static int cf_compare_string (const void *p1, const void *p2)
665 return strcmp (*(const char **) p1, *(const char **) p2);
668 static oconfig_item_t *cf_read_dir (const char *dir,
669 const char *pattern, int depth)
671 oconfig_item_t *root = NULL;
674 char **filenames = NULL;
675 int filenames_num = 0;
679 assert (depth < CF_MAX_DEPTH);
685 ERROR ("configfile: opendir failed: %s",
686 sstrerror (errno, errbuf, sizeof (errbuf)));
690 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
693 ERROR ("configfile: malloc failed.");
696 memset (root, 0, sizeof (oconfig_item_t));
698 while ((de = readdir (dh)) != NULL)
703 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
706 status = ssnprintf (name, sizeof (name), "%s/%s",
708 if ((status < 0) || ((size_t) status >= sizeof (name)))
710 ERROR ("configfile: Not including `%s/%s' because its"
711 " name is too long.",
713 for (i = 0; i < filenames_num; ++i)
721 tmp = (char **) realloc (filenames,
722 filenames_num * sizeof (*filenames));
724 ERROR ("configfile: realloc failed.");
725 for (i = 0; i < filenames_num - 1; ++i)
733 filenames[filenames_num - 1] = sstrdup (name);
736 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
739 for (i = 0; i < filenames_num; ++i)
741 oconfig_item_t *temp;
742 char *name = filenames[i];
744 temp = cf_read_generic (name, pattern, depth);
747 /* An error should already have been reported. */
752 cf_ci_append_children (root, temp);
753 sfree (temp->children);
761 } /* oconfig_item_t *cf_read_dir */
766 * Path is stat'ed and either cf_read_file or cf_read_dir is called
769 * There are two versions of this function: If `wordexp' exists shell wildcards
770 * will be expanded and the function will include all matches found. If
771 * `wordexp' (or, more precisely, it's header file) is not available the
772 * simpler function is used which does not do any such expansion.
775 static oconfig_item_t *cf_read_generic (const char *path,
776 const char *pattern, int depth)
778 oconfig_item_t *root = NULL;
780 const char *path_ptr;
784 if (depth >= CF_MAX_DEPTH)
786 ERROR ("configfile: Not including `%s' because the maximum "
787 "nesting depth has been reached.", path);
791 status = wordexp (path, &we, WRDE_NOCMD);
794 ERROR ("configfile: wordexp (%s) failed.", path);
798 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
801 ERROR ("configfile: malloc failed.");
804 memset (root, '\0', sizeof (oconfig_item_t));
806 /* wordexp() might return a sorted list already. That's not
807 * documented though, so let's make sure we get what we want. */
808 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
811 for (i = 0; i < we.we_wordc; i++)
813 oconfig_item_t *temp;
816 path_ptr = we.we_wordv[i];
818 status = stat (path_ptr, &statbuf);
822 WARNING ("configfile: stat (%s) failed: %s",
824 sstrerror (errno, errbuf, sizeof (errbuf)));
828 if (S_ISREG (statbuf.st_mode))
829 temp = cf_read_file (path_ptr, pattern, depth);
830 else if (S_ISDIR (statbuf.st_mode))
831 temp = cf_read_dir (path_ptr, pattern, depth);
834 WARNING ("configfile: %s is neither a file nor a "
844 cf_ci_append_children (root, temp);
845 sfree (temp->children);
852 } /* oconfig_item_t *cf_read_generic */
853 /* #endif HAVE_WORDEXP_H */
855 #else /* if !HAVE_WORDEXP_H */
856 static oconfig_item_t *cf_read_generic (const char *path,
857 const char *pattern, int depth)
862 if (depth >= CF_MAX_DEPTH)
864 ERROR ("configfile: Not including `%s' because the maximum "
865 "nesting depth has been reached.", path);
869 status = stat (path, &statbuf);
873 ERROR ("configfile: stat (%s) failed: %s",
875 sstrerror (errno, errbuf, sizeof (errbuf)));
879 if (S_ISREG (statbuf.st_mode))
880 return (cf_read_file (path, pattern, depth));
881 else if (S_ISDIR (statbuf.st_mode))
882 return (cf_read_dir (path, pattern, depth));
884 ERROR ("configfile: %s is neither a file nor a directory.", path);
886 } /* oconfig_item_t *cf_read_generic */
887 #endif /* !HAVE_WORDEXP_H */
892 int global_option_set (const char *option, const char *value)
896 DEBUG ("option = %s; value = %s;", option, value);
898 for (i = 0; i < cf_global_options_num; i++)
899 if (strcasecmp (cf_global_options[i].key, option) == 0)
902 if (i >= cf_global_options_num)
905 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
907 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
908 "command-line option `-P' take precedence.");
912 sfree (cf_global_options[i].value);
915 cf_global_options[i].value = strdup (value);
917 cf_global_options[i].value = NULL;
922 const char *global_option_get (const char *option)
926 for (i = 0; i < cf_global_options_num; i++)
927 if (strcasecmp (cf_global_options[i].key, option) == 0)
930 if (i >= cf_global_options_num)
933 return ((cf_global_options[i].value != NULL)
934 ? cf_global_options[i].value
935 : cf_global_options[i].def);
936 } /* char *global_option_get */
938 long global_option_get_long (const char *option, long default_value)
943 str = global_option_get (option);
945 return (default_value);
948 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
950 return (default_value);
953 } /* char *global_option_get_long */
955 cdtime_t cf_get_default_interval (void)
957 char const *str = global_option_get ("Interval");
958 double interval_double = COLLECTD_DEFAULT_INTERVAL;
963 double tmp = strtod (str, &endptr);
965 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
966 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
969 ERROR ("cf_get_default_interval: Interval must be a positive number. "
970 "The current number is %g.", tmp);
972 interval_double = tmp;
975 return (DOUBLE_TO_CDTIME_T (interval_double));
976 } /* }}} cdtime_t cf_get_default_interval */
978 void cf_unregister (const char *type)
980 cf_callback_t *this, *prev;
982 for (prev = NULL, this = first_callback;
984 prev = this, this = this->next)
985 if (strcasecmp (this->type, type) == 0)
988 first_callback = this->next;
990 prev->next = this->next;
995 } /* void cf_unregister */
997 void cf_unregister_complex (const char *type)
999 cf_complex_callback_t *this, *prev;
1001 for (prev = NULL, this = complex_callback_head;
1003 prev = this, this = this->next)
1004 if (strcasecmp (this->type, type) == 0)
1007 complex_callback_head = this->next;
1009 prev->next = this->next;
1015 } /* void cf_unregister */
1017 void cf_register (const char *type,
1018 int (*callback) (const char *, const char *),
1019 const char **keys, int keys_num)
1021 cf_callback_t *cf_cb;
1023 /* Remove this module from the list, if it already exists */
1024 cf_unregister (type);
1026 /* This pointer will be free'd in `cf_unregister' */
1027 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1031 cf_cb->callback = callback;
1033 cf_cb->keys_num = keys_num;
1034 cf_cb->ctx = plugin_get_ctx ();
1036 cf_cb->next = first_callback;
1037 first_callback = cf_cb;
1038 } /* void cf_register */
1040 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1042 cf_complex_callback_t *new;
1044 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1048 new->type = strdup (type);
1049 if (new->type == NULL)
1055 new->callback = callback;
1058 new->ctx = plugin_get_ctx ();
1060 if (complex_callback_head == NULL)
1062 complex_callback_head = new;
1066 cf_complex_callback_t *last = complex_callback_head;
1067 while (last->next != NULL)
1073 } /* int cf_register_complex */
1075 int cf_read (char *filename)
1077 oconfig_item_t *conf;
1080 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1083 ERROR ("Unable to read config file %s.", filename);
1086 else if (conf->children_num == 0)
1088 ERROR ("Configuration file %s is empty.", filename);
1089 oconfig_free (conf);
1093 for (i = 0; i < conf->children_num; i++)
1095 if (conf->children[i].children == NULL)
1096 dispatch_value (conf->children + i);
1098 dispatch_block (conf->children + i);
1101 oconfig_free (conf);
1103 /* Read the default types.db if no `TypesDB' option was given. */
1104 if (cf_default_typesdb)
1105 read_types_list (PKGDATADIR"/types.db");
1110 /* Assures the config option is a string, duplicates it and returns the copy in
1111 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1113 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1117 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1119 ERROR ("cf_util_get_string: The %s option requires "
1120 "exactly one string argument.", ci->key);
1124 string = strdup (ci->values[0].value.string);
1128 if (*ret_string != NULL)
1129 sfree (*ret_string);
1130 *ret_string = string;
1133 } /* }}} int cf_util_get_string */
1135 /* Assures the config option is a string and copies it to the provided buffer.
1136 * Assures null-termination. */
1137 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1140 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1143 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1145 ERROR ("cf_util_get_string_buffer: The %s option requires "
1146 "exactly one string argument.", ci->key);
1150 strncpy (buffer, ci->values[0].value.string, buffer_size);
1151 buffer[buffer_size - 1] = 0;
1154 } /* }}} int cf_util_get_string_buffer */
1156 /* Assures the config option is a number and returns it as an int. */
1157 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1159 if ((ci == NULL) || (ret_value == NULL))
1162 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1164 ERROR ("cf_util_get_int: The %s option requires "
1165 "exactly one numeric argument.", ci->key);
1169 *ret_value = (int) ci->values[0].value.number;
1172 } /* }}} int cf_util_get_int */
1174 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1176 if ((ci == NULL) || (ret_value == NULL))
1179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1181 ERROR ("cf_util_get_double: The %s option requires "
1182 "exactly one numeric argument.", ci->key);
1186 *ret_value = ci->values[0].value.number;
1189 } /* }}} int cf_util_get_double */
1191 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1193 if ((ci == NULL) || (ret_bool == NULL))
1196 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1198 ERROR ("cf_util_get_boolean: The %s option requires "
1199 "exactly one boolean argument.", ci->key);
1203 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1206 } /* }}} int cf_util_get_boolean */
1208 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1209 unsigned int *ret_value, unsigned int flag)
1214 if (ret_value == NULL)
1218 status = cf_util_get_boolean (ci, &b);
1228 *ret_value &= ~flag;
1232 } /* }}} int cf_util_get_flag */
1234 /* Assures that the config option is a string or a number if the correct range
1235 * of 1-65535. The string is then converted to a port number using
1236 * `service_name_to_port_number' and returned.
1237 * Returns the port number in the range [1-65535] or less than zero upon
1239 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1243 if ((ci->values_num != 1)
1244 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1245 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1247 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1248 "exactly one string argument.", ci->key);
1252 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1253 return (service_name_to_port_number (ci->values[0].value.string));
1255 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1256 tmp = (int) (ci->values[0].value.number + 0.5);
1257 if ((tmp < 1) || (tmp > 65535))
1259 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1260 "a service name or a port number. The number "
1261 "you specified, %i, is not in the valid "
1262 "range of 1-65535.",
1268 } /* }}} int cf_util_get_port_number */
1270 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1276 if (ci->values_num != 1)
1278 ERROR ("cf_util_get_service: The %s option requires exactly "
1279 "one argument.", ci->key);
1283 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1284 return (cf_util_get_string (ci, ret_string));
1285 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1287 ERROR ("cf_util_get_service: The %s option requires "
1288 "exactly one string or numeric argument.",
1293 status = cf_util_get_int (ci, &port);
1296 else if ((port < 1) || (port > 65535))
1298 ERROR ("cf_util_get_service: The port number given "
1299 "for the %s option is out of "
1300 "range (%i).", ci->key, port);
1304 service = malloc (6);
1305 if (service == NULL)
1307 ERROR ("cf_util_get_service: Out of memory.");
1310 ssnprintf (service, 6, "%i", port);
1312 sfree (*ret_string);
1313 *ret_string = service;
1316 } /* }}} int cf_util_get_service */
1318 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1320 if ((ci == NULL) || (ret_value == NULL))
1323 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1325 ERROR ("cf_util_get_cdtime: The %s option requires "
1326 "exactly one numeric argument.", ci->key);
1330 if (ci->values[0].value.number < 0.0)
1332 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1333 "option must not be negative.", ci->key);
1337 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1340 } /* }}} int cf_util_get_cdtime */