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) (oconfig_item_t *);
79 typedef struct cf_global_option_s
87 * Prototypes of callback functions
89 static int dispatch_value_typesdb (oconfig_item_t *ci);
90 static int dispatch_value_plugindir (oconfig_item_t *ci);
91 static int dispatch_loadplugin (oconfig_item_t *ci);
92 static int dispatch_block_plugin (oconfig_item_t *ci);
97 static cf_callback_t *first_callback = NULL;
98 static cf_complex_callback_t *complex_callback_head = NULL;
100 static cf_value_map_t cf_value_map[] =
102 {"TypesDB", dispatch_value_typesdb},
103 {"PluginDir", dispatch_value_plugindir},
104 {"LoadPlugin", dispatch_loadplugin},
105 {"Plugin", dispatch_block_plugin}
107 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
109 static cf_global_option_t cf_global_options[] =
111 {"BaseDir", NULL, PKGLOCALSTATEDIR},
112 {"PIDFile", NULL, PIDFILE},
113 {"Hostname", NULL, NULL},
114 {"FQDNLookup", NULL, "true"},
115 {"Interval", NULL, NULL},
116 {"ReadThreads", NULL, "5"},
117 {"WriteThreads", NULL, "5"},
118 {"WriteQueueLimitHigh", NULL, NULL},
119 {"WriteQueueLimitLow", NULL, NULL},
120 {"Timeout", NULL, "2"},
121 {"AutoLoadPlugin", NULL, "false"},
122 {"CollectInternalStats", NULL, "false"},
123 {"PreCacheChain", NULL, "PreCache"},
124 {"PostCacheChain", NULL, "PostCache"},
125 {"MaxReadInterval", NULL, "86400"}
127 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
129 static int cf_default_typesdb = 1;
132 * Functions to handle register/unregister, search, and other plugin related
135 static cf_callback_t *cf_search (const char *type)
137 cf_callback_t *cf_cb;
142 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
143 if (strcasecmp (cf_cb->type, type) == 0)
149 static int cf_dispatch (const char *type, const char *orig_key,
150 const char *orig_value)
152 cf_callback_t *cf_cb;
153 plugin_ctx_t old_ctx;
159 if (orig_key == NULL)
162 DEBUG ("type = %s, key = %s, value = %s",
165 ESCAPE_NULL(orig_value));
167 if ((cf_cb = cf_search (type)) == NULL)
169 WARNING ("Found a configuration for the `%s' plugin, but "
170 "the plugin isn't loaded or didn't register "
171 "a configuration callback.", type);
175 if ((key = strdup (orig_key)) == NULL)
177 if ((value = strdup (orig_value)) == NULL)
185 old_ctx = plugin_set_ctx (cf_cb->ctx);
187 for (i = 0; i < cf_cb->keys_num; i++)
189 if ((cf_cb->keys[i] != NULL)
190 && (strcasecmp (cf_cb->keys[i], key) == 0))
192 ret = (*cf_cb->callback) (key, value);
197 plugin_set_ctx (old_ctx);
199 if (i >= cf_cb->keys_num)
200 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
206 } /* int cf_dispatch */
208 static int dispatch_global_option (const oconfig_item_t *ci)
210 if (ci->values_num != 1)
212 if (ci->values[0].type == OCONFIG_TYPE_STRING)
213 return (global_option_set (ci->key, ci->values[0].value.string));
214 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
217 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
218 return (global_option_set (ci->key, tmp));
220 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
222 if (ci->values[0].value.boolean)
223 return (global_option_set (ci->key, "true"));
225 return (global_option_set (ci->key, "false"));
229 } /* int dispatch_global_option */
231 static int dispatch_value_typesdb (oconfig_item_t *ci)
235 assert (strcasecmp (ci->key, "TypesDB") == 0);
237 cf_default_typesdb = 0;
239 if (ci->values_num < 1) {
240 ERROR ("configfile: `TypesDB' needs at least one argument.");
244 for (i = 0; i < ci->values_num; ++i)
246 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
247 WARNING ("configfile: TypesDB: Skipping %i. argument which "
248 "is not a string.", i + 1);
252 read_types_list (ci->values[i].value.string);
255 } /* int dispatch_value_typesdb */
257 static int dispatch_value_plugindir (oconfig_item_t *ci)
259 assert (strcasecmp (ci->key, "PluginDir") == 0);
261 if (ci->values_num != 1)
263 if (ci->values[0].type != OCONFIG_TYPE_STRING)
266 plugin_set_dir (ci->values[0].value.string);
270 static int dispatch_loadplugin (oconfig_item_t *ci)
274 unsigned int flags = 0;
276 plugin_ctx_t old_ctx;
279 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
281 if (ci->values_num != 1)
283 if (ci->values[0].type != OCONFIG_TYPE_STRING)
286 name = ci->values[0].value.string;
287 if (strcmp ("libvirt", name) == 0)
290 /* default to the global interval set before loading this plugin */
291 memset (&ctx, 0, sizeof (ctx));
292 ctx.interval = cf_get_default_interval ();
294 for (i = 0; i < ci->children_num; ++i) {
295 if (strcasecmp("Globals", ci->children[i].key) == 0)
296 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
297 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
298 if (cf_util_get_cdtime (ci->children + i, &ctx.interval) != 0) {
299 /* cf_util_get_cdtime will log an error */
304 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
306 ci->children[i].key, ci->values[0].value.string);
310 old_ctx = plugin_set_ctx (ctx);
311 ret_val = plugin_load (name, (uint32_t) flags);
312 /* reset to the "global" context */
313 plugin_set_ctx (old_ctx);
316 } /* int dispatch_value_loadplugin */
318 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
326 buffer_free = sizeof (buffer);
328 for (i = 0; i < ci->values_num; i++)
332 if (ci->values[i].type == OCONFIG_TYPE_STRING)
333 status = ssnprintf (buffer_ptr, buffer_free, " %s",
334 ci->values[i].value.string);
335 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
336 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
337 ci->values[i].value.number);
338 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
339 status = ssnprintf (buffer_ptr, buffer_free, " %s",
340 ci->values[i].value.boolean
343 if ((status < 0) || (status >= buffer_free))
345 buffer_free -= status;
346 buffer_ptr += status;
348 /* skip the initial space */
349 buffer_ptr = buffer + 1;
351 return (cf_dispatch (plugin, ci->key, buffer_ptr));
352 } /* int dispatch_value_plugin */
354 static int dispatch_value (oconfig_item_t *ci)
359 for (i = 0; i < cf_value_map_num; i++)
360 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
362 ret = cf_value_map[i].func (ci);
366 for (i = 0; i < cf_global_options_num; i++)
367 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
369 ret = dispatch_global_option (ci);
374 } /* int dispatch_value */
376 static int dispatch_block_plugin (oconfig_item_t *ci)
381 cf_complex_callback_t *cb;
383 if (strcasecmp (ci->key, "Plugin") != 0)
385 if (ci->values_num < 1)
387 if (ci->values[0].type != OCONFIG_TYPE_STRING)
390 name = ci->values[0].value.string;
391 if (strcmp ("libvirt", name) == 0)
393 /* TODO(octo): Remove this legacy. */
394 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
395 "Your configuration is still using the old name. "
396 "Please change it to use \"virt\" as soon as possible. "
397 "This compatibility code will go away eventually.");
401 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
404 plugin_ctx_t old_ctx;
407 /* default to the global interval set before loading this plugin */
408 memset (&ctx, 0, sizeof (ctx));
409 ctx.interval = cf_get_default_interval ();
411 old_ctx = plugin_set_ctx (ctx);
412 status = plugin_load (name, /* flags = */ 0);
413 /* reset to the "global" context */
414 plugin_set_ctx (old_ctx);
418 ERROR ("Automatically loading plugin \"%s\" failed "
419 "with status %i.", name, status);
424 /* Check for a complex callback first */
425 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
427 if (strcasecmp (name, cb->type) == 0)
429 plugin_ctx_t old_ctx;
432 old_ctx = plugin_set_ctx (cb->ctx);
433 ret_val = (cb->callback (ci));
434 plugin_set_ctx (old_ctx);
439 /* Hm, no complex plugin found. Dispatch the values one by one */
440 for (i = 0; i < ci->children_num; i++)
442 if (ci->children[i].children == NULL)
443 dispatch_value_plugin (name, ci->children + i);
446 WARNING ("There is a `%s' block within the "
447 "configuration for the %s plugin. "
448 "The plugin either only expects "
449 "\"simple\" configuration statements "
450 "or wasn't loaded using `LoadPlugin'."
451 " Please check your configuration.",
452 ci->children[i].key, name);
460 static int dispatch_block (oconfig_item_t *ci)
462 if (strcasecmp (ci->key, "LoadPlugin") == 0)
463 return (dispatch_loadplugin (ci));
464 else if (strcasecmp (ci->key, "Plugin") == 0)
465 return (dispatch_block_plugin (ci));
466 else if (strcasecmp (ci->key, "Chain") == 0)
467 return (fc_configure (ci));
472 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
475 oconfig_item_t *temp;
478 assert (offset >= 0);
479 assert (dst->children_num > offset);
481 /* Free the memory used by the replaced child. Usually that's the
482 * `Include "blah"' statement. */
483 temp = dst->children + offset;
484 for (i = 0; i < temp->values_num; i++)
486 if (temp->values[i].type == OCONFIG_TYPE_STRING)
488 sfree (temp->values[i].value.string);
491 sfree (temp->values);
494 /* If (src->children_num == 0) the array size is decreased. If offset
495 * is _not_ the last element, (offset < (dst->children_num - 1)), then
496 * we need to move the trailing elements before resizing the array. */
497 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
499 int nmemb = dst->children_num - (offset + 1);
500 memmove (dst->children + offset, dst->children + offset + 1,
501 sizeof (oconfig_item_t) * nmemb);
504 /* Resize the memory containing the children to be big enough to hold
506 if (dst->children_num + src->children_num - 1 == 0)
508 dst->children_num = 0;
512 temp = (oconfig_item_t *) realloc (dst->children,
513 sizeof (oconfig_item_t)
514 * (dst->children_num + src->children_num - 1));
517 ERROR ("configfile: realloc failed.");
520 dst->children = temp;
522 /* If there are children behind the include statement, and they have
523 * not yet been moved because (src->children_num == 0), then move them
524 * to the end of the list, so that the new children have room before
526 if ((src->children_num > 0)
527 && ((dst->children_num - (offset + 1)) > 0))
529 int nmemb = dst->children_num - (offset + 1);
530 int old_offset = offset + 1;
531 int new_offset = offset + src->children_num;
533 memmove (dst->children + new_offset,
534 dst->children + old_offset,
535 sizeof (oconfig_item_t) * nmemb);
538 /* Last but not least: If there are new children, copy them to the
539 * memory reserved for them. */
540 if (src->children_num > 0)
542 memcpy (dst->children + offset,
544 sizeof (oconfig_item_t) * src->children_num);
547 /* Update the number of children. */
548 dst->children_num += (src->children_num - 1);
551 } /* int cf_ci_replace_child */
553 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
555 oconfig_item_t *temp;
557 if ((src == NULL) || (src->children_num == 0))
560 temp = (oconfig_item_t *) realloc (dst->children,
561 sizeof (oconfig_item_t)
562 * (dst->children_num + src->children_num));
565 ERROR ("configfile: realloc failed.");
568 dst->children = temp;
570 memcpy (dst->children + dst->children_num,
572 sizeof (oconfig_item_t)
573 * src->children_num);
574 dst->children_num += src->children_num;
577 } /* int cf_ci_append_children */
579 #define CF_MAX_DEPTH 8
580 static oconfig_item_t *cf_read_generic (const char *path,
581 const char *pattern, int depth);
583 static int cf_include_all (oconfig_item_t *root, int depth)
587 for (i = 0; i < root->children_num; i++)
592 char *pattern = NULL;
596 if (strcasecmp (root->children[i].key, "Include") != 0)
599 old = root->children + i;
601 if ((old->values_num != 1)
602 || (old->values[0].type != OCONFIG_TYPE_STRING))
604 ERROR ("configfile: `Include' needs exactly one string argument.");
608 for (j = 0; j < old->children_num; ++j)
610 oconfig_item_t *child = old->children + j;
612 if (strcasecmp (child->key, "Filter") == 0)
613 cf_util_get_string (child, &pattern);
615 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
619 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
625 /* Now replace the i'th child in `root' with `new'. */
626 if (cf_ci_replace_child (root, new, i) < 0) {
632 /* ... and go back to the new i'th child. */
637 } /* for (i = 0; i < root->children_num; i++) */
640 } /* int cf_include_all */
642 static oconfig_item_t *cf_read_file (const char *file,
643 const char *pattern, int depth)
645 oconfig_item_t *root;
648 assert (depth < CF_MAX_DEPTH);
650 if (pattern != NULL) {
651 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
652 char *tmp = sstrdup (file);
653 char *filename = basename (tmp);
655 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
656 DEBUG ("configfile: Not including `%s' because it "
657 "does not match pattern `%s'.",
665 ERROR ("configfile: Cannot apply pattern filter '%s' "
666 "to file '%s': functions basename() and / or "
667 "fnmatch() not available.", pattern, file);
668 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
671 root = oconfig_parse_file (file);
674 ERROR ("configfile: Cannot read file `%s'.", file);
678 status = cf_include_all (root, depth);
686 } /* oconfig_item_t *cf_read_file */
688 static int cf_compare_string (const void *p1, const void *p2)
690 return strcmp (*(const char **) p1, *(const char **) p2);
693 static oconfig_item_t *cf_read_dir (const char *dir,
694 const char *pattern, int depth)
696 oconfig_item_t *root = NULL;
699 char **filenames = NULL;
700 int filenames_num = 0;
704 assert (depth < CF_MAX_DEPTH);
710 ERROR ("configfile: opendir failed: %s",
711 sstrerror (errno, errbuf, sizeof (errbuf)));
715 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
718 ERROR ("configfile: malloc failed.");
722 memset (root, 0, sizeof (oconfig_item_t));
724 while ((de = readdir (dh)) != NULL)
729 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
732 status = ssnprintf (name, sizeof (name), "%s/%s",
734 if ((status < 0) || ((size_t) status >= sizeof (name)))
736 ERROR ("configfile: Not including `%s/%s' because its"
737 " name is too long.",
740 for (i = 0; i < filenames_num; ++i)
748 tmp = (char **) realloc (filenames,
749 filenames_num * sizeof (*filenames));
751 ERROR ("configfile: realloc failed.");
753 for (i = 0; i < filenames_num - 1; ++i)
761 filenames[filenames_num - 1] = sstrdup (name);
764 if (filenames == NULL)
770 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
773 for (i = 0; i < filenames_num; ++i)
775 oconfig_item_t *temp;
776 char *name = filenames[i];
778 temp = cf_read_generic (name, pattern, depth);
781 /* An error should already have been reported. */
786 cf_ci_append_children (root, temp);
787 sfree (temp->children);
796 } /* oconfig_item_t *cf_read_dir */
801 * Path is stat'ed and either cf_read_file or cf_read_dir is called
804 * There are two versions of this function: If `wordexp' exists shell wildcards
805 * will be expanded and the function will include all matches found. If
806 * `wordexp' (or, more precisely, it's header file) is not available the
807 * simpler function is used which does not do any such expansion.
810 static oconfig_item_t *cf_read_generic (const char *path,
811 const char *pattern, int depth)
813 oconfig_item_t *root = NULL;
815 const char *path_ptr;
819 if (depth >= CF_MAX_DEPTH)
821 ERROR ("configfile: Not including `%s' because the maximum "
822 "nesting depth has been reached.", path);
826 status = wordexp (path, &we, WRDE_NOCMD);
829 ERROR ("configfile: wordexp (%s) failed.", path);
833 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
836 ERROR ("configfile: malloc failed.");
839 memset (root, '\0', sizeof (oconfig_item_t));
841 /* wordexp() might return a sorted list already. That's not
842 * documented though, so let's make sure we get what we want. */
843 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
846 for (i = 0; i < we.we_wordc; i++)
848 oconfig_item_t *temp;
851 path_ptr = we.we_wordv[i];
853 status = stat (path_ptr, &statbuf);
857 WARNING ("configfile: stat (%s) failed: %s",
859 sstrerror (errno, errbuf, sizeof (errbuf)));
863 if (S_ISREG (statbuf.st_mode))
864 temp = cf_read_file (path_ptr, pattern, depth);
865 else if (S_ISDIR (statbuf.st_mode))
866 temp = cf_read_dir (path_ptr, pattern, depth);
869 WARNING ("configfile: %s is neither a file nor a "
879 cf_ci_append_children (root, temp);
880 sfree (temp->children);
887 } /* oconfig_item_t *cf_read_generic */
888 /* #endif HAVE_WORDEXP_H */
890 #else /* if !HAVE_WORDEXP_H */
891 static oconfig_item_t *cf_read_generic (const char *path,
892 const char *pattern, int depth)
897 if (depth >= CF_MAX_DEPTH)
899 ERROR ("configfile: Not including `%s' because the maximum "
900 "nesting depth has been reached.", path);
904 status = stat (path, &statbuf);
908 ERROR ("configfile: stat (%s) failed: %s",
910 sstrerror (errno, errbuf, sizeof (errbuf)));
914 if (S_ISREG (statbuf.st_mode))
915 return (cf_read_file (path, pattern, depth));
916 else if (S_ISDIR (statbuf.st_mode))
917 return (cf_read_dir (path, pattern, depth));
919 ERROR ("configfile: %s is neither a file nor a directory.", path);
921 } /* oconfig_item_t *cf_read_generic */
922 #endif /* !HAVE_WORDEXP_H */
927 int global_option_set (const char *option, const char *value)
931 DEBUG ("option = %s; value = %s;", option, value);
933 for (i = 0; i < cf_global_options_num; i++)
934 if (strcasecmp (cf_global_options[i].key, option) == 0)
937 if (i >= cf_global_options_num)
940 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
942 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
943 "command-line option `-P' take precedence.");
947 sfree (cf_global_options[i].value);
950 cf_global_options[i].value = strdup (value);
952 cf_global_options[i].value = NULL;
957 const char *global_option_get (const char *option)
961 for (i = 0; i < cf_global_options_num; i++)
962 if (strcasecmp (cf_global_options[i].key, option) == 0)
965 if (i >= cf_global_options_num)
968 return ((cf_global_options[i].value != NULL)
969 ? cf_global_options[i].value
970 : cf_global_options[i].def);
971 } /* char *global_option_get */
973 long global_option_get_long (const char *option, long default_value)
978 str = global_option_get (option);
980 return (default_value);
983 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
985 return (default_value);
988 } /* char *global_option_get_long */
990 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
996 optstr = global_option_get (name);
1001 v = strtod (optstr, &endptr);
1002 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1007 return (DOUBLE_TO_CDTIME_T (v));
1008 } /* }}} cdtime_t global_option_get_time */
1010 cdtime_t cf_get_default_interval (void)
1012 return (global_option_get_time ("Interval",
1013 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1016 void cf_unregister (const char *type)
1018 cf_callback_t *this, *prev;
1020 for (prev = NULL, this = first_callback;
1022 prev = this, this = this->next)
1023 if (strcasecmp (this->type, type) == 0)
1026 first_callback = this->next;
1028 prev->next = this->next;
1033 } /* void cf_unregister */
1035 void cf_unregister_complex (const char *type)
1037 cf_complex_callback_t *this, *prev;
1039 for (prev = NULL, this = complex_callback_head;
1041 prev = this, this = this->next)
1042 if (strcasecmp (this->type, type) == 0)
1045 complex_callback_head = this->next;
1047 prev->next = this->next;
1053 } /* void cf_unregister */
1055 void cf_register (const char *type,
1056 int (*callback) (const char *, const char *),
1057 const char **keys, int keys_num)
1059 cf_callback_t *cf_cb;
1061 /* Remove this module from the list, if it already exists */
1062 cf_unregister (type);
1064 /* This pointer will be free'd in `cf_unregister' */
1065 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1069 cf_cb->callback = callback;
1071 cf_cb->keys_num = keys_num;
1072 cf_cb->ctx = plugin_get_ctx ();
1074 cf_cb->next = first_callback;
1075 first_callback = cf_cb;
1076 } /* void cf_register */
1078 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1080 cf_complex_callback_t *new;
1082 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1086 new->type = strdup (type);
1087 if (new->type == NULL)
1093 new->callback = callback;
1096 new->ctx = plugin_get_ctx ();
1098 if (complex_callback_head == NULL)
1100 complex_callback_head = new;
1104 cf_complex_callback_t *last = complex_callback_head;
1105 while (last->next != NULL)
1111 } /* int cf_register_complex */
1113 int cf_read (char *filename)
1115 oconfig_item_t *conf;
1118 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1121 ERROR ("Unable to read config file %s.", filename);
1124 else if (conf->children_num == 0)
1126 ERROR ("Configuration file %s is empty.", filename);
1127 oconfig_free (conf);
1131 for (i = 0; i < conf->children_num; i++)
1133 if (conf->children[i].children == NULL)
1134 dispatch_value (conf->children + i);
1136 dispatch_block (conf->children + i);
1139 oconfig_free (conf);
1141 /* Read the default types.db if no `TypesDB' option was given. */
1142 if (cf_default_typesdb)
1143 read_types_list (PKGDATADIR"/types.db");
1148 /* Assures the config option is a string, duplicates it and returns the copy in
1149 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1151 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1155 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1157 ERROR ("cf_util_get_string: The %s option requires "
1158 "exactly one string argument.", ci->key);
1162 string = strdup (ci->values[0].value.string);
1166 if (*ret_string != NULL)
1167 sfree (*ret_string);
1168 *ret_string = string;
1171 } /* }}} int cf_util_get_string */
1173 /* Assures the config option is a string and copies it to the provided buffer.
1174 * Assures null-termination. */
1175 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1178 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1181 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1183 ERROR ("cf_util_get_string_buffer: The %s option requires "
1184 "exactly one string argument.", ci->key);
1188 strncpy (buffer, ci->values[0].value.string, buffer_size);
1189 buffer[buffer_size - 1] = 0;
1192 } /* }}} int cf_util_get_string_buffer */
1194 /* Assures the config option is a number and returns it as an int. */
1195 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1197 if ((ci == NULL) || (ret_value == NULL))
1200 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1202 ERROR ("cf_util_get_int: The %s option requires "
1203 "exactly one numeric argument.", ci->key);
1207 *ret_value = (int) ci->values[0].value.number;
1210 } /* }}} int cf_util_get_int */
1212 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1214 if ((ci == NULL) || (ret_value == NULL))
1217 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1219 ERROR ("cf_util_get_double: The %s option requires "
1220 "exactly one numeric argument.", ci->key);
1224 *ret_value = ci->values[0].value.number;
1227 } /* }}} int cf_util_get_double */
1229 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1231 if ((ci == NULL) || (ret_bool == NULL))
1234 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1236 ERROR ("cf_util_get_boolean: The %s option requires "
1237 "exactly one boolean argument.", ci->key);
1241 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1244 } /* }}} int cf_util_get_boolean */
1246 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1247 unsigned int *ret_value, unsigned int flag)
1252 if (ret_value == NULL)
1256 status = cf_util_get_boolean (ci, &b);
1266 *ret_value &= ~flag;
1270 } /* }}} int cf_util_get_flag */
1272 /* Assures that the config option is a string or a number if the correct range
1273 * of 1-65535. The string is then converted to a port number using
1274 * `service_name_to_port_number' and returned.
1275 * Returns the port number in the range [1-65535] or less than zero upon
1277 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1281 if ((ci->values_num != 1)
1282 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1283 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1285 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1286 "exactly one string argument.", ci->key);
1290 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1291 return (service_name_to_port_number (ci->values[0].value.string));
1293 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1294 tmp = (int) (ci->values[0].value.number + 0.5);
1295 if ((tmp < 1) || (tmp > 65535))
1297 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1298 "a service name or a port number. The number "
1299 "you specified, %i, is not in the valid "
1300 "range of 1-65535.",
1306 } /* }}} int cf_util_get_port_number */
1308 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1314 if (ci->values_num != 1)
1316 ERROR ("cf_util_get_service: The %s option requires exactly "
1317 "one argument.", ci->key);
1321 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1322 return (cf_util_get_string (ci, ret_string));
1323 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1325 ERROR ("cf_util_get_service: The %s option requires "
1326 "exactly one string or numeric argument.",
1331 status = cf_util_get_int (ci, &port);
1334 else if ((port < 1) || (port > 65535))
1336 ERROR ("cf_util_get_service: The port number given "
1337 "for the %s option is out of "
1338 "range (%i).", ci->key, port);
1342 service = malloc (6);
1343 if (service == NULL)
1345 ERROR ("cf_util_get_service: Out of memory.");
1348 ssnprintf (service, 6, "%i", port);
1350 sfree (*ret_string);
1351 *ret_string = service;
1354 } /* }}} int cf_util_get_service */
1356 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1358 if ((ci == NULL) || (ret_value == NULL))
1361 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1363 ERROR ("cf_util_get_cdtime: The %s option requires "
1364 "exactly one numeric argument.", ci->key);
1368 if (ci->values[0].value.number < 0.0)
1370 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1371 "option must not be negative.", ci->key);
1375 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1378 } /* }}} int cf_util_get_cdtime */