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"},
122 {"MaxReadInterval", NULL, "86400"}
124 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
126 static int cf_default_typesdb = 1;
129 * Functions to handle register/unregister, search, and other plugin related
132 static cf_callback_t *cf_search (const char *type)
134 cf_callback_t *cf_cb;
139 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
140 if (strcasecmp (cf_cb->type, type) == 0)
146 static int cf_dispatch (const char *type, const char *orig_key,
147 const char *orig_value)
149 cf_callback_t *cf_cb;
150 plugin_ctx_t old_ctx;
156 DEBUG ("type = %s, key = %s, value = %s",
158 ESCAPE_NULL(orig_key),
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);
199 DEBUG ("cf_dispatch: return (%i)", ret);
202 } /* int cf_dispatch */
204 static int dispatch_global_option (const oconfig_item_t *ci)
206 if (ci->values_num != 1)
208 if (ci->values[0].type == OCONFIG_TYPE_STRING)
209 return (global_option_set (ci->key, ci->values[0].value.string));
210 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
213 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
214 return (global_option_set (ci->key, tmp));
216 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
218 if (ci->values[0].value.boolean)
219 return (global_option_set (ci->key, "true"));
221 return (global_option_set (ci->key, "false"));
225 } /* int dispatch_global_option */
227 static int dispatch_value_typesdb (const oconfig_item_t *ci)
231 assert (strcasecmp (ci->key, "TypesDB") == 0);
233 cf_default_typesdb = 0;
235 if (ci->values_num < 1) {
236 ERROR ("configfile: `TypesDB' needs at least one argument.");
240 for (i = 0; i < ci->values_num; ++i)
242 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
243 WARNING ("configfile: TypesDB: Skipping %i. argument which "
244 "is not a string.", i + 1);
248 read_types_list (ci->values[i].value.string);
251 } /* int dispatch_value_typesdb */
253 static int dispatch_value_plugindir (const oconfig_item_t *ci)
255 assert (strcasecmp (ci->key, "PluginDir") == 0);
257 if (ci->values_num != 1)
259 if (ci->values[0].type != OCONFIG_TYPE_STRING)
262 plugin_set_dir (ci->values[0].value.string);
266 static int dispatch_loadplugin (const oconfig_item_t *ci)
270 unsigned int flags = 0;
272 plugin_ctx_t old_ctx;
275 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
277 if (ci->values_num != 1)
279 if (ci->values[0].type != OCONFIG_TYPE_STRING)
282 name = ci->values[0].value.string;
284 /* default to the global interval set before loading this plugin */
285 memset (&ctx, 0, sizeof (ctx));
286 ctx.interval = cf_get_default_interval ();
288 for (i = 0; i < ci->children_num; ++i) {
289 if (strcasecmp("Globals", ci->children[i].key) == 0)
290 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
291 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
292 if (cf_util_get_cdtime (ci->children + i, &ctx.interval) != 0) {
293 /* cf_util_get_cdtime will log an error */
298 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
300 ci->children[i].key, ci->values[0].value.string);
304 old_ctx = plugin_set_ctx (ctx);
305 ret_val = plugin_load (name, (uint32_t) flags);
306 /* reset to the "global" context */
307 plugin_set_ctx (old_ctx);
310 } /* int dispatch_value_loadplugin */
312 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
320 buffer_free = sizeof (buffer);
322 for (i = 0; i < ci->values_num; i++)
326 if (ci->values[i].type == OCONFIG_TYPE_STRING)
327 status = ssnprintf (buffer_ptr, buffer_free, " %s",
328 ci->values[i].value.string);
329 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
330 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
331 ci->values[i].value.number);
332 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
333 status = ssnprintf (buffer_ptr, buffer_free, " %s",
334 ci->values[i].value.boolean
337 if ((status < 0) || (status >= buffer_free))
339 buffer_free -= status;
340 buffer_ptr += status;
342 /* skip the initial space */
343 buffer_ptr = buffer + 1;
345 return (cf_dispatch (plugin, ci->key, buffer_ptr));
346 } /* int dispatch_value_plugin */
348 static int dispatch_value (const oconfig_item_t *ci)
353 for (i = 0; i < cf_value_map_num; i++)
354 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
356 ret = cf_value_map[i].func (ci);
360 for (i = 0; i < cf_global_options_num; i++)
361 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
363 ret = dispatch_global_option (ci);
368 } /* int dispatch_value */
370 static int dispatch_block_plugin (oconfig_item_t *ci)
375 cf_complex_callback_t *cb;
377 if (strcasecmp (ci->key, "Plugin") != 0)
379 if (ci->values_num < 1)
381 if (ci->values[0].type != OCONFIG_TYPE_STRING)
384 name = ci->values[0].value.string;
386 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
390 status = plugin_load (name, /* flags = */ 0);
393 ERROR ("Automatically loading plugin \"%s\" failed "
394 "with status %i.", name, status);
399 /* Check for a complex callback first */
400 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
402 if (strcasecmp (name, cb->type) == 0)
404 plugin_ctx_t old_ctx;
407 old_ctx = plugin_set_ctx (cb->ctx);
408 ret_val = (cb->callback (ci));
409 plugin_set_ctx (old_ctx);
414 /* Hm, no complex plugin found. Dispatch the values one by one */
415 for (i = 0; i < ci->children_num; i++)
417 if (ci->children[i].children == NULL)
418 dispatch_value_plugin (name, ci->children + i);
421 WARNING ("There is a `%s' block within the "
422 "configuration for the %s plugin. "
423 "The plugin either only expects "
424 "\"simple\" configuration statements "
425 "or wasn't loaded using `LoadPlugin'."
426 " Please check your configuration.",
427 ci->children[i].key, name);
435 static int dispatch_block (oconfig_item_t *ci)
437 if (strcasecmp (ci->key, "LoadPlugin") == 0)
438 return (dispatch_loadplugin (ci));
439 else if (strcasecmp (ci->key, "Plugin") == 0)
440 return (dispatch_block_plugin (ci));
441 else if (strcasecmp (ci->key, "Chain") == 0)
442 return (fc_configure (ci));
447 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
450 oconfig_item_t *temp;
453 assert (offset >= 0);
454 assert (dst->children_num > offset);
456 /* Free the memory used by the replaced child. Usually that's the
457 * `Include "blah"' statement. */
458 temp = dst->children + offset;
459 for (i = 0; i < temp->values_num; i++)
461 if (temp->values[i].type == OCONFIG_TYPE_STRING)
463 sfree (temp->values[i].value.string);
466 sfree (temp->values);
469 /* If (src->children_num == 0) the array size is decreased. If offset
470 * is _not_ the last element, (offset < (dst->children_num - 1)), then
471 * we need to move the trailing elements before resizing the array. */
472 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
474 int nmemb = dst->children_num - (offset + 1);
475 memmove (dst->children + offset, dst->children + offset + 1,
476 sizeof (oconfig_item_t) * nmemb);
479 /* Resize the memory containing the children to be big enough to hold
481 if (dst->children_num + src->children_num - 1 == 0)
483 dst->children_num = 0;
487 temp = (oconfig_item_t *) realloc (dst->children,
488 sizeof (oconfig_item_t)
489 * (dst->children_num + src->children_num - 1));
492 ERROR ("configfile: realloc failed.");
495 dst->children = temp;
497 /* If there are children behind the include statement, and they have
498 * not yet been moved because (src->children_num == 0), then move them
499 * to the end of the list, so that the new children have room before
501 if ((src->children_num > 0)
502 && ((dst->children_num - (offset + 1)) > 0))
504 int nmemb = dst->children_num - (offset + 1);
505 int old_offset = offset + 1;
506 int new_offset = offset + src->children_num;
508 memmove (dst->children + new_offset,
509 dst->children + old_offset,
510 sizeof (oconfig_item_t) * nmemb);
513 /* Last but not least: If there are new children, copy them to the
514 * memory reserved for them. */
515 if (src->children_num > 0)
517 memcpy (dst->children + offset,
519 sizeof (oconfig_item_t) * src->children_num);
522 /* Update the number of children. */
523 dst->children_num += (src->children_num - 1);
526 } /* int cf_ci_replace_child */
528 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
530 oconfig_item_t *temp;
532 if ((src == NULL) || (src->children_num == 0))
535 temp = (oconfig_item_t *) realloc (dst->children,
536 sizeof (oconfig_item_t)
537 * (dst->children_num + src->children_num));
540 ERROR ("configfile: realloc failed.");
543 dst->children = temp;
545 memcpy (dst->children + dst->children_num,
547 sizeof (oconfig_item_t)
548 * src->children_num);
549 dst->children_num += src->children_num;
552 } /* int cf_ci_append_children */
554 #define CF_MAX_DEPTH 8
555 static oconfig_item_t *cf_read_generic (const char *path,
556 const char *pattern, int depth);
558 static int cf_include_all (oconfig_item_t *root, int depth)
562 for (i = 0; i < root->children_num; i++)
567 char *pattern = NULL;
571 if (strcasecmp (root->children[i].key, "Include") != 0)
574 old = root->children + i;
576 if ((old->values_num != 1)
577 || (old->values[0].type != OCONFIG_TYPE_STRING))
579 ERROR ("configfile: `Include' needs exactly one string argument.");
583 for (j = 0; j < old->children_num; ++j)
585 oconfig_item_t *child = old->children + j;
587 if (strcasecmp (child->key, "Filter") == 0)
588 cf_util_get_string (child, &pattern);
590 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
594 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
600 /* Now replace the i'th child in `root' with `new'. */
601 if (cf_ci_replace_child (root, new, i) < 0)
604 /* ... and go back to the new i'th child. */
609 } /* for (i = 0; i < root->children_num; i++) */
612 } /* int cf_include_all */
614 static oconfig_item_t *cf_read_file (const char *file,
615 const char *pattern, int depth)
617 oconfig_item_t *root;
620 assert (depth < CF_MAX_DEPTH);
622 if (pattern != NULL) {
623 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
624 char *tmp = sstrdup (file);
625 char *filename = basename (tmp);
627 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
628 DEBUG ("configfile: Not including `%s' because it "
629 "does not match pattern `%s'.",
637 ERROR ("configfile: Cannot apply pattern filter '%s' "
638 "to file '%s': functions basename() and / or "
639 "fnmatch() not available.", pattern, file);
640 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
643 root = oconfig_parse_file (file);
646 ERROR ("configfile: Cannot read file `%s'.", file);
650 status = cf_include_all (root, depth);
658 } /* oconfig_item_t *cf_read_file */
660 static int cf_compare_string (const void *p1, const void *p2)
662 return strcmp (*(const char **) p1, *(const char **) p2);
665 static oconfig_item_t *cf_read_dir (const char *dir,
666 const char *pattern, int depth)
668 oconfig_item_t *root = NULL;
671 char **filenames = NULL;
672 int filenames_num = 0;
676 assert (depth < CF_MAX_DEPTH);
682 ERROR ("configfile: opendir failed: %s",
683 sstrerror (errno, errbuf, sizeof (errbuf)));
687 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
690 ERROR ("configfile: malloc failed.");
693 memset (root, 0, sizeof (oconfig_item_t));
695 while ((de = readdir (dh)) != NULL)
700 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
703 status = ssnprintf (name, sizeof (name), "%s/%s",
705 if ((status < 0) || ((size_t) status >= sizeof (name)))
707 ERROR ("configfile: Not including `%s/%s' because its"
708 " name is too long.",
710 for (i = 0; i < filenames_num; ++i)
718 tmp = (char **) realloc (filenames,
719 filenames_num * sizeof (*filenames));
721 ERROR ("configfile: realloc failed.");
722 for (i = 0; i < filenames_num - 1; ++i)
730 filenames[filenames_num - 1] = sstrdup (name);
733 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
736 for (i = 0; i < filenames_num; ++i)
738 oconfig_item_t *temp;
739 char *name = filenames[i];
741 temp = cf_read_generic (name, pattern, depth);
744 /* An error should already have been reported. */
749 cf_ci_append_children (root, temp);
750 sfree (temp->children);
758 } /* oconfig_item_t *cf_read_dir */
763 * Path is stat'ed and either cf_read_file or cf_read_dir is called
766 * There are two versions of this function: If `wordexp' exists shell wildcards
767 * will be expanded and the function will include all matches found. If
768 * `wordexp' (or, more precisely, it's header file) is not available the
769 * simpler function is used which does not do any such expansion.
772 static oconfig_item_t *cf_read_generic (const char *path,
773 const char *pattern, int depth)
775 oconfig_item_t *root = NULL;
777 const char *path_ptr;
781 if (depth >= CF_MAX_DEPTH)
783 ERROR ("configfile: Not including `%s' because the maximum "
784 "nesting depth has been reached.", path);
788 status = wordexp (path, &we, WRDE_NOCMD);
791 ERROR ("configfile: wordexp (%s) failed.", path);
795 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
798 ERROR ("configfile: malloc failed.");
801 memset (root, '\0', sizeof (oconfig_item_t));
803 /* wordexp() might return a sorted list already. That's not
804 * documented though, so let's make sure we get what we want. */
805 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
808 for (i = 0; i < we.we_wordc; i++)
810 oconfig_item_t *temp;
813 path_ptr = we.we_wordv[i];
815 status = stat (path_ptr, &statbuf);
819 WARNING ("configfile: stat (%s) failed: %s",
821 sstrerror (errno, errbuf, sizeof (errbuf)));
825 if (S_ISREG (statbuf.st_mode))
826 temp = cf_read_file (path_ptr, pattern, depth);
827 else if (S_ISDIR (statbuf.st_mode))
828 temp = cf_read_dir (path_ptr, pattern, depth);
831 WARNING ("configfile: %s is neither a file nor a "
841 cf_ci_append_children (root, temp);
842 sfree (temp->children);
849 } /* oconfig_item_t *cf_read_generic */
850 /* #endif HAVE_WORDEXP_H */
852 #else /* if !HAVE_WORDEXP_H */
853 static oconfig_item_t *cf_read_generic (const char *path,
854 const char *pattern, int depth)
859 if (depth >= CF_MAX_DEPTH)
861 ERROR ("configfile: Not including `%s' because the maximum "
862 "nesting depth has been reached.", path);
866 status = stat (path, &statbuf);
870 ERROR ("configfile: stat (%s) failed: %s",
872 sstrerror (errno, errbuf, sizeof (errbuf)));
876 if (S_ISREG (statbuf.st_mode))
877 return (cf_read_file (path, pattern, depth));
878 else if (S_ISDIR (statbuf.st_mode))
879 return (cf_read_dir (path, pattern, depth));
881 ERROR ("configfile: %s is neither a file nor a directory.", path);
883 } /* oconfig_item_t *cf_read_generic */
884 #endif /* !HAVE_WORDEXP_H */
889 int global_option_set (const char *option, const char *value)
893 DEBUG ("option = %s; value = %s;", option, value);
895 for (i = 0; i < cf_global_options_num; i++)
896 if (strcasecmp (cf_global_options[i].key, option) == 0)
899 if (i >= cf_global_options_num)
902 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
904 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
905 "command-line option `-P' take precedence.");
909 sfree (cf_global_options[i].value);
912 cf_global_options[i].value = strdup (value);
914 cf_global_options[i].value = NULL;
919 const char *global_option_get (const char *option)
923 for (i = 0; i < cf_global_options_num; i++)
924 if (strcasecmp (cf_global_options[i].key, option) == 0)
927 if (i >= cf_global_options_num)
930 return ((cf_global_options[i].value != NULL)
931 ? cf_global_options[i].value
932 : cf_global_options[i].def);
933 } /* char *global_option_get */
935 long global_option_get_long (const char *option, long default_value)
940 str = global_option_get (option);
942 return (default_value);
945 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
947 return (default_value);
950 } /* char *global_option_get_long */
952 cdtime_t cf_get_default_interval (void)
954 char const *str = global_option_get ("Interval");
955 double interval_double = COLLECTD_DEFAULT_INTERVAL;
960 double tmp = strtod (str, &endptr);
962 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
963 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
966 ERROR ("cf_get_default_interval: Interval must be a positive number. "
967 "The current number is %g.", tmp);
969 interval_double = tmp;
972 return (DOUBLE_TO_CDTIME_T (interval_double));
973 } /* }}} cdtime_t cf_get_default_interval */
975 void cf_unregister (const char *type)
977 cf_callback_t *this, *prev;
979 for (prev = NULL, this = first_callback;
981 prev = this, this = this->next)
982 if (strcasecmp (this->type, type) == 0)
985 first_callback = this->next;
987 prev->next = this->next;
992 } /* void cf_unregister */
994 void cf_unregister_complex (const char *type)
996 cf_complex_callback_t *this, *prev;
998 for (prev = NULL, this = complex_callback_head;
1000 prev = this, this = this->next)
1001 if (strcasecmp (this->type, type) == 0)
1004 complex_callback_head = this->next;
1006 prev->next = this->next;
1012 } /* void cf_unregister */
1014 void cf_register (const char *type,
1015 int (*callback) (const char *, const char *),
1016 const char **keys, int keys_num)
1018 cf_callback_t *cf_cb;
1020 /* Remove this module from the list, if it already exists */
1021 cf_unregister (type);
1023 /* This pointer will be free'd in `cf_unregister' */
1024 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1028 cf_cb->callback = callback;
1030 cf_cb->keys_num = keys_num;
1031 cf_cb->ctx = plugin_get_ctx ();
1033 cf_cb->next = first_callback;
1034 first_callback = cf_cb;
1035 } /* void cf_register */
1037 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1039 cf_complex_callback_t *new;
1041 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1045 new->type = strdup (type);
1046 if (new->type == NULL)
1052 new->callback = callback;
1055 new->ctx = plugin_get_ctx ();
1057 if (complex_callback_head == NULL)
1059 complex_callback_head = new;
1063 cf_complex_callback_t *last = complex_callback_head;
1064 while (last->next != NULL)
1070 } /* int cf_register_complex */
1072 int cf_read (char *filename)
1074 oconfig_item_t *conf;
1077 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1080 ERROR ("Unable to read config file %s.", filename);
1083 else if (conf->children_num == 0)
1085 ERROR ("Configuration file %s is empty.", filename);
1086 oconfig_free (conf);
1090 for (i = 0; i < conf->children_num; i++)
1092 if (conf->children[i].children == NULL)
1093 dispatch_value (conf->children + i);
1095 dispatch_block (conf->children + i);
1098 oconfig_free (conf);
1100 /* Read the default types.db if no `TypesDB' option was given. */
1101 if (cf_default_typesdb)
1102 read_types_list (PKGDATADIR"/types.db");
1107 /* Assures the config option is a string, duplicates it and returns the copy in
1108 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1110 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1114 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1116 ERROR ("cf_util_get_string: The %s option requires "
1117 "exactly one string argument.", ci->key);
1121 string = strdup (ci->values[0].value.string);
1125 if (*ret_string != NULL)
1126 sfree (*ret_string);
1127 *ret_string = string;
1130 } /* }}} int cf_util_get_string */
1132 /* Assures the config option is a string and copies it to the provided buffer.
1133 * Assures null-termination. */
1134 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1137 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1140 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1142 ERROR ("cf_util_get_string_buffer: The %s option requires "
1143 "exactly one string argument.", ci->key);
1147 strncpy (buffer, ci->values[0].value.string, buffer_size);
1148 buffer[buffer_size - 1] = 0;
1151 } /* }}} int cf_util_get_string_buffer */
1153 /* Assures the config option is a number and returns it as an int. */
1154 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1156 if ((ci == NULL) || (ret_value == NULL))
1159 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1161 ERROR ("cf_util_get_int: The %s option requires "
1162 "exactly one numeric argument.", ci->key);
1166 *ret_value = (int) ci->values[0].value.number;
1169 } /* }}} int cf_util_get_int */
1171 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1173 if ((ci == NULL) || (ret_value == NULL))
1176 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1178 ERROR ("cf_util_get_double: The %s option requires "
1179 "exactly one numeric argument.", ci->key);
1183 *ret_value = ci->values[0].value.number;
1186 } /* }}} int cf_util_get_double */
1188 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1190 if ((ci == NULL) || (ret_bool == NULL))
1193 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1195 ERROR ("cf_util_get_boolean: The %s option requires "
1196 "exactly one boolean argument.", ci->key);
1200 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1203 } /* }}} int cf_util_get_boolean */
1205 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1206 unsigned int *ret_value, unsigned int flag)
1211 if (ret_value == NULL)
1215 status = cf_util_get_boolean (ci, &b);
1225 *ret_value &= ~flag;
1229 } /* }}} int cf_util_get_flag */
1231 /* Assures that the config option is a string or a number if the correct range
1232 * of 1-65535. The string is then converted to a port number using
1233 * `service_name_to_port_number' and returned.
1234 * Returns the port number in the range [1-65535] or less than zero upon
1236 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1240 if ((ci->values_num != 1)
1241 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1242 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1244 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1245 "exactly one string argument.", ci->key);
1249 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1250 return (service_name_to_port_number (ci->values[0].value.string));
1252 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1253 tmp = (int) (ci->values[0].value.number + 0.5);
1254 if ((tmp < 1) || (tmp > 65535))
1256 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1257 "a service name or a port number. The number "
1258 "you specified, %i, is not in the valid "
1259 "range of 1-65535.",
1265 } /* }}} int cf_util_get_port_number */
1267 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1273 if (ci->values_num != 1)
1275 ERROR ("cf_util_get_service: The %s option requires exactly "
1276 "one argument.", ci->key);
1280 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1281 return (cf_util_get_string (ci, ret_string));
1282 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1284 ERROR ("cf_util_get_service: The %s option requires "
1285 "exactly one string or numeric argument.",
1290 status = cf_util_get_int (ci, &port);
1293 else if ((port < 1) || (port > 65535))
1295 ERROR ("cf_util_get_service: The port number given "
1296 "for the %s option is out of "
1297 "range (%i).", ci->key, port);
1301 service = malloc (6);
1302 if (service == NULL)
1304 ERROR ("cf_util_get_service: Out of memory.");
1307 ssnprintf (service, 6, "%i", port);
1309 sfree (*ret_string);
1310 *ret_string = service;
1313 } /* }}} int cf_util_get_service */
1315 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1317 if ((ci == NULL) || (ret_value == NULL))
1320 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1322 ERROR ("cf_util_get_cdtime: The %s option requires "
1323 "exactly one numeric argument.", ci->key);
1327 if (ci->values[0].value.number < 0.0)
1329 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1330 "option must not be negative.", ci->key);
1334 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1337 } /* }}} int cf_util_get_cdtime */