d6c224fd74966e23f6758ee79be11b75a67e5960
1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2011 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
22 **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
48 /*
49 * Private types
50 */
51 typedef struct cf_callback
52 {
53 const char *type;
54 int (*callback) (const char *, const char *);
55 const char **keys;
56 int keys_num;
57 plugin_ctx_t ctx;
58 struct cf_callback *next;
59 } cf_callback_t;
61 typedef struct cf_complex_callback_s
62 {
63 char *type;
64 int (*callback) (oconfig_item_t *);
65 plugin_ctx_t ctx;
66 struct cf_complex_callback_s *next;
67 } cf_complex_callback_t;
69 typedef struct cf_value_map_s
70 {
71 char *key;
72 int (*func) (const oconfig_item_t *);
73 } cf_value_map_t;
75 typedef struct cf_global_option_s
76 {
77 char *key;
78 char *value;
79 char *def;
80 } cf_global_option_t;
82 /*
83 * Prototypes of callback functions
84 */
85 static int dispatch_value_typesdb (const oconfig_item_t *ci);
86 static int dispatch_value_plugindir (const oconfig_item_t *ci);
87 static int dispatch_loadplugin (const oconfig_item_t *ci);
89 /*
90 * Private variables
91 */
92 static cf_callback_t *first_callback = NULL;
93 static cf_complex_callback_t *complex_callback_head = NULL;
95 static cf_value_map_t cf_value_map[] =
96 {
97 {"TypesDB", dispatch_value_typesdb},
98 {"PluginDir", dispatch_value_plugindir},
99 {"LoadPlugin", dispatch_loadplugin}
100 };
101 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
103 static cf_global_option_t cf_global_options[] =
104 {
105 {"BaseDir", NULL, PKGLOCALSTATEDIR},
106 {"PIDFile", NULL, PIDFILE},
107 {"Hostname", NULL, NULL},
108 {"FQDNLookup", NULL, "true"},
109 {"Interval", NULL, NULL},
110 {"ReadThreads", NULL, "5"},
111 {"WriteThreads", NULL, "5"},
112 {"Timeout", NULL, "2"},
113 {"AutoLoadPlugin", NULL, "false"},
114 {"PreCacheChain", NULL, "PreCache"},
115 {"PostCacheChain", NULL, "PostCache"}
116 };
117 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
119 static int cf_default_typesdb = 1;
121 /*
122 * Functions to handle register/unregister, search, and other plugin related
123 * stuff
124 */
125 static cf_callback_t *cf_search (const char *type)
126 {
127 cf_callback_t *cf_cb;
129 if (type == NULL)
130 return (NULL);
132 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
133 if (strcasecmp (cf_cb->type, type) == 0)
134 break;
136 return (cf_cb);
137 }
139 static int cf_dispatch (const char *type, const char *orig_key,
140 const char *orig_value)
141 {
142 cf_callback_t *cf_cb;
143 plugin_ctx_t old_ctx;
144 char *key;
145 char *value;
146 int ret;
147 int i;
149 DEBUG ("type = %s, key = %s, value = %s",
150 ESCAPE_NULL(type),
151 ESCAPE_NULL(orig_key),
152 ESCAPE_NULL(orig_value));
154 if ((cf_cb = cf_search (type)) == NULL)
155 {
156 WARNING ("Found a configuration for the `%s' plugin, but "
157 "the plugin isn't loaded or didn't register "
158 "a configuration callback.", type);
159 return (-1);
160 }
162 if ((key = strdup (orig_key)) == NULL)
163 return (1);
164 if ((value = strdup (orig_value)) == NULL)
165 {
166 free (key);
167 return (2);
168 }
170 ret = -1;
172 old_ctx = plugin_set_ctx (cf_cb->ctx);
174 for (i = 0; i < cf_cb->keys_num; i++)
175 {
176 if ((cf_cb->keys[i] != NULL)
177 && (strcasecmp (cf_cb->keys[i], key) == 0))
178 {
179 ret = (*cf_cb->callback) (key, value);
180 break;
181 }
182 }
184 plugin_set_ctx (old_ctx);
186 if (i >= cf_cb->keys_num)
187 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
189 free (key);
190 free (value);
192 DEBUG ("cf_dispatch: return (%i)", ret);
194 return (ret);
195 } /* int cf_dispatch */
197 static int dispatch_global_option (const oconfig_item_t *ci)
198 {
199 if (ci->values_num != 1)
200 return (-1);
201 if (ci->values[0].type == OCONFIG_TYPE_STRING)
202 return (global_option_set (ci->key, ci->values[0].value.string));
203 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
204 {
205 char tmp[128];
206 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
207 return (global_option_set (ci->key, tmp));
208 }
209 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
210 {
211 if (ci->values[0].value.boolean)
212 return (global_option_set (ci->key, "true"));
213 else
214 return (global_option_set (ci->key, "false"));
215 }
217 return (-1);
218 } /* int dispatch_global_option */
220 static int dispatch_value_typesdb (const oconfig_item_t *ci)
221 {
222 int i = 0;
224 assert (strcasecmp (ci->key, "TypesDB") == 0);
226 cf_default_typesdb = 0;
228 if (ci->values_num < 1) {
229 ERROR ("configfile: `TypesDB' needs at least one argument.");
230 return (-1);
231 }
233 for (i = 0; i < ci->values_num; ++i)
234 {
235 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
236 WARNING ("configfile: TypesDB: Skipping %i. argument which "
237 "is not a string.", i + 1);
238 continue;
239 }
241 read_types_list (ci->values[i].value.string);
242 }
243 return (0);
244 } /* int dispatch_value_typesdb */
246 static int dispatch_value_plugindir (const oconfig_item_t *ci)
247 {
248 assert (strcasecmp (ci->key, "PluginDir") == 0);
250 if (ci->values_num != 1)
251 return (-1);
252 if (ci->values[0].type != OCONFIG_TYPE_STRING)
253 return (-1);
255 plugin_set_dir (ci->values[0].value.string);
256 return (0);
257 }
259 static int dispatch_loadplugin (const oconfig_item_t *ci)
260 {
261 int i;
262 const char *name;
263 unsigned int flags = 0;
264 plugin_ctx_t ctx;
265 plugin_ctx_t old_ctx;
266 int ret_val;
268 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
270 if (ci->values_num != 1)
271 return (-1);
272 if (ci->values[0].type != OCONFIG_TYPE_STRING)
273 return (-1);
275 name = ci->values[0].value.string;
277 /* default to the global interval set before loading this plugin */
278 memset (&ctx, 0, sizeof (ctx));
279 ctx.interval = cf_get_default_interval ();
281 for (i = 0; i < ci->children_num; ++i) {
282 if (strcasecmp("Globals", ci->children[i].key) == 0)
283 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
284 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
285 double interval = 0.0;
287 if (cf_util_get_double (ci->children + i, &interval) != 0) {
288 /* cf_util_get_double will log an error */
289 continue;
290 }
292 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
293 }
294 else {
295 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
296 "for plugin \"%s\"",
297 ci->children[i].key, ci->values[0].value.string);
298 }
299 }
301 old_ctx = plugin_set_ctx (ctx);
302 ret_val = plugin_load (name, (uint32_t) flags);
303 /* reset to the "global" context */
304 plugin_set_ctx (old_ctx);
306 return (ret_val);
307 } /* int dispatch_value_loadplugin */
309 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
310 {
311 char buffer[4096];
312 char *buffer_ptr;
313 int buffer_free;
314 int i;
316 buffer_ptr = buffer;
317 buffer_free = sizeof (buffer);
319 for (i = 0; i < ci->values_num; i++)
320 {
321 int status = -1;
323 if (ci->values[i].type == OCONFIG_TYPE_STRING)
324 status = ssnprintf (buffer_ptr, buffer_free, " %s",
325 ci->values[i].value.string);
326 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
327 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
328 ci->values[i].value.number);
329 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
330 status = ssnprintf (buffer_ptr, buffer_free, " %s",
331 ci->values[i].value.boolean
332 ? "true" : "false");
334 if ((status < 0) || (status >= buffer_free))
335 return (-1);
336 buffer_free -= status;
337 buffer_ptr += status;
338 }
339 /* skip the initial space */
340 buffer_ptr = buffer + 1;
342 return (cf_dispatch (plugin, ci->key, buffer_ptr));
343 } /* int dispatch_value_plugin */
345 static int dispatch_value (const oconfig_item_t *ci)
346 {
347 int ret = -2;
348 int i;
350 for (i = 0; i < cf_value_map_num; i++)
351 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
352 {
353 ret = cf_value_map[i].func (ci);
354 break;
355 }
357 for (i = 0; i < cf_global_options_num; i++)
358 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
359 {
360 ret = dispatch_global_option (ci);
361 break;
362 }
364 return (ret);
365 } /* int dispatch_value */
367 static int dispatch_block_plugin (oconfig_item_t *ci)
368 {
369 int i;
370 char *name;
372 cf_complex_callback_t *cb;
374 if (strcasecmp (ci->key, "Plugin") != 0)
375 return (-1);
376 if (ci->values_num < 1)
377 return (-1);
378 if (ci->values[0].type != OCONFIG_TYPE_STRING)
379 return (-1);
381 name = ci->values[0].value.string;
383 if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
384 {
385 int status;
387 status = plugin_load (name, /* flags = */ 0);
388 if (status != 0)
389 {
390 ERROR ("Automatically loading plugin \"%s\" failed "
391 "with status %i.", name, status);
392 return (status);
393 }
394 }
396 /* Check for a complex callback first */
397 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
398 {
399 if (strcasecmp (name, cb->type) == 0)
400 {
401 plugin_ctx_t old_ctx;
402 int ret_val;
404 old_ctx = plugin_set_ctx (cb->ctx);
405 ret_val = (cb->callback (ci));
406 plugin_set_ctx (old_ctx);
407 return (ret_val);
408 }
409 }
411 /* Hm, no complex plugin found. Dispatch the values one by one */
412 for (i = 0; i < ci->children_num; i++)
413 {
414 if (ci->children[i].children == NULL)
415 dispatch_value_plugin (name, ci->children + i);
416 else
417 {
418 WARNING ("There is a `%s' block within the "
419 "configuration for the %s plugin. "
420 "The plugin either only expects "
421 "\"simple\" configuration statements "
422 "or wasn't loaded using `LoadPlugin'."
423 " Please check your configuration.",
424 ci->children[i].key, name);
425 }
426 }
428 return (0);
429 }
432 static int dispatch_block (oconfig_item_t *ci)
433 {
434 if (strcasecmp (ci->key, "LoadPlugin") == 0)
435 return (dispatch_loadplugin (ci));
436 else if (strcasecmp (ci->key, "Plugin") == 0)
437 return (dispatch_block_plugin (ci));
438 else if (strcasecmp (ci->key, "Chain") == 0)
439 return (fc_configure (ci));
441 return (0);
442 }
444 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
445 int offset)
446 {
447 oconfig_item_t *temp;
448 int i;
450 assert (offset >= 0);
451 assert (dst->children_num > offset);
453 /* Free the memory used by the replaced child. Usually that's the
454 * `Include "blah"' statement. */
455 temp = dst->children + offset;
456 for (i = 0; i < temp->values_num; i++)
457 {
458 if (temp->values[i].type == OCONFIG_TYPE_STRING)
459 {
460 sfree (temp->values[i].value.string);
461 }
462 }
463 sfree (temp->values);
464 temp = NULL;
466 /* If (src->children_num == 0) the array size is decreased. If offset
467 * is _not_ the last element, (offset < (dst->children_num - 1)), then
468 * we need to move the trailing elements before resizing the array. */
469 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
470 {
471 int nmemb = dst->children_num - (offset + 1);
472 memmove (dst->children + offset, dst->children + offset + 1,
473 sizeof (oconfig_item_t) * nmemb);
474 }
476 /* Resize the memory containing the children to be big enough to hold
477 * all children. */
478 temp = (oconfig_item_t *) realloc (dst->children,
479 sizeof (oconfig_item_t)
480 * (dst->children_num + src->children_num - 1));
481 if (temp == NULL)
482 {
483 ERROR ("configfile: realloc failed.");
484 return (-1);
485 }
486 dst->children = temp;
488 /* If there are children behind the include statement, and they have
489 * not yet been moved because (src->children_num == 0), then move them
490 * to the end of the list, so that the new children have room before
491 * them. */
492 if ((src->children_num > 0)
493 && ((dst->children_num - (offset + 1)) > 0))
494 {
495 int nmemb = dst->children_num - (offset + 1);
496 int old_offset = offset + 1;
497 int new_offset = offset + src->children_num;
499 memmove (dst->children + new_offset,
500 dst->children + old_offset,
501 sizeof (oconfig_item_t) * nmemb);
502 }
504 /* Last but not least: If there are new children, copy them to the
505 * memory reserved for them. */
506 if (src->children_num > 0)
507 {
508 memcpy (dst->children + offset,
509 src->children,
510 sizeof (oconfig_item_t) * src->children_num);
511 }
513 /* Update the number of children. */
514 dst->children_num += (src->children_num - 1);
516 return (0);
517 } /* int cf_ci_replace_child */
519 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
520 {
521 oconfig_item_t *temp;
523 if ((src == NULL) || (src->children_num == 0))
524 return (0);
526 temp = (oconfig_item_t *) realloc (dst->children,
527 sizeof (oconfig_item_t)
528 * (dst->children_num + src->children_num));
529 if (temp == NULL)
530 {
531 ERROR ("configfile: realloc failed.");
532 return (-1);
533 }
534 dst->children = temp;
536 memcpy (dst->children + dst->children_num,
537 src->children,
538 sizeof (oconfig_item_t)
539 * src->children_num);
540 dst->children_num += src->children_num;
542 return (0);
543 } /* int cf_ci_append_children */
545 #define CF_MAX_DEPTH 8
546 static oconfig_item_t *cf_read_generic (const char *path,
547 const char *pattern, int depth);
549 static int cf_include_all (oconfig_item_t *root, int depth)
550 {
551 int i;
553 for (i = 0; i < root->children_num; i++)
554 {
555 oconfig_item_t *new;
556 oconfig_item_t *old;
558 char *pattern = NULL;
560 int j;
562 if (strcasecmp (root->children[i].key, "Include") != 0)
563 continue;
565 old = root->children + i;
567 if ((old->values_num != 1)
568 || (old->values[0].type != OCONFIG_TYPE_STRING))
569 {
570 ERROR ("configfile: `Include' needs exactly one string argument.");
571 continue;
572 }
574 for (j = 0; j < old->children_num; ++j)
575 {
576 oconfig_item_t *child = old->children + j;
578 if (strcasecmp (child->key, "Filter") == 0)
579 cf_util_get_string (child, &pattern);
580 else
581 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
582 child->key);
583 }
585 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
586 sfree (pattern);
588 if (new == NULL)
589 return (-1);
591 /* Now replace the i'th child in `root' with `new'. */
592 cf_ci_replace_child (root, new, i);
594 /* ... and go back to the new i'th child. */
595 --i;
597 sfree (new->values);
598 sfree (new);
599 } /* for (i = 0; i < root->children_num; i++) */
601 return (0);
602 } /* int cf_include_all */
604 static oconfig_item_t *cf_read_file (const char *file,
605 const char *pattern, int depth)
606 {
607 oconfig_item_t *root;
608 int status;
610 assert (depth < CF_MAX_DEPTH);
612 if (pattern != NULL) {
613 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
614 char *tmp = sstrdup (file);
615 char *filename = basename (tmp);
617 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
618 DEBUG ("configfile: Not including `%s' because it "
619 "does not match pattern `%s'.",
620 filename, pattern);
621 free (tmp);
622 return (NULL);
623 }
625 free (tmp);
626 #else
627 ERROR ("configfile: Cannot apply pattern filter '%s' "
628 "to file '%s': functions basename() and / or "
629 "fnmatch() not available.", pattern, file);
630 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
631 }
633 root = oconfig_parse_file (file);
634 if (root == NULL)
635 {
636 ERROR ("configfile: Cannot read file `%s'.", file);
637 return (NULL);
638 }
640 status = cf_include_all (root, depth);
641 if (status != 0)
642 {
643 oconfig_free (root);
644 return (NULL);
645 }
647 return (root);
648 } /* oconfig_item_t *cf_read_file */
650 static int cf_compare_string (const void *p1, const void *p2)
651 {
652 return strcmp (*(const char **) p1, *(const char **) p2);
653 }
655 static oconfig_item_t *cf_read_dir (const char *dir,
656 const char *pattern, int depth)
657 {
658 oconfig_item_t *root = NULL;
659 DIR *dh;
660 struct dirent *de;
661 char **filenames = NULL;
662 int filenames_num = 0;
663 int status;
664 int i;
666 assert (depth < CF_MAX_DEPTH);
668 dh = opendir (dir);
669 if (dh == NULL)
670 {
671 char errbuf[1024];
672 ERROR ("configfile: opendir failed: %s",
673 sstrerror (errno, errbuf, sizeof (errbuf)));
674 return (NULL);
675 }
677 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
678 if (root == NULL)
679 {
680 ERROR ("configfile: malloc failed.");
681 return (NULL);
682 }
683 memset (root, 0, sizeof (oconfig_item_t));
685 while ((de = readdir (dh)) != NULL)
686 {
687 char name[1024];
688 char **tmp;
690 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
691 continue;
693 status = ssnprintf (name, sizeof (name), "%s/%s",
694 dir, de->d_name);
695 if ((status < 0) || ((size_t) status >= sizeof (name)))
696 {
697 ERROR ("configfile: Not including `%s/%s' because its"
698 " name is too long.",
699 dir, de->d_name);
700 for (i = 0; i < filenames_num; ++i)
701 free (filenames[i]);
702 free (filenames);
703 free (root);
704 return (NULL);
705 }
707 ++filenames_num;
708 tmp = (char **) realloc (filenames,
709 filenames_num * sizeof (*filenames));
710 if (tmp == NULL) {
711 ERROR ("configfile: realloc failed.");
712 for (i = 0; i < filenames_num - 1; ++i)
713 free (filenames[i]);
714 free (filenames);
715 free (root);
716 return (NULL);
717 }
718 filenames = tmp;
720 filenames[filenames_num - 1] = sstrdup (name);
721 }
723 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
724 cf_compare_string);
726 for (i = 0; i < filenames_num; ++i)
727 {
728 oconfig_item_t *temp;
729 char *name = filenames[i];
731 temp = cf_read_generic (name, pattern, depth);
732 if (temp == NULL)
733 {
734 /* An error should already have been reported. */
735 sfree (name);
736 continue;
737 }
739 cf_ci_append_children (root, temp);
740 sfree (temp->children);
741 sfree (temp);
743 free (name);
744 }
746 free(filenames);
747 return (root);
748 } /* oconfig_item_t *cf_read_dir */
750 /*
751 * cf_read_generic
752 *
753 * Path is stat'ed and either cf_read_file or cf_read_dir is called
754 * accordingly.
755 *
756 * There are two versions of this function: If `wordexp' exists shell wildcards
757 * will be expanded and the function will include all matches found. If
758 * `wordexp' (or, more precisely, it's header file) is not available the
759 * simpler function is used which does not do any such expansion.
760 */
761 #if HAVE_WORDEXP_H
762 static oconfig_item_t *cf_read_generic (const char *path,
763 const char *pattern, int depth)
764 {
765 oconfig_item_t *root = NULL;
766 int status;
767 const char *path_ptr;
768 wordexp_t we;
769 size_t i;
771 if (depth >= CF_MAX_DEPTH)
772 {
773 ERROR ("configfile: Not including `%s' because the maximum "
774 "nesting depth has been reached.", path);
775 return (NULL);
776 }
778 status = wordexp (path, &we, WRDE_NOCMD);
779 if (status != 0)
780 {
781 ERROR ("configfile: wordexp (%s) failed.", path);
782 return (NULL);
783 }
785 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
786 if (root == NULL)
787 {
788 ERROR ("configfile: malloc failed.");
789 return (NULL);
790 }
791 memset (root, '\0', sizeof (oconfig_item_t));
793 /* wordexp() might return a sorted list already. That's not
794 * documented though, so let's make sure we get what we want. */
795 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
796 cf_compare_string);
798 for (i = 0; i < we.we_wordc; i++)
799 {
800 oconfig_item_t *temp;
801 struct stat statbuf;
803 path_ptr = we.we_wordv[i];
805 status = stat (path_ptr, &statbuf);
806 if (status != 0)
807 {
808 char errbuf[1024];
809 WARNING ("configfile: stat (%s) failed: %s",
810 path_ptr,
811 sstrerror (errno, errbuf, sizeof (errbuf)));
812 continue;
813 }
815 if (S_ISREG (statbuf.st_mode))
816 temp = cf_read_file (path_ptr, pattern, depth);
817 else if (S_ISDIR (statbuf.st_mode))
818 temp = cf_read_dir (path_ptr, pattern, depth);
819 else
820 {
821 WARNING ("configfile: %s is neither a file nor a "
822 "directory.", path);
823 continue;
824 }
826 if (temp == NULL) {
827 oconfig_free (root);
828 return (NULL);
829 }
831 cf_ci_append_children (root, temp);
832 sfree (temp->children);
833 sfree (temp);
834 }
836 wordfree (&we);
838 return (root);
839 } /* oconfig_item_t *cf_read_generic */
840 /* #endif HAVE_WORDEXP_H */
842 #else /* if !HAVE_WORDEXP_H */
843 static oconfig_item_t *cf_read_generic (const char *path,
844 const char *pattern, int depth)
845 {
846 struct stat statbuf;
847 int status;
849 if (depth >= CF_MAX_DEPTH)
850 {
851 ERROR ("configfile: Not including `%s' because the maximum "
852 "nesting depth has been reached.", path);
853 return (NULL);
854 }
856 status = stat (path, &statbuf);
857 if (status != 0)
858 {
859 char errbuf[1024];
860 ERROR ("configfile: stat (%s) failed: %s",
861 path,
862 sstrerror (errno, errbuf, sizeof (errbuf)));
863 return (NULL);
864 }
866 if (S_ISREG (statbuf.st_mode))
867 return (cf_read_file (path, pattern, depth));
868 else if (S_ISDIR (statbuf.st_mode))
869 return (cf_read_dir (path, pattern, depth));
871 ERROR ("configfile: %s is neither a file nor a directory.", path);
872 return (NULL);
873 } /* oconfig_item_t *cf_read_generic */
874 #endif /* !HAVE_WORDEXP_H */
876 /*
877 * Public functions
878 */
879 int global_option_set (const char *option, const char *value)
880 {
881 int i;
883 DEBUG ("option = %s; value = %s;", option, value);
885 for (i = 0; i < cf_global_options_num; i++)
886 if (strcasecmp (cf_global_options[i].key, option) == 0)
887 break;
889 if (i >= cf_global_options_num)
890 return (-1);
892 sfree (cf_global_options[i].value);
894 if (value != NULL)
895 cf_global_options[i].value = strdup (value);
896 else
897 cf_global_options[i].value = NULL;
899 return (0);
900 }
902 const char *global_option_get (const char *option)
903 {
904 int i;
906 for (i = 0; i < cf_global_options_num; i++)
907 if (strcasecmp (cf_global_options[i].key, option) == 0)
908 break;
910 if (i >= cf_global_options_num)
911 return (NULL);
913 return ((cf_global_options[i].value != NULL)
914 ? cf_global_options[i].value
915 : cf_global_options[i].def);
916 } /* char *global_option_get */
918 cdtime_t cf_get_default_interval (void)
919 {
920 char const *str = global_option_get ("Interval");
921 double interval_double = COLLECTD_DEFAULT_INTERVAL;
923 if (str != NULL)
924 {
925 char *endptr = NULL;
926 double tmp = strtod (str, &endptr);
928 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
929 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
930 "as number.", str);
931 else if (tmp <= 0.0)
932 ERROR ("cf_get_default_interval: Interval must be a positive number. "
933 "The current number is %g.", tmp);
934 else
935 interval_double = tmp;
936 }
938 return (DOUBLE_TO_CDTIME_T (interval_double));
939 } /* }}} cdtime_t cf_get_default_interval */
941 void cf_unregister (const char *type)
942 {
943 cf_callback_t *this, *prev;
945 for (prev = NULL, this = first_callback;
946 this != NULL;
947 prev = this, this = this->next)
948 if (strcasecmp (this->type, type) == 0)
949 {
950 if (prev == NULL)
951 first_callback = this->next;
952 else
953 prev->next = this->next;
955 free (this);
956 break;
957 }
958 } /* void cf_unregister */
960 void cf_unregister_complex (const char *type)
961 {
962 cf_complex_callback_t *this, *prev;
964 for (prev = NULL, this = complex_callback_head;
965 this != NULL;
966 prev = this, this = this->next)
967 if (strcasecmp (this->type, type) == 0)
968 {
969 if (prev == NULL)
970 complex_callback_head = this->next;
971 else
972 prev->next = this->next;
974 sfree (this->type);
975 sfree (this);
976 break;
977 }
978 } /* void cf_unregister */
980 void cf_register (const char *type,
981 int (*callback) (const char *, const char *),
982 const char **keys, int keys_num)
983 {
984 cf_callback_t *cf_cb;
986 /* Remove this module from the list, if it already exists */
987 cf_unregister (type);
989 /* This pointer will be free'd in `cf_unregister' */
990 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
991 return;
993 cf_cb->type = type;
994 cf_cb->callback = callback;
995 cf_cb->keys = keys;
996 cf_cb->keys_num = keys_num;
997 cf_cb->ctx = plugin_get_ctx ();
999 cf_cb->next = first_callback;
1000 first_callback = cf_cb;
1001 } /* void cf_register */
1003 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1004 {
1005 cf_complex_callback_t *new;
1007 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1008 if (new == NULL)
1009 return (-1);
1011 new->type = strdup (type);
1012 if (new->type == NULL)
1013 {
1014 sfree (new);
1015 return (-1);
1016 }
1018 new->callback = callback;
1019 new->next = NULL;
1021 new->ctx = plugin_get_ctx ();
1023 if (complex_callback_head == NULL)
1024 {
1025 complex_callback_head = new;
1026 }
1027 else
1028 {
1029 cf_complex_callback_t *last = complex_callback_head;
1030 while (last->next != NULL)
1031 last = last->next;
1032 last->next = new;
1033 }
1035 return (0);
1036 } /* int cf_register_complex */
1038 int cf_read (char *filename)
1039 {
1040 oconfig_item_t *conf;
1041 int i;
1043 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1044 if (conf == NULL)
1045 {
1046 ERROR ("Unable to read config file %s.", filename);
1047 return (-1);
1048 }
1049 else if (conf->children_num == 0)
1050 {
1051 ERROR ("Configuration file %s is empty.", filename);
1052 oconfig_free (conf);
1053 return (-1);
1054 }
1056 for (i = 0; i < conf->children_num; i++)
1057 {
1058 if (conf->children[i].children == NULL)
1059 dispatch_value (conf->children + i);
1060 else
1061 dispatch_block (conf->children + i);
1062 }
1064 oconfig_free (conf);
1066 /* Read the default types.db if no `TypesDB' option was given. */
1067 if (cf_default_typesdb)
1068 read_types_list (PKGDATADIR"/types.db");
1070 return (0);
1071 } /* int cf_read */
1073 /* Assures the config option is a string, duplicates it and returns the copy in
1074 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1075 * success. */
1076 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1077 {
1078 char *string;
1080 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1081 {
1082 ERROR ("cf_util_get_string: The %s option requires "
1083 "exactly one string argument.", ci->key);
1084 return (-1);
1085 }
1087 string = strdup (ci->values[0].value.string);
1088 if (string == NULL)
1089 return (-1);
1091 if (*ret_string != NULL)
1092 sfree (*ret_string);
1093 *ret_string = string;
1095 return (0);
1096 } /* }}} int cf_util_get_string */
1098 /* Assures the config option is a string and copies it to the provided buffer.
1099 * Assures null-termination. */
1100 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1101 size_t buffer_size)
1102 {
1103 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1104 return (EINVAL);
1106 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1107 {
1108 ERROR ("cf_util_get_string_buffer: The %s option requires "
1109 "exactly one string argument.", ci->key);
1110 return (-1);
1111 }
1113 strncpy (buffer, ci->values[0].value.string, buffer_size);
1114 buffer[buffer_size - 1] = 0;
1116 return (0);
1117 } /* }}} int cf_util_get_string_buffer */
1119 /* Assures the config option is a number and returns it as an int. */
1120 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1121 {
1122 if ((ci == NULL) || (ret_value == NULL))
1123 return (EINVAL);
1125 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1126 {
1127 ERROR ("cf_util_get_int: The %s option requires "
1128 "exactly one numeric argument.", ci->key);
1129 return (-1);
1130 }
1132 *ret_value = (int) ci->values[0].value.number;
1134 return (0);
1135 } /* }}} int cf_util_get_int */
1137 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1138 {
1139 if ((ci == NULL) || (ret_value == NULL))
1140 return (EINVAL);
1142 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1143 {
1144 ERROR ("cf_util_get_double: The %s option requires "
1145 "exactly one numeric argument.", ci->key);
1146 return (-1);
1147 }
1149 *ret_value = ci->values[0].value.number;
1151 return (0);
1152 } /* }}} int cf_util_get_double */
1154 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1155 {
1156 if ((ci == NULL) || (ret_bool == NULL))
1157 return (EINVAL);
1159 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1160 {
1161 ERROR ("cf_util_get_boolean: The %s option requires "
1162 "exactly one boolean argument.", ci->key);
1163 return (-1);
1164 }
1166 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1168 return (0);
1169 } /* }}} int cf_util_get_boolean */
1171 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1172 unsigned int *ret_value, unsigned int flag)
1173 {
1174 int status;
1175 _Bool b;
1177 if (ret_value == NULL)
1178 return (EINVAL);
1180 b = 0;
1181 status = cf_util_get_boolean (ci, &b);
1182 if (status != 0)
1183 return (status);
1185 if (b)
1186 {
1187 *ret_value |= flag;
1188 }
1189 else
1190 {
1191 *ret_value &= ~flag;
1192 }
1194 return (0);
1195 } /* }}} int cf_util_get_flag */
1197 /* Assures that the config option is a string or a number if the correct range
1198 * of 1-65535. The string is then converted to a port number using
1199 * `service_name_to_port_number' and returned.
1200 * Returns the port number in the range [1-65535] or less than zero upon
1201 * failure. */
1202 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1203 {
1204 int tmp;
1206 if ((ci->values_num != 1)
1207 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1208 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1209 {
1210 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1211 "exactly one string argument.", ci->key);
1212 return (-1);
1213 }
1215 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1216 return (service_name_to_port_number (ci->values[0].value.string));
1218 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1219 tmp = (int) (ci->values[0].value.number + 0.5);
1220 if ((tmp < 1) || (tmp > 65535))
1221 {
1222 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1223 "a service name or a port number. The number "
1224 "you specified, %i, is not in the valid "
1225 "range of 1-65535.",
1226 ci->key, tmp);
1227 return (-1);
1228 }
1230 return (tmp);
1231 } /* }}} int cf_util_get_port_number */
1233 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1234 {
1235 int port;
1236 char *service;
1237 int status;
1239 if (ci->values_num != 1)
1240 {
1241 ERROR ("cf_util_get_service: The %s option requires exactly "
1242 "one argument.", ci->key);
1243 return (-1);
1244 }
1246 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1247 return (cf_util_get_string (ci, ret_string));
1248 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1249 {
1250 ERROR ("cf_util_get_service: The %s option requires "
1251 "exactly one string or numeric argument.",
1252 ci->key);
1253 }
1255 port = 0;
1256 status = cf_util_get_int (ci, &port);
1257 if (status != 0)
1258 return (status);
1259 else if ((port < 1) || (port > 65535))
1260 {
1261 ERROR ("cf_util_get_service: The port number given "
1262 "for the %s option is out of "
1263 "range (%i).", ci->key, port);
1264 return (-1);
1265 }
1267 service = malloc (6);
1268 if (service == NULL)
1269 {
1270 ERROR ("cf_util_get_service: Out of memory.");
1271 return (-1);
1272 }
1273 ssnprintf (service, 6, "%i", port);
1275 sfree (*ret_string);
1276 *ret_string = service;
1278 return (0);
1279 } /* }}} int cf_util_get_service */
1281 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1282 {
1283 if ((ci == NULL) || (ret_value == NULL))
1284 return (EINVAL);
1286 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1287 {
1288 ERROR ("cf_util_get_cdtime: The %s option requires "
1289 "exactly one numeric argument.", ci->key);
1290 return (-1);
1291 }
1293 if (ci->values[0].value.number < 0.0)
1294 {
1295 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1296 "option must not be negative.", ci->key);
1297 return (-1);
1298 }
1300 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1302 return (0);
1303 } /* }}} int cf_util_get_cdtime */