1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian tokkee Harl <sh at tokkee.org>
26 **/
28 #include "collectd.h"
30 #include "liboconfig/oconfig.h"
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "types_list.h"
36 #include "filter_chain.h"
38 #if HAVE_WORDEXP_H
39 # include <wordexp.h>
40 #endif /* HAVE_WORDEXP_H */
42 #if HAVE_FNMATCH_H
43 # include <fnmatch.h>
44 #endif /* HAVE_FNMATCH_H */
46 #if HAVE_LIBGEN_H
47 # include <libgen.h>
48 #endif /* HAVE_LIBGEN_H */
50 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
52 /*
53 * Private types
54 */
55 typedef struct cf_callback
56 {
57 const char *type;
58 int (*callback) (const char *, const char *);
59 const char **keys;
60 int keys_num;
61 plugin_ctx_t ctx;
62 struct cf_callback *next;
63 } cf_callback_t;
65 typedef struct cf_complex_callback_s
66 {
67 char *type;
68 int (*callback) (oconfig_item_t *);
69 plugin_ctx_t ctx;
70 struct cf_complex_callback_s *next;
71 } cf_complex_callback_t;
73 typedef struct cf_value_map_s
74 {
75 char *key;
76 int (*func) (oconfig_item_t *);
77 } cf_value_map_t;
79 typedef struct cf_global_option_s
80 {
81 char *key;
82 char *value;
83 char *def;
84 } cf_global_option_t;
86 /*
87 * Prototypes of callback functions
88 */
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);
94 /*
95 * Private variables
96 */
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[] =
101 {
102 {"TypesDB", dispatch_value_typesdb},
103 {"PluginDir", dispatch_value_plugindir},
104 {"LoadPlugin", dispatch_loadplugin},
105 {"Plugin", dispatch_block_plugin}
106 };
107 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
109 static cf_global_option_t cf_global_options[] =
110 {
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"}
126 };
127 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
129 static int cf_default_typesdb = 1;
131 /*
132 * Functions to handle register/unregister, search, and other plugin related
133 * stuff
134 */
135 static cf_callback_t *cf_search (const char *type)
136 {
137 cf_callback_t *cf_cb;
139 if (type == NULL)
140 return (NULL);
142 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
143 if (strcasecmp (cf_cb->type, type) == 0)
144 break;
146 return (cf_cb);
147 }
149 static int cf_dispatch (const char *type, const char *orig_key,
150 const char *orig_value)
151 {
152 cf_callback_t *cf_cb;
153 plugin_ctx_t old_ctx;
154 char *key;
155 char *value;
156 int ret;
157 int i;
159 DEBUG ("type = %s, key = %s, value = %s",
160 ESCAPE_NULL(type),
161 ESCAPE_NULL(orig_key),
162 ESCAPE_NULL(orig_value));
164 if ((cf_cb = cf_search (type)) == NULL)
165 {
166 WARNING ("Found a configuration for the `%s' plugin, but "
167 "the plugin isn't loaded or didn't register "
168 "a configuration callback.", type);
169 return (-1);
170 }
172 if ((key = strdup (orig_key)) == NULL)
173 return (1);
174 if ((value = strdup (orig_value)) == NULL)
175 {
176 free (key);
177 return (2);
178 }
180 ret = -1;
182 old_ctx = plugin_set_ctx (cf_cb->ctx);
184 for (i = 0; i < cf_cb->keys_num; i++)
185 {
186 if ((cf_cb->keys[i] != NULL)
187 && (strcasecmp (cf_cb->keys[i], key) == 0))
188 {
189 ret = (*cf_cb->callback) (key, value);
190 break;
191 }
192 }
194 plugin_set_ctx (old_ctx);
196 if (i >= cf_cb->keys_num)
197 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
199 free (key);
200 free (value);
202 DEBUG ("cf_dispatch: return (%i)", ret);
204 return (ret);
205 } /* int cf_dispatch */
207 static int dispatch_global_option (const oconfig_item_t *ci)
208 {
209 if (ci->values_num != 1)
210 return (-1);
211 if (ci->values[0].type == OCONFIG_TYPE_STRING)
212 return (global_option_set (ci->key, ci->values[0].value.string));
213 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
214 {
215 char tmp[128];
216 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
217 return (global_option_set (ci->key, tmp));
218 }
219 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
220 {
221 if (ci->values[0].value.boolean)
222 return (global_option_set (ci->key, "true"));
223 else
224 return (global_option_set (ci->key, "false"));
225 }
227 return (-1);
228 } /* int dispatch_global_option */
230 static int dispatch_value_typesdb (oconfig_item_t *ci)
231 {
232 int i = 0;
234 assert (strcasecmp (ci->key, "TypesDB") == 0);
236 cf_default_typesdb = 0;
238 if (ci->values_num < 1) {
239 ERROR ("configfile: `TypesDB' needs at least one argument.");
240 return (-1);
241 }
243 for (i = 0; i < ci->values_num; ++i)
244 {
245 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
246 WARNING ("configfile: TypesDB: Skipping %i. argument which "
247 "is not a string.", i + 1);
248 continue;
249 }
251 read_types_list (ci->values[i].value.string);
252 }
253 return (0);
254 } /* int dispatch_value_typesdb */
256 static int dispatch_value_plugindir (oconfig_item_t *ci)
257 {
258 assert (strcasecmp (ci->key, "PluginDir") == 0);
260 if (ci->values_num != 1)
261 return (-1);
262 if (ci->values[0].type != OCONFIG_TYPE_STRING)
263 return (-1);
265 plugin_set_dir (ci->values[0].value.string);
266 return (0);
267 }
269 static int dispatch_loadplugin (oconfig_item_t *ci)
270 {
271 int i;
272 const char *name;
273 unsigned int flags = 0;
274 plugin_ctx_t ctx;
275 plugin_ctx_t old_ctx;
276 int ret_val;
278 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
280 if (ci->values_num != 1)
281 return (-1);
282 if (ci->values[0].type != OCONFIG_TYPE_STRING)
283 return (-1);
285 name = ci->values[0].value.string;
286 if (strcmp ("libvirt", name) == 0)
287 name = "virt";
289 /* default to the global interval set before loading this plugin */
290 memset (&ctx, 0, sizeof (ctx));
291 ctx.interval = cf_get_default_interval ();
293 for (i = 0; i < ci->children_num; ++i) {
294 if (strcasecmp("Globals", ci->children[i].key) == 0)
295 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
296 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
297 if (cf_util_get_cdtime (ci->children + i, &ctx.interval) != 0) {
298 /* cf_util_get_cdtime will log an error */
299 continue;
300 }
301 }
302 else {
303 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
304 "for plugin \"%s\"",
305 ci->children[i].key, ci->values[0].value.string);
306 }
307 }
309 old_ctx = plugin_set_ctx (ctx);
310 ret_val = plugin_load (name, (uint32_t) flags);
311 /* reset to the "global" context */
312 plugin_set_ctx (old_ctx);
314 return (ret_val);
315 } /* int dispatch_value_loadplugin */
317 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
318 {
319 char buffer[4096];
320 char *buffer_ptr;
321 int buffer_free;
322 int i;
324 buffer_ptr = buffer;
325 buffer_free = sizeof (buffer);
327 for (i = 0; i < ci->values_num; i++)
328 {
329 int status = -1;
331 if (ci->values[i].type == OCONFIG_TYPE_STRING)
332 status = ssnprintf (buffer_ptr, buffer_free, " %s",
333 ci->values[i].value.string);
334 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
335 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
336 ci->values[i].value.number);
337 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
338 status = ssnprintf (buffer_ptr, buffer_free, " %s",
339 ci->values[i].value.boolean
340 ? "true" : "false");
342 if ((status < 0) || (status >= buffer_free))
343 return (-1);
344 buffer_free -= status;
345 buffer_ptr += status;
346 }
347 /* skip the initial space */
348 buffer_ptr = buffer + 1;
350 return (cf_dispatch (plugin, ci->key, buffer_ptr));
351 } /* int dispatch_value_plugin */
353 static int dispatch_value (oconfig_item_t *ci)
354 {
355 int ret = -2;
356 int i;
358 for (i = 0; i < cf_value_map_num; i++)
359 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
360 {
361 ret = cf_value_map[i].func (ci);
362 break;
363 }
365 for (i = 0; i < cf_global_options_num; i++)
366 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
367 {
368 ret = dispatch_global_option (ci);
369 break;
370 }
372 return (ret);
373 } /* int dispatch_value */
375 static int dispatch_block_plugin (oconfig_item_t *ci)
376 {
377 int i;
378 char *name;
380 cf_complex_callback_t *cb;
382 if (strcasecmp (ci->key, "Plugin") != 0)
383 return (-1);
384 if (ci->values_num < 1)
385 return (-1);
386 if (ci->values[0].type != OCONFIG_TYPE_STRING)
387 return (-1);
389 name = ci->values[0].value.string;
390 if (strcmp ("libvirt", name) == 0)
391 {
392 /* TODO(octo): Remove this legacy. */
393 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
394 "Your configuration is still using the old name. "
395 "Please change it to use \"virt\" as soon as possible. "
396 "This compatibility code will go away eventually.");
397 name = "virt";
398 }
400 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
401 {
402 plugin_ctx_t ctx;
403 plugin_ctx_t old_ctx;
404 int status;
406 /* default to the global interval set before loading this plugin */
407 memset (&ctx, 0, sizeof (ctx));
408 ctx.interval = cf_get_default_interval ();
410 old_ctx = plugin_set_ctx (ctx);
411 status = plugin_load (name, /* flags = */ 0);
412 /* reset to the "global" context */
413 plugin_set_ctx (old_ctx);
415 if (status != 0)
416 {
417 ERROR ("Automatically loading plugin \"%s\" failed "
418 "with status %i.", name, status);
419 return (status);
420 }
421 }
423 /* Check for a complex callback first */
424 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
425 {
426 if (strcasecmp (name, cb->type) == 0)
427 {
428 plugin_ctx_t old_ctx;
429 int ret_val;
431 old_ctx = plugin_set_ctx (cb->ctx);
432 ret_val = (cb->callback (ci));
433 plugin_set_ctx (old_ctx);
434 return (ret_val);
435 }
436 }
438 /* Hm, no complex plugin found. Dispatch the values one by one */
439 for (i = 0; i < ci->children_num; i++)
440 {
441 if (ci->children[i].children == NULL)
442 dispatch_value_plugin (name, ci->children + i);
443 else
444 {
445 WARNING ("There is a `%s' block within the "
446 "configuration for the %s plugin. "
447 "The plugin either only expects "
448 "\"simple\" configuration statements "
449 "or wasn't loaded using `LoadPlugin'."
450 " Please check your configuration.",
451 ci->children[i].key, name);
452 }
453 }
455 return (0);
456 }
459 static int dispatch_block (oconfig_item_t *ci)
460 {
461 if (strcasecmp (ci->key, "LoadPlugin") == 0)
462 return (dispatch_loadplugin (ci));
463 else if (strcasecmp (ci->key, "Plugin") == 0)
464 return (dispatch_block_plugin (ci));
465 else if (strcasecmp (ci->key, "Chain") == 0)
466 return (fc_configure (ci));
468 return (0);
469 }
471 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
472 int offset)
473 {
474 oconfig_item_t *temp;
475 int i;
477 assert (offset >= 0);
478 assert (dst->children_num > offset);
480 /* Free the memory used by the replaced child. Usually that's the
481 * `Include "blah"' statement. */
482 temp = dst->children + offset;
483 for (i = 0; i < temp->values_num; i++)
484 {
485 if (temp->values[i].type == OCONFIG_TYPE_STRING)
486 {
487 sfree (temp->values[i].value.string);
488 }
489 }
490 sfree (temp->values);
491 temp = NULL;
493 /* If (src->children_num == 0) the array size is decreased. If offset
494 * is _not_ the last element, (offset < (dst->children_num - 1)), then
495 * we need to move the trailing elements before resizing the array. */
496 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
497 {
498 int nmemb = dst->children_num - (offset + 1);
499 memmove (dst->children + offset, dst->children + offset + 1,
500 sizeof (oconfig_item_t) * nmemb);
501 }
503 /* Resize the memory containing the children to be big enough to hold
504 * all children. */
505 if (dst->children_num + src->children_num - 1 == 0)
506 {
507 dst->children_num = 0;
508 return (0);
509 }
511 temp = (oconfig_item_t *) realloc (dst->children,
512 sizeof (oconfig_item_t)
513 * (dst->children_num + src->children_num - 1));
514 if (temp == NULL)
515 {
516 ERROR ("configfile: realloc failed.");
517 return (-1);
518 }
519 dst->children = temp;
521 /* If there are children behind the include statement, and they have
522 * not yet been moved because (src->children_num == 0), then move them
523 * to the end of the list, so that the new children have room before
524 * them. */
525 if ((src->children_num > 0)
526 && ((dst->children_num - (offset + 1)) > 0))
527 {
528 int nmemb = dst->children_num - (offset + 1);
529 int old_offset = offset + 1;
530 int new_offset = offset + src->children_num;
532 memmove (dst->children + new_offset,
533 dst->children + old_offset,
534 sizeof (oconfig_item_t) * nmemb);
535 }
537 /* Last but not least: If there are new children, copy them to the
538 * memory reserved for them. */
539 if (src->children_num > 0)
540 {
541 memcpy (dst->children + offset,
542 src->children,
543 sizeof (oconfig_item_t) * src->children_num);
544 }
546 /* Update the number of children. */
547 dst->children_num += (src->children_num - 1);
549 return (0);
550 } /* int cf_ci_replace_child */
552 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
553 {
554 oconfig_item_t *temp;
556 if ((src == NULL) || (src->children_num == 0))
557 return (0);
559 temp = (oconfig_item_t *) realloc (dst->children,
560 sizeof (oconfig_item_t)
561 * (dst->children_num + src->children_num));
562 if (temp == NULL)
563 {
564 ERROR ("configfile: realloc failed.");
565 return (-1);
566 }
567 dst->children = temp;
569 memcpy (dst->children + dst->children_num,
570 src->children,
571 sizeof (oconfig_item_t)
572 * src->children_num);
573 dst->children_num += src->children_num;
575 return (0);
576 } /* int cf_ci_append_children */
578 #define CF_MAX_DEPTH 8
579 static oconfig_item_t *cf_read_generic (const char *path,
580 const char *pattern, int depth);
582 static int cf_include_all (oconfig_item_t *root, int depth)
583 {
584 int i;
586 for (i = 0; i < root->children_num; i++)
587 {
588 oconfig_item_t *new;
589 oconfig_item_t *old;
591 char *pattern = NULL;
593 int j;
595 if (strcasecmp (root->children[i].key, "Include") != 0)
596 continue;
598 old = root->children + i;
600 if ((old->values_num != 1)
601 || (old->values[0].type != OCONFIG_TYPE_STRING))
602 {
603 ERROR ("configfile: `Include' needs exactly one string argument.");
604 continue;
605 }
607 for (j = 0; j < old->children_num; ++j)
608 {
609 oconfig_item_t *child = old->children + j;
611 if (strcasecmp (child->key, "Filter") == 0)
612 cf_util_get_string (child, &pattern);
613 else
614 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
615 child->key);
616 }
618 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
619 sfree (pattern);
621 if (new == NULL)
622 return (-1);
624 /* Now replace the i'th child in `root' with `new'. */
625 if (cf_ci_replace_child (root, new, i) < 0)
626 return (-1);
628 /* ... and go back to the new i'th child. */
629 --i;
631 sfree (new->values);
632 sfree (new);
633 } /* for (i = 0; i < root->children_num; i++) */
635 return (0);
636 } /* int cf_include_all */
638 static oconfig_item_t *cf_read_file (const char *file,
639 const char *pattern, int depth)
640 {
641 oconfig_item_t *root;
642 int status;
644 assert (depth < CF_MAX_DEPTH);
646 if (pattern != NULL) {
647 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
648 char *tmp = sstrdup (file);
649 char *filename = basename (tmp);
651 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
652 DEBUG ("configfile: Not including `%s' because it "
653 "does not match pattern `%s'.",
654 filename, pattern);
655 free (tmp);
656 return (NULL);
657 }
659 free (tmp);
660 #else
661 ERROR ("configfile: Cannot apply pattern filter '%s' "
662 "to file '%s': functions basename() and / or "
663 "fnmatch() not available.", pattern, file);
664 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
665 }
667 root = oconfig_parse_file (file);
668 if (root == NULL)
669 {
670 ERROR ("configfile: Cannot read file `%s'.", file);
671 return (NULL);
672 }
674 status = cf_include_all (root, depth);
675 if (status != 0)
676 {
677 oconfig_free (root);
678 return (NULL);
679 }
681 return (root);
682 } /* oconfig_item_t *cf_read_file */
684 static int cf_compare_string (const void *p1, const void *p2)
685 {
686 return strcmp (*(const char **) p1, *(const char **) p2);
687 }
689 static oconfig_item_t *cf_read_dir (const char *dir,
690 const char *pattern, int depth)
691 {
692 oconfig_item_t *root = NULL;
693 DIR *dh;
694 struct dirent *de;
695 char **filenames = NULL;
696 int filenames_num = 0;
697 int status;
698 int i;
700 assert (depth < CF_MAX_DEPTH);
702 dh = opendir (dir);
703 if (dh == NULL)
704 {
705 char errbuf[1024];
706 ERROR ("configfile: opendir failed: %s",
707 sstrerror (errno, errbuf, sizeof (errbuf)));
708 return (NULL);
709 }
711 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
712 if (root == NULL)
713 {
714 ERROR ("configfile: malloc failed.");
715 return (NULL);
716 }
717 memset (root, 0, sizeof (oconfig_item_t));
719 while ((de = readdir (dh)) != NULL)
720 {
721 char name[1024];
722 char **tmp;
724 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
725 continue;
727 status = ssnprintf (name, sizeof (name), "%s/%s",
728 dir, de->d_name);
729 if ((status < 0) || ((size_t) status >= sizeof (name)))
730 {
731 ERROR ("configfile: Not including `%s/%s' because its"
732 " name is too long.",
733 dir, de->d_name);
734 for (i = 0; i < filenames_num; ++i)
735 free (filenames[i]);
736 free (filenames);
737 free (root);
738 return (NULL);
739 }
741 ++filenames_num;
742 tmp = (char **) realloc (filenames,
743 filenames_num * sizeof (*filenames));
744 if (tmp == NULL) {
745 ERROR ("configfile: realloc failed.");
746 for (i = 0; i < filenames_num - 1; ++i)
747 free (filenames[i]);
748 free (filenames);
749 free (root);
750 return (NULL);
751 }
752 filenames = tmp;
754 filenames[filenames_num - 1] = sstrdup (name);
755 }
757 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
758 cf_compare_string);
760 for (i = 0; i < filenames_num; ++i)
761 {
762 oconfig_item_t *temp;
763 char *name = filenames[i];
765 temp = cf_read_generic (name, pattern, depth);
766 if (temp == NULL)
767 {
768 /* An error should already have been reported. */
769 sfree (name);
770 continue;
771 }
773 cf_ci_append_children (root, temp);
774 sfree (temp->children);
775 sfree (temp);
777 free (name);
778 }
780 free(filenames);
781 return (root);
782 } /* oconfig_item_t *cf_read_dir */
784 /*
785 * cf_read_generic
786 *
787 * Path is stat'ed and either cf_read_file or cf_read_dir is called
788 * accordingly.
789 *
790 * There are two versions of this function: If `wordexp' exists shell wildcards
791 * will be expanded and the function will include all matches found. If
792 * `wordexp' (or, more precisely, it's header file) is not available the
793 * simpler function is used which does not do any such expansion.
794 */
795 #if HAVE_WORDEXP_H
796 static oconfig_item_t *cf_read_generic (const char *path,
797 const char *pattern, int depth)
798 {
799 oconfig_item_t *root = NULL;
800 int status;
801 const char *path_ptr;
802 wordexp_t we;
803 size_t i;
805 if (depth >= CF_MAX_DEPTH)
806 {
807 ERROR ("configfile: Not including `%s' because the maximum "
808 "nesting depth has been reached.", path);
809 return (NULL);
810 }
812 status = wordexp (path, &we, WRDE_NOCMD);
813 if (status != 0)
814 {
815 ERROR ("configfile: wordexp (%s) failed.", path);
816 return (NULL);
817 }
819 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
820 if (root == NULL)
821 {
822 ERROR ("configfile: malloc failed.");
823 return (NULL);
824 }
825 memset (root, '\0', sizeof (oconfig_item_t));
827 /* wordexp() might return a sorted list already. That's not
828 * documented though, so let's make sure we get what we want. */
829 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
830 cf_compare_string);
832 for (i = 0; i < we.we_wordc; i++)
833 {
834 oconfig_item_t *temp;
835 struct stat statbuf;
837 path_ptr = we.we_wordv[i];
839 status = stat (path_ptr, &statbuf);
840 if (status != 0)
841 {
842 char errbuf[1024];
843 WARNING ("configfile: stat (%s) failed: %s",
844 path_ptr,
845 sstrerror (errno, errbuf, sizeof (errbuf)));
846 continue;
847 }
849 if (S_ISREG (statbuf.st_mode))
850 temp = cf_read_file (path_ptr, pattern, depth);
851 else if (S_ISDIR (statbuf.st_mode))
852 temp = cf_read_dir (path_ptr, pattern, depth);
853 else
854 {
855 WARNING ("configfile: %s is neither a file nor a "
856 "directory.", path);
857 continue;
858 }
860 if (temp == NULL) {
861 oconfig_free (root);
862 return (NULL);
863 }
865 cf_ci_append_children (root, temp);
866 sfree (temp->children);
867 sfree (temp);
868 }
870 wordfree (&we);
872 return (root);
873 } /* oconfig_item_t *cf_read_generic */
874 /* #endif HAVE_WORDEXP_H */
876 #else /* if !HAVE_WORDEXP_H */
877 static oconfig_item_t *cf_read_generic (const char *path,
878 const char *pattern, int depth)
879 {
880 struct stat statbuf;
881 int status;
883 if (depth >= CF_MAX_DEPTH)
884 {
885 ERROR ("configfile: Not including `%s' because the maximum "
886 "nesting depth has been reached.", path);
887 return (NULL);
888 }
890 status = stat (path, &statbuf);
891 if (status != 0)
892 {
893 char errbuf[1024];
894 ERROR ("configfile: stat (%s) failed: %s",
895 path,
896 sstrerror (errno, errbuf, sizeof (errbuf)));
897 return (NULL);
898 }
900 if (S_ISREG (statbuf.st_mode))
901 return (cf_read_file (path, pattern, depth));
902 else if (S_ISDIR (statbuf.st_mode))
903 return (cf_read_dir (path, pattern, depth));
905 ERROR ("configfile: %s is neither a file nor a directory.", path);
906 return (NULL);
907 } /* oconfig_item_t *cf_read_generic */
908 #endif /* !HAVE_WORDEXP_H */
910 /*
911 * Public functions
912 */
913 int global_option_set (const char *option, const char *value)
914 {
915 int i;
917 DEBUG ("option = %s; value = %s;", option, value);
919 for (i = 0; i < cf_global_options_num; i++)
920 if (strcasecmp (cf_global_options[i].key, option) == 0)
921 break;
923 if (i >= cf_global_options_num)
924 return (-1);
926 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
927 {
928 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
929 "command-line option `-P' take precedence.");
930 return (0);
931 }
933 sfree (cf_global_options[i].value);
935 if (value != NULL)
936 cf_global_options[i].value = strdup (value);
937 else
938 cf_global_options[i].value = NULL;
940 return (0);
941 }
943 const char *global_option_get (const char *option)
944 {
945 int i;
947 for (i = 0; i < cf_global_options_num; i++)
948 if (strcasecmp (cf_global_options[i].key, option) == 0)
949 break;
951 if (i >= cf_global_options_num)
952 return (NULL);
954 return ((cf_global_options[i].value != NULL)
955 ? cf_global_options[i].value
956 : cf_global_options[i].def);
957 } /* char *global_option_get */
959 long global_option_get_long (const char *option, long default_value)
960 {
961 const char *str;
962 long value;
964 str = global_option_get (option);
965 if (NULL == str)
966 return (default_value);
968 errno = 0;
969 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
970 if (errno != 0)
971 return (default_value);
973 return (value);
974 } /* char *global_option_get_long */
976 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
977 {
978 char const *optstr;
979 char *endptr = NULL;
980 double v;
982 optstr = global_option_get (name);
983 if (optstr == NULL)
984 return (def);
986 errno = 0;
987 v = strtod (optstr, &endptr);
988 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
989 return (def);
990 else if (v <= 0.0)
991 return (def);
993 return (DOUBLE_TO_CDTIME_T (v));
994 } /* }}} cdtime_t global_option_get_time */
996 cdtime_t cf_get_default_interval (void)
997 {
998 return (global_option_get_time ("Interval",
999 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1000 }
1002 void cf_unregister (const char *type)
1003 {
1004 cf_callback_t *this, *prev;
1006 for (prev = NULL, this = first_callback;
1007 this != NULL;
1008 prev = this, this = this->next)
1009 if (strcasecmp (this->type, type) == 0)
1010 {
1011 if (prev == NULL)
1012 first_callback = this->next;
1013 else
1014 prev->next = this->next;
1016 free (this);
1017 break;
1018 }
1019 } /* void cf_unregister */
1021 void cf_unregister_complex (const char *type)
1022 {
1023 cf_complex_callback_t *this, *prev;
1025 for (prev = NULL, this = complex_callback_head;
1026 this != NULL;
1027 prev = this, this = this->next)
1028 if (strcasecmp (this->type, type) == 0)
1029 {
1030 if (prev == NULL)
1031 complex_callback_head = this->next;
1032 else
1033 prev->next = this->next;
1035 sfree (this->type);
1036 sfree (this);
1037 break;
1038 }
1039 } /* void cf_unregister */
1041 void cf_register (const char *type,
1042 int (*callback) (const char *, const char *),
1043 const char **keys, int keys_num)
1044 {
1045 cf_callback_t *cf_cb;
1047 /* Remove this module from the list, if it already exists */
1048 cf_unregister (type);
1050 /* This pointer will be free'd in `cf_unregister' */
1051 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1052 return;
1054 cf_cb->type = type;
1055 cf_cb->callback = callback;
1056 cf_cb->keys = keys;
1057 cf_cb->keys_num = keys_num;
1058 cf_cb->ctx = plugin_get_ctx ();
1060 cf_cb->next = first_callback;
1061 first_callback = cf_cb;
1062 } /* void cf_register */
1064 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1065 {
1066 cf_complex_callback_t *new;
1068 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1069 if (new == NULL)
1070 return (-1);
1072 new->type = strdup (type);
1073 if (new->type == NULL)
1074 {
1075 sfree (new);
1076 return (-1);
1077 }
1079 new->callback = callback;
1080 new->next = NULL;
1082 new->ctx = plugin_get_ctx ();
1084 if (complex_callback_head == NULL)
1085 {
1086 complex_callback_head = new;
1087 }
1088 else
1089 {
1090 cf_complex_callback_t *last = complex_callback_head;
1091 while (last->next != NULL)
1092 last = last->next;
1093 last->next = new;
1094 }
1096 return (0);
1097 } /* int cf_register_complex */
1099 int cf_read (char *filename)
1100 {
1101 oconfig_item_t *conf;
1102 int i;
1104 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1105 if (conf == NULL)
1106 {
1107 ERROR ("Unable to read config file %s.", filename);
1108 return (-1);
1109 }
1110 else if (conf->children_num == 0)
1111 {
1112 ERROR ("Configuration file %s is empty.", filename);
1113 oconfig_free (conf);
1114 return (-1);
1115 }
1117 for (i = 0; i < conf->children_num; i++)
1118 {
1119 if (conf->children[i].children == NULL)
1120 dispatch_value (conf->children + i);
1121 else
1122 dispatch_block (conf->children + i);
1123 }
1125 oconfig_free (conf);
1127 /* Read the default types.db if no `TypesDB' option was given. */
1128 if (cf_default_typesdb)
1129 read_types_list (PKGDATADIR"/types.db");
1131 return (0);
1132 } /* int cf_read */
1134 /* Assures the config option is a string, duplicates it and returns the copy in
1135 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1136 * success. */
1137 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1138 {
1139 char *string;
1141 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1142 {
1143 ERROR ("cf_util_get_string: The %s option requires "
1144 "exactly one string argument.", ci->key);
1145 return (-1);
1146 }
1148 string = strdup (ci->values[0].value.string);
1149 if (string == NULL)
1150 return (-1);
1152 if (*ret_string != NULL)
1153 sfree (*ret_string);
1154 *ret_string = string;
1156 return (0);
1157 } /* }}} int cf_util_get_string */
1159 /* Assures the config option is a string and copies it to the provided buffer.
1160 * Assures null-termination. */
1161 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1162 size_t buffer_size)
1163 {
1164 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1165 return (EINVAL);
1167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1168 {
1169 ERROR ("cf_util_get_string_buffer: The %s option requires "
1170 "exactly one string argument.", ci->key);
1171 return (-1);
1172 }
1174 strncpy (buffer, ci->values[0].value.string, buffer_size);
1175 buffer[buffer_size - 1] = 0;
1177 return (0);
1178 } /* }}} int cf_util_get_string_buffer */
1180 /* Assures the config option is a number and returns it as an int. */
1181 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1182 {
1183 if ((ci == NULL) || (ret_value == NULL))
1184 return (EINVAL);
1186 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1187 {
1188 ERROR ("cf_util_get_int: The %s option requires "
1189 "exactly one numeric argument.", ci->key);
1190 return (-1);
1191 }
1193 *ret_value = (int) ci->values[0].value.number;
1195 return (0);
1196 } /* }}} int cf_util_get_int */
1198 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1199 {
1200 if ((ci == NULL) || (ret_value == NULL))
1201 return (EINVAL);
1203 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1204 {
1205 ERROR ("cf_util_get_double: The %s option requires "
1206 "exactly one numeric argument.", ci->key);
1207 return (-1);
1208 }
1210 *ret_value = ci->values[0].value.number;
1212 return (0);
1213 } /* }}} int cf_util_get_double */
1215 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1216 {
1217 if ((ci == NULL) || (ret_bool == NULL))
1218 return (EINVAL);
1220 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1221 {
1222 ERROR ("cf_util_get_boolean: The %s option requires "
1223 "exactly one boolean argument.", ci->key);
1224 return (-1);
1225 }
1227 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1229 return (0);
1230 } /* }}} int cf_util_get_boolean */
1232 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1233 unsigned int *ret_value, unsigned int flag)
1234 {
1235 int status;
1236 _Bool b;
1238 if (ret_value == NULL)
1239 return (EINVAL);
1241 b = 0;
1242 status = cf_util_get_boolean (ci, &b);
1243 if (status != 0)
1244 return (status);
1246 if (b)
1247 {
1248 *ret_value |= flag;
1249 }
1250 else
1251 {
1252 *ret_value &= ~flag;
1253 }
1255 return (0);
1256 } /* }}} int cf_util_get_flag */
1258 /* Assures that the config option is a string or a number if the correct range
1259 * of 1-65535. The string is then converted to a port number using
1260 * `service_name_to_port_number' and returned.
1261 * Returns the port number in the range [1-65535] or less than zero upon
1262 * failure. */
1263 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1264 {
1265 int tmp;
1267 if ((ci->values_num != 1)
1268 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1269 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1270 {
1271 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1272 "exactly one string argument.", ci->key);
1273 return (-1);
1274 }
1276 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1277 return (service_name_to_port_number (ci->values[0].value.string));
1279 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1280 tmp = (int) (ci->values[0].value.number + 0.5);
1281 if ((tmp < 1) || (tmp > 65535))
1282 {
1283 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1284 "a service name or a port number. The number "
1285 "you specified, %i, is not in the valid "
1286 "range of 1-65535.",
1287 ci->key, tmp);
1288 return (-1);
1289 }
1291 return (tmp);
1292 } /* }}} int cf_util_get_port_number */
1294 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1295 {
1296 int port;
1297 char *service;
1298 int status;
1300 if (ci->values_num != 1)
1301 {
1302 ERROR ("cf_util_get_service: The %s option requires exactly "
1303 "one argument.", ci->key);
1304 return (-1);
1305 }
1307 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1308 return (cf_util_get_string (ci, ret_string));
1309 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1310 {
1311 ERROR ("cf_util_get_service: The %s option requires "
1312 "exactly one string or numeric argument.",
1313 ci->key);
1314 }
1316 port = 0;
1317 status = cf_util_get_int (ci, &port);
1318 if (status != 0)
1319 return (status);
1320 else if ((port < 1) || (port > 65535))
1321 {
1322 ERROR ("cf_util_get_service: The port number given "
1323 "for the %s option is out of "
1324 "range (%i).", ci->key, port);
1325 return (-1);
1326 }
1328 service = malloc (6);
1329 if (service == NULL)
1330 {
1331 ERROR ("cf_util_get_service: Out of memory.");
1332 return (-1);
1333 }
1334 ssnprintf (service, 6, "%i", port);
1336 sfree (*ret_string);
1337 *ret_string = service;
1339 return (0);
1340 } /* }}} int cf_util_get_service */
1342 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1343 {
1344 if ((ci == NULL) || (ret_value == NULL))
1345 return (EINVAL);
1347 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1348 {
1349 ERROR ("cf_util_get_cdtime: The %s option requires "
1350 "exactly one numeric argument.", ci->key);
1351 return (-1);
1352 }
1354 if (ci->values[0].value.number < 0.0)
1355 {
1356 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1357 "option must not be negative.", ci->key);
1358 return (-1);
1359 }
1361 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1363 return (0);
1364 } /* }}} int cf_util_get_cdtime */