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 {"MaxEffectiveInterval", 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 double interval = 0.0;
294 if (cf_util_get_double (ci->children + i, &interval) != 0) {
295 /* cf_util_get_double will log an error */
299 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
302 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
304 ci->children[i].key, ci->values[0].value.string);
308 old_ctx = plugin_set_ctx (ctx);
309 ret_val = plugin_load (name, (uint32_t) flags);
310 /* reset to the "global" context */
311 plugin_set_ctx (old_ctx);
314 } /* int dispatch_value_loadplugin */
316 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
324 buffer_free = sizeof (buffer);
326 for (i = 0; i < ci->values_num; i++)
330 if (ci->values[i].type == OCONFIG_TYPE_STRING)
331 status = ssnprintf (buffer_ptr, buffer_free, " %s",
332 ci->values[i].value.string);
333 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
334 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
335 ci->values[i].value.number);
336 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
337 status = ssnprintf (buffer_ptr, buffer_free, " %s",
338 ci->values[i].value.boolean
341 if ((status < 0) || (status >= buffer_free))
343 buffer_free -= status;
344 buffer_ptr += status;
346 /* skip the initial space */
347 buffer_ptr = buffer + 1;
349 return (cf_dispatch (plugin, ci->key, buffer_ptr));
350 } /* int dispatch_value_plugin */
352 static int dispatch_value (const oconfig_item_t *ci)
357 for (i = 0; i < cf_value_map_num; i++)
358 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
360 ret = cf_value_map[i].func (ci);
364 for (i = 0; i < cf_global_options_num; i++)
365 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
367 ret = dispatch_global_option (ci);
372 } /* int dispatch_value */
374 static int dispatch_block_plugin (oconfig_item_t *ci)
379 cf_complex_callback_t *cb;
381 if (strcasecmp (ci->key, "Plugin") != 0)
383 if (ci->values_num < 1)
385 if (ci->values[0].type != OCONFIG_TYPE_STRING)
388 name = ci->values[0].value.string;
390 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
394 status = plugin_load (name, /* flags = */ 0);
397 ERROR ("Automatically loading plugin \"%s\" failed "
398 "with status %i.", name, status);
403 /* Check for a complex callback first */
404 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
406 if (strcasecmp (name, cb->type) == 0)
408 plugin_ctx_t old_ctx;
411 old_ctx = plugin_set_ctx (cb->ctx);
412 ret_val = (cb->callback (ci));
413 plugin_set_ctx (old_ctx);
418 /* Hm, no complex plugin found. Dispatch the values one by one */
419 for (i = 0; i < ci->children_num; i++)
421 if (ci->children[i].children == NULL)
422 dispatch_value_plugin (name, ci->children + i);
425 WARNING ("There is a `%s' block within the "
426 "configuration for the %s plugin. "
427 "The plugin either only expects "
428 "\"simple\" configuration statements "
429 "or wasn't loaded using `LoadPlugin'."
430 " Please check your configuration.",
431 ci->children[i].key, name);
439 static int dispatch_block (oconfig_item_t *ci)
441 if (strcasecmp (ci->key, "LoadPlugin") == 0)
442 return (dispatch_loadplugin (ci));
443 else if (strcasecmp (ci->key, "Plugin") == 0)
444 return (dispatch_block_plugin (ci));
445 else if (strcasecmp (ci->key, "Chain") == 0)
446 return (fc_configure (ci));
451 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
454 oconfig_item_t *temp;
457 assert (offset >= 0);
458 assert (dst->children_num > offset);
460 /* Free the memory used by the replaced child. Usually that's the
461 * `Include "blah"' statement. */
462 temp = dst->children + offset;
463 for (i = 0; i < temp->values_num; i++)
465 if (temp->values[i].type == OCONFIG_TYPE_STRING)
467 sfree (temp->values[i].value.string);
470 sfree (temp->values);
473 /* If (src->children_num == 0) the array size is decreased. If offset
474 * is _not_ the last element, (offset < (dst->children_num - 1)), then
475 * we need to move the trailing elements before resizing the array. */
476 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
478 int nmemb = dst->children_num - (offset + 1);
479 memmove (dst->children + offset, dst->children + offset + 1,
480 sizeof (oconfig_item_t) * nmemb);
483 /* Resize the memory containing the children to be big enough to hold
485 if (dst->children_num + src->children_num - 1 == 0)
487 dst->children_num = 0;
491 temp = (oconfig_item_t *) realloc (dst->children,
492 sizeof (oconfig_item_t)
493 * (dst->children_num + src->children_num - 1));
496 ERROR ("configfile: realloc failed.");
499 dst->children = temp;
501 /* If there are children behind the include statement, and they have
502 * not yet been moved because (src->children_num == 0), then move them
503 * to the end of the list, so that the new children have room before
505 if ((src->children_num > 0)
506 && ((dst->children_num - (offset + 1)) > 0))
508 int nmemb = dst->children_num - (offset + 1);
509 int old_offset = offset + 1;
510 int new_offset = offset + src->children_num;
512 memmove (dst->children + new_offset,
513 dst->children + old_offset,
514 sizeof (oconfig_item_t) * nmemb);
517 /* Last but not least: If there are new children, copy them to the
518 * memory reserved for them. */
519 if (src->children_num > 0)
521 memcpy (dst->children + offset,
523 sizeof (oconfig_item_t) * src->children_num);
526 /* Update the number of children. */
527 dst->children_num += (src->children_num - 1);
530 } /* int cf_ci_replace_child */
532 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
534 oconfig_item_t *temp;
536 if ((src == NULL) || (src->children_num == 0))
539 temp = (oconfig_item_t *) realloc (dst->children,
540 sizeof (oconfig_item_t)
541 * (dst->children_num + src->children_num));
544 ERROR ("configfile: realloc failed.");
547 dst->children = temp;
549 memcpy (dst->children + dst->children_num,
551 sizeof (oconfig_item_t)
552 * src->children_num);
553 dst->children_num += src->children_num;
556 } /* int cf_ci_append_children */
558 #define CF_MAX_DEPTH 8
559 static oconfig_item_t *cf_read_generic (const char *path,
560 const char *pattern, int depth);
562 static int cf_include_all (oconfig_item_t *root, int depth)
566 for (i = 0; i < root->children_num; i++)
571 char *pattern = NULL;
575 if (strcasecmp (root->children[i].key, "Include") != 0)
578 old = root->children + i;
580 if ((old->values_num != 1)
581 || (old->values[0].type != OCONFIG_TYPE_STRING))
583 ERROR ("configfile: `Include' needs exactly one string argument.");
587 for (j = 0; j < old->children_num; ++j)
589 oconfig_item_t *child = old->children + j;
591 if (strcasecmp (child->key, "Filter") == 0)
592 cf_util_get_string (child, &pattern);
594 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
598 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
604 /* Now replace the i'th child in `root' with `new'. */
605 if (cf_ci_replace_child (root, new, i) < 0)
608 /* ... and go back to the new i'th child. */
613 } /* for (i = 0; i < root->children_num; i++) */
616 } /* int cf_include_all */
618 static oconfig_item_t *cf_read_file (const char *file,
619 const char *pattern, int depth)
621 oconfig_item_t *root;
624 assert (depth < CF_MAX_DEPTH);
626 if (pattern != NULL) {
627 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
628 char *tmp = sstrdup (file);
629 char *filename = basename (tmp);
631 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
632 DEBUG ("configfile: Not including `%s' because it "
633 "does not match pattern `%s'.",
641 ERROR ("configfile: Cannot apply pattern filter '%s' "
642 "to file '%s': functions basename() and / or "
643 "fnmatch() not available.", pattern, file);
644 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
647 root = oconfig_parse_file (file);
650 ERROR ("configfile: Cannot read file `%s'.", file);
654 status = cf_include_all (root, depth);
662 } /* oconfig_item_t *cf_read_file */
664 static int cf_compare_string (const void *p1, const void *p2)
666 return strcmp (*(const char **) p1, *(const char **) p2);
669 static oconfig_item_t *cf_read_dir (const char *dir,
670 const char *pattern, int depth)
672 oconfig_item_t *root = NULL;
675 char **filenames = NULL;
676 int filenames_num = 0;
680 assert (depth < CF_MAX_DEPTH);
686 ERROR ("configfile: opendir failed: %s",
687 sstrerror (errno, errbuf, sizeof (errbuf)));
691 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
694 ERROR ("configfile: malloc failed.");
697 memset (root, 0, sizeof (oconfig_item_t));
699 while ((de = readdir (dh)) != NULL)
704 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
707 status = ssnprintf (name, sizeof (name), "%s/%s",
709 if ((status < 0) || ((size_t) status >= sizeof (name)))
711 ERROR ("configfile: Not including `%s/%s' because its"
712 " name is too long.",
714 for (i = 0; i < filenames_num; ++i)
722 tmp = (char **) realloc (filenames,
723 filenames_num * sizeof (*filenames));
725 ERROR ("configfile: realloc failed.");
726 for (i = 0; i < filenames_num - 1; ++i)
734 filenames[filenames_num - 1] = sstrdup (name);
737 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
740 for (i = 0; i < filenames_num; ++i)
742 oconfig_item_t *temp;
743 char *name = filenames[i];
745 temp = cf_read_generic (name, pattern, depth);
748 /* An error should already have been reported. */
753 cf_ci_append_children (root, temp);
754 sfree (temp->children);
762 } /* oconfig_item_t *cf_read_dir */
767 * Path is stat'ed and either cf_read_file or cf_read_dir is called
770 * There are two versions of this function: If `wordexp' exists shell wildcards
771 * will be expanded and the function will include all matches found. If
772 * `wordexp' (or, more precisely, it's header file) is not available the
773 * simpler function is used which does not do any such expansion.
776 static oconfig_item_t *cf_read_generic (const char *path,
777 const char *pattern, int depth)
779 oconfig_item_t *root = NULL;
781 const char *path_ptr;
785 if (depth >= CF_MAX_DEPTH)
787 ERROR ("configfile: Not including `%s' because the maximum "
788 "nesting depth has been reached.", path);
792 status = wordexp (path, &we, WRDE_NOCMD);
795 ERROR ("configfile: wordexp (%s) failed.", path);
799 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
802 ERROR ("configfile: malloc failed.");
805 memset (root, '\0', sizeof (oconfig_item_t));
807 /* wordexp() might return a sorted list already. That's not
808 * documented though, so let's make sure we get what we want. */
809 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
812 for (i = 0; i < we.we_wordc; i++)
814 oconfig_item_t *temp;
817 path_ptr = we.we_wordv[i];
819 status = stat (path_ptr, &statbuf);
823 WARNING ("configfile: stat (%s) failed: %s",
825 sstrerror (errno, errbuf, sizeof (errbuf)));
829 if (S_ISREG (statbuf.st_mode))
830 temp = cf_read_file (path_ptr, pattern, depth);
831 else if (S_ISDIR (statbuf.st_mode))
832 temp = cf_read_dir (path_ptr, pattern, depth);
835 WARNING ("configfile: %s is neither a file nor a "
845 cf_ci_append_children (root, temp);
846 sfree (temp->children);
853 } /* oconfig_item_t *cf_read_generic */
854 /* #endif HAVE_WORDEXP_H */
856 #else /* if !HAVE_WORDEXP_H */
857 static oconfig_item_t *cf_read_generic (const char *path,
858 const char *pattern, int depth)
863 if (depth >= CF_MAX_DEPTH)
865 ERROR ("configfile: Not including `%s' because the maximum "
866 "nesting depth has been reached.", path);
870 status = stat (path, &statbuf);
874 ERROR ("configfile: stat (%s) failed: %s",
876 sstrerror (errno, errbuf, sizeof (errbuf)));
880 if (S_ISREG (statbuf.st_mode))
881 return (cf_read_file (path, pattern, depth));
882 else if (S_ISDIR (statbuf.st_mode))
883 return (cf_read_dir (path, pattern, depth));
885 ERROR ("configfile: %s is neither a file nor a directory.", path);
887 } /* oconfig_item_t *cf_read_generic */
888 #endif /* !HAVE_WORDEXP_H */
893 int global_option_set (const char *option, const char *value)
897 DEBUG ("option = %s; value = %s;", option, value);
899 for (i = 0; i < cf_global_options_num; i++)
900 if (strcasecmp (cf_global_options[i].key, option) == 0)
903 if (i >= cf_global_options_num)
906 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
908 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
909 "command-line option `-P' take precedence.");
913 sfree (cf_global_options[i].value);
916 cf_global_options[i].value = strdup (value);
918 cf_global_options[i].value = NULL;
923 const char *global_option_get (const char *option)
927 for (i = 0; i < cf_global_options_num; i++)
928 if (strcasecmp (cf_global_options[i].key, option) == 0)
931 if (i >= cf_global_options_num)
934 return ((cf_global_options[i].value != NULL)
935 ? cf_global_options[i].value
936 : cf_global_options[i].def);
937 } /* char *global_option_get */
939 long global_option_get_long (const char *option, long default_value)
944 str = global_option_get (option);
946 return (default_value);
949 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
951 return (default_value);
954 } /* char *global_option_get_long */
956 cdtime_t cf_get_default_interval (void)
958 char const *str = global_option_get ("Interval");
959 double interval_double = COLLECTD_DEFAULT_INTERVAL;
964 double tmp = strtod (str, &endptr);
966 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
967 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
970 ERROR ("cf_get_default_interval: Interval must be a positive number. "
971 "The current number is %g.", tmp);
973 interval_double = tmp;
976 return (DOUBLE_TO_CDTIME_T (interval_double));
977 } /* }}} cdtime_t cf_get_default_interval */
979 void cf_unregister (const char *type)
981 cf_callback_t *this, *prev;
983 for (prev = NULL, this = first_callback;
985 prev = this, this = this->next)
986 if (strcasecmp (this->type, type) == 0)
989 first_callback = this->next;
991 prev->next = this->next;
996 } /* void cf_unregister */
998 void cf_unregister_complex (const char *type)
1000 cf_complex_callback_t *this, *prev;
1002 for (prev = NULL, this = complex_callback_head;
1004 prev = this, this = this->next)
1005 if (strcasecmp (this->type, type) == 0)
1008 complex_callback_head = this->next;
1010 prev->next = this->next;
1016 } /* void cf_unregister */
1018 void cf_register (const char *type,
1019 int (*callback) (const char *, const char *),
1020 const char **keys, int keys_num)
1022 cf_callback_t *cf_cb;
1024 /* Remove this module from the list, if it already exists */
1025 cf_unregister (type);
1027 /* This pointer will be free'd in `cf_unregister' */
1028 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1032 cf_cb->callback = callback;
1034 cf_cb->keys_num = keys_num;
1035 cf_cb->ctx = plugin_get_ctx ();
1037 cf_cb->next = first_callback;
1038 first_callback = cf_cb;
1039 } /* void cf_register */
1041 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1043 cf_complex_callback_t *new;
1045 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1049 new->type = strdup (type);
1050 if (new->type == NULL)
1056 new->callback = callback;
1059 new->ctx = plugin_get_ctx ();
1061 if (complex_callback_head == NULL)
1063 complex_callback_head = new;
1067 cf_complex_callback_t *last = complex_callback_head;
1068 while (last->next != NULL)
1074 } /* int cf_register_complex */
1076 int cf_read (char *filename)
1078 oconfig_item_t *conf;
1081 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1084 ERROR ("Unable to read config file %s.", filename);
1087 else if (conf->children_num == 0)
1089 ERROR ("Configuration file %s is empty.", filename);
1090 oconfig_free (conf);
1094 for (i = 0; i < conf->children_num; i++)
1096 if (conf->children[i].children == NULL)
1097 dispatch_value (conf->children + i);
1099 dispatch_block (conf->children + i);
1102 oconfig_free (conf);
1104 /* Read the default types.db if no `TypesDB' option was given. */
1105 if (cf_default_typesdb)
1106 read_types_list (PKGDATADIR"/types.db");
1111 /* Assures the config option is a string, duplicates it and returns the copy in
1112 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1114 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1118 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1120 ERROR ("cf_util_get_string: The %s option requires "
1121 "exactly one string argument.", ci->key);
1125 string = strdup (ci->values[0].value.string);
1129 if (*ret_string != NULL)
1130 sfree (*ret_string);
1131 *ret_string = string;
1134 } /* }}} int cf_util_get_string */
1136 /* Assures the config option is a string and copies it to the provided buffer.
1137 * Assures null-termination. */
1138 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1141 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1144 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1146 ERROR ("cf_util_get_string_buffer: The %s option requires "
1147 "exactly one string argument.", ci->key);
1151 strncpy (buffer, ci->values[0].value.string, buffer_size);
1152 buffer[buffer_size - 1] = 0;
1155 } /* }}} int cf_util_get_string_buffer */
1157 /* Assures the config option is a number and returns it as an int. */
1158 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1160 if ((ci == NULL) || (ret_value == NULL))
1163 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1165 ERROR ("cf_util_get_int: The %s option requires "
1166 "exactly one numeric argument.", ci->key);
1170 *ret_value = (int) ci->values[0].value.number;
1173 } /* }}} int cf_util_get_int */
1175 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1177 if ((ci == NULL) || (ret_value == NULL))
1180 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1182 ERROR ("cf_util_get_double: The %s option requires "
1183 "exactly one numeric argument.", ci->key);
1187 *ret_value = ci->values[0].value.number;
1190 } /* }}} int cf_util_get_double */
1192 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1194 if ((ci == NULL) || (ret_bool == NULL))
1197 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1199 ERROR ("cf_util_get_boolean: The %s option requires "
1200 "exactly one boolean argument.", ci->key);
1204 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1207 } /* }}} int cf_util_get_boolean */
1209 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1210 unsigned int *ret_value, unsigned int flag)
1215 if (ret_value == NULL)
1219 status = cf_util_get_boolean (ci, &b);
1229 *ret_value &= ~flag;
1233 } /* }}} int cf_util_get_flag */
1235 /* Assures that the config option is a string or a number if the correct range
1236 * of 1-65535. The string is then converted to a port number using
1237 * `service_name_to_port_number' and returned.
1238 * Returns the port number in the range [1-65535] or less than zero upon
1240 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1244 if ((ci->values_num != 1)
1245 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1246 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1248 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1249 "exactly one string argument.", ci->key);
1253 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1254 return (service_name_to_port_number (ci->values[0].value.string));
1256 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1257 tmp = (int) (ci->values[0].value.number + 0.5);
1258 if ((tmp < 1) || (tmp > 65535))
1260 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1261 "a service name or a port number. The number "
1262 "you specified, %i, is not in the valid "
1263 "range of 1-65535.",
1269 } /* }}} int cf_util_get_port_number */
1271 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1277 if (ci->values_num != 1)
1279 ERROR ("cf_util_get_service: The %s option requires exactly "
1280 "one argument.", ci->key);
1284 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1285 return (cf_util_get_string (ci, ret_string));
1286 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1288 ERROR ("cf_util_get_service: The %s option requires "
1289 "exactly one string or numeric argument.",
1294 status = cf_util_get_int (ci, &port);
1297 else if ((port < 1) || (port > 65535))
1299 ERROR ("cf_util_get_service: The port number given "
1300 "for the %s option is out of "
1301 "range (%i).", ci->key, port);
1305 service = malloc (6);
1306 if (service == NULL)
1308 ERROR ("cf_util_get_service: Out of memory.");
1311 ssnprintf (service, 6, "%i", port);
1313 sfree (*ret_string);
1314 *ret_string = service;
1317 } /* }}} int cf_util_get_service */
1319 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1321 if ((ci == NULL) || (ret_value == NULL))
1324 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1326 ERROR ("cf_util_get_cdtime: The %s option requires "
1327 "exactly one numeric argument.", ci->key);
1331 if (ci->values[0].value.number < 0.0)
1333 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1334 "option must not be negative.", ci->key);
1338 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1341 } /* }}} int cf_util_get_cdtime */