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 _Bool from_cli; /* value set from CLI */
84 const char *def;
85 } cf_global_option_t;
87 /*
88 * Prototypes of callback functions
89 */
90 static int dispatch_value_typesdb (oconfig_item_t *ci);
91 static int dispatch_value_plugindir (oconfig_item_t *ci);
92 static int dispatch_loadplugin (oconfig_item_t *ci);
93 static int dispatch_block_plugin (oconfig_item_t *ci);
95 /*
96 * Private variables
97 */
98 static cf_callback_t *first_callback = NULL;
99 static cf_complex_callback_t *complex_callback_head = NULL;
101 static cf_value_map_t cf_value_map[] =
102 {
103 {"TypesDB", dispatch_value_typesdb},
104 {"PluginDir", dispatch_value_plugindir},
105 {"LoadPlugin", dispatch_loadplugin},
106 {"Plugin", dispatch_block_plugin}
107 };
108 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
110 static cf_global_option_t cf_global_options[] =
111 {
112 {"BaseDir", NULL, 0, PKGLOCALSTATEDIR},
113 {"PIDFile", NULL, 0, PIDFILE},
114 {"Hostname", NULL, 0, NULL},
115 {"FQDNLookup", NULL, 0, "true"},
116 {"Interval", NULL, 0, NULL},
117 {"ReadThreads", NULL, 0, "5"},
118 {"WriteThreads", NULL, 0, "5"},
119 {"WriteQueueLimitHigh", NULL, 0, NULL},
120 {"WriteQueueLimitLow", NULL, 0, NULL},
121 {"Timeout", NULL, 0, "2"},
122 {"AutoLoadPlugin", NULL, 0, "false"},
123 {"CollectInternalStats", NULL, 0, "false"},
124 {"PreCacheChain", NULL, 0, "PreCache"},
125 {"PostCacheChain", NULL, 0, "PostCache"},
126 {"MaxReadInterval", NULL, 0, "86400"}
127 };
128 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
130 static int cf_default_typesdb = 1;
132 /*
133 * Functions to handle register/unregister, search, and other plugin related
134 * stuff
135 */
136 static cf_callback_t *cf_search (const char *type)
137 {
138 cf_callback_t *cf_cb;
140 if (type == NULL)
141 return (NULL);
143 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
144 if (strcasecmp (cf_cb->type, type) == 0)
145 break;
147 return (cf_cb);
148 }
150 static int cf_dispatch (const char *type, const char *orig_key,
151 const char *orig_value)
152 {
153 cf_callback_t *cf_cb;
154 plugin_ctx_t old_ctx;
155 char *key;
156 char *value;
157 int ret;
158 int i = 0;
160 if (orig_key == NULL)
161 return (EINVAL);
163 DEBUG ("type = %s, key = %s, value = %s",
164 ESCAPE_NULL(type),
165 orig_key,
166 ESCAPE_NULL(orig_value));
168 if ((cf_cb = cf_search (type)) == NULL)
169 {
170 WARNING ("Found a configuration for the `%s' plugin, but "
171 "the plugin isn't loaded or didn't register "
172 "a configuration callback.", type);
173 return (-1);
174 }
176 if ((key = strdup (orig_key)) == NULL)
177 return (1);
178 if ((value = strdup (orig_value)) == NULL)
179 {
180 free (key);
181 return (2);
182 }
184 ret = -1;
186 old_ctx = plugin_set_ctx (cf_cb->ctx);
188 for (i = 0; i < cf_cb->keys_num; i++)
189 {
190 if ((cf_cb->keys[i] != NULL)
191 && (strcasecmp (cf_cb->keys[i], key) == 0))
192 {
193 ret = (*cf_cb->callback) (key, value);
194 break;
195 }
196 }
198 plugin_set_ctx (old_ctx);
200 if (i >= cf_cb->keys_num)
201 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
203 free (key);
204 free (value);
206 return (ret);
207 } /* int cf_dispatch */
209 static int dispatch_global_option (const oconfig_item_t *ci)
210 {
211 if (ci->values_num != 1)
212 return (-1);
213 if (ci->values[0].type == OCONFIG_TYPE_STRING)
214 return (global_option_set (ci->key, ci->values[0].value.string, 0));
215 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
216 {
217 char tmp[128];
218 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
219 return (global_option_set (ci->key, tmp, 0));
220 }
221 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
222 {
223 if (ci->values[0].value.boolean)
224 return (global_option_set (ci->key, "true", 0));
225 else
226 return (global_option_set (ci->key, "false", 0));
227 }
229 return (-1);
230 } /* int dispatch_global_option */
232 static int dispatch_value_typesdb (oconfig_item_t *ci)
233 {
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 (int 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 const char *name;
272 unsigned int flags = 0;
273 plugin_ctx_t ctx = { 0 };
274 plugin_ctx_t old_ctx;
275 int ret_val;
277 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
279 if (ci->values_num != 1)
280 return (-1);
281 if (ci->values[0].type != OCONFIG_TYPE_STRING)
282 return (-1);
284 name = ci->values[0].value.string;
285 if (strcmp ("libvirt", name) == 0)
286 name = "virt";
288 /* default to the global interval set before loading this plugin */
289 ctx.interval = cf_get_default_interval ();
290 ctx.flush_interval = 0;
291 ctx.flush_timeout = 0;
293 for (int i = 0; i < ci->children_num; ++i)
294 {
295 oconfig_item_t *child = ci->children + i;
297 if (strcasecmp("Globals", child->key) == 0)
298 cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
299 else if (strcasecmp ("Interval", child->key) == 0)
300 cf_util_get_cdtime (child, &ctx.interval);
301 else if (strcasecmp ("FlushInterval", child->key) == 0)
302 cf_util_get_cdtime (child, &ctx.flush_interval);
303 else if (strcasecmp ("FlushTimeout", child->key) == 0)
304 cf_util_get_cdtime (child, &ctx.flush_timeout);
305 else {
306 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
307 "for plugin \"%s\"",
308 child->key, ci->values[0].value.string);
309 }
310 }
312 old_ctx = plugin_set_ctx (ctx);
313 ret_val = plugin_load (name, (uint32_t) flags);
314 /* reset to the "global" context */
315 plugin_set_ctx (old_ctx);
317 return (ret_val);
318 } /* int dispatch_value_loadplugin */
320 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
321 {
322 char buffer[4096];
323 char *buffer_ptr;
324 int buffer_free;
326 buffer_ptr = buffer;
327 buffer_free = sizeof (buffer);
329 for (int i = 0; i < ci->values_num; i++)
330 {
331 int status = -1;
333 if (ci->values[i].type == OCONFIG_TYPE_STRING)
334 status = ssnprintf (buffer_ptr, buffer_free, " %s",
335 ci->values[i].value.string);
336 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
337 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
338 ci->values[i].value.number);
339 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
340 status = ssnprintf (buffer_ptr, buffer_free, " %s",
341 ci->values[i].value.boolean
342 ? "true" : "false");
344 if ((status < 0) || (status >= buffer_free))
345 return (-1);
346 buffer_free -= status;
347 buffer_ptr += status;
348 }
349 /* skip the initial space */
350 buffer_ptr = buffer + 1;
352 return (cf_dispatch (plugin, ci->key, buffer_ptr));
353 } /* int dispatch_value_plugin */
355 static int dispatch_value (oconfig_item_t *ci)
356 {
357 int ret = 0;
359 for (int i = 0; i < cf_value_map_num; i++)
360 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
361 {
362 ret = cf_value_map[i].func (ci);
363 break;
364 }
366 for (int i = 0; i < cf_global_options_num; i++)
367 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
368 {
369 ret = dispatch_global_option (ci);
370 break;
371 }
373 return (ret);
374 } /* int dispatch_value */
376 static int dispatch_block_plugin (oconfig_item_t *ci)
377 {
378 const char *name;
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 plugin_ctx_t ctx = { 0 };
401 plugin_ctx_t old_ctx;
402 int status;
404 /* default to the global interval set before loading this plugin */
405 ctx.interval = cf_get_default_interval ();
407 old_ctx = plugin_set_ctx (ctx);
408 status = plugin_load (name, /* flags = */ 0);
409 /* reset to the "global" context */
410 plugin_set_ctx (old_ctx);
412 if (status != 0)
413 {
414 ERROR ("Automatically loading plugin \"%s\" failed "
415 "with status %i.", name, status);
416 return (status);
417 }
418 }
420 /* Check for a complex callback first */
421 for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL; cb = cb->next)
422 {
423 if (strcasecmp (name, cb->type) == 0)
424 {
425 plugin_ctx_t old_ctx;
426 int ret_val;
428 old_ctx = plugin_set_ctx (cb->ctx);
429 ret_val = (cb->callback (ci));
430 plugin_set_ctx (old_ctx);
431 return (ret_val);
432 }
433 }
435 /* Hm, no complex plugin found. Dispatch the values one by one */
436 for (int i = 0; i < ci->children_num; i++)
437 {
438 if (ci->children[i].children == NULL)
439 dispatch_value_plugin (name, ci->children + i);
440 else
441 {
442 WARNING ("There is a `%s' block within the "
443 "configuration for the %s plugin. "
444 "The plugin either only expects "
445 "\"simple\" configuration statements "
446 "or wasn't loaded using `LoadPlugin'."
447 " Please check your configuration.",
448 ci->children[i].key, name);
449 }
450 }
452 return (0);
453 }
456 static int dispatch_block (oconfig_item_t *ci)
457 {
458 if (strcasecmp (ci->key, "LoadPlugin") == 0)
459 return (dispatch_loadplugin (ci));
460 else if (strcasecmp (ci->key, "Plugin") == 0)
461 return (dispatch_block_plugin (ci));
462 else if (strcasecmp (ci->key, "Chain") == 0)
463 return (fc_configure (ci));
465 return (0);
466 }
468 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
469 int offset)
470 {
471 oconfig_item_t *temp;
473 assert (offset >= 0);
474 assert (dst->children_num > offset);
476 /* Free the memory used by the replaced child. Usually that's the
477 * `Include "blah"' statement. */
478 temp = dst->children + offset;
479 for (int i = 0; i < temp->values_num; i++)
480 {
481 if (temp->values[i].type == OCONFIG_TYPE_STRING)
482 {
483 sfree (temp->values[i].value.string);
484 }
485 }
486 sfree (temp->values);
487 temp = NULL;
489 /* If (src->children_num == 0) the array size is decreased. If offset
490 * is _not_ the last element, (offset < (dst->children_num - 1)), then
491 * we need to move the trailing elements before resizing the array. */
492 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
493 {
494 int nmemb = dst->children_num - (offset + 1);
495 memmove (dst->children + offset, dst->children + offset + 1,
496 sizeof (oconfig_item_t) * nmemb);
497 }
499 /* Resize the memory containing the children to be big enough to hold
500 * all children. */
501 if (dst->children_num + src->children_num - 1 == 0)
502 {
503 dst->children_num = 0;
504 return (0);
505 }
507 temp = realloc (dst->children,
508 sizeof (oconfig_item_t)
509 * (dst->children_num + src->children_num - 1));
510 if (temp == NULL)
511 {
512 ERROR ("configfile: realloc failed.");
513 return (-1);
514 }
515 dst->children = temp;
517 /* If there are children behind the include statement, and they have
518 * not yet been moved because (src->children_num == 0), then move them
519 * to the end of the list, so that the new children have room before
520 * them. */
521 if ((src->children_num > 0)
522 && ((dst->children_num - (offset + 1)) > 0))
523 {
524 int nmemb = dst->children_num - (offset + 1);
525 int old_offset = offset + 1;
526 int new_offset = offset + src->children_num;
528 memmove (dst->children + new_offset,
529 dst->children + old_offset,
530 sizeof (oconfig_item_t) * nmemb);
531 }
533 /* Last but not least: If there are new children, copy them to the
534 * memory reserved for them. */
535 if (src->children_num > 0)
536 {
537 memcpy (dst->children + offset,
538 src->children,
539 sizeof (oconfig_item_t) * src->children_num);
540 }
542 /* Update the number of children. */
543 dst->children_num += (src->children_num - 1);
545 return (0);
546 } /* int cf_ci_replace_child */
548 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
549 {
550 oconfig_item_t *temp;
552 if ((src == NULL) || (src->children_num == 0))
553 return (0);
555 temp = realloc (dst->children,
556 sizeof (oconfig_item_t)
557 * (dst->children_num + src->children_num));
558 if (temp == NULL)
559 {
560 ERROR ("configfile: realloc failed.");
561 return (-1);
562 }
563 dst->children = temp;
565 memcpy (dst->children + dst->children_num,
566 src->children,
567 sizeof (oconfig_item_t)
568 * src->children_num);
569 dst->children_num += src->children_num;
571 return (0);
572 } /* int cf_ci_append_children */
574 #define CF_MAX_DEPTH 8
575 static oconfig_item_t *cf_read_generic (const char *path,
576 const char *pattern, int depth);
578 static int cf_include_all (oconfig_item_t *root, int depth)
579 {
580 for (int i = 0; i < root->children_num; i++)
581 {
582 oconfig_item_t *new;
583 oconfig_item_t *old;
585 char *pattern = NULL;
587 if (strcasecmp (root->children[i].key, "Include") != 0)
588 continue;
590 old = root->children + i;
592 if ((old->values_num != 1)
593 || (old->values[0].type != OCONFIG_TYPE_STRING))
594 {
595 ERROR ("configfile: `Include' needs exactly one string argument.");
596 continue;
597 }
599 for (int j = 0; j < old->children_num; ++j)
600 {
601 oconfig_item_t *child = old->children + j;
603 if (strcasecmp (child->key, "Filter") == 0)
604 cf_util_get_string (child, &pattern);
605 else
606 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
607 child->key);
608 }
610 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
611 sfree (pattern);
613 if (new == NULL)
614 return (-1);
616 /* Now replace the i'th child in `root' with `new'. */
617 if (cf_ci_replace_child (root, new, i) < 0) {
618 sfree (new->values);
619 sfree (new);
620 return (-1);
621 }
623 /* ... and go back to the new i'th child. */
624 --i;
626 sfree (new->values);
627 sfree (new);
628 } /* for (i = 0; i < root->children_num; i++) */
630 return (0);
631 } /* int cf_include_all */
633 static oconfig_item_t *cf_read_file (const char *file,
634 const char *pattern, int depth)
635 {
636 oconfig_item_t *root;
637 int status;
639 assert (depth < CF_MAX_DEPTH);
641 if (pattern != NULL) {
642 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
643 char *tmp = sstrdup (file);
644 char *filename = basename (tmp);
646 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
647 DEBUG ("configfile: Not including `%s' because it "
648 "does not match pattern `%s'.",
649 filename, pattern);
650 free (tmp);
651 return (NULL);
652 }
654 free (tmp);
655 #else
656 ERROR ("configfile: Cannot apply pattern filter '%s' "
657 "to file '%s': functions basename() and / or "
658 "fnmatch() not available.", pattern, file);
659 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
660 }
662 root = oconfig_parse_file (file);
663 if (root == NULL)
664 {
665 ERROR ("configfile: Cannot read file `%s'.", file);
666 return (NULL);
667 }
669 status = cf_include_all (root, depth);
670 if (status != 0)
671 {
672 oconfig_free (root);
673 return (NULL);
674 }
676 return (root);
677 } /* oconfig_item_t *cf_read_file */
679 static int cf_compare_string (const void *p1, const void *p2)
680 {
681 return strcmp (*(const char **) p1, *(const char **) p2);
682 }
684 static oconfig_item_t *cf_read_dir (const char *dir,
685 const char *pattern, int depth)
686 {
687 oconfig_item_t *root = NULL;
688 DIR *dh;
689 struct dirent *de;
690 char **filenames = NULL;
691 int filenames_num = 0;
692 int status;
694 assert (depth < CF_MAX_DEPTH);
696 dh = opendir (dir);
697 if (dh == NULL)
698 {
699 char errbuf[1024];
700 ERROR ("configfile: opendir failed: %s",
701 sstrerror (errno, errbuf, sizeof (errbuf)));
702 return (NULL);
703 }
705 root = calloc (1, sizeof (*root));
706 if (root == NULL)
707 {
708 ERROR ("configfile: calloc failed.");
709 closedir (dh);
710 return (NULL);
711 }
713 while ((de = readdir (dh)) != NULL)
714 {
715 char name[1024];
716 char **tmp;
718 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
719 continue;
721 status = ssnprintf (name, sizeof (name), "%s/%s",
722 dir, de->d_name);
723 if ((status < 0) || ((size_t) status >= sizeof (name)))
724 {
725 ERROR ("configfile: Not including `%s/%s' because its"
726 " name is too long.",
727 dir, de->d_name);
728 closedir (dh);
729 for (int i = 0; i < filenames_num; ++i)
730 free (filenames[i]);
731 free (filenames);
732 free (root);
733 return (NULL);
734 }
736 ++filenames_num;
737 tmp = realloc (filenames,
738 filenames_num * sizeof (*filenames));
739 if (tmp == NULL) {
740 ERROR ("configfile: realloc failed.");
741 closedir (dh);
742 for (int i = 0; i < filenames_num - 1; ++i)
743 free (filenames[i]);
744 free (filenames);
745 free (root);
746 return (NULL);
747 }
748 filenames = tmp;
750 filenames[filenames_num - 1] = sstrdup (name);
751 }
753 if (filenames == NULL)
754 {
755 closedir (dh);
756 return (root);
757 }
759 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
760 cf_compare_string);
762 for (int i = 0; i < filenames_num; ++i)
763 {
764 oconfig_item_t *temp;
765 char *name = filenames[i];
767 temp = cf_read_generic (name, pattern, depth);
768 if (temp == NULL)
769 {
770 /* An error should already have been reported. */
771 sfree (name);
772 continue;
773 }
775 cf_ci_append_children (root, temp);
776 sfree (temp->children);
777 sfree (temp);
779 free (name);
780 }
782 closedir (dh);
783 free(filenames);
784 return (root);
785 } /* oconfig_item_t *cf_read_dir */
787 /*
788 * cf_read_generic
789 *
790 * Path is stat'ed and either cf_read_file or cf_read_dir is called
791 * accordingly.
792 *
793 * There are two versions of this function: If `wordexp' exists shell wildcards
794 * will be expanded and the function will include all matches found. If
795 * `wordexp' (or, more precisely, it's header file) is not available the
796 * simpler function is used which does not do any such expansion.
797 */
798 #if HAVE_WORDEXP_H
799 static oconfig_item_t *cf_read_generic (const char *path,
800 const char *pattern, int depth)
801 {
802 oconfig_item_t *root = NULL;
803 int status;
804 const char *path_ptr;
805 wordexp_t we;
807 if (depth >= CF_MAX_DEPTH)
808 {
809 ERROR ("configfile: Not including `%s' because the maximum "
810 "nesting depth has been reached.", path);
811 return (NULL);
812 }
814 status = wordexp (path, &we, WRDE_NOCMD);
815 if (status != 0)
816 {
817 ERROR ("configfile: wordexp (%s) failed.", path);
818 return (NULL);
819 }
821 root = calloc (1, sizeof (*root));
822 if (root == NULL)
823 {
824 ERROR ("configfile: calloc failed.");
825 return (NULL);
826 }
828 /* wordexp() might return a sorted list already. That's not
829 * documented though, so let's make sure we get what we want. */
830 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
831 cf_compare_string);
833 for (size_t i = 0; i < we.we_wordc; i++)
834 {
835 oconfig_item_t *temp;
836 struct stat statbuf;
838 path_ptr = we.we_wordv[i];
840 status = stat (path_ptr, &statbuf);
841 if (status != 0)
842 {
843 char errbuf[1024];
844 WARNING ("configfile: stat (%s) failed: %s",
845 path_ptr,
846 sstrerror (errno, errbuf, sizeof (errbuf)));
847 continue;
848 }
850 if (S_ISREG (statbuf.st_mode))
851 temp = cf_read_file (path_ptr, pattern, depth);
852 else if (S_ISDIR (statbuf.st_mode))
853 temp = cf_read_dir (path_ptr, pattern, depth);
854 else
855 {
856 WARNING ("configfile: %s is neither a file nor a "
857 "directory.", path);
858 continue;
859 }
861 if (temp == NULL) {
862 oconfig_free (root);
863 return (NULL);
864 }
866 cf_ci_append_children (root, temp);
867 sfree (temp->children);
868 sfree (temp);
869 }
871 wordfree (&we);
873 return (root);
874 } /* oconfig_item_t *cf_read_generic */
875 /* #endif HAVE_WORDEXP_H */
877 #else /* if !HAVE_WORDEXP_H */
878 static oconfig_item_t *cf_read_generic (const char *path,
879 const char *pattern, int depth)
880 {
881 struct stat statbuf;
882 int status;
884 if (depth >= CF_MAX_DEPTH)
885 {
886 ERROR ("configfile: Not including `%s' because the maximum "
887 "nesting depth has been reached.", path);
888 return (NULL);
889 }
891 status = stat (path, &statbuf);
892 if (status != 0)
893 {
894 char errbuf[1024];
895 ERROR ("configfile: stat (%s) failed: %s",
896 path,
897 sstrerror (errno, errbuf, sizeof (errbuf)));
898 return (NULL);
899 }
901 if (S_ISREG (statbuf.st_mode))
902 return (cf_read_file (path, pattern, depth));
903 else if (S_ISDIR (statbuf.st_mode))
904 return (cf_read_dir (path, pattern, depth));
906 ERROR ("configfile: %s is neither a file nor a directory.", path);
907 return (NULL);
908 } /* oconfig_item_t *cf_read_generic */
909 #endif /* !HAVE_WORDEXP_H */
911 /*
912 * Public functions
913 */
914 int global_option_set (const char *option, const char *value, _Bool from_cli)
915 {
916 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 {
925 ERROR ("configfile: Cannot set unknown global option `%s'.", option);
926 return (-1);
927 }
929 if (cf_global_options[i].from_cli && (! from_cli))
930 {
931 DEBUG ("configfile: Ignoring %s `%s' option because "
932 "it was overriden by a command-line option.",
933 option, value);
934 return (0);
935 }
937 sfree (cf_global_options[i].value);
939 if (value != NULL)
940 cf_global_options[i].value = strdup (value);
941 else
942 cf_global_options[i].value = NULL;
944 cf_global_options[i].from_cli = from_cli;
946 return (0);
947 }
949 const char *global_option_get (const char *option)
950 {
951 int i;
952 for (i = 0; i < cf_global_options_num; i++)
953 if (strcasecmp (cf_global_options[i].key, option) == 0)
954 break;
956 if (i >= cf_global_options_num)
957 {
958 ERROR ("configfile: Cannot get unknown global option `%s'.", option);
959 return (NULL);
960 }
962 return ((cf_global_options[i].value != NULL)
963 ? cf_global_options[i].value
964 : cf_global_options[i].def);
965 } /* char *global_option_get */
967 long global_option_get_long (const char *option, long default_value)
968 {
969 const char *str;
970 long value;
972 str = global_option_get (option);
973 if (NULL == str)
974 return (default_value);
976 errno = 0;
977 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
978 if (errno != 0)
979 return (default_value);
981 return (value);
982 } /* char *global_option_get_long */
984 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
985 {
986 char const *optstr;
987 char *endptr = NULL;
988 double v;
990 optstr = global_option_get (name);
991 if (optstr == NULL)
992 return (def);
994 errno = 0;
995 v = strtod (optstr, &endptr);
996 if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
997 return (def);
998 else if (v <= 0.0)
999 return (def);
1001 return (DOUBLE_TO_CDTIME_T (v));
1002 } /* }}} cdtime_t global_option_get_time */
1004 cdtime_t cf_get_default_interval (void)
1005 {
1006 return (global_option_get_time ("Interval",
1007 DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1008 }
1010 void cf_unregister (const char *type)
1011 {
1012 for (cf_callback_t *prev = NULL, *this = first_callback;
1013 this != NULL;
1014 prev = this, this = this->next)
1015 if (strcasecmp (this->type, type) == 0)
1016 {
1017 if (prev == NULL)
1018 first_callback = this->next;
1019 else
1020 prev->next = this->next;
1022 free (this);
1023 break;
1024 }
1025 } /* void cf_unregister */
1027 void cf_unregister_complex (const char *type)
1028 {
1029 for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head;
1030 this != NULL;
1031 prev = this, this = this->next)
1032 if (strcasecmp (this->type, type) == 0)
1033 {
1034 if (prev == NULL)
1035 complex_callback_head = this->next;
1036 else
1037 prev->next = this->next;
1039 sfree (this->type);
1040 sfree (this);
1041 break;
1042 }
1043 } /* void cf_unregister */
1045 void cf_register (const char *type,
1046 int (*callback) (const char *, const char *),
1047 const char **keys, int keys_num)
1048 {
1049 cf_callback_t *cf_cb;
1051 /* Remove this module from the list, if it already exists */
1052 cf_unregister (type);
1054 /* This pointer will be free'd in `cf_unregister' */
1055 if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL)
1056 return;
1058 cf_cb->type = type;
1059 cf_cb->callback = callback;
1060 cf_cb->keys = keys;
1061 cf_cb->keys_num = keys_num;
1062 cf_cb->ctx = plugin_get_ctx ();
1064 cf_cb->next = first_callback;
1065 first_callback = cf_cb;
1066 } /* void cf_register */
1068 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1069 {
1070 cf_complex_callback_t *new;
1072 new = malloc (sizeof (*new));
1073 if (new == NULL)
1074 return (-1);
1076 new->type = strdup (type);
1077 if (new->type == NULL)
1078 {
1079 sfree (new);
1080 return (-1);
1081 }
1083 new->callback = callback;
1084 new->next = NULL;
1086 new->ctx = plugin_get_ctx ();
1088 if (complex_callback_head == NULL)
1089 {
1090 complex_callback_head = new;
1091 }
1092 else
1093 {
1094 cf_complex_callback_t *last = complex_callback_head;
1095 while (last->next != NULL)
1096 last = last->next;
1097 last->next = new;
1098 }
1100 return (0);
1101 } /* int cf_register_complex */
1103 int cf_read (const char *filename)
1104 {
1105 oconfig_item_t *conf;
1106 int ret = 0;
1108 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1109 if (conf == NULL)
1110 {
1111 ERROR ("Unable to read config file %s.", filename);
1112 return (-1);
1113 }
1114 else if (conf->children_num == 0)
1115 {
1116 ERROR ("Configuration file %s is empty.", filename);
1117 oconfig_free (conf);
1118 return (-1);
1119 }
1121 for (int i = 0; i < conf->children_num; i++)
1122 {
1123 if (conf->children[i].children == NULL)
1124 {
1125 if (dispatch_value (conf->children + i) != 0)
1126 ret = -1;
1127 }
1128 else
1129 {
1130 if (dispatch_block (conf->children + i) != 0)
1131 ret = -1;
1132 }
1133 }
1135 oconfig_free (conf);
1137 /* Read the default types.db if no `TypesDB' option was given. */
1138 if (cf_default_typesdb)
1139 {
1140 if (read_types_list (PKGDATADIR"/types.db") != 0)
1141 ret = -1;
1142 }
1144 return ret;
1145 } /* int cf_read */
1147 /* Assures the config option is a string, duplicates it and returns the copy in
1148 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1149 * success. */
1150 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1151 {
1152 char *string;
1154 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1155 {
1156 ERROR ("cf_util_get_string: The %s option requires "
1157 "exactly one string argument.", ci->key);
1158 return (-1);
1159 }
1161 string = strdup (ci->values[0].value.string);
1162 if (string == NULL)
1163 return (-1);
1165 if (*ret_string != NULL)
1166 sfree (*ret_string);
1167 *ret_string = string;
1169 return (0);
1170 } /* }}} int cf_util_get_string */
1172 /* Assures the config option is a string and copies it to the provided buffer.
1173 * Assures null-termination. */
1174 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1175 size_t buffer_size)
1176 {
1177 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1178 return (EINVAL);
1180 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1181 {
1182 ERROR ("cf_util_get_string_buffer: The %s option requires "
1183 "exactly one string argument.", ci->key);
1184 return (-1);
1185 }
1187 strncpy (buffer, ci->values[0].value.string, buffer_size);
1188 buffer[buffer_size - 1] = 0;
1190 return (0);
1191 } /* }}} int cf_util_get_string_buffer */
1193 /* Assures the config option is a number and returns it as an int. */
1194 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1195 {
1196 if ((ci == NULL) || (ret_value == NULL))
1197 return (EINVAL);
1199 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1200 {
1201 ERROR ("cf_util_get_int: The %s option requires "
1202 "exactly one numeric argument.", ci->key);
1203 return (-1);
1204 }
1206 *ret_value = (int) ci->values[0].value.number;
1208 return (0);
1209 } /* }}} int cf_util_get_int */
1211 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1212 {
1213 if ((ci == NULL) || (ret_value == NULL))
1214 return (EINVAL);
1216 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1217 {
1218 ERROR ("cf_util_get_double: The %s option requires "
1219 "exactly one numeric argument.", ci->key);
1220 return (-1);
1221 }
1223 *ret_value = ci->values[0].value.number;
1225 return (0);
1226 } /* }}} int cf_util_get_double */
1228 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1229 {
1230 if ((ci == NULL) || (ret_bool == NULL))
1231 return (EINVAL);
1233 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1234 {
1235 ERROR ("cf_util_get_boolean: The %s option requires "
1236 "exactly one boolean argument.", ci->key);
1237 return (-1);
1238 }
1240 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1242 return (0);
1243 } /* }}} int cf_util_get_boolean */
1245 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1246 unsigned int *ret_value, unsigned int flag)
1247 {
1248 int status;
1249 _Bool b;
1251 if (ret_value == NULL)
1252 return (EINVAL);
1254 b = 0;
1255 status = cf_util_get_boolean (ci, &b);
1256 if (status != 0)
1257 return (status);
1259 if (b)
1260 {
1261 *ret_value |= flag;
1262 }
1263 else
1264 {
1265 *ret_value &= ~flag;
1266 }
1268 return (0);
1269 } /* }}} int cf_util_get_flag */
1271 /* Assures that the config option is a string or a number if the correct range
1272 * of 1-65535. The string is then converted to a port number using
1273 * `service_name_to_port_number' and returned.
1274 * Returns the port number in the range [1-65535] or less than zero upon
1275 * failure. */
1276 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1277 {
1278 int tmp;
1280 if ((ci->values_num != 1)
1281 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1282 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1283 {
1284 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1285 "exactly one string argument.", ci->key);
1286 return (-1);
1287 }
1289 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1290 return (service_name_to_port_number (ci->values[0].value.string));
1292 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1293 tmp = (int) (ci->values[0].value.number + 0.5);
1294 if ((tmp < 1) || (tmp > 65535))
1295 {
1296 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1297 "a service name or a port number. The number "
1298 "you specified, %i, is not in the valid "
1299 "range of 1-65535.",
1300 ci->key, tmp);
1301 return (-1);
1302 }
1304 return (tmp);
1305 } /* }}} int cf_util_get_port_number */
1307 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1308 {
1309 int port;
1310 char *service;
1311 int status;
1313 if (ci->values_num != 1)
1314 {
1315 ERROR ("cf_util_get_service: The %s option requires exactly "
1316 "one argument.", ci->key);
1317 return (-1);
1318 }
1320 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1321 return (cf_util_get_string (ci, ret_string));
1322 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1323 {
1324 ERROR ("cf_util_get_service: The %s option requires "
1325 "exactly one string or numeric argument.",
1326 ci->key);
1327 }
1329 port = 0;
1330 status = cf_util_get_int (ci, &port);
1331 if (status != 0)
1332 return (status);
1333 else if ((port < 1) || (port > 65535))
1334 {
1335 ERROR ("cf_util_get_service: The port number given "
1336 "for the %s option is out of "
1337 "range (%i).", ci->key, port);
1338 return (-1);
1339 }
1341 service = malloc (6);
1342 if (service == NULL)
1343 {
1344 ERROR ("cf_util_get_service: Out of memory.");
1345 return (-1);
1346 }
1347 ssnprintf (service, 6, "%i", port);
1349 sfree (*ret_string);
1350 *ret_string = service;
1352 return (0);
1353 } /* }}} int cf_util_get_service */
1355 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1356 {
1357 if ((ci == NULL) || (ret_value == NULL))
1358 return (EINVAL);
1360 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1361 {
1362 ERROR ("cf_util_get_cdtime: The %s option requires "
1363 "exactly one numeric argument.", ci->key);
1364 return (-1);
1365 }
1367 if (ci->values[0].value.number < 0.0)
1368 {
1369 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1370 "option must not be negative.", ci->key);
1371 return (-1);
1372 }
1374 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1376 return (0);
1377 } /* }}} int cf_util_get_cdtime */