8a7621d2b5fb912b02d64c8c9f86456d42dbea08
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 const char *key;
76 int (*func) (oconfig_item_t *);
77 } cf_value_map_t;
79 typedef struct cf_global_option_s
80 {
81 const char *key;
82 char *value;
83 const 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 if (orig_key == NULL)
160 return (EINVAL);
162 DEBUG ("type = %s, key = %s, value = %s",
163 ESCAPE_NULL(type),
164 orig_key,
165 ESCAPE_NULL(orig_value));
167 if ((cf_cb = cf_search (type)) == NULL)
168 {
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);
172 return (-1);
173 }
175 if ((key = strdup (orig_key)) == NULL)
176 return (1);
177 if ((value = strdup (orig_value)) == NULL)
178 {
179 free (key);
180 return (2);
181 }
183 ret = -1;
185 old_ctx = plugin_set_ctx (cf_cb->ctx);
187 for (i = 0; i < cf_cb->keys_num; i++)
188 {
189 if ((cf_cb->keys[i] != NULL)
190 && (strcasecmp (cf_cb->keys[i], key) == 0))
191 {
192 ret = (*cf_cb->callback) (key, value);
193 break;
194 }
195 }
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);
202 free (key);
203 free (value);
205 return (ret);
206 } /* int cf_dispatch */
208 static int dispatch_global_option (const oconfig_item_t *ci)
209 {
210 if (ci->values_num != 1)
211 return (-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)
215 {
216 char tmp[128];
217 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
218 return (global_option_set (ci->key, tmp));
219 }
220 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
221 {
222 if (ci->values[0].value.boolean)
223 return (global_option_set (ci->key, "true"));
224 else
225 return (global_option_set (ci->key, "false"));
226 }
228 return (-1);
229 } /* int dispatch_global_option */
231 static int dispatch_value_typesdb (oconfig_item_t *ci)
232 {
233 int i = 0;
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.");
241 return (-1);
242 }
244 for (i = 0; i < ci->values_num; ++i)
245 {
246 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
247 WARNING ("configfile: TypesDB: Skipping %i. argument which "
248 "is not a string.", i + 1);
249 continue;
250 }
252 read_types_list (ci->values[i].value.string);
253 }
254 return (0);
255 } /* int dispatch_value_typesdb */
257 static int dispatch_value_plugindir (oconfig_item_t *ci)
258 {
259 assert (strcasecmp (ci->key, "PluginDir") == 0);
261 if (ci->values_num != 1)
262 return (-1);
263 if (ci->values[0].type != OCONFIG_TYPE_STRING)
264 return (-1);
266 plugin_set_dir (ci->values[0].value.string);
267 return (0);
268 }
270 static int dispatch_loadplugin (oconfig_item_t *ci)
271 {
272 int i;
273 const char *name;
274 unsigned int flags = 0;
275 plugin_ctx_t ctx;
276 plugin_ctx_t old_ctx;
277 int ret_val;
279 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
281 if (ci->values_num != 1)
282 return (-1);
283 if (ci->values[0].type != OCONFIG_TYPE_STRING)
284 return (-1);
286 name = ci->values[0].value.string;
287 if (strcmp ("libvirt", name) == 0)
288 name = "virt";
290 /* default to the global interval set before loading this plugin */
291 memset (&ctx, 0, sizeof (ctx));
292 ctx.interval = cf_get_default_interval ();
293 ctx.flush_interval = 0;
294 ctx.flush_timeout = 0;
296 for (i = 0; i < ci->children_num; ++i)
297 {
298 oconfig_item_t *child = ci->children + i;
300 if (strcasecmp("Globals", child->key) == 0)
301 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
302 else if (strcasecmp ("Interval", child->key) == 0)
303 cf_util_get_cdtime (child, &ctx.interval);
304 else if (strcasecmp ("FlushInterval", child->key) == 0)
305 cf_util_get_cdtime (child, &ctx.flush_interval);
306 else if (strcasecmp ("FlushTimeout", child->key) == 0)
307 cf_util_get_cdtime (child, &ctx.flush_timeout);
308 else {
309 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
310 "for plugin \"%s\"",
311 child->key, ci->values[0].value.string);
312 }
313 }
315 old_ctx = plugin_set_ctx (ctx);
316 ret_val = plugin_load (name, (uint32_t) flags);
317 /* reset to the "global" context */
318 plugin_set_ctx (old_ctx);
320 return (ret_val);
321 } /* int dispatch_value_loadplugin */
323 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
324 {
325 char buffer[4096];
326 char *buffer_ptr;
327 int buffer_free;
328 int i;
330 buffer_ptr = buffer;
331 buffer_free = sizeof (buffer);
333 for (i = 0; i < ci->values_num; i++)
334 {
335 int status = -1;
337 if (ci->values[i].type == OCONFIG_TYPE_STRING)
338 status = ssnprintf (buffer_ptr, buffer_free, " %s",
339 ci->values[i].value.string);
340 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
341 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
342 ci->values[i].value.number);
343 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
344 status = ssnprintf (buffer_ptr, buffer_free, " %s",
345 ci->values[i].value.boolean
346 ? "true" : "false");
348 if ((status < 0) || (status >= buffer_free))
349 return (-1);
350 buffer_free -= status;
351 buffer_ptr += status;
352 }
353 /* skip the initial space */
354 buffer_ptr = buffer + 1;
356 return (cf_dispatch (plugin, ci->key, buffer_ptr));
357 } /* int dispatch_value_plugin */
359 static int dispatch_value (oconfig_item_t *ci)
360 {
361 int ret = -2;
362 int i;
364 for (i = 0; i < cf_value_map_num; i++)
365 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
366 {
367 ret = cf_value_map[i].func (ci);
368 break;
369 }
371 for (i = 0; i < cf_global_options_num; i++)
372 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
373 {
374 ret = dispatch_global_option (ci);
375 break;
376 }
378 return (ret);
379 } /* int dispatch_value */
381 static int dispatch_block_plugin (oconfig_item_t *ci)
382 {
383 int i;
384 const char *name;
386 cf_complex_callback_t *cb;
388 if (strcasecmp (ci->key, "Plugin") != 0)
389 return (-1);
390 if (ci->values_num < 1)
391 return (-1);
392 if (ci->values[0].type != OCONFIG_TYPE_STRING)
393 return (-1);
395 name = ci->values[0].value.string;
396 if (strcmp ("libvirt", name) == 0)
397 {
398 /* TODO(octo): Remove this legacy. */
399 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
400 "Your configuration is still using the old name. "
401 "Please change it to use \"virt\" as soon as possible. "
402 "This compatibility code will go away eventually.");
403 name = "virt";
404 }
406 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
407 {
408 plugin_ctx_t ctx;
409 plugin_ctx_t old_ctx;
410 int status;
412 /* default to the global interval set before loading this plugin */
413 memset (&ctx, 0, sizeof (ctx));
414 ctx.interval = cf_get_default_interval ();
416 old_ctx = plugin_set_ctx (ctx);
417 status = plugin_load (name, /* flags = */ 0);
418 /* reset to the "global" context */
419 plugin_set_ctx (old_ctx);
421 if (status != 0)
422 {
423 ERROR ("Automatically loading plugin \"%s\" failed "
424 "with status %i.", name, status);
425 return (status);
426 }
427 }
429 /* Check for a complex callback first */
430 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
431 {
432 if (strcasecmp (name, cb->type) == 0)
433 {
434 plugin_ctx_t old_ctx;
435 int ret_val;
437 old_ctx = plugin_set_ctx (cb->ctx);
438 ret_val = (cb->callback (ci));
439 plugin_set_ctx (old_ctx);
440 return (ret_val);
441 }
442 }
444 /* Hm, no complex plugin found. Dispatch the values one by one */
445 for (i = 0; i < ci->children_num; i++)
446 {
447 if (ci->children[i].children == NULL)
448 dispatch_value_plugin (name, ci->children + i);
449 else
450 {
451 WARNING ("There is a `%s' block within the "
452 "configuration for the %s plugin. "
453 "The plugin either only expects "
454 "\"simple\" configuration statements "
455 "or wasn't loaded using `LoadPlugin'."
456 " Please check your configuration.",
457 ci->children[i].key, name);
458 }
459 }
461 return (0);
462 }
465 static int dispatch_block (oconfig_item_t *ci)
466 {
467 if (strcasecmp (ci->key, "LoadPlugin") == 0)
468 return (dispatch_loadplugin (ci));
469 else if (strcasecmp (ci->key, "Plugin") == 0)
470 return (dispatch_block_plugin (ci));
471 else if (strcasecmp (ci->key, "Chain") == 0)
472 return (fc_configure (ci));
474 return (0);
475 }
477 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
478 int offset)
479 {
480 oconfig_item_t *temp;
481 int i;
483 assert (offset >= 0);
484 assert (dst->children_num > offset);
486 /* Free the memory used by the replaced child. Usually that's the
487 * `Include "blah"' statement. */
488 temp = dst->children + offset;
489 for (i = 0; i < temp->values_num; i++)
490 {
491 if (temp->values[i].type == OCONFIG_TYPE_STRING)
492 {
493 sfree (temp->values[i].value.string);
494 }
495 }
496 sfree (temp->values);
497 temp = NULL;
499 /* If (src->children_num == 0) the array size is decreased. If offset
500 * is _not_ the last element, (offset < (dst->children_num - 1)), then
501 * we need to move the trailing elements before resizing the array. */
502 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
503 {
504 int nmemb = dst->children_num - (offset + 1);
505 memmove (dst->children + offset, dst->children + offset + 1,
506 sizeof (oconfig_item_t) * nmemb);
507 }
509 /* Resize the memory containing the children to be big enough to hold
510 * all children. */
511 if (dst->children_num + src->children_num - 1 == 0)
512 {
513 dst->children_num = 0;
514 return (0);
515 }
517 temp = (oconfig_item_t *) realloc (dst->children,
518 sizeof (oconfig_item_t)
519 * (dst->children_num + src->children_num - 1));
520 if (temp == NULL)
521 {
522 ERROR ("configfile: realloc failed.");
523 return (-1);
524 }
525 dst->children = temp;
527 /* If there are children behind the include statement, and they have
528 * not yet been moved because (src->children_num == 0), then move them
529 * to the end of the list, so that the new children have room before
530 * them. */
531 if ((src->children_num > 0)
532 && ((dst->children_num - (offset + 1)) > 0))
533 {
534 int nmemb = dst->children_num - (offset + 1);
535 int old_offset = offset + 1;
536 int new_offset = offset + src->children_num;
538 memmove (dst->children + new_offset,
539 dst->children + old_offset,
540 sizeof (oconfig_item_t) * nmemb);
541 }
543 /* Last but not least: If there are new children, copy them to the
544 * memory reserved for them. */
545 if (src->children_num > 0)
546 {
547 memcpy (dst->children + offset,
548 src->children,
549 sizeof (oconfig_item_t) * src->children_num);
550 }
552 /* Update the number of children. */
553 dst->children_num += (src->children_num - 1);
555 return (0);
556 } /* int cf_ci_replace_child */
558 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
559 {
560 oconfig_item_t *temp;
562 if ((src == NULL) || (src->children_num == 0))
563 return (0);
565 temp = (oconfig_item_t *) realloc (dst->children,
566 sizeof (oconfig_item_t)
567 * (dst->children_num + src->children_num));
568 if (temp == NULL)
569 {
570 ERROR ("configfile: realloc failed.");
571 return (-1);
572 }
573 dst->children = temp;
575 memcpy (dst->children + dst->children_num,
576 src->children,
577 sizeof (oconfig_item_t)
578 * src->children_num);
579 dst->children_num += src->children_num;
581 return (0);
582 } /* int cf_ci_append_children */
584 #define CF_MAX_DEPTH 8
585 static oconfig_item_t *cf_read_generic (const char *path,
586 const char *pattern, int depth);
588 static int cf_include_all (oconfig_item_t *root, int depth)
589 {
590 int i;
592 for (i = 0; i < root->children_num; i++)
593 {
594 oconfig_item_t *new;
595 oconfig_item_t *old;
597 char *pattern = NULL;
599 int j;
601 if (strcasecmp (root->children[i].key, "Include") != 0)
602 continue;
604 old = root->children + i;
606 if ((old->values_num != 1)
607 || (old->values[0].type != OCONFIG_TYPE_STRING))
608 {
609 ERROR ("configfile: `Include' needs exactly one string argument.");
610 continue;
611 }
613 for (j = 0; j < old->children_num; ++j)
614 {
615 oconfig_item_t *child = old->children + j;
617 if (strcasecmp (child->key, "Filter") == 0)
618 cf_util_get_string (child, &pattern);
619 else
620 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
621 child->key);
622 }
624 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
625 sfree (pattern);
627 if (new == NULL)
628 return (-1);
630 /* Now replace the i'th child in `root' with `new'. */
631 if (cf_ci_replace_child (root, new, i) < 0) {
632 sfree (new->values);
633 sfree (new);
634 return (-1);
635 }
637 /* ... and go back to the new i'th child. */
638 --i;
640 sfree (new->values);
641 sfree (new);
642 } /* for (i = 0; i < root->children_num; i++) */
644 return (0);
645 } /* int cf_include_all */
647 static oconfig_item_t *cf_read_file (const char *file,
648 const char *pattern, int depth)
649 {
650 oconfig_item_t *root;
651 int status;
653 assert (depth < CF_MAX_DEPTH);
655 if (pattern != NULL) {
656 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
657 char *tmp = sstrdup (file);
658 char *filename = basename (tmp);
660 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
661 DEBUG ("configfile: Not including `%s' because it "
662 "does not match pattern `%s'.",
663 filename, pattern);
664 free (tmp);
665 return (NULL);
666 }
668 free (tmp);
669 #else
670 ERROR ("configfile: Cannot apply pattern filter '%s' "
671 "to file '%s': functions basename() and / or "
672 "fnmatch() not available.", pattern, file);
673 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
674 }
676 root = oconfig_parse_file (file);
677 if (root == NULL)
678 {
679 ERROR ("configfile: Cannot read file `%s'.", file);
680 return (NULL);
681 }
683 status = cf_include_all (root, depth);
684 if (status != 0)
685 {
686 oconfig_free (root);
687 return (NULL);
688 }
690 return (root);
691 } /* oconfig_item_t *cf_read_file */
693 static int cf_compare_string (const void *p1, const void *p2)
694 {
695 return strcmp (*(const char **) p1, *(const char **) p2);
696 }
698 static oconfig_item_t *cf_read_dir (const char *dir,
699 const char *pattern, int depth)
700 {
701 oconfig_item_t *root = NULL;
702 DIR *dh;
703 struct dirent *de;
704 char **filenames = NULL;
705 int filenames_num = 0;
706 int status;
707 int i;
709 assert (depth < CF_MAX_DEPTH);
711 dh = opendir (dir);
712 if (dh == NULL)
713 {
714 char errbuf[1024];
715 ERROR ("configfile: opendir failed: %s",
716 sstrerror (errno, errbuf, sizeof (errbuf)));
717 return (NULL);
718 }
720 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
721 if (root == NULL)
722 {
723 ERROR ("configfile: malloc failed.");
724 closedir (dh);
725 return (NULL);
726 }
727 memset (root, 0, sizeof (oconfig_item_t));
729 while ((de = readdir (dh)) != NULL)
730 {
731 char name[1024];
732 char **tmp;
734 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
735 continue;
737 status = ssnprintf (name, sizeof (name), "%s/%s",
738 dir, de->d_name);
739 if ((status < 0) || ((size_t) status >= sizeof (name)))
740 {
741 ERROR ("configfile: Not including `%s/%s' because its"
742 " name is too long.",
743 dir, de->d_name);
744 closedir (dh);
745 for (i = 0; i < filenames_num; ++i)
746 free (filenames[i]);
747 free (filenames);
748 free (root);
749 return (NULL);
750 }
752 ++filenames_num;
753 tmp = (char **) realloc (filenames,
754 filenames_num * sizeof (*filenames));
755 if (tmp == NULL) {
756 ERROR ("configfile: realloc failed.");
757 closedir (dh);
758 for (i = 0; i < filenames_num - 1; ++i)
759 free (filenames[i]);
760 free (filenames);
761 free (root);
762 return (NULL);
763 }
764 filenames = tmp;
766 filenames[filenames_num - 1] = sstrdup (name);
767 }
769 if (filenames == NULL)
770 {
771 closedir (dh);
772 return (root);
773 }
775 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
776 cf_compare_string);
778 for (i = 0; i < filenames_num; ++i)
779 {
780 oconfig_item_t *temp;
781 char *name = filenames[i];
783 temp = cf_read_generic (name, pattern, depth);
784 if (temp == NULL)
785 {
786 /* An error should already have been reported. */
787 sfree (name);
788 continue;
789 }
791 cf_ci_append_children (root, temp);
792 sfree (temp->children);
793 sfree (temp);
795 free (name);
796 }
798 closedir (dh);
799 free(filenames);
800 return (root);
801 } /* oconfig_item_t *cf_read_dir */
803 /*
804 * cf_read_generic
805 *
806 * Path is stat'ed and either cf_read_file or cf_read_dir is called
807 * accordingly.
808 *
809 * There are two versions of this function: If `wordexp' exists shell wildcards
810 * will be expanded and the function will include all matches found. If
811 * `wordexp' (or, more precisely, it's header file) is not available the
812 * simpler function is used which does not do any such expansion.
813 */
814 #if HAVE_WORDEXP_H
815 static oconfig_item_t *cf_read_generic (const char *path,
816 const char *pattern, int depth)
817 {
818 oconfig_item_t *root = NULL;
819 int status;
820 const char *path_ptr;
821 wordexp_t we;
822 size_t i;
824 if (depth >= CF_MAX_DEPTH)
825 {
826 ERROR ("configfile: Not including `%s' because the maximum "
827 "nesting depth has been reached.", path);
828 return (NULL);
829 }
831 status = wordexp (path, &we, WRDE_NOCMD);
832 if (status != 0)
833 {
834 ERROR ("configfile: wordexp (%s) failed.", path);
835 return (NULL);
836 }
838 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
839 if (root == NULL)
840 {
841 ERROR ("configfile: malloc failed.");
842 return (NULL);
843 }
844 memset (root, '\0', sizeof (oconfig_item_t));
846 /* wordexp() might return a sorted list already. That's not
847 * documented though, so let's make sure we get what we want. */
848 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
849 cf_compare_string);
851 for (i = 0; i < we.we_wordc; i++)
852 {
853 oconfig_item_t *temp;
854 struct stat statbuf;
856 path_ptr = we.we_wordv[i];
858 status = stat (path_ptr, &statbuf);
859 if (status != 0)
860 {
861 char errbuf[1024];
862 WARNING ("configfile: stat (%s) failed: %s",
863 path_ptr,
864 sstrerror (errno, errbuf, sizeof (errbuf)));
865 continue;
866 }
868 if (S_ISREG (statbuf.st_mode))
869 temp = cf_read_file (path_ptr, pattern, depth);
870 else if (S_ISDIR (statbuf.st_mode))
871 temp = cf_read_dir (path_ptr, pattern, depth);
872 else
873 {
874 WARNING ("configfile: %s is neither a file nor a "
875 "directory.", path);
876 continue;
877 }
879 if (temp == NULL) {
880 oconfig_free (root);
881 return (NULL);
882 }
884 cf_ci_append_children (root, temp);
885 sfree (temp->children);
886 sfree (temp);
887 }
889 wordfree (&we);
891 return (root);
892 } /* oconfig_item_t *cf_read_generic */
893 /* #endif HAVE_WORDEXP_H */
895 #else /* if !HAVE_WORDEXP_H */
896 static oconfig_item_t *cf_read_generic (const char *path,
897 const char *pattern, int depth)
898 {
899 struct stat statbuf;
900 int status;
902 if (depth >= CF_MAX_DEPTH)
903 {
904 ERROR ("configfile: Not including `%s' because the maximum "
905 "nesting depth has been reached.", path);
906 return (NULL);
907 }
909 status = stat (path, &statbuf);
910 if (status != 0)
911 {
912 char errbuf[1024];
913 ERROR ("configfile: stat (%s) failed: %s",
914 path,
915 sstrerror (errno, errbuf, sizeof (errbuf)));
916 return (NULL);
917 }
919 if (S_ISREG (statbuf.st_mode))
920 return (cf_read_file (path, pattern, depth));
921 else if (S_ISDIR (statbuf.st_mode))
922 return (cf_read_dir (path, pattern, depth));
924 ERROR ("configfile: %s is neither a file nor a directory.", path);
925 return (NULL);
926 } /* oconfig_item_t *cf_read_generic */
927 #endif /* !HAVE_WORDEXP_H */
929 /*
930 * Public functions
931 */
932 int global_option_set (const char *option, const char *value)
933 {
934 int i;
936 DEBUG ("option = %s; value = %s;", option, value);
938 for (i = 0; i < cf_global_options_num; i++)
939 if (strcasecmp (cf_global_options[i].key, option) == 0)
940 break;
942 if (i >= cf_global_options_num)
943 return (-1);
945 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
946 {
947 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
948 "command-line option `-P' take precedence.");
949 return (0);
950 }
952 sfree (cf_global_options[i].value);
954 if (value != NULL)
955 cf_global_options[i].value = strdup (value);
956 else
957 cf_global_options[i].value = NULL;
959 return (0);
960 }
962 const char *global_option_get (const char *option)
963 {
964 int i;
966 for (i = 0; i < cf_global_options_num; i++)
967 if (strcasecmp (cf_global_options[i].key, option) == 0)
968 break;
970 if (i >= cf_global_options_num)
971 return (NULL);
973 return ((cf_global_options[i].value != NULL)
974 ? cf_global_options[i].value
975 : cf_global_options[i].def);
976 } /* char *global_option_get */
978 long global_option_get_long (const char *option, long default_value)
979 {
980 const char *str;
981 long value;
983 str = global_option_get (option);
984 if (NULL == str)
985 return (default_value);
987 errno = 0;
988 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
989 if (errno != 0)
990 return (default_value);
992 return (value);
993 } /* char *global_option_get_long */
995 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
996 {
997 char const *optstr;
998 char *endptr = NULL;
999 double v;
1001 optstr = global_option_get (name);
1002 if (optstr == NULL)
1003 return (def);
1005 errno = 0;
1006 v = strtod (optstr, &endptr);
1007 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
1008 return (def);
1009 else if (v <= 0.0)
1010 return (def);
1012 return (DOUBLE_TO_CDTIME_T (v));
1013 } /* }}} cdtime_t global_option_get_time */
1015 cdtime_t cf_get_default_interval (void)
1016 {
1017 return (global_option_get_time ("Interval",
1018 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1019 }
1021 void cf_unregister (const char *type)
1022 {
1023 cf_callback_t *this, *prev;
1025 for (prev = NULL, this = first_callback;
1026 this != NULL;
1027 prev = this, this = this->next)
1028 if (strcasecmp (this->type, type) == 0)
1029 {
1030 if (prev == NULL)
1031 first_callback = this->next;
1032 else
1033 prev->next = this->next;
1035 free (this);
1036 break;
1037 }
1038 } /* void cf_unregister */
1040 void cf_unregister_complex (const char *type)
1041 {
1042 cf_complex_callback_t *this, *prev;
1044 for (prev = NULL, this = complex_callback_head;
1045 this != NULL;
1046 prev = this, this = this->next)
1047 if (strcasecmp (this->type, type) == 0)
1048 {
1049 if (prev == NULL)
1050 complex_callback_head = this->next;
1051 else
1052 prev->next = this->next;
1054 sfree (this->type);
1055 sfree (this);
1056 break;
1057 }
1058 } /* void cf_unregister */
1060 void cf_register (const char *type,
1061 int (*callback) (const char *, const char *),
1062 const char **keys, int keys_num)
1063 {
1064 cf_callback_t *cf_cb;
1066 /* Remove this module from the list, if it already exists */
1067 cf_unregister (type);
1069 /* This pointer will be free'd in `cf_unregister' */
1070 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1071 return;
1073 cf_cb->type = type;
1074 cf_cb->callback = callback;
1075 cf_cb->keys = keys;
1076 cf_cb->keys_num = keys_num;
1077 cf_cb->ctx = plugin_get_ctx ();
1079 cf_cb->next = first_callback;
1080 first_callback = cf_cb;
1081 } /* void cf_register */
1083 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1084 {
1085 cf_complex_callback_t *new;
1087 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1088 if (new == NULL)
1089 return (-1);
1091 new->type = strdup (type);
1092 if (new->type == NULL)
1093 {
1094 sfree (new);
1095 return (-1);
1096 }
1098 new->callback = callback;
1099 new->next = NULL;
1101 new->ctx = plugin_get_ctx ();
1103 if (complex_callback_head == NULL)
1104 {
1105 complex_callback_head = new;
1106 }
1107 else
1108 {
1109 cf_complex_callback_t *last = complex_callback_head;
1110 while (last->next != NULL)
1111 last = last->next;
1112 last->next = new;
1113 }
1115 return (0);
1116 } /* int cf_register_complex */
1118 int cf_read (const char *filename)
1119 {
1120 oconfig_item_t *conf;
1121 int i;
1123 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1124 if (conf == NULL)
1125 {
1126 ERROR ("Unable to read config file %s.", filename);
1127 return (-1);
1128 }
1129 else if (conf->children_num == 0)
1130 {
1131 ERROR ("Configuration file %s is empty.", filename);
1132 oconfig_free (conf);
1133 return (-1);
1134 }
1136 for (i = 0; i < conf->children_num; i++)
1137 {
1138 if (conf->children[i].children == NULL)
1139 dispatch_value (conf->children + i);
1140 else
1141 dispatch_block (conf->children + i);
1142 }
1144 oconfig_free (conf);
1146 /* Read the default types.db if no `TypesDB' option was given. */
1147 if (cf_default_typesdb)
1148 read_types_list (PKGDATADIR"/types.db");
1150 return (0);
1151 } /* int cf_read */
1153 /* Assures the config option is a string, duplicates it and returns the copy in
1154 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1155 * success. */
1156 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1157 {
1158 char *string;
1160 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1161 {
1162 ERROR ("cf_util_get_string: The %s option requires "
1163 "exactly one string argument.", ci->key);
1164 return (-1);
1165 }
1167 string = strdup (ci->values[0].value.string);
1168 if (string == NULL)
1169 return (-1);
1171 if (*ret_string != NULL)
1172 sfree (*ret_string);
1173 *ret_string = string;
1175 return (0);
1176 } /* }}} int cf_util_get_string */
1178 /* Assures the config option is a string and copies it to the provided buffer.
1179 * Assures null-termination. */
1180 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1181 size_t buffer_size)
1182 {
1183 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1184 return (EINVAL);
1186 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1187 {
1188 ERROR ("cf_util_get_string_buffer: The %s option requires "
1189 "exactly one string argument.", ci->key);
1190 return (-1);
1191 }
1193 strncpy (buffer, ci->values[0].value.string, buffer_size);
1194 buffer[buffer_size - 1] = 0;
1196 return (0);
1197 } /* }}} int cf_util_get_string_buffer */
1199 /* Assures the config option is a number and returns it as an int. */
1200 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1201 {
1202 if ((ci == NULL) || (ret_value == NULL))
1203 return (EINVAL);
1205 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1206 {
1207 ERROR ("cf_util_get_int: The %s option requires "
1208 "exactly one numeric argument.", ci->key);
1209 return (-1);
1210 }
1212 *ret_value = (int) ci->values[0].value.number;
1214 return (0);
1215 } /* }}} int cf_util_get_int */
1217 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1218 {
1219 if ((ci == NULL) || (ret_value == NULL))
1220 return (EINVAL);
1222 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1223 {
1224 ERROR ("cf_util_get_double: The %s option requires "
1225 "exactly one numeric argument.", ci->key);
1226 return (-1);
1227 }
1229 *ret_value = ci->values[0].value.number;
1231 return (0);
1232 } /* }}} int cf_util_get_double */
1234 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1235 {
1236 if ((ci == NULL) || (ret_bool == NULL))
1237 return (EINVAL);
1239 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1240 {
1241 ERROR ("cf_util_get_boolean: The %s option requires "
1242 "exactly one boolean argument.", ci->key);
1243 return (-1);
1244 }
1246 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1248 return (0);
1249 } /* }}} int cf_util_get_boolean */
1251 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1252 unsigned int *ret_value, unsigned int flag)
1253 {
1254 int status;
1255 _Bool b;
1257 if (ret_value == NULL)
1258 return (EINVAL);
1260 b = 0;
1261 status = cf_util_get_boolean (ci, &b);
1262 if (status != 0)
1263 return (status);
1265 if (b)
1266 {
1267 *ret_value |= flag;
1268 }
1269 else
1270 {
1271 *ret_value &= ~flag;
1272 }
1274 return (0);
1275 } /* }}} int cf_util_get_flag */
1277 /* Assures that the config option is a string or a number if the correct range
1278 * of 1-65535. The string is then converted to a port number using
1279 * `service_name_to_port_number' and returned.
1280 * Returns the port number in the range [1-65535] or less than zero upon
1281 * failure. */
1282 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1283 {
1284 int tmp;
1286 if ((ci->values_num != 1)
1287 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1288 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1289 {
1290 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1291 "exactly one string argument.", ci->key);
1292 return (-1);
1293 }
1295 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1296 return (service_name_to_port_number (ci->values[0].value.string));
1298 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1299 tmp = (int) (ci->values[0].value.number + 0.5);
1300 if ((tmp < 1) || (tmp > 65535))
1301 {
1302 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1303 "a service name or a port number. The number "
1304 "you specified, %i, is not in the valid "
1305 "range of 1-65535.",
1306 ci->key, tmp);
1307 return (-1);
1308 }
1310 return (tmp);
1311 } /* }}} int cf_util_get_port_number */
1313 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1314 {
1315 int port;
1316 char *service;
1317 int status;
1319 if (ci->values_num != 1)
1320 {
1321 ERROR ("cf_util_get_service: The %s option requires exactly "
1322 "one argument.", ci->key);
1323 return (-1);
1324 }
1326 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1327 return (cf_util_get_string (ci, ret_string));
1328 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1329 {
1330 ERROR ("cf_util_get_service: The %s option requires "
1331 "exactly one string or numeric argument.",
1332 ci->key);
1333 }
1335 port = 0;
1336 status = cf_util_get_int (ci, &port);
1337 if (status != 0)
1338 return (status);
1339 else if ((port < 1) || (port > 65535))
1340 {
1341 ERROR ("cf_util_get_service: The port number given "
1342 "for the %s option is out of "
1343 "range (%i).", ci->key, port);
1344 return (-1);
1345 }
1347 service = malloc (6);
1348 if (service == NULL)
1349 {
1350 ERROR ("cf_util_get_service: Out of memory.");
1351 return (-1);
1352 }
1353 ssnprintf (service, 6, "%i", port);
1355 sfree (*ret_string);
1356 *ret_string = service;
1358 return (0);
1359 } /* }}} int cf_util_get_service */
1361 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1362 {
1363 if ((ci == NULL) || (ret_value == NULL))
1364 return (EINVAL);
1366 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1367 {
1368 ERROR ("cf_util_get_cdtime: The %s option requires "
1369 "exactly one numeric argument.", ci->key);
1370 return (-1);
1371 }
1373 if (ci->values[0].value.number < 0.0)
1374 {
1375 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1376 "option must not be negative.", ci->key);
1377 return (-1);
1378 }
1380 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1382 return (0);
1383 } /* }}} int cf_util_get_cdtime */