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 {"CollectInternalStats", NULL, "false"},
121 {"PreCacheChain", NULL, "PreCache"},
122 {"PostCacheChain", NULL, "PostCache"},
123 {"MaxReadInterval", NULL, "86400"}
125 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
127 static int cf_default_typesdb = 1;
130 * Functions to handle register/unregister, search, and other plugin related
133 static cf_callback_t *cf_search (const char *type)
135 cf_callback_t *cf_cb;
140 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
141 if (strcasecmp (cf_cb->type, type) == 0)
147 static int cf_dispatch (const char *type, const char *orig_key,
148 const char *orig_value)
150 cf_callback_t *cf_cb;
151 plugin_ctx_t old_ctx;
157 DEBUG ("type = %s, key = %s, value = %s",
159 ESCAPE_NULL(orig_key),
160 ESCAPE_NULL(orig_value));
162 if ((cf_cb = cf_search (type)) == NULL)
164 WARNING ("Found a configuration for the `%s' plugin, but "
165 "the plugin isn't loaded or didn't register "
166 "a configuration callback.", type);
170 if ((key = strdup (orig_key)) == NULL)
172 if ((value = strdup (orig_value)) == NULL)
180 old_ctx = plugin_set_ctx (cf_cb->ctx);
182 for (i = 0; i < cf_cb->keys_num; i++)
184 if ((cf_cb->keys[i] != NULL)
185 && (strcasecmp (cf_cb->keys[i], key) == 0))
187 ret = (*cf_cb->callback) (key, value);
192 plugin_set_ctx (old_ctx);
194 if (i >= cf_cb->keys_num)
195 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
200 DEBUG ("cf_dispatch: return (%i)", ret);
203 } /* int cf_dispatch */
205 static int dispatch_global_option (const oconfig_item_t *ci)
207 if (ci->values_num != 1)
209 if (ci->values[0].type == OCONFIG_TYPE_STRING)
210 return (global_option_set (ci->key, ci->values[0].value.string));
211 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
214 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
215 return (global_option_set (ci->key, tmp));
217 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
219 if (ci->values[0].value.boolean)
220 return (global_option_set (ci->key, "true"));
222 return (global_option_set (ci->key, "false"));
226 } /* int dispatch_global_option */
228 static int dispatch_value_typesdb (const oconfig_item_t *ci)
232 assert (strcasecmp (ci->key, "TypesDB") == 0);
234 cf_default_typesdb = 0;
236 if (ci->values_num < 1) {
237 ERROR ("configfile: `TypesDB' needs at least one argument.");
241 for (i = 0; i < ci->values_num; ++i)
243 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
244 WARNING ("configfile: TypesDB: Skipping %i. argument which "
245 "is not a string.", i + 1);
249 read_types_list (ci->values[i].value.string);
252 } /* int dispatch_value_typesdb */
254 static int dispatch_value_plugindir (const oconfig_item_t *ci)
256 assert (strcasecmp (ci->key, "PluginDir") == 0);
258 if (ci->values_num != 1)
260 if (ci->values[0].type != OCONFIG_TYPE_STRING)
263 plugin_set_dir (ci->values[0].value.string);
267 static int dispatch_loadplugin (const oconfig_item_t *ci)
271 unsigned int flags = 0;
273 plugin_ctx_t old_ctx;
276 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
278 if (ci->values_num != 1)
280 if (ci->values[0].type != OCONFIG_TYPE_STRING)
283 name = ci->values[0].value.string;
284 if (strcmp ("libvirt", name) == 0)
287 /* default to the global interval set before loading this plugin */
288 memset (&ctx, 0, sizeof (ctx));
289 ctx.interval = cf_get_default_interval ();
290 ctx.flush_interval = 0;
291 ctx.flush_timeout = 0;
293 for (i = 0; i < ci->children_num; ++i)
295 oconfig_item_t *child = ci->children + i;
297 if (strcasecmp("Globals", child->key) == 0)
298 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
299 else if (strcasecmp ("Interval", child->key) == 0)
300 cf_util_get_cdtime (child, &ctx.interval);
301 else if (strcasecmp ("FlushInterval", child->key) == 0)
302 cf_util_get_cdtime (child, &ctx.flush_interval);
303 else if (strcasecmp ("FlushTimeout", child->key) == 0)
304 cf_util_get_cdtime (child, &ctx.flush_timeout);
306 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
308 child->key, ci->values[0].value.string);
312 old_ctx = plugin_set_ctx (ctx);
313 ret_val = plugin_load (name, (uint32_t) flags);
314 /* reset to the "global" context */
315 plugin_set_ctx (old_ctx);
318 } /* int dispatch_value_loadplugin */
320 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
328 buffer_free = sizeof (buffer);
330 for (i = 0; i < ci->values_num; i++)
334 if (ci->values[i].type == OCONFIG_TYPE_STRING)
335 status = ssnprintf (buffer_ptr, buffer_free, " %s",
336 ci->values[i].value.string);
337 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
338 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
339 ci->values[i].value.number);
340 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
341 status = ssnprintf (buffer_ptr, buffer_free, " %s",
342 ci->values[i].value.boolean
345 if ((status < 0) || (status >= buffer_free))
347 buffer_free -= status;
348 buffer_ptr += status;
350 /* skip the initial space */
351 buffer_ptr = buffer + 1;
353 return (cf_dispatch (plugin, ci->key, buffer_ptr));
354 } /* int dispatch_value_plugin */
356 static int dispatch_value (const oconfig_item_t *ci)
361 for (i = 0; i < cf_value_map_num; i++)
362 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
364 ret = cf_value_map[i].func (ci);
368 for (i = 0; i < cf_global_options_num; i++)
369 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
371 ret = dispatch_global_option (ci);
376 } /* int dispatch_value */
378 static int dispatch_block_plugin (oconfig_item_t *ci)
383 cf_complex_callback_t *cb;
385 if (strcasecmp (ci->key, "Plugin") != 0)
387 if (ci->values_num < 1)
389 if (ci->values[0].type != OCONFIG_TYPE_STRING)
392 name = ci->values[0].value.string;
393 if (strcmp ("libvirt", name) == 0)
395 /* TODO(octo): Remove this legacy. */
396 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
397 "Your configuration is still using the old name. "
398 "Please change it to use \"virt\" as soon as possible. "
399 "This compatibility code will go away eventually.");
403 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
406 plugin_ctx_t old_ctx;
409 /* default to the global interval set before loading this plugin */
410 memset (&ctx, 0, sizeof (ctx));
411 ctx.interval = cf_get_default_interval ();
413 old_ctx = plugin_set_ctx (ctx);
414 status = plugin_load (name, /* flags = */ 0);
415 /* reset to the "global" context */
416 plugin_set_ctx (old_ctx);
420 ERROR ("Automatically loading plugin \"%s\" failed "
421 "with status %i.", name, status);
426 /* Check for a complex callback first */
427 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
429 if (strcasecmp (name, cb->type) == 0)
431 plugin_ctx_t old_ctx;
434 old_ctx = plugin_set_ctx (cb->ctx);
435 ret_val = (cb->callback (ci));
436 plugin_set_ctx (old_ctx);
441 /* Hm, no complex plugin found. Dispatch the values one by one */
442 for (i = 0; i < ci->children_num; i++)
444 if (ci->children[i].children == NULL)
445 dispatch_value_plugin (name, ci->children + i);
448 WARNING ("There is a `%s' block within the "
449 "configuration for the %s plugin. "
450 "The plugin either only expects "
451 "\"simple\" configuration statements "
452 "or wasn't loaded using `LoadPlugin'."
453 " Please check your configuration.",
454 ci->children[i].key, name);
462 static int dispatch_block (oconfig_item_t *ci)
464 if (strcasecmp (ci->key, "LoadPlugin") == 0)
465 return (dispatch_loadplugin (ci));
466 else if (strcasecmp (ci->key, "Plugin") == 0)
467 return (dispatch_block_plugin (ci));
468 else if (strcasecmp (ci->key, "Chain") == 0)
469 return (fc_configure (ci));
474 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
477 oconfig_item_t *temp;
480 assert (offset >= 0);
481 assert (dst->children_num > offset);
483 /* Free the memory used by the replaced child. Usually that's the
484 * `Include "blah"' statement. */
485 temp = dst->children + offset;
486 for (i = 0; i < temp->values_num; i++)
488 if (temp->values[i].type == OCONFIG_TYPE_STRING)
490 sfree (temp->values[i].value.string);
493 sfree (temp->values);
496 /* If (src->children_num == 0) the array size is decreased. If offset
497 * is _not_ the last element, (offset < (dst->children_num - 1)), then
498 * we need to move the trailing elements before resizing the array. */
499 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
501 int nmemb = dst->children_num - (offset + 1);
502 memmove (dst->children + offset, dst->children + offset + 1,
503 sizeof (oconfig_item_t) * nmemb);
506 /* Resize the memory containing the children to be big enough to hold
508 if (dst->children_num + src->children_num - 1 == 0)
510 dst->children_num = 0;
514 temp = (oconfig_item_t *) realloc (dst->children,
515 sizeof (oconfig_item_t)
516 * (dst->children_num + src->children_num - 1));
519 ERROR ("configfile: realloc failed.");
522 dst->children = temp;
524 /* If there are children behind the include statement, and they have
525 * not yet been moved because (src->children_num == 0), then move them
526 * to the end of the list, so that the new children have room before
528 if ((src->children_num > 0)
529 && ((dst->children_num - (offset + 1)) > 0))
531 int nmemb = dst->children_num - (offset + 1);
532 int old_offset = offset + 1;
533 int new_offset = offset + src->children_num;
535 memmove (dst->children + new_offset,
536 dst->children + old_offset,
537 sizeof (oconfig_item_t) * nmemb);
540 /* Last but not least: If there are new children, copy them to the
541 * memory reserved for them. */
542 if (src->children_num > 0)
544 memcpy (dst->children + offset,
546 sizeof (oconfig_item_t) * src->children_num);
549 /* Update the number of children. */
550 dst->children_num += (src->children_num - 1);
553 } /* int cf_ci_replace_child */
555 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
557 oconfig_item_t *temp;
559 if ((src == NULL) || (src->children_num == 0))
562 temp = (oconfig_item_t *) realloc (dst->children,
563 sizeof (oconfig_item_t)
564 * (dst->children_num + src->children_num));
567 ERROR ("configfile: realloc failed.");
570 dst->children = temp;
572 memcpy (dst->children + dst->children_num,
574 sizeof (oconfig_item_t)
575 * src->children_num);
576 dst->children_num += src->children_num;
579 } /* int cf_ci_append_children */
581 #define CF_MAX_DEPTH 8
582 static oconfig_item_t *cf_read_generic (const char *path,
583 const char *pattern, int depth);
585 static int cf_include_all (oconfig_item_t *root, int depth)
589 for (i = 0; i < root->children_num; i++)
594 char *pattern = NULL;
598 if (strcasecmp (root->children[i].key, "Include") != 0)
601 old = root->children + i;
603 if ((old->values_num != 1)
604 || (old->values[0].type != OCONFIG_TYPE_STRING))
606 ERROR ("configfile: `Include' needs exactly one string argument.");
610 for (j = 0; j < old->children_num; ++j)
612 oconfig_item_t *child = old->children + j;
614 if (strcasecmp (child->key, "Filter") == 0)
615 cf_util_get_string (child, &pattern);
617 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
621 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
627 /* Now replace the i'th child in `root' with `new'. */
628 if (cf_ci_replace_child (root, new, i) < 0)
631 /* ... and go back to the new i'th child. */
636 } /* for (i = 0; i < root->children_num; i++) */
639 } /* int cf_include_all */
641 static oconfig_item_t *cf_read_file (const char *file,
642 const char *pattern, int depth)
644 oconfig_item_t *root;
647 assert (depth < CF_MAX_DEPTH);
649 if (pattern != NULL) {
650 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
651 char *tmp = sstrdup (file);
652 char *filename = basename (tmp);
654 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
655 DEBUG ("configfile: Not including `%s' because it "
656 "does not match pattern `%s'.",
664 ERROR ("configfile: Cannot apply pattern filter '%s' "
665 "to file '%s': functions basename() and / or "
666 "fnmatch() not available.", pattern, file);
667 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
670 root = oconfig_parse_file (file);
673 ERROR ("configfile: Cannot read file `%s'.", file);
677 status = cf_include_all (root, depth);
685 } /* oconfig_item_t *cf_read_file */
687 static int cf_compare_string (const void *p1, const void *p2)
689 return strcmp (*(const char **) p1, *(const char **) p2);
692 static oconfig_item_t *cf_read_dir (const char *dir,
693 const char *pattern, int depth)
695 oconfig_item_t *root = NULL;
698 char **filenames = NULL;
699 int filenames_num = 0;
703 assert (depth < CF_MAX_DEPTH);
709 ERROR ("configfile: opendir failed: %s",
710 sstrerror (errno, errbuf, sizeof (errbuf)));
714 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
717 ERROR ("configfile: malloc failed.");
720 memset (root, 0, sizeof (oconfig_item_t));
722 while ((de = readdir (dh)) != NULL)
727 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
730 status = ssnprintf (name, sizeof (name), "%s/%s",
732 if ((status < 0) || ((size_t) status >= sizeof (name)))
734 ERROR ("configfile: Not including `%s/%s' because its"
735 " name is too long.",
737 for (i = 0; i < filenames_num; ++i)
745 tmp = (char **) realloc (filenames,
746 filenames_num * sizeof (*filenames));
748 ERROR ("configfile: realloc failed.");
749 for (i = 0; i < filenames_num - 1; ++i)
757 filenames[filenames_num - 1] = sstrdup (name);
760 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
763 for (i = 0; i < filenames_num; ++i)
765 oconfig_item_t *temp;
766 char *name = filenames[i];
768 temp = cf_read_generic (name, pattern, depth);
771 /* An error should already have been reported. */
776 cf_ci_append_children (root, temp);
777 sfree (temp->children);
785 } /* oconfig_item_t *cf_read_dir */
790 * Path is stat'ed and either cf_read_file or cf_read_dir is called
793 * There are two versions of this function: If `wordexp' exists shell wildcards
794 * will be expanded and the function will include all matches found. If
795 * `wordexp' (or, more precisely, it's header file) is not available the
796 * simpler function is used which does not do any such expansion.
799 static oconfig_item_t *cf_read_generic (const char *path,
800 const char *pattern, int depth)
802 oconfig_item_t *root = NULL;
804 const char *path_ptr;
808 if (depth >= CF_MAX_DEPTH)
810 ERROR ("configfile: Not including `%s' because the maximum "
811 "nesting depth has been reached.", path);
815 status = wordexp (path, &we, WRDE_NOCMD);
818 ERROR ("configfile: wordexp (%s) failed.", path);
822 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
825 ERROR ("configfile: malloc failed.");
828 memset (root, '\0', sizeof (oconfig_item_t));
830 /* wordexp() might return a sorted list already. That's not
831 * documented though, so let's make sure we get what we want. */
832 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
835 for (i = 0; i < we.we_wordc; i++)
837 oconfig_item_t *temp;
840 path_ptr = we.we_wordv[i];
842 status = stat (path_ptr, &statbuf);
846 WARNING ("configfile: stat (%s) failed: %s",
848 sstrerror (errno, errbuf, sizeof (errbuf)));
852 if (S_ISREG (statbuf.st_mode))
853 temp = cf_read_file (path_ptr, pattern, depth);
854 else if (S_ISDIR (statbuf.st_mode))
855 temp = cf_read_dir (path_ptr, pattern, depth);
858 WARNING ("configfile: %s is neither a file nor a "
868 cf_ci_append_children (root, temp);
869 sfree (temp->children);
876 } /* oconfig_item_t *cf_read_generic */
877 /* #endif HAVE_WORDEXP_H */
879 #else /* if !HAVE_WORDEXP_H */
880 static oconfig_item_t *cf_read_generic (const char *path,
881 const char *pattern, int depth)
886 if (depth >= CF_MAX_DEPTH)
888 ERROR ("configfile: Not including `%s' because the maximum "
889 "nesting depth has been reached.", path);
893 status = stat (path, &statbuf);
897 ERROR ("configfile: stat (%s) failed: %s",
899 sstrerror (errno, errbuf, sizeof (errbuf)));
903 if (S_ISREG (statbuf.st_mode))
904 return (cf_read_file (path, pattern, depth));
905 else if (S_ISDIR (statbuf.st_mode))
906 return (cf_read_dir (path, pattern, depth));
908 ERROR ("configfile: %s is neither a file nor a directory.", path);
910 } /* oconfig_item_t *cf_read_generic */
911 #endif /* !HAVE_WORDEXP_H */
916 int global_option_set (const char *option, const char *value)
920 DEBUG ("option = %s; value = %s;", option, value);
922 for (i = 0; i < cf_global_options_num; i++)
923 if (strcasecmp (cf_global_options[i].key, option) == 0)
926 if (i >= cf_global_options_num)
929 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
931 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
932 "command-line option `-P' take precedence.");
936 sfree (cf_global_options[i].value);
939 cf_global_options[i].value = strdup (value);
941 cf_global_options[i].value = NULL;
946 const char *global_option_get (const char *option)
950 for (i = 0; i < cf_global_options_num; i++)
951 if (strcasecmp (cf_global_options[i].key, option) == 0)
954 if (i >= cf_global_options_num)
957 return ((cf_global_options[i].value != NULL)
958 ? cf_global_options[i].value
959 : cf_global_options[i].def);
960 } /* char *global_option_get */
962 long global_option_get_long (const char *option, long default_value)
967 str = global_option_get (option);
969 return (default_value);
972 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
974 return (default_value);
977 } /* char *global_option_get_long */
979 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
985 optstr = global_option_get (name);
990 v = strtod (optstr, &endptr);
991 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
996 return (DOUBLE_TO_CDTIME_T (v));
997 } /* }}} cdtime_t global_option_get_time */
999 cdtime_t cf_get_default_interval (void)
1001 return (global_option_get_time ("Interval",
1002 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1005 void cf_unregister (const char *type)
1007 cf_callback_t *this, *prev;
1009 for (prev = NULL, this = first_callback;
1011 prev = this, this = this->next)
1012 if (strcasecmp (this->type, type) == 0)
1015 first_callback = this->next;
1017 prev->next = this->next;
1022 } /* void cf_unregister */
1024 void cf_unregister_complex (const char *type)
1026 cf_complex_callback_t *this, *prev;
1028 for (prev = NULL, this = complex_callback_head;
1030 prev = this, this = this->next)
1031 if (strcasecmp (this->type, type) == 0)
1034 complex_callback_head = this->next;
1036 prev->next = this->next;
1042 } /* void cf_unregister */
1044 void cf_register (const char *type,
1045 int (*callback) (const char *, const char *),
1046 const char **keys, int keys_num)
1048 cf_callback_t *cf_cb;
1050 /* Remove this module from the list, if it already exists */
1051 cf_unregister (type);
1053 /* This pointer will be free'd in `cf_unregister' */
1054 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1058 cf_cb->callback = callback;
1060 cf_cb->keys_num = keys_num;
1061 cf_cb->ctx = plugin_get_ctx ();
1063 cf_cb->next = first_callback;
1064 first_callback = cf_cb;
1065 } /* void cf_register */
1067 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1069 cf_complex_callback_t *new;
1071 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1075 new->type = strdup (type);
1076 if (new->type == NULL)
1082 new->callback = callback;
1085 new->ctx = plugin_get_ctx ();
1087 if (complex_callback_head == NULL)
1089 complex_callback_head = new;
1093 cf_complex_callback_t *last = complex_callback_head;
1094 while (last->next != NULL)
1100 } /* int cf_register_complex */
1102 int cf_read (char *filename)
1104 oconfig_item_t *conf;
1107 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1110 ERROR ("Unable to read config file %s.", filename);
1113 else if (conf->children_num == 0)
1115 ERROR ("Configuration file %s is empty.", filename);
1116 oconfig_free (conf);
1120 for (i = 0; i < conf->children_num; i++)
1122 if (conf->children[i].children == NULL)
1123 dispatch_value (conf->children + i);
1125 dispatch_block (conf->children + i);
1128 oconfig_free (conf);
1130 /* Read the default types.db if no `TypesDB' option was given. */
1131 if (cf_default_typesdb)
1132 read_types_list (PKGDATADIR"/types.db");
1137 /* Assures the config option is a string, duplicates it and returns the copy in
1138 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1140 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1144 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1146 ERROR ("cf_util_get_string: The %s option requires "
1147 "exactly one string argument.", ci->key);
1151 string = strdup (ci->values[0].value.string);
1155 if (*ret_string != NULL)
1156 sfree (*ret_string);
1157 *ret_string = string;
1160 } /* }}} int cf_util_get_string */
1162 /* Assures the config option is a string and copies it to the provided buffer.
1163 * Assures null-termination. */
1164 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1167 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1170 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1172 ERROR ("cf_util_get_string_buffer: The %s option requires "
1173 "exactly one string argument.", ci->key);
1177 strncpy (buffer, ci->values[0].value.string, buffer_size);
1178 buffer[buffer_size - 1] = 0;
1181 } /* }}} int cf_util_get_string_buffer */
1183 /* Assures the config option is a number and returns it as an int. */
1184 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1186 if ((ci == NULL) || (ret_value == NULL))
1189 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1191 ERROR ("cf_util_get_int: The %s option requires "
1192 "exactly one numeric argument.", ci->key);
1196 *ret_value = (int) ci->values[0].value.number;
1199 } /* }}} int cf_util_get_int */
1201 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1203 if ((ci == NULL) || (ret_value == NULL))
1206 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1208 ERROR ("cf_util_get_double: The %s option requires "
1209 "exactly one numeric argument.", ci->key);
1213 *ret_value = ci->values[0].value.number;
1216 } /* }}} int cf_util_get_double */
1218 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1220 if ((ci == NULL) || (ret_bool == NULL))
1223 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1225 ERROR ("cf_util_get_boolean: The %s option requires "
1226 "exactly one boolean argument.", ci->key);
1230 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1233 } /* }}} int cf_util_get_boolean */
1235 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1236 unsigned int *ret_value, unsigned int flag)
1241 if (ret_value == NULL)
1245 status = cf_util_get_boolean (ci, &b);
1255 *ret_value &= ~flag;
1259 } /* }}} int cf_util_get_flag */
1261 /* Assures that the config option is a string or a number if the correct range
1262 * of 1-65535. The string is then converted to a port number using
1263 * `service_name_to_port_number' and returned.
1264 * Returns the port number in the range [1-65535] or less than zero upon
1266 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1270 if ((ci->values_num != 1)
1271 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1272 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1274 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1275 "exactly one string argument.", ci->key);
1279 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1280 return (service_name_to_port_number (ci->values[0].value.string));
1282 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1283 tmp = (int) (ci->values[0].value.number + 0.5);
1284 if ((tmp < 1) || (tmp > 65535))
1286 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1287 "a service name or a port number. The number "
1288 "you specified, %i, is not in the valid "
1289 "range of 1-65535.",
1295 } /* }}} int cf_util_get_port_number */
1297 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1303 if (ci->values_num != 1)
1305 ERROR ("cf_util_get_service: The %s option requires exactly "
1306 "one argument.", ci->key);
1310 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1311 return (cf_util_get_string (ci, ret_string));
1312 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1314 ERROR ("cf_util_get_service: The %s option requires "
1315 "exactly one string or numeric argument.",
1320 status = cf_util_get_int (ci, &port);
1323 else if ((port < 1) || (port > 65535))
1325 ERROR ("cf_util_get_service: The port number given "
1326 "for the %s option is out of "
1327 "range (%i).", ci->key, port);
1331 service = malloc (6);
1332 if (service == NULL)
1334 ERROR ("cf_util_get_service: Out of memory.");
1337 ssnprintf (service, 6, "%i", port);
1339 sfree (*ret_string);
1340 *ret_string = service;
1343 } /* }}} int cf_util_get_service */
1345 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1347 if ((ci == NULL) || (ret_value == NULL))
1350 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1352 ERROR ("cf_util_get_cdtime: The %s option requires "
1353 "exactly one numeric argument.", ci->key);
1357 if (ci->values[0].value.number < 0.0)
1359 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1360 "option must not be negative.", ci->key);
1364 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1367 } /* }}} int cf_util_get_cdtime */