1a9e28a624fed56c302cdec73c30bc563ef89597
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_LEN (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 {"Timeout", NULL, "2"},
112 {"PreCacheChain", NULL, "PreCache"},
113 {"PostCacheChain", NULL, "PostCache"}
114 };
115 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
117 static int cf_default_typesdb = 1;
119 /*
120 * Functions to handle register/unregister, search, and other plugin related
121 * stuff
122 */
123 static cf_callback_t *cf_search (const char *type)
124 {
125 cf_callback_t *cf_cb;
127 if (type == NULL)
128 return (NULL);
130 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
131 if (strcasecmp (cf_cb->type, type) == 0)
132 break;
134 return (cf_cb);
135 }
137 static int cf_dispatch (const char *type, const char *orig_key,
138 const char *orig_value)
139 {
140 cf_callback_t *cf_cb;
141 plugin_ctx_t old_ctx;
142 char *key;
143 char *value;
144 int ret;
145 int i;
147 DEBUG ("type = %s, key = %s, value = %s",
148 ESCAPE_NULL(type),
149 ESCAPE_NULL(orig_key),
150 ESCAPE_NULL(orig_value));
152 if ((cf_cb = cf_search (type)) == NULL)
153 {
154 WARNING ("Found a configuration for the `%s' plugin, but "
155 "the plugin isn't loaded or didn't register "
156 "a configuration callback.", type);
157 return (-1);
158 }
160 if ((key = strdup (orig_key)) == NULL)
161 return (1);
162 if ((value = strdup (orig_value)) == NULL)
163 {
164 free (key);
165 return (2);
166 }
168 ret = -1;
170 old_ctx = plugin_set_ctx (cf_cb->ctx);
172 for (i = 0; i < cf_cb->keys_num; i++)
173 {
174 if ((cf_cb->keys[i] != NULL)
175 && (strcasecmp (cf_cb->keys[i], key) == 0))
176 {
177 ret = (*cf_cb->callback) (key, value);
178 break;
179 }
180 }
182 plugin_set_ctx (old_ctx);
184 if (i >= cf_cb->keys_num)
185 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
187 free (key);
188 free (value);
190 DEBUG ("cf_dispatch: return (%i)", ret);
192 return (ret);
193 } /* int cf_dispatch */
195 static int dispatch_global_option (const oconfig_item_t *ci)
196 {
197 if (ci->values_num != 1)
198 return (-1);
199 if (ci->values[0].type == OCONFIG_TYPE_STRING)
200 return (global_option_set (ci->key, ci->values[0].value.string));
201 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
202 {
203 char tmp[128];
204 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
205 return (global_option_set (ci->key, tmp));
206 }
207 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
208 {
209 if (ci->values[0].value.boolean)
210 return (global_option_set (ci->key, "true"));
211 else
212 return (global_option_set (ci->key, "false"));
213 }
215 return (-1);
216 } /* int dispatch_global_option */
218 static int dispatch_value_typesdb (const oconfig_item_t *ci)
219 {
220 int i = 0;
222 assert (strcasecmp (ci->key, "TypesDB") == 0);
224 cf_default_typesdb = 0;
226 if (ci->values_num < 1) {
227 ERROR ("configfile: `TypesDB' needs at least one argument.");
228 return (-1);
229 }
231 for (i = 0; i < ci->values_num; ++i)
232 {
233 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
234 WARNING ("configfile: TypesDB: Skipping %i. argument which "
235 "is not a string.", i + 1);
236 continue;
237 }
239 read_types_list (ci->values[i].value.string);
240 }
241 return (0);
242 } /* int dispatch_value_typesdb */
244 static int dispatch_value_plugindir (const oconfig_item_t *ci)
245 {
246 assert (strcasecmp (ci->key, "PluginDir") == 0);
248 if (ci->values_num != 1)
249 return (-1);
250 if (ci->values[0].type != OCONFIG_TYPE_STRING)
251 return (-1);
253 plugin_set_dir (ci->values[0].value.string);
254 return (0);
255 }
257 static int dispatch_loadplugin (const oconfig_item_t *ci)
258 {
259 int i;
260 const char *name;
261 unsigned int flags = 0;
262 plugin_ctx_t ctx;
263 plugin_ctx_t old_ctx;
264 int ret_val;
266 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
268 if (ci->values_num != 1)
269 return (-1);
270 if (ci->values[0].type != OCONFIG_TYPE_STRING)
271 return (-1);
273 name = ci->values[0].value.string;
275 /* default to the global interval set before loading this plugin */
276 memset (&ctx, 0, sizeof (ctx));
277 ctx.interval = cf_get_default_interval ();
279 /*
280 * XXX: Magic at work:
281 *
282 * Some of the language bindings, for example the Python and Perl
283 * plugins, need to be able to export symbols to the scripts they run.
284 * For this to happen, the "Globals" flag needs to be set.
285 * Unfortunately, this technical detail is hard to explain to the
286 * average user and she shouldn't have to worry about this, ideally.
287 * So in order to save everyone's sanity use a different default for a
288 * handful of special plugins. --octo
289 */
290 if ((strcasecmp ("Perl", name) == 0)
291 || (strcasecmp ("Python", name) == 0))
292 flags |= PLUGIN_FLAGS_GLOBAL;
294 for (i = 0; i < ci->children_num; ++i) {
295 if (strcasecmp("Globals", ci->children[i].key) == 0)
296 cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
297 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
298 double interval = 0.0;
300 if (cf_util_get_double (ci->children + i, &interval) != 0) {
301 /* cf_util_get_double will log an error */
302 continue;
303 }
305 ctx.interval = DOUBLE_TO_CDTIME_T (interval);
306 }
307 else {
308 WARNING("Ignoring unknown LoadPlugin option \"%s\" "
309 "for plugin \"%s\"",
310 ci->children[i].key, ci->values[0].value.string);
311 }
312 }
314 old_ctx = plugin_set_ctx (ctx);
315 ret_val = plugin_load (name, (uint32_t) flags);
316 /* reset to the "global" context */
317 plugin_set_ctx (old_ctx);
319 return (ret_val);
320 } /* int dispatch_value_loadplugin */
322 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
323 {
324 char buffer[4096];
325 char *buffer_ptr;
326 int buffer_free;
327 int i;
329 buffer_ptr = buffer;
330 buffer_free = sizeof (buffer);
332 for (i = 0; i < ci->values_num; i++)
333 {
334 int status = -1;
336 if (ci->values[i].type == OCONFIG_TYPE_STRING)
337 status = ssnprintf (buffer_ptr, buffer_free, " %s",
338 ci->values[i].value.string);
339 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
340 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
341 ci->values[i].value.number);
342 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
343 status = ssnprintf (buffer_ptr, buffer_free, " %s",
344 ci->values[i].value.boolean
345 ? "true" : "false");
347 if ((status < 0) || (status >= buffer_free))
348 return (-1);
349 buffer_free -= status;
350 buffer_ptr += status;
351 }
352 /* skip the initial space */
353 buffer_ptr = buffer + 1;
355 return (cf_dispatch (plugin, ci->key, buffer_ptr));
356 } /* int dispatch_value_plugin */
358 static int dispatch_value (const oconfig_item_t *ci)
359 {
360 int ret = -2;
361 int i;
363 for (i = 0; i < cf_value_map_num; i++)
364 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
365 {
366 ret = cf_value_map[i].func (ci);
367 break;
368 }
370 for (i = 0; i < cf_global_options_num; i++)
371 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
372 {
373 ret = dispatch_global_option (ci);
374 break;
375 }
377 return (ret);
378 } /* int dispatch_value */
380 static int dispatch_block_plugin (oconfig_item_t *ci)
381 {
382 int i;
383 char *name;
385 cf_complex_callback_t *cb;
387 if (strcasecmp (ci->key, "Plugin") != 0)
388 return (-1);
389 if (ci->values_num < 1)
390 return (-1);
391 if (ci->values[0].type != OCONFIG_TYPE_STRING)
392 return (-1);
394 name = ci->values[0].value.string;
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 /* Ignore all blocks, including `Include' blocks. */
559 if (root->children[i].children_num != 0)
560 continue;
562 if (strcasecmp (root->children[i].key, "Include") != 0)
563 continue;
565 old = root->children + i;
567 if ((old->values_num < 1) || (old->values_num > 2)
568 || (old->values[0].type != OCONFIG_TYPE_STRING)
569 || ((old->values_num == 2)
570 && (old->values[1].type != OCONFIG_TYPE_STRING)))
571 {
572 ERROR ("configfile: `Include' needs exactly one or two string argument.");
573 continue;
574 }
576 new = cf_read_generic (old->values[0].value.string,
577 (old->values_num == 2) ? old->values[1].value.string : NULL,
578 depth + 1);
579 if (new == NULL)
580 continue;
582 /* Now replace the i'th child in `root' with `new'. */
583 cf_ci_replace_child (root, new, i);
585 /* ... and go back to the new i'th child. */
586 --i;
588 sfree (new->values);
589 sfree (new);
590 } /* for (i = 0; i < root->children_num; i++) */
592 return (0);
593 } /* int cf_include_all */
595 static oconfig_item_t *cf_read_file (const char *file,
596 const char *pattern, int depth)
597 {
598 oconfig_item_t *root;
600 assert (depth < CF_MAX_DEPTH);
602 if (pattern != NULL) {
603 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
604 char *tmp = sstrdup (file);
605 char *filename = basename (tmp);
607 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
608 free (tmp);
609 return (NULL);
610 }
612 free (tmp);
613 #else
614 ERROR ("configfile: Cannot apply pattern filter '%s' "
615 "to file '%s': functions basename() and / or "
616 "fnmatch() not available.", pattern, file);
617 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
618 }
620 root = oconfig_parse_file (file);
621 if (root == NULL)
622 {
623 ERROR ("configfile: Cannot read file `%s'.", file);
624 return (NULL);
625 }
627 cf_include_all (root, depth);
629 return (root);
630 } /* oconfig_item_t *cf_read_file */
632 static int cf_compare_string (const void *p1, const void *p2)
633 {
634 return strcmp (*(const char **) p1, *(const char **) p2);
635 }
637 static oconfig_item_t *cf_read_dir (const char *dir,
638 const char *pattern, int depth)
639 {
640 oconfig_item_t *root = NULL;
641 DIR *dh;
642 struct dirent *de;
643 char **filenames = NULL;
644 int filenames_num = 0;
645 int status;
646 int i;
648 assert (depth < CF_MAX_DEPTH);
650 dh = opendir (dir);
651 if (dh == NULL)
652 {
653 char errbuf[1024];
654 ERROR ("configfile: opendir failed: %s",
655 sstrerror (errno, errbuf, sizeof (errbuf)));
656 return (NULL);
657 }
659 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
660 if (root == NULL)
661 {
662 ERROR ("configfile: malloc failed.");
663 return (NULL);
664 }
665 memset (root, 0, sizeof (oconfig_item_t));
667 while ((de = readdir (dh)) != NULL)
668 {
669 char name[1024];
670 char **tmp;
672 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
673 continue;
675 status = ssnprintf (name, sizeof (name), "%s/%s",
676 dir, de->d_name);
677 if ((status < 0) || ((size_t) status >= sizeof (name)))
678 {
679 ERROR ("configfile: Not including `%s/%s' because its"
680 " name is too long.",
681 dir, de->d_name);
682 for (i = 0; i < filenames_num; ++i)
683 free (filenames[i]);
684 free (filenames);
685 free (root);
686 return (NULL);
687 }
689 ++filenames_num;
690 tmp = (char **) realloc (filenames,
691 filenames_num * sizeof (*filenames));
692 if (tmp == NULL) {
693 ERROR ("configfile: realloc failed.");
694 for (i = 0; i < filenames_num - 1; ++i)
695 free (filenames[i]);
696 free (filenames);
697 free (root);
698 return (NULL);
699 }
700 filenames = tmp;
702 filenames[filenames_num - 1] = sstrdup (name);
703 }
705 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
706 cf_compare_string);
708 for (i = 0; i < filenames_num; ++i)
709 {
710 oconfig_item_t *temp;
711 char *name = filenames[i];
713 temp = cf_read_generic (name, pattern, depth);
714 if (temp == NULL)
715 {
716 /* An error should already have been reported. */
717 sfree (name);
718 continue;
719 }
721 cf_ci_append_children (root, temp);
722 sfree (temp->children);
723 sfree (temp);
725 free (name);
726 }
728 free(filenames);
729 return (root);
730 } /* oconfig_item_t *cf_read_dir */
732 /*
733 * cf_read_generic
734 *
735 * Path is stat'ed and either cf_read_file or cf_read_dir is called
736 * accordingly.
737 *
738 * There are two versions of this function: If `wordexp' exists shell wildcards
739 * will be expanded and the function will include all matches found. If
740 * `wordexp' (or, more precisely, it's header file) is not available the
741 * simpler function is used which does not do any such expansion.
742 */
743 #if HAVE_WORDEXP_H
744 static oconfig_item_t *cf_read_generic (const char *path,
745 const char *pattern, int depth)
746 {
747 oconfig_item_t *root = NULL;
748 int status;
749 const char *path_ptr;
750 wordexp_t we;
751 size_t i;
753 if (depth >= CF_MAX_DEPTH)
754 {
755 ERROR ("configfile: Not including `%s' because the maximum "
756 "nesting depth has been reached.", path);
757 return (NULL);
758 }
760 status = wordexp (path, &we, WRDE_NOCMD);
761 if (status != 0)
762 {
763 ERROR ("configfile: wordexp (%s) failed.", path);
764 return (NULL);
765 }
767 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
768 if (root == NULL)
769 {
770 ERROR ("configfile: malloc failed.");
771 return (NULL);
772 }
773 memset (root, '\0', sizeof (oconfig_item_t));
775 /* wordexp() might return a sorted list already. That's not
776 * documented though, so let's make sure we get what we want. */
777 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
778 cf_compare_string);
780 for (i = 0; i < we.we_wordc; i++)
781 {
782 oconfig_item_t *temp;
783 struct stat statbuf;
785 path_ptr = we.we_wordv[i];
787 status = stat (path_ptr, &statbuf);
788 if (status != 0)
789 {
790 char errbuf[1024];
791 WARNING ("configfile: stat (%s) failed: %s",
792 path_ptr,
793 sstrerror (errno, errbuf, sizeof (errbuf)));
794 continue;
795 }
797 if (S_ISREG (statbuf.st_mode))
798 temp = cf_read_file (path_ptr, pattern, depth);
799 else if (S_ISDIR (statbuf.st_mode))
800 temp = cf_read_dir (path_ptr, pattern, depth);
801 else
802 {
803 WARNING ("configfile: %s is neither a file nor a "
804 "directory.", path);
805 continue;
806 }
808 if (temp == NULL) {
809 oconfig_free (root);
810 return (NULL);
811 }
813 cf_ci_append_children (root, temp);
814 sfree (temp->children);
815 sfree (temp);
816 }
818 wordfree (&we);
820 if (root->children == NULL)
821 {
822 oconfig_free (root);
823 return (NULL);
824 }
826 return (root);
827 } /* oconfig_item_t *cf_read_generic */
828 /* #endif HAVE_WORDEXP_H */
830 #else /* if !HAVE_WORDEXP_H */
831 static oconfig_item_t *cf_read_generic (const char *path,
832 const char *pattern, int depth)
833 {
834 struct stat statbuf;
835 int status;
837 if (depth >= CF_MAX_DEPTH)
838 {
839 ERROR ("configfile: Not including `%s' because the maximum "
840 "nesting depth has been reached.", path);
841 return (NULL);
842 }
844 status = stat (path, &statbuf);
845 if (status != 0)
846 {
847 char errbuf[1024];
848 ERROR ("configfile: stat (%s) failed: %s",
849 path,
850 sstrerror (errno, errbuf, sizeof (errbuf)));
851 return (NULL);
852 }
854 if (S_ISREG (statbuf.st_mode))
855 return (cf_read_file (path, pattern, depth));
856 else if (S_ISDIR (statbuf.st_mode))
857 return (cf_read_dir (path, pattern, depth));
859 ERROR ("configfile: %s is neither a file nor a directory.", path);
860 return (NULL);
861 } /* oconfig_item_t *cf_read_generic */
862 #endif /* !HAVE_WORDEXP_H */
864 /*
865 * Public functions
866 */
867 int global_option_set (const char *option, const char *value)
868 {
869 int i;
871 DEBUG ("option = %s; value = %s;", option, value);
873 for (i = 0; i < cf_global_options_num; i++)
874 if (strcasecmp (cf_global_options[i].key, option) == 0)
875 break;
877 if (i >= cf_global_options_num)
878 return (-1);
880 sfree (cf_global_options[i].value);
882 if (value != NULL)
883 cf_global_options[i].value = strdup (value);
884 else
885 cf_global_options[i].value = NULL;
887 return (0);
888 }
890 const char *global_option_get (const char *option)
891 {
892 int i;
894 for (i = 0; i < cf_global_options_num; i++)
895 if (strcasecmp (cf_global_options[i].key, option) == 0)
896 break;
898 if (i >= cf_global_options_num)
899 return (NULL);
901 return ((cf_global_options[i].value != NULL)
902 ? cf_global_options[i].value
903 : cf_global_options[i].def);
904 } /* char *global_option_get */
906 cdtime_t cf_get_default_interval (void)
907 {
908 char const *str = global_option_get ("Interval");
909 double interval_double = COLLECTD_DEFAULT_INTERVAL;
911 if (str != NULL)
912 {
913 char *endptr = NULL;
914 double tmp = strtod (str, &endptr);
916 if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
917 ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
918 "as number.", str);
919 else if (tmp <= 0.0)
920 ERROR ("cf_get_default_interval: Interval must be a positive number. "
921 "The current number is %g.", tmp);
922 else
923 interval_double = tmp;
924 }
926 return (DOUBLE_TO_CDTIME_T (interval_double));
927 } /* }}} cdtime_t cf_get_default_interval */
929 void cf_unregister (const char *type)
930 {
931 cf_callback_t *this, *prev;
933 for (prev = NULL, this = first_callback;
934 this != NULL;
935 prev = this, this = this->next)
936 if (strcasecmp (this->type, type) == 0)
937 {
938 if (prev == NULL)
939 first_callback = this->next;
940 else
941 prev->next = this->next;
943 free (this);
944 break;
945 }
946 } /* void cf_unregister */
948 void cf_unregister_complex (const char *type)
949 {
950 cf_complex_callback_t *this, *prev;
952 for (prev = NULL, this = complex_callback_head;
953 this != NULL;
954 prev = this, this = this->next)
955 if (strcasecmp (this->type, type) == 0)
956 {
957 if (prev == NULL)
958 complex_callback_head = this->next;
959 else
960 prev->next = this->next;
962 sfree (this->type);
963 sfree (this);
964 break;
965 }
966 } /* void cf_unregister */
968 void cf_register (const char *type,
969 int (*callback) (const char *, const char *),
970 const char **keys, int keys_num)
971 {
972 cf_callback_t *cf_cb;
974 /* Remove this module from the list, if it already exists */
975 cf_unregister (type);
977 /* This pointer will be free'd in `cf_unregister' */
978 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
979 return;
981 cf_cb->type = type;
982 cf_cb->callback = callback;
983 cf_cb->keys = keys;
984 cf_cb->keys_num = keys_num;
985 cf_cb->ctx = plugin_get_ctx ();
987 cf_cb->next = first_callback;
988 first_callback = cf_cb;
989 } /* void cf_register */
991 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
992 {
993 cf_complex_callback_t *new;
995 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
996 if (new == NULL)
997 return (-1);
999 new->type = strdup (type);
1000 if (new->type == NULL)
1001 {
1002 sfree (new);
1003 return (-1);
1004 }
1006 new->callback = callback;
1007 new->next = NULL;
1009 new->ctx = plugin_get_ctx ();
1011 if (complex_callback_head == NULL)
1012 {
1013 complex_callback_head = new;
1014 }
1015 else
1016 {
1017 cf_complex_callback_t *last = complex_callback_head;
1018 while (last->next != NULL)
1019 last = last->next;
1020 last->next = new;
1021 }
1023 return (0);
1024 } /* int cf_register_complex */
1026 int cf_read (char *filename)
1027 {
1028 oconfig_item_t *conf;
1029 int i;
1031 conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1032 if (conf == NULL)
1033 {
1034 ERROR ("Unable to read config file %s.", filename);
1035 return (-1);
1036 }
1038 for (i = 0; i < conf->children_num; i++)
1039 {
1040 if (conf->children[i].children == NULL)
1041 dispatch_value (conf->children + i);
1042 else
1043 dispatch_block (conf->children + i);
1044 }
1046 oconfig_free (conf);
1048 /* Read the default types.db if no `TypesDB' option was given. */
1049 if (cf_default_typesdb)
1050 read_types_list (PKGDATADIR"/types.db");
1052 return (0);
1053 } /* int cf_read */
1055 /* Assures the config option is a string, duplicates it and returns the copy in
1056 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1057 * success. */
1058 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1059 {
1060 char *string;
1062 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1063 {
1064 ERROR ("cf_util_get_string: The %s option requires "
1065 "exactly one string argument.", ci->key);
1066 return (-1);
1067 }
1069 string = strdup (ci->values[0].value.string);
1070 if (string == NULL)
1071 return (-1);
1073 if (*ret_string != NULL)
1074 sfree (*ret_string);
1075 *ret_string = string;
1077 return (0);
1078 } /* }}} int cf_util_get_string */
1080 /* Assures the config option is a string and copies it to the provided buffer.
1081 * Assures null-termination. */
1082 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1083 size_t buffer_size)
1084 {
1085 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1086 return (EINVAL);
1088 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1089 {
1090 ERROR ("cf_util_get_string_buffer: The %s option requires "
1091 "exactly one string argument.", ci->key);
1092 return (-1);
1093 }
1095 strncpy (buffer, ci->values[0].value.string, buffer_size);
1096 buffer[buffer_size - 1] = 0;
1098 return (0);
1099 } /* }}} int cf_util_get_string_buffer */
1101 /* Assures the config option is a number and returns it as an int. */
1102 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1103 {
1104 if ((ci == NULL) || (ret_value == NULL))
1105 return (EINVAL);
1107 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1108 {
1109 ERROR ("cf_util_get_int: The %s option requires "
1110 "exactly one numeric argument.", ci->key);
1111 return (-1);
1112 }
1114 *ret_value = (int) ci->values[0].value.number;
1116 return (0);
1117 } /* }}} int cf_util_get_int */
1119 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1120 {
1121 if ((ci == NULL) || (ret_value == NULL))
1122 return (EINVAL);
1124 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1125 {
1126 ERROR ("cf_util_get_double: The %s option requires "
1127 "exactly one numeric argument.", ci->key);
1128 return (-1);
1129 }
1131 *ret_value = ci->values[0].value.number;
1133 return (0);
1134 } /* }}} int cf_util_get_double */
1136 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1137 {
1138 if ((ci == NULL) || (ret_bool == NULL))
1139 return (EINVAL);
1141 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1142 {
1143 ERROR ("cf_util_get_boolean: The %s option requires "
1144 "exactly one boolean argument.", ci->key);
1145 return (-1);
1146 }
1148 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1150 return (0);
1151 } /* }}} int cf_util_get_boolean */
1153 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1154 unsigned int *ret_value, unsigned int flag)
1155 {
1156 int status;
1157 _Bool b;
1159 if (ret_value == NULL)
1160 return (EINVAL);
1162 b = 0;
1163 status = cf_util_get_boolean (ci, &b);
1164 if (status != 0)
1165 return (status);
1167 if (b)
1168 {
1169 *ret_value |= flag;
1170 }
1171 else
1172 {
1173 *ret_value &= ~flag;
1174 }
1176 return (0);
1177 } /* }}} int cf_util_get_flag */
1179 /* Assures that the config option is a string or a number if the correct range
1180 * of 1-65535. The string is then converted to a port number using
1181 * `service_name_to_port_number' and returned.
1182 * Returns the port number in the range [1-65535] or less than zero upon
1183 * failure. */
1184 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1185 {
1186 int tmp;
1188 if ((ci->values_num != 1)
1189 || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1190 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1191 {
1192 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1193 "exactly one string argument.", ci->key);
1194 return (-1);
1195 }
1197 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1198 return (service_name_to_port_number (ci->values[0].value.string));
1200 assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1201 tmp = (int) (ci->values[0].value.number + 0.5);
1202 if ((tmp < 1) || (tmp > 65535))
1203 {
1204 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1205 "a service name or a port number. The number "
1206 "you specified, %i, is not in the valid "
1207 "range of 1-65535.",
1208 ci->key, tmp);
1209 return (-1);
1210 }
1212 return (tmp);
1213 } /* }}} int cf_util_get_port_number */
1215 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1216 {
1217 int port;
1218 char *service;
1219 int status;
1221 if (ci->values_num != 1)
1222 {
1223 ERROR ("cf_util_get_service: The %s option requires exactly "
1224 "one argument.", ci->key);
1225 return (-1);
1226 }
1228 if (ci->values[0].type == OCONFIG_TYPE_STRING)
1229 return (cf_util_get_string (ci, ret_string));
1230 if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1231 {
1232 ERROR ("cf_util_get_service: The %s option requires "
1233 "exactly one string or numeric argument.",
1234 ci->key);
1235 }
1237 port = 0;
1238 status = cf_util_get_int (ci, &port);
1239 if (status != 0)
1240 return (status);
1241 else if ((port < 1) || (port > 65535))
1242 {
1243 ERROR ("cf_util_get_service: The port number given "
1244 "for the %s option is out of "
1245 "range (%i).", ci->key, port);
1246 return (-1);
1247 }
1249 service = malloc (6);
1250 if (service == NULL)
1251 {
1252 ERROR ("cf_util_get_service: Out of memory.");
1253 return (-1);
1254 }
1255 ssnprintf (service, 6, "%i", port);
1257 sfree (*ret_string);
1258 *ret_string = service;
1260 return (0);
1261 } /* }}} int cf_util_get_service */
1263 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1264 {
1265 if ((ci == NULL) || (ret_value == NULL))
1266 return (EINVAL);
1268 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1269 {
1270 ERROR ("cf_util_get_cdtime: The %s option requires "
1271 "exactly one numeric argument.", ci->key);
1272 return (-1);
1273 }
1275 if (ci->values[0].value.number < 0.0)
1276 {
1277 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1278 "option must not be negative.", ci->key);
1279 return (-1);
1280 }
1282 *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1284 return (0);
1285 } /* }}} int cf_util_get_cdtime */