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) (const 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 (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);
93 /*
94 * Private variables
95 */
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[] =
100 {
101 {"TypesDB", dispatch_value_typesdb},
102 {"PluginDir", dispatch_value_plugindir},
103 {"LoadPlugin", dispatch_loadplugin}
104 };
105 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
107 static cf_global_option_t cf_global_options[] =
108 {
109 {"BaseDir", NULL, PKGLOCALSTATEDIR},
110 {"PIDFile", NULL, PIDFILE},
111 {"Hostname", NULL, NULL},
112 {"FQDNLookup", NULL, "true"},
113 {"Interval", NULL, NULL},
114 {"ReadThreads", NULL, "5"},
115 {"WriteThreads", NULL, "5"},
116 {"WriteQueueLimitHigh", NULL, NULL},
117 {"WriteQueueLimitLow", NULL, NULL},
118 {"Timeout", NULL, "2"},
119 {"AutoLoadPlugin", NULL, "false"},
120 {"CollectInternalStats", NULL, "false"},
121 {"PreCacheChain", NULL, "PreCache"},
122 {"PostCacheChain", NULL, "PostCache"},
123 {"MaxReadInterval", NULL, "86400"}
124 };
125 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
127 static int cf_default_typesdb = 1;
129 /*
130 * Functions to handle register/unregister, search, and other plugin related
131 * stuff
132 */
133 static cf_callback_t *cf_search (const char *type)
134 {
135 cf_callback_t *cf_cb;
137 if (type == NULL)
138 return (NULL);
140 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
141 if (strcasecmp (cf_cb->type, type) == 0)
142 break;
144 return (cf_cb);
145 }
147 static int cf_dispatch (const char *type, const char *orig_key,
148 const char *orig_value)
149 {
150 cf_callback_t *cf_cb;
151 plugin_ctx_t old_ctx;
152 char *key;
153 char *value;
154 int ret;
155 int i;
157 DEBUG ("type = %s, key = %s, value = %s",
158 ESCAPE_NULL(type),
159 ESCAPE_NULL(orig_key),
160 ESCAPE_NULL(orig_value));
162 if ((cf_cb = cf_search (type)) == NULL)
163 {
164 WARNING ("Found a configuration for the `%s' plugin, but "
165 "the plugin isn't loaded or didn't register "
166 "a configuration callback.", type);
167 return (-1);
168 }
170 if ((key = strdup (orig_key)) == NULL)
171 return (1);
172 if ((value = strdup (orig_value)) == NULL)
173 {
174 free (key);
175 return (2);
176 }
178 ret = -1;
180 old_ctx = plugin_set_ctx (cf_cb->ctx);
182 for (i = 0; i < cf_cb->keys_num; i++)
183 {
184 if ((cf_cb->keys[i] != NULL)
185 && (strcasecmp (cf_cb->keys[i], key) == 0))
186 {
187 ret = (*cf_cb->callback) (key, value);
188 break;
189 }
190 }
192 plugin_set_ctx (old_ctx);
194 if (i >= cf_cb->keys_num)
195 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
197 free (key);
198 free (value);
200 DEBUG ("cf_dispatch: return (%i)", ret);
202 return (ret);
203 } /* int cf_dispatch */
205 static int dispatch_global_option (const oconfig_item_t *ci)
206 {
207 if (ci->values_num != 1)
208 return (-1);
209 if (ci->values[0].type == OCONFIG_TYPE_STRING)
210 return (global_option_set (ci->key, ci->values[0].value.string));
211 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
212 {
213 char tmp[128];
214 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
215 return (global_option_set (ci->key, tmp));
216 }
217 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
218 {
219 if (ci->values[0].value.boolean)
220 return (global_option_set (ci->key, "true"));
221 else
222 return (global_option_set (ci->key, "false"));
223 }
225 return (-1);
226 } /* int dispatch_global_option */
228 static int dispatch_value_typesdb (const oconfig_item_t *ci)
229 {
230 int i = 0;
232 assert (strcasecmp (ci->key, "TypesDB") == 0);
234 cf_default_typesdb = 0;
236 if (ci->values_num < 1) {
237 ERROR ("configfile: `TypesDB' needs at least one argument.");
238 return (-1);
239 }
241 for (i = 0; i < ci->values_num; ++i)
242 {
243 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
244 WARNING ("configfile: TypesDB: Skipping %i. argument which "
245 "is not a string.", i + 1);
246 continue;
247 }
249 read_types_list (ci->values[i].value.string);
250 }
251 return (0);
252 } /* int dispatch_value_typesdb */
254 static int dispatch_value_plugindir (const oconfig_item_t *ci)
255 {
256 assert (strcasecmp (ci->key, "PluginDir") == 0);
258 if (ci->values_num != 1)
259 return (-1);
260 if (ci->values[0].type != OCONFIG_TYPE_STRING)
261 return (-1);
263 plugin_set_dir (ci->values[0].value.string);
264 return (0);
265 }
267 static int dispatch_loadplugin (const oconfig_item_t *ci)
268 {
269 int i;
270 const char *name;
271 unsigned int flags = 0;
272 plugin_ctx_t ctx;
273 plugin_ctx_t old_ctx;
274 int ret_val;
276 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
278 if (ci->values_num != 1)
279 return (-1);
280 if (ci->values[0].type != OCONFIG_TYPE_STRING)
281 return (-1);
283 name = ci->values[0].value.string;
284 if (strcmp ("libvirt", name) == 0)
285 name = "virt";
287 /* default to the global interval set before loading this plugin */
288 memset (&ctx, 0, sizeof (ctx));
289 ctx.interval = cf_get_default_interval ();
291 for (i = 0; i < ci->children_num; ++i) {
292 if (strcasecmp("Globals", ci->children[i].key) == 0)
293 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
294 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
295 if (cf_util_get_cdtime (ci->children + i, &ctx.interval) != 0) {
296 /* cf_util_get_cdtime will log an error */
297 continue;
298 }
299 }
300 else {
301 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
302 "for plugin \"%s\"",
303 ci->children[i].key, ci->values[0].value.string);
304 }
305 }
307 old_ctx = plugin_set_ctx (ctx);
308 ret_val = plugin_load (name, (uint32_t) flags);
309 /* reset to the "global" context */
310 plugin_set_ctx (old_ctx);
312 return (ret_val);
313 } /* int dispatch_value_loadplugin */
315 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
316 {
317 char buffer[4096];
318 char *buffer_ptr;
319 int buffer_free;
320 int i;
322 buffer_ptr = buffer;
323 buffer_free = sizeof (buffer);
325 for (i = 0; i < ci->values_num; i++)
326 {
327 int status = -1;
329 if (ci->values[i].type == OCONFIG_TYPE_STRING)
330 status = ssnprintf (buffer_ptr, buffer_free, " %s",
331 ci->values[i].value.string);
332 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
333 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
334 ci->values[i].value.number);
335 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
336 status = ssnprintf (buffer_ptr, buffer_free, " %s",
337 ci->values[i].value.boolean
338 ? "true" : "false");
340 if ((status < 0) || (status >= buffer_free))
341 return (-1);
342 buffer_free -= status;
343 buffer_ptr += status;
344 }
345 /* skip the initial space */
346 buffer_ptr = buffer + 1;
348 return (cf_dispatch (plugin, ci->key, buffer_ptr));
349 } /* int dispatch_value_plugin */
351 static int dispatch_value (const oconfig_item_t *ci)
352 {
353 int ret = -2;
354 int i;
356 for (i = 0; i < cf_value_map_num; i++)
357 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
358 {
359 ret = cf_value_map[i].func (ci);
360 break;
361 }
363 for (i = 0; i < cf_global_options_num; i++)
364 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
365 {
366 ret = dispatch_global_option (ci);
367 break;
368 }
370 return (ret);
371 } /* int dispatch_value */
373 static int dispatch_block_plugin (oconfig_item_t *ci)
374 {
375 int i;
376 char *name;
378 cf_complex_callback_t *cb;
380 if (strcasecmp (ci->key, "Plugin") != 0)
381 return (-1);
382 if (ci->values_num < 1)
383 return (-1);
384 if (ci->values[0].type != OCONFIG_TYPE_STRING)
385 return (-1);
387 name = ci->values[0].value.string;
388 if (strcmp ("libvirt", name) == 0)
389 {
390 /* TODO(octo): Remove this legacy. */
391 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
392 "Your configuration is still using the old name. "
393 "Please change it to use \"virt\" as soon as possible. "
394 "This compatibility code will go away eventually.");
395 name = "virt";
396 }
398 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
399 {
400 int status;
402 status = plugin_load (name, /* flags = */ 0);
403 if (status != 0)
404 {
405 ERROR ("Automatically loading plugin \"%s\" failed "
406 "with status %i.", name, status);
407 return (status);
408 }
409 }
411 /* Check for a complex callback first */
412 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
413 {
414 if (strcasecmp (name, cb->type) == 0)
415 {
416 plugin_ctx_t old_ctx;
417 int ret_val;
419 old_ctx = plugin_set_ctx (cb->ctx);
420 ret_val = (cb->callback (ci));
421 plugin_set_ctx (old_ctx);
422 return (ret_val);
423 }
424 }
426 /* Hm, no complex plugin found. Dispatch the values one by one */
427 for (i = 0; i < ci->children_num; i++)
428 {
429 if (ci->children[i].children == NULL)
430 dispatch_value_plugin (name, ci->children + i);
431 else
432 {
433 WARNING ("There is a `%s' block within the "
434 "configuration for the %s plugin. "
435 "The plugin either only expects "
436 "\"simple\" configuration statements "
437 "or wasn't loaded using `LoadPlugin'."
438 " Please check your configuration.",
439 ci->children[i].key, name);
440 }
441 }
443 return (0);
444 }
447 static int dispatch_block (oconfig_item_t *ci)
448 {
449 if (strcasecmp (ci->key, "LoadPlugin") == 0)
450 return (dispatch_loadplugin (ci));
451 else if (strcasecmp (ci->key, "Plugin") == 0)
452 return (dispatch_block_plugin (ci));
453 else if (strcasecmp (ci->key, "Chain") == 0)
454 return (fc_configure (ci));
456 return (0);
457 }
459 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
460 int offset)
461 {
462 oconfig_item_t *temp;
463 int i;
465 assert (offset >= 0);
466 assert (dst->children_num > offset);
468 /* Free the memory used by the replaced child. Usually that's the
469 * `Include "blah"' statement. */
470 temp = dst->children + offset;
471 for (i = 0; i < temp->values_num; i++)
472 {
473 if (temp->values[i].type == OCONFIG_TYPE_STRING)
474 {
475 sfree (temp->values[i].value.string);
476 }
477 }
478 sfree (temp->values);
479 temp = NULL;
481 /* If (src->children_num == 0) the array size is decreased. If offset
482 * is _not_ the last element, (offset < (dst->children_num - 1)), then
483 * we need to move the trailing elements before resizing the array. */
484 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
485 {
486 int nmemb = dst->children_num - (offset + 1);
487 memmove (dst->children + offset, dst->children + offset + 1,
488 sizeof (oconfig_item_t) * nmemb);
489 }
491 /* Resize the memory containing the children to be big enough to hold
492 * all children. */
493 if (dst->children_num + src->children_num - 1 == 0)
494 {
495 dst->children_num = 0;
496 return (0);
497 }
499 temp = (oconfig_item_t *) realloc (dst->children,
500 sizeof (oconfig_item_t)
501 * (dst->children_num + src->children_num - 1));
502 if (temp == NULL)
503 {
504 ERROR ("configfile: realloc failed.");
505 return (-1);
506 }
507 dst->children = temp;
509 /* If there are children behind the include statement, and they have
510 * not yet been moved because (src->children_num == 0), then move them
511 * to the end of the list, so that the new children have room before
512 * them. */
513 if ((src->children_num > 0)
514 && ((dst->children_num - (offset + 1)) > 0))
515 {
516 int nmemb = dst->children_num - (offset + 1);
517 int old_offset = offset + 1;
518 int new_offset = offset + src->children_num;
520 memmove (dst->children + new_offset,
521 dst->children + old_offset,
522 sizeof (oconfig_item_t) * nmemb);
523 }
525 /* Last but not least: If there are new children, copy them to the
526 * memory reserved for them. */
527 if (src->children_num > 0)
528 {
529 memcpy (dst->children + offset,
530 src->children,
531 sizeof (oconfig_item_t) * src->children_num);
532 }
534 /* Update the number of children. */
535 dst->children_num += (src->children_num - 1);
537 return (0);
538 } /* int cf_ci_replace_child */
540 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
541 {
542 oconfig_item_t *temp;
544 if ((src == NULL) || (src->children_num == 0))
545 return (0);
547 temp = (oconfig_item_t *) realloc (dst->children,
548 sizeof (oconfig_item_t)
549 * (dst->children_num + src->children_num));
550 if (temp == NULL)
551 {
552 ERROR ("configfile: realloc failed.");
553 return (-1);
554 }
555 dst->children = temp;
557 memcpy (dst->children + dst->children_num,
558 src->children,
559 sizeof (oconfig_item_t)
560 * src->children_num);
561 dst->children_num += src->children_num;
563 return (0);
564 } /* int cf_ci_append_children */
566 #define CF_MAX_DEPTH 8
567 static oconfig_item_t *cf_read_generic (const char *path,
568 const char *pattern, int depth);
570 static int cf_include_all (oconfig_item_t *root, int depth)
571 {
572 int i;
574 for (i = 0; i < root->children_num; i++)
575 {
576 oconfig_item_t *new;
577 oconfig_item_t *old;
579 char *pattern = NULL;
581 int j;
583 if (strcasecmp (root->children[i].key, "Include") != 0)
584 continue;
586 old = root->children + i;
588 if ((old->values_num != 1)
589 || (old->values[0].type != OCONFIG_TYPE_STRING))
590 {
591 ERROR ("configfile: `Include' needs exactly one string argument.");
592 continue;
593 }
595 for (j = 0; j < old->children_num; ++j)
596 {
597 oconfig_item_t *child = old->children + j;
599 if (strcasecmp (child->key, "Filter") == 0)
600 cf_util_get_string (child, &pattern);
601 else
602 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
603 child->key);
604 }
606 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
607 sfree (pattern);
609 if (new == NULL)
610 return (-1);
612 /* Now replace the i'th child in `root' with `new'. */
613 if (cf_ci_replace_child (root, new, i) < 0)
614 return (-1);
616 /* ... and go back to the new i'th child. */
617 --i;
619 sfree (new->values);
620 sfree (new);
621 } /* for (i = 0; i < root->children_num; i++) */
623 return (0);
624 } /* int cf_include_all */
626 static oconfig_item_t *cf_read_file (const char *file,
627 const char *pattern, int depth)
628 {
629 oconfig_item_t *root;
630 int status;
632 assert (depth < CF_MAX_DEPTH);
634 if (pattern != NULL) {
635 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
636 char *tmp = sstrdup (file);
637 char *filename = basename (tmp);
639 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
640 DEBUG ("configfile: Not including `%s' because it "
641 "does not match pattern `%s'.",
642 filename, pattern);
643 free (tmp);
644 return (NULL);
645 }
647 free (tmp);
648 #else
649 ERROR ("configfile: Cannot apply pattern filter '%s' "
650 "to file '%s': functions basename() and / or "
651 "fnmatch() not available.", pattern, file);
652 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
653 }
655 root = oconfig_parse_file (file);
656 if (root == NULL)
657 {
658 ERROR ("configfile: Cannot read file `%s'.", file);
659 return (NULL);
660 }
662 status = cf_include_all (root, depth);
663 if (status != 0)
664 {
665 oconfig_free (root);
666 return (NULL);
667 }
669 return (root);
670 } /* oconfig_item_t *cf_read_file */
672 static int cf_compare_string (const void *p1, const void *p2)
673 {
674 return strcmp (*(const char **) p1, *(const char **) p2);
675 }
677 static oconfig_item_t *cf_read_dir (const char *dir,
678 const char *pattern, int depth)
679 {
680 oconfig_item_t *root = NULL;
681 DIR *dh;
682 struct dirent *de;
683 char **filenames = NULL;
684 int filenames_num = 0;
685 int status;
686 int i;
688 assert (depth < CF_MAX_DEPTH);
690 dh = opendir (dir);
691 if (dh == NULL)
692 {
693 char errbuf[1024];
694 ERROR ("configfile: opendir failed: %s",
695 sstrerror (errno, errbuf, sizeof (errbuf)));
696 return (NULL);
697 }
699 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
700 if (root == NULL)
701 {
702 ERROR ("configfile: malloc failed.");
703 return (NULL);
704 }
705 memset (root, 0, sizeof (oconfig_item_t));
707 while ((de = readdir (dh)) != NULL)
708 {
709 char name[1024];
710 char **tmp;
712 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
713 continue;
715 status = ssnprintf (name, sizeof (name), "%s/%s",
716 dir, de->d_name);
717 if ((status < 0) || ((size_t) status >= sizeof (name)))
718 {
719 ERROR ("configfile: Not including `%s/%s' because its"
720 " name is too long.",
721 dir, de->d_name);
722 for (i = 0; i < filenames_num; ++i)
723 free (filenames[i]);
724 free (filenames);
725 free (root);
726 return (NULL);
727 }
729 ++filenames_num;
730 tmp = (char **) realloc (filenames,
731 filenames_num * sizeof (*filenames));
732 if (tmp == NULL) {
733 ERROR ("configfile: realloc failed.");
734 for (i = 0; i < filenames_num - 1; ++i)
735 free (filenames[i]);
736 free (filenames);
737 free (root);
738 return (NULL);
739 }
740 filenames = tmp;
742 filenames[filenames_num - 1] = sstrdup (name);
743 }
745 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
746 cf_compare_string);
748 for (i = 0; i < filenames_num; ++i)
749 {
750 oconfig_item_t *temp;
751 char *name = filenames[i];
753 temp = cf_read_generic (name, pattern, depth);
754 if (temp == NULL)
755 {
756 /* An error should already have been reported. */
757 sfree (name);
758 continue;
759 }
761 cf_ci_append_children (root, temp);
762 sfree (temp->children);
763 sfree (temp);
765 free (name);
766 }
768 free(filenames);
769 return (root);
770 } /* oconfig_item_t *cf_read_dir */
772 /*
773 * cf_read_generic
774 *
775 * Path is stat'ed and either cf_read_file or cf_read_dir is called
776 * accordingly.
777 *
778 * There are two versions of this function: If `wordexp' exists shell wildcards
779 * will be expanded and the function will include all matches found. If
780 * `wordexp' (or, more precisely, it's header file) is not available the
781 * simpler function is used which does not do any such expansion.
782 */
783 #if HAVE_WORDEXP_H
784 static oconfig_item_t *cf_read_generic (const char *path,
785 const char *pattern, int depth)
786 {
787 oconfig_item_t *root = NULL;
788 int status;
789 const char *path_ptr;
790 wordexp_t we;
791 size_t i;
793 if (depth >= CF_MAX_DEPTH)
794 {
795 ERROR ("configfile: Not including `%s' because the maximum "
796 "nesting depth has been reached.", path);
797 return (NULL);
798 }
800 status = wordexp (path, &we, WRDE_NOCMD);
801 if (status != 0)
802 {
803 ERROR ("configfile: wordexp (%s) failed.", path);
804 return (NULL);
805 }
807 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
808 if (root == NULL)
809 {
810 ERROR ("configfile: malloc failed.");
811 return (NULL);
812 }
813 memset (root, '\0', sizeof (oconfig_item_t));
815 /* wordexp() might return a sorted list already. That's not
816 * documented though, so let's make sure we get what we want. */
817 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
818 cf_compare_string);
820 for (i = 0; i < we.we_wordc; i++)
821 {
822 oconfig_item_t *temp;
823 struct stat statbuf;
825 path_ptr = we.we_wordv[i];
827 status = stat (path_ptr, &statbuf);
828 if (status != 0)
829 {
830 char errbuf[1024];
831 WARNING ("configfile: stat (%s) failed: %s",
832 path_ptr,
833 sstrerror (errno, errbuf, sizeof (errbuf)));
834 continue;
835 }
837 if (S_ISREG (statbuf.st_mode))
838 temp = cf_read_file (path_ptr, pattern, depth);
839 else if (S_ISDIR (statbuf.st_mode))
840 temp = cf_read_dir (path_ptr, pattern, depth);
841 else
842 {
843 WARNING ("configfile: %s is neither a file nor a "
844 "directory.", path);
845 continue;
846 }
848 if (temp == NULL) {
849 oconfig_free (root);
850 return (NULL);
851 }
853 cf_ci_append_children (root, temp);
854 sfree (temp->children);
855 sfree (temp);
856 }
858 wordfree (&we);
860 return (root);
861 } /* oconfig_item_t *cf_read_generic */
862 /* #endif HAVE_WORDEXP_H */
864 #else /* if !HAVE_WORDEXP_H */
865 static oconfig_item_t *cf_read_generic (const char *path,
866 const char *pattern, int depth)
867 {
868 struct stat statbuf;
869 int status;
871 if (depth >= CF_MAX_DEPTH)
872 {
873 ERROR ("configfile: Not including `%s' because the maximum "
874 "nesting depth has been reached.", path);
875 return (NULL);
876 }
878 status = stat (path, &statbuf);
879 if (status != 0)
880 {
881 char errbuf[1024];
882 ERROR ("configfile: stat (%s) failed: %s",
883 path,
884 sstrerror (errno, errbuf, sizeof (errbuf)));
885 return (NULL);
886 }
888 if (S_ISREG (statbuf.st_mode))
889 return (cf_read_file (path, pattern, depth));
890 else if (S_ISDIR (statbuf.st_mode))
891 return (cf_read_dir (path, pattern, depth));
893 ERROR ("configfile: %s is neither a file nor a directory.", path);
894 return (NULL);
895 } /* oconfig_item_t *cf_read_generic */
896 #endif /* !HAVE_WORDEXP_H */
898 /*
899 * Public functions
900 */
901 int global_option_set (const char *option, const char *value)
902 {
903 int i;
905 DEBUG ("option = %s; value = %s;", option, value);
907 for (i = 0; i < cf_global_options_num; i++)
908 if (strcasecmp (cf_global_options[i].key, option) == 0)
909 break;
911 if (i >= cf_global_options_num)
912 return (-1);
914 if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
915 {
916 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
917 "command-line option `-P' take precedence.");
918 return (0);
919 }
921 sfree (cf_global_options[i].value);
923 if (value != NULL)
924 cf_global_options[i].value = strdup (value);
925 else
926 cf_global_options[i].value = NULL;
928 return (0);
929 }
931 const char *global_option_get (const char *option)
932 {
933 int i;
935 for (i = 0; i < cf_global_options_num; i++)
936 if (strcasecmp (cf_global_options[i].key, option) == 0)
937 break;
939 if (i >= cf_global_options_num)
940 return (NULL);
942 return ((cf_global_options[i].value != NULL)
943 ? cf_global_options[i].value
944 : cf_global_options[i].def);
945 } /* char *global_option_get */
947 long global_option_get_long (const char *option, long default_value)
948 {
949 const char *str;
950 long value;
952 str = global_option_get (option);
953 if (NULL == str)
954 return (default_value);
956 errno = 0;
957 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
958 if (errno != 0)
959 return (default_value);
961 return (value);
962 } /* char *global_option_get_long */
964 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
965 {
966 char const *optstr;
967 char *endptr = NULL;
968 double v;
970 optstr = global_option_get (name);
971 if (optstr == NULL)
972 return (def);
974 errno = 0;
975 v = strtod (optstr, &endptr);
976 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
977 return (def);
978 else if (v <= 0.0)
979 return (def);
981 return (DOUBLE_TO_CDTIME_T (v));
982 } /* }}} cdtime_t global_option_get_time */
984 cdtime_t cf_get_default_interval (void)
985 {
986 return (global_option_get_time ("Interval",
987 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
988 }
990 void cf_unregister (const char *type)
991 {
992 cf_callback_t *this, *prev;
994 for (prev = NULL, this = first_callback;
995 this != NULL;
996 prev = this, this = this->next)
997 if (strcasecmp (this->type, type) == 0)
998 {
999 if (prev == NULL)
1000 first_callback = this->next;
1001 else
1002 prev->next = this->next;
1004 free (this);
1005 break;
1006 }
1007 } /* void cf_unregister */
1009 void cf_unregister_complex (const char *type)
1010 {
1011 cf_complex_callback_t *this, *prev;
1013 for (prev = NULL, this = complex_callback_head;
1014 this != NULL;
1015 prev = this, this = this->next)
1016 if (strcasecmp (this->type, type) == 0)
1017 {
1018 if (prev == NULL)
1019 complex_callback_head = this->next;
1020 else
1021 prev->next = this->next;
1023 sfree (this->type);
1024 sfree (this);
1025 break;
1026 }
1027 } /* void cf_unregister */
1029 void cf_register (const char *type,
1030 int (*callback) (const char *, const char *),
1031 const char **keys, int keys_num)
1032 {
1033 cf_callback_t *cf_cb;
1035 /* Remove this module from the list, if it already exists */
1036 cf_unregister (type);
1038 /* This pointer will be free'd in `cf_unregister' */
1039 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1040 return;
1042 cf_cb->type = type;
1043 cf_cb->callback = callback;
1044 cf_cb->keys = keys;
1045 cf_cb->keys_num = keys_num;
1046 cf_cb->ctx = plugin_get_ctx ();
1048 cf_cb->next = first_callback;
1049 first_callback = cf_cb;
1050 } /* void cf_register */
1052 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1053 {
1054 cf_complex_callback_t *new;
1056 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1057 if (new == NULL)
1058 return (-1);
1060 new->type = strdup (type);
1061 if (new->type == NULL)
1062 {
1063 sfree (new);
1064 return (-1);
1065 }
1067 new->callback = callback;
1068 new->next = NULL;
1070 new->ctx = plugin_get_ctx ();
1072 if (complex_callback_head == NULL)
1073 {
1074 complex_callback_head = new;
1075 }
1076 else
1077 {
1078 cf_complex_callback_t *last = complex_callback_head;
1079 while (last->next != NULL)
1080 last = last->next;
1081 last->next = new;
1082 }
1084 return (0);
1085 } /* int cf_register_complex */
1087 int cf_read (char *filename)
1088 {
1089 oconfig_item_t *conf;
1090 int i;
1092 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1093 if (conf == NULL)
1094 {
1095 ERROR ("Unable to read config file %s.", filename);
1096 return (-1);
1097 }
1098 else if (conf->children_num == 0)
1099 {
1100 ERROR ("Configuration file %s is empty.", filename);
1101 oconfig_free (conf);
1102 return (-1);
1103 }
1105 for (i = 0; i < conf->children_num; i++)
1106 {
1107 if (conf->children[i].children == NULL)
1108 dispatch_value (conf->children + i);
1109 else
1110 dispatch_block (conf->children + i);
1111 }
1113 oconfig_free (conf);
1115 /* Read the default types.db if no `TypesDB' option was given. */
1116 if (cf_default_typesdb)
1117 read_types_list (PKGDATADIR"/types.db");
1119 return (0);
1120 } /* int cf_read */
1122 /* Assures the config option is a string, duplicates it and returns the copy in
1123 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1124 * success. */
1125 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1126 {
1127 char *string;
1129 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1130 {
1131 ERROR ("cf_util_get_string: The %s option requires "
1132 "exactly one string argument.", ci->key);
1133 return (-1);
1134 }
1136 string = strdup (ci->values[0].value.string);
1137 if (string == NULL)
1138 return (-1);
1140 if (*ret_string != NULL)
1141 sfree (*ret_string);
1142 *ret_string = string;
1144 return (0);
1145 } /* }}} int cf_util_get_string */
1147 /* Assures the config option is a string and copies it to the provided buffer.
1148 * Assures null-termination. */
1149 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1150 size_t buffer_size)
1151 {
1152 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1153 return (EINVAL);
1155 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1156 {
1157 ERROR ("cf_util_get_string_buffer: The %s option requires "
1158 "exactly one string argument.", ci->key);
1159 return (-1);
1160 }
1162 strncpy (buffer, ci->values[0].value.string, buffer_size);
1163 buffer[buffer_size - 1] = 0;
1165 return (0);
1166 } /* }}} int cf_util_get_string_buffer */
1168 /* Assures the config option is a number and returns it as an int. */
1169 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1170 {
1171 if ((ci == NULL) || (ret_value == NULL))
1172 return (EINVAL);
1174 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1175 {
1176 ERROR ("cf_util_get_int: The %s option requires "
1177 "exactly one numeric argument.", ci->key);
1178 return (-1);
1179 }
1181 *ret_value = (int) ci->values[0].value.number;
1183 return (0);
1184 } /* }}} int cf_util_get_int */
1186 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1187 {
1188 if ((ci == NULL) || (ret_value == NULL))
1189 return (EINVAL);
1191 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1192 {
1193 ERROR ("cf_util_get_double: The %s option requires "
1194 "exactly one numeric argument.", ci->key);
1195 return (-1);
1196 }
1198 *ret_value = ci->values[0].value.number;
1200 return (0);
1201 } /* }}} int cf_util_get_double */
1203 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1204 {
1205 if ((ci == NULL) || (ret_bool == NULL))
1206 return (EINVAL);
1208 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1209 {
1210 ERROR ("cf_util_get_boolean: The %s option requires "
1211 "exactly one boolean argument.", ci->key);
1212 return (-1);
1213 }
1215 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1217 return (0);
1218 } /* }}} int cf_util_get_boolean */
1220 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1221 unsigned int *ret_value, unsigned int flag)
1222 {
1223 int status;
1224 _Bool b;
1226 if (ret_value == NULL)
1227 return (EINVAL);
1229 b = 0;
1230 status = cf_util_get_boolean (ci, &b);
1231 if (status != 0)
1232 return (status);
1234 if (b)
1235 {
1236 *ret_value |= flag;
1237 }
1238 else
1239 {
1240 *ret_value &= ~flag;
1241 }
1243 return (0);
1244 } /* }}} int cf_util_get_flag */
1246 /* Assures that the config option is a string or a number if the correct range
1247 * of 1-65535. The string is then converted to a port number using
1248 * `service_name_to_port_number' and returned.
1249 * Returns the port number in the range [1-65535] or less than zero upon
1250 * failure. */
1251 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1252 {
1253 int tmp;
1255 if ((ci->values_num != 1)
1256 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1257 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1258 {
1259 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1260 "exactly one string argument.", ci->key);
1261 return (-1);
1262 }
1264 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1265 return (service_name_to_port_number (ci->values[0].value.string));
1267 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1268 tmp = (int) (ci->values[0].value.number + 0.5);
1269 if ((tmp < 1) || (tmp > 65535))
1270 {
1271 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1272 "a service name or a port number. The number "
1273 "you specified, %i, is not in the valid "
1274 "range of 1-65535.",
1275 ci->key, tmp);
1276 return (-1);
1277 }
1279 return (tmp);
1280 } /* }}} int cf_util_get_port_number */
1282 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1283 {
1284 int port;
1285 char *service;
1286 int status;
1288 if (ci->values_num != 1)
1289 {
1290 ERROR ("cf_util_get_service: The %s option requires exactly "
1291 "one argument.", ci->key);
1292 return (-1);
1293 }
1295 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1296 return (cf_util_get_string (ci, ret_string));
1297 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1298 {
1299 ERROR ("cf_util_get_service: The %s option requires "
1300 "exactly one string or numeric argument.",
1301 ci->key);
1302 }
1304 port = 0;
1305 status = cf_util_get_int (ci, &port);
1306 if (status != 0)
1307 return (status);
1308 else if ((port < 1) || (port > 65535))
1309 {
1310 ERROR ("cf_util_get_service: The port number given "
1311 "for the %s option is out of "
1312 "range (%i).", ci->key, port);
1313 return (-1);
1314 }
1316 service = malloc (6);
1317 if (service == NULL)
1318 {
1319 ERROR ("cf_util_get_service: Out of memory.");
1320 return (-1);
1321 }
1322 ssnprintf (service, 6, "%i", port);
1324 sfree (*ret_string);
1325 *ret_string = service;
1327 return (0);
1328 } /* }}} int cf_util_get_service */
1330 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1331 {
1332 if ((ci == NULL) || (ret_value == NULL))
1333 return (EINVAL);
1335 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1336 {
1337 ERROR ("cf_util_get_cdtime: The %s option requires "
1338 "exactly one numeric argument.", ci->key);
1339 return (-1);
1340 }
1342 if (ci->values[0].value.number < 0.0)
1343 {
1344 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1345 "option must not be negative.", ci->key);
1346 return (-1);
1347 }
1349 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1351 return (0);
1352 } /* }}} int cf_util_get_cdtime */