Merge pull request #1596 from rubenk/fix-a-few-more-prototypes
[collectd.git] / src / configfile.c
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  **/
23
24 #include "collectd.h"
25
26 #include "liboconfig/oconfig.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
33
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
37
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
41
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
45
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
47
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;
60
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;
68
69 typedef struct cf_value_map_s
70 {
71         char *key;
72         int (*func) (oconfig_item_t *);
73 } cf_value_map_t;
74
75 typedef struct cf_global_option_s
76 {
77         char *key;
78         char *value;
79         char *def;
80 } cf_global_option_t;
81
82 /*
83  * Prototypes of callback functions
84  */
85 static int dispatch_value_typesdb (oconfig_item_t *ci);
86 static int dispatch_value_plugindir (oconfig_item_t *ci);
87 static int dispatch_loadplugin (oconfig_item_t *ci);
88 static int dispatch_block_plugin (oconfig_item_t *ci);
89
90 /*
91  * Private variables
92  */
93 static cf_callback_t *first_callback = NULL;
94 static cf_complex_callback_t *complex_callback_head = NULL;
95
96 static cf_value_map_t cf_value_map[] =
97 {
98         {"TypesDB",    dispatch_value_typesdb},
99         {"PluginDir",  dispatch_value_plugindir},
100         {"LoadPlugin", dispatch_loadplugin},
101         {"Plugin",     dispatch_block_plugin}
102 };
103 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
104
105 static cf_global_option_t cf_global_options[] =
106 {
107         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
108         {"PIDFile",     NULL, PIDFILE},
109         {"Hostname",    NULL, NULL},
110         {"FQDNLookup",  NULL, "true"},
111         {"Interval",    NULL, NULL},
112         {"ReadThreads", NULL, "5"},
113         {"WriteThreads", NULL, "5"},
114         {"WriteQueueLimitHigh", NULL, NULL},
115         {"WriteQueueLimitLow", NULL, NULL},
116         {"Timeout",     NULL, "2"},
117         {"AutoLoadPlugin", NULL, "false"},
118         {"PreCacheChain",  NULL, "PreCache"},
119         {"PostCacheChain", NULL, "PostCache"}
120 };
121 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
122
123 static int cf_default_typesdb = 1;
124
125 /*
126  * Functions to handle register/unregister, search, and other plugin related
127  * stuff
128  */
129 static cf_callback_t *cf_search (const char *type)
130 {
131         cf_callback_t *cf_cb;
132
133         if (type == NULL)
134                 return (NULL);
135
136         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
137                 if (strcasecmp (cf_cb->type, type) == 0)
138                         break;
139
140         return (cf_cb);
141 }
142
143 static int cf_dispatch (const char *type, const char *orig_key,
144                 const char *orig_value)
145 {
146         cf_callback_t *cf_cb;
147         plugin_ctx_t old_ctx;
148         char *key;
149         char *value;
150         int ret;
151         int i;
152
153         if (orig_key == NULL)
154                 return (EINVAL);
155
156         DEBUG ("type = %s, key = %s, value = %s",
157                         ESCAPE_NULL(type),
158                         orig_key,
159                         ESCAPE_NULL(orig_value));
160
161         if ((cf_cb = cf_search (type)) == NULL)
162         {
163                 WARNING ("Found a configuration for the `%s' plugin, but "
164                                 "the plugin isn't loaded or didn't register "
165                                 "a configuration callback.", type);
166                 return (-1);
167         }
168
169         if ((key = strdup (orig_key)) == NULL)
170                 return (1);
171         if ((value = strdup (orig_value)) == NULL)
172         {
173                 free (key);
174                 return (2);
175         }
176
177         ret = -1;
178
179         old_ctx = plugin_set_ctx (cf_cb->ctx);
180
181         for (i = 0; i < cf_cb->keys_num; i++)
182         {
183                 if ((cf_cb->keys[i] != NULL)
184                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
185                 {
186                         ret = (*cf_cb->callback) (key, value);
187                         break;
188                 }
189         }
190
191         plugin_set_ctx (old_ctx);
192
193         if (i >= cf_cb->keys_num)
194                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
195
196         free (key);
197         free (value);
198
199         return (ret);
200 } /* int cf_dispatch */
201
202 static int dispatch_global_option (const oconfig_item_t *ci)
203 {
204         if (ci->values_num != 1)
205                 return (-1);
206         if (ci->values[0].type == OCONFIG_TYPE_STRING)
207                 return (global_option_set (ci->key, ci->values[0].value.string));
208         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
209         {
210                 char tmp[128];
211                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
212                 return (global_option_set (ci->key, tmp));
213         }
214         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
215         {
216                 if (ci->values[0].value.boolean)
217                         return (global_option_set (ci->key, "true"));
218                 else
219                         return (global_option_set (ci->key, "false"));
220         }
221
222         return (-1);
223 } /* int dispatch_global_option */
224
225 static int dispatch_value_typesdb (oconfig_item_t *ci)
226 {
227         int i = 0;
228
229         assert (strcasecmp (ci->key, "TypesDB") == 0);
230
231         cf_default_typesdb = 0;
232
233         if (ci->values_num < 1) {
234                 ERROR ("configfile: `TypesDB' needs at least one argument.");
235                 return (-1);
236         }
237
238         for (i = 0; i < ci->values_num; ++i)
239         {
240                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
241                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
242                                         "is not a string.", i + 1);
243                         continue;
244                 }
245
246                 read_types_list (ci->values[i].value.string);
247         }
248         return (0);
249 } /* int dispatch_value_typesdb */
250
251 static int dispatch_value_plugindir (oconfig_item_t *ci)
252 {
253         assert (strcasecmp (ci->key, "PluginDir") == 0);
254         
255         if (ci->values_num != 1)
256                 return (-1);
257         if (ci->values[0].type != OCONFIG_TYPE_STRING)
258                 return (-1);
259
260         plugin_set_dir (ci->values[0].value.string);
261         return (0);
262 }
263
264 static int dispatch_loadplugin (oconfig_item_t *ci)
265 {
266         int i;
267         const char *name;
268         unsigned int flags = 0;
269         plugin_ctx_t ctx;
270         plugin_ctx_t old_ctx;
271         int ret_val;
272
273         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
274
275         if (ci->values_num != 1)
276                 return (-1);
277         if (ci->values[0].type != OCONFIG_TYPE_STRING)
278                 return (-1);
279
280         name = ci->values[0].value.string;
281
282         /* default to the global interval set before loading this plugin */
283         memset (&ctx, 0, sizeof (ctx));
284         ctx.interval = cf_get_default_interval ();
285
286         for (i = 0; i < ci->children_num; ++i) {
287                 if (strcasecmp("Globals", ci->children[i].key) == 0)
288                         cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
289                 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
290                         double interval = 0.0;
291
292                         if (cf_util_get_double (ci->children + i, &interval) != 0) {
293                                 /* cf_util_get_double will log an error */
294                                 continue;
295                         }
296
297                         ctx.interval = DOUBLE_TO_CDTIME_T (interval);
298                 }
299                 else {
300                         WARNING("Ignoring unknown LoadPlugin option \"%s\" "
301                                         "for plugin \"%s\"",
302                                         ci->children[i].key, ci->values[0].value.string);
303                 }
304         }
305
306         old_ctx = plugin_set_ctx (ctx);
307         ret_val = plugin_load (name, (uint32_t) flags);
308         /* reset to the "global" context */
309         plugin_set_ctx (old_ctx);
310
311         return (ret_val);
312 } /* int dispatch_value_loadplugin */
313
314 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
315 {
316         char  buffer[4096];
317         char *buffer_ptr;
318         int   buffer_free;
319         int i;
320
321         buffer_ptr = buffer;
322         buffer_free = sizeof (buffer);
323
324         for (i = 0; i < ci->values_num; i++)
325         {
326                 int status = -1;
327
328                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
329                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
330                                         ci->values[i].value.string);
331                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
332                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
333                                         ci->values[i].value.number);
334                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
335                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
336                                         ci->values[i].value.boolean
337                                         ? "true" : "false");
338
339                 if ((status < 0) || (status >= buffer_free))
340                         return (-1);
341                 buffer_free -= status;
342                 buffer_ptr  += status;
343         }
344         /* skip the initial space */
345         buffer_ptr = buffer + 1;
346
347         return (cf_dispatch (plugin, ci->key, buffer_ptr));
348 } /* int dispatch_value_plugin */
349
350 static int dispatch_value (oconfig_item_t *ci)
351 {
352         int ret = -2;
353         int i;
354
355         for (i = 0; i < cf_value_map_num; i++)
356                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
357                 {
358                         ret = cf_value_map[i].func (ci);
359                         break;
360                 }
361
362         for (i = 0; i < cf_global_options_num; i++)
363                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
364                 {
365                         ret = dispatch_global_option (ci);
366                         break;
367                 }
368
369         return (ret);
370 } /* int dispatch_value */
371
372 static int dispatch_block_plugin (oconfig_item_t *ci)
373 {
374         int i;
375         char *name;
376
377         cf_complex_callback_t *cb;
378
379         if (strcasecmp (ci->key, "Plugin") != 0)
380                 return (-1);
381         if (ci->values_num < 1)
382                 return (-1);
383         if (ci->values[0].type != OCONFIG_TYPE_STRING)
384                 return (-1);
385
386         name = ci->values[0].value.string;
387
388         if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
389         {
390                 plugin_ctx_t ctx;
391                 plugin_ctx_t old_ctx;
392                 int status;
393
394                 /* default to the global interval set before loading this plugin */
395                 memset (&ctx, 0, sizeof (ctx));
396                 ctx.interval = cf_get_default_interval ();
397
398                 old_ctx = plugin_set_ctx (ctx);
399                 status = plugin_load (name, /* flags = */ 0);
400                 /* reset to the "global" context */
401                 plugin_set_ctx (old_ctx);
402
403                 if (status != 0)
404                 {
405                         ERROR ("Automatically loading plugin \"%s\" failed "
406                                         "with status %i.", name, status);
407                         return (status);
408                 }
409         }
410
411         /* Check for a complex callback first */
412         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
413         {
414                 if (strcasecmp (name, cb->type) == 0)
415                 {
416                         plugin_ctx_t old_ctx;
417                         int ret_val;
418
419                         old_ctx = plugin_set_ctx (cb->ctx);
420                         ret_val = (cb->callback (ci));
421                         plugin_set_ctx (old_ctx);
422                         return (ret_val);
423                 }
424         }
425
426         /* Hm, no complex plugin found. Dispatch the values one by one */
427         for (i = 0; i < ci->children_num; i++)
428         {
429                 if (ci->children[i].children == NULL)
430                         dispatch_value_plugin (name, ci->children + i);
431                 else
432                 {
433                         WARNING ("There is a `%s' block within the "
434                                         "configuration for the %s plugin. "
435                                         "The plugin either only expects "
436                                         "\"simple\" configuration statements "
437                                         "or wasn't loaded using `LoadPlugin'."
438                                         " Please check your configuration.",
439                                         ci->children[i].key, name);
440                 }
441         }
442
443         return (0);
444 }
445
446
447 static int dispatch_block (oconfig_item_t *ci)
448 {
449         if (strcasecmp (ci->key, "LoadPlugin") == 0)
450                 return (dispatch_loadplugin (ci));
451         else if (strcasecmp (ci->key, "Plugin") == 0)
452                 return (dispatch_block_plugin (ci));
453         else if (strcasecmp (ci->key, "Chain") == 0)
454                 return (fc_configure (ci));
455
456         return (0);
457 }
458
459 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
460                 int offset)
461 {
462         oconfig_item_t *temp;
463         int i;
464
465         assert (offset >= 0);
466         assert (dst->children_num > offset);
467
468         /* Free the memory used by the replaced child. Usually that's the
469          * `Include "blah"' statement. */
470         temp = dst->children + offset;
471         for (i = 0; i < temp->values_num; i++)
472         {
473                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
474                 {
475                         sfree (temp->values[i].value.string);
476                 }
477         }
478         sfree (temp->values);
479         temp = NULL;
480
481         /* If (src->children_num == 0) the array size is decreased. If offset
482          * is _not_ the last element, (offset < (dst->children_num - 1)), then
483          * we need to move the trailing elements before resizing the array. */
484         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
485         {
486                 int nmemb = dst->children_num - (offset + 1);
487                 memmove (dst->children + offset, dst->children + offset + 1,
488                                 sizeof (oconfig_item_t) * nmemb);
489         }
490
491         /* Resize the memory containing the children to be big enough to hold
492          * all children. */
493         if (dst->children_num + src->children_num - 1 == 0)
494         {
495                 dst->children_num = 0;
496                 return (0);
497         }
498
499         temp = (oconfig_item_t *) realloc (dst->children,
500                         sizeof (oconfig_item_t)
501                         * (dst->children_num + src->children_num - 1));
502         if (temp == NULL)
503         {
504                 ERROR ("configfile: realloc failed.");
505                 return (-1);
506         }
507         dst->children = temp;
508
509         /* If there are children behind the include statement, and they have
510          * not yet been moved because (src->children_num == 0), then move them
511          * to the end of the list, so that the new children have room before
512          * them. */
513         if ((src->children_num > 0)
514                         && ((dst->children_num - (offset + 1)) > 0))
515         {
516                 int nmemb = dst->children_num - (offset + 1);
517                 int old_offset = offset + 1;
518                 int new_offset = offset + src->children_num;
519
520                 memmove (dst->children + new_offset,
521                                 dst->children + old_offset,
522                                 sizeof (oconfig_item_t) * nmemb);
523         }
524
525         /* Last but not least: If there are new children, copy them to the
526          * memory reserved for them. */
527         if (src->children_num > 0)
528         {
529                 memcpy (dst->children + offset,
530                                 src->children,
531                                 sizeof (oconfig_item_t) * src->children_num);
532         }
533
534         /* Update the number of children. */
535         dst->children_num += (src->children_num - 1);
536
537         return (0);
538 } /* int cf_ci_replace_child */
539
540 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
541 {
542         oconfig_item_t *temp;
543
544         if ((src == NULL) || (src->children_num == 0))
545                 return (0);
546
547         temp = (oconfig_item_t *) realloc (dst->children,
548                         sizeof (oconfig_item_t)
549                         * (dst->children_num + src->children_num));
550         if (temp == NULL)
551         {
552                 ERROR ("configfile: realloc failed.");
553                 return (-1);
554         }
555         dst->children = temp;
556
557         memcpy (dst->children + dst->children_num,
558                         src->children,
559                         sizeof (oconfig_item_t)
560                         * src->children_num);
561         dst->children_num += src->children_num;
562
563         return (0);
564 } /* int cf_ci_append_children */
565
566 #define CF_MAX_DEPTH 8
567 static oconfig_item_t *cf_read_generic (const char *path,
568                 const char *pattern, int depth);
569
570 static int cf_include_all (oconfig_item_t *root, int depth)
571 {
572         int i;
573
574         for (i = 0; i < root->children_num; i++)
575         {
576                 oconfig_item_t *new;
577                 oconfig_item_t *old;
578
579                 char *pattern = NULL;
580
581                 int j;
582
583                 if (strcasecmp (root->children[i].key, "Include") != 0)
584                         continue;
585
586                 old = root->children + i;
587
588                 if ((old->values_num != 1)
589                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
590                 {
591                         ERROR ("configfile: `Include' needs exactly one string argument.");
592                         continue;
593                 }
594
595                 for (j = 0; j < old->children_num; ++j)
596                 {
597                         oconfig_item_t *child = old->children + j;
598
599                         if (strcasecmp (child->key, "Filter") == 0)
600                                 cf_util_get_string (child, &pattern);
601                         else
602                                 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
603                                                 child->key);
604                 }
605
606                 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
607                 sfree (pattern);
608
609                 if (new == NULL)
610                         return (-1);
611
612                 /* Now replace the i'th child in `root' with `new'. */
613                 if (cf_ci_replace_child (root, new, i) < 0) {
614                         sfree (new->values);
615                         sfree (new);
616                         return (-1);
617                 }
618
619                 /* ... and go back to the new i'th child. */
620                 --i;
621
622                 sfree (new->values);
623                 sfree (new);
624         } /* for (i = 0; i < root->children_num; i++) */
625
626         return (0);
627 } /* int cf_include_all */
628
629 static oconfig_item_t *cf_read_file (const char *file,
630                 const char *pattern, int depth)
631 {
632         oconfig_item_t *root;
633         int status;
634
635         assert (depth < CF_MAX_DEPTH);
636
637         if (pattern != NULL) {
638 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
639                 char *tmp = sstrdup (file);
640                 char *filename = basename (tmp);
641
642                 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
643                         DEBUG ("configfile: Not including `%s' because it "
644                                         "does not match pattern `%s'.",
645                                         filename, pattern);
646                         free (tmp);
647                         return (NULL);
648                 }
649
650                 free (tmp);
651 #else
652                 ERROR ("configfile: Cannot apply pattern filter '%s' "
653                                 "to file '%s': functions basename() and / or "
654                                 "fnmatch() not available.", pattern, file);
655 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
656         }
657
658         root = oconfig_parse_file (file);
659         if (root == NULL)
660         {
661                 ERROR ("configfile: Cannot read file `%s'.", file);
662                 return (NULL);
663         }
664
665         status = cf_include_all (root, depth);
666         if (status != 0)
667         {
668                 oconfig_free (root);
669                 return (NULL);
670         }
671
672         return (root);
673 } /* oconfig_item_t *cf_read_file */
674
675 static int cf_compare_string (const void *p1, const void *p2)
676 {
677         return strcmp (*(const char **) p1, *(const char **) p2);
678 }
679
680 static oconfig_item_t *cf_read_dir (const char *dir,
681                 const char *pattern, int depth)
682 {
683         oconfig_item_t *root = NULL;
684         DIR *dh;
685         struct dirent *de;
686         char **filenames = NULL;
687         int filenames_num = 0;
688         int status;
689         int i;
690
691         assert (depth < CF_MAX_DEPTH);
692
693         dh = opendir (dir);
694         if (dh == NULL)
695         {
696                 char errbuf[1024];
697                 ERROR ("configfile: opendir failed: %s",
698                                 sstrerror (errno, errbuf, sizeof (errbuf)));
699                 return (NULL);
700         }
701
702         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
703         if (root == NULL)
704         {
705                 ERROR ("configfile: malloc failed.");
706                 closedir (dh);
707                 return (NULL);
708         }
709         memset (root, 0, sizeof (oconfig_item_t));
710
711         while ((de = readdir (dh)) != NULL)
712         {
713                 char   name[1024];
714                 char **tmp;
715
716                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
717                         continue;
718
719                 status = ssnprintf (name, sizeof (name), "%s/%s",
720                                 dir, de->d_name);
721                 if ((status < 0) || ((size_t) status >= sizeof (name)))
722                 {
723                         ERROR ("configfile: Not including `%s/%s' because its"
724                                         " name is too long.",
725                                         dir, de->d_name);
726                         closedir (dh);
727                         for (i = 0; i < filenames_num; ++i)
728                                 free (filenames[i]);
729                         free (filenames);
730                         free (root);
731                         return (NULL);
732                 }
733
734                 ++filenames_num;
735                 tmp = (char **) realloc (filenames,
736                                 filenames_num * sizeof (*filenames));
737                 if (tmp == NULL) {
738                         ERROR ("configfile: realloc failed.");
739                         closedir (dh);
740                         for (i = 0; i < filenames_num - 1; ++i)
741                                 free (filenames[i]);
742                         free (filenames);
743                         free (root);
744                         return (NULL);
745                 }
746                 filenames = tmp;
747
748                 filenames[filenames_num - 1] = sstrdup (name);
749         }
750
751         if (filenames == NULL)
752         {
753                 closedir (dh);
754                 return (root);
755         }
756
757         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
758                         cf_compare_string);
759
760         for (i = 0; i < filenames_num; ++i)
761         {
762                 oconfig_item_t *temp;
763                 char *name = filenames[i];
764
765                 temp = cf_read_generic (name, pattern, depth);
766                 if (temp == NULL)
767                 {
768                         /* An error should already have been reported. */
769                         sfree (name);
770                         continue;
771                 }
772
773                 cf_ci_append_children (root, temp);
774                 sfree (temp->children);
775                 sfree (temp);
776
777                 free (name);
778         }
779
780         closedir (dh);
781         free(filenames);
782         return (root);
783 } /* oconfig_item_t *cf_read_dir */
784
785 /*
786  * cf_read_generic
787  *
788  * Path is stat'ed and either cf_read_file or cf_read_dir is called
789  * accordingly.
790  *
791  * There are two versions of this function: If `wordexp' exists shell wildcards
792  * will be expanded and the function will include all matches found. If
793  * `wordexp' (or, more precisely, it's header file) is not available the
794  * simpler function is used which does not do any such expansion.
795  */
796 #if HAVE_WORDEXP_H
797 static oconfig_item_t *cf_read_generic (const char *path,
798                 const char *pattern, int depth)
799 {
800         oconfig_item_t *root = NULL;
801         int status;
802         const char *path_ptr;
803         wordexp_t we;
804         size_t i;
805
806         if (depth >= CF_MAX_DEPTH)
807         {
808                 ERROR ("configfile: Not including `%s' because the maximum "
809                                 "nesting depth has been reached.", path);
810                 return (NULL);
811         }
812
813         status = wordexp (path, &we, WRDE_NOCMD);
814         if (status != 0)
815         {
816                 ERROR ("configfile: wordexp (%s) failed.", path);
817                 return (NULL);
818         }
819
820         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
821         if (root == NULL)
822         {
823                 ERROR ("configfile: malloc failed.");
824                 return (NULL);
825         }
826         memset (root, '\0', sizeof (oconfig_item_t));
827
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);
832
833         for (i = 0; i < we.we_wordc; i++)
834         {
835                 oconfig_item_t *temp;
836                 struct stat statbuf;
837
838                 path_ptr = we.we_wordv[i];
839
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                 }
849
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                 }
860
861                 if (temp == NULL) {
862                         oconfig_free (root);
863                         return (NULL);
864                 }
865
866                 cf_ci_append_children (root, temp);
867                 sfree (temp->children);
868                 sfree (temp);
869         }
870
871         wordfree (&we);
872
873         return (root);
874 } /* oconfig_item_t *cf_read_generic */
875 /* #endif HAVE_WORDEXP_H */
876
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;
883
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         }
890
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         }
900
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));
905
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 */
910
911 /* 
912  * Public functions
913  */
914 int global_option_set (const char *option, const char *value)
915 {
916         int i;
917
918         DEBUG ("option = %s; value = %s;", option, value);
919
920         for (i = 0; i < cf_global_options_num; i++)
921                 if (strcasecmp (cf_global_options[i].key, option) == 0)
922                         break;
923
924         if (i >= cf_global_options_num)
925                 return (-1);
926
927         sfree (cf_global_options[i].value);
928
929         if (value != NULL)
930                 cf_global_options[i].value = strdup (value);
931         else
932                 cf_global_options[i].value = NULL;
933
934         return (0);
935 }
936
937 const char *global_option_get (const char *option)
938 {
939         int i;
940
941         for (i = 0; i < cf_global_options_num; i++)
942                 if (strcasecmp (cf_global_options[i].key, option) == 0)
943                         break;
944
945         if (i >= cf_global_options_num)
946                 return (NULL);
947         
948         return ((cf_global_options[i].value != NULL)
949                         ? cf_global_options[i].value
950                         : cf_global_options[i].def);
951 } /* char *global_option_get */
952
953 long global_option_get_long (const char *option, long default_value)
954 {
955                 const char *str;
956                 long value;
957
958                 str = global_option_get (option);
959                 if (NULL == str)
960                         return (default_value);
961
962                 errno = 0;
963                 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
964                 if (errno != 0)
965                         return (default_value);
966
967                 return (value);
968 } /* char *global_option_get_long */
969
970 cdtime_t cf_get_default_interval (void)
971 {
972   char const *str = global_option_get ("Interval");
973   double interval_double = COLLECTD_DEFAULT_INTERVAL;
974
975   if (str != NULL)
976   {
977     char *endptr = NULL;
978     double tmp = strtod (str, &endptr);
979
980     if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
981       ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
982           "as number.", str);
983     else if (tmp <= 0.0)
984       ERROR ("cf_get_default_interval: Interval must be a positive number. "
985           "The current number is %g.", tmp);
986     else
987       interval_double = tmp;
988   }
989
990   return (DOUBLE_TO_CDTIME_T (interval_double));
991 } /* }}} cdtime_t cf_get_default_interval */
992
993 void cf_unregister (const char *type)
994 {
995         cf_callback_t *this, *prev;
996
997         for (prev = NULL, this = first_callback;
998                         this != NULL;
999                         prev = this, this = this->next)
1000                 if (strcasecmp (this->type, type) == 0)
1001                 {
1002                         if (prev == NULL)
1003                                 first_callback = this->next;
1004                         else
1005                                 prev->next = this->next;
1006
1007                         free (this);
1008                         break;
1009                 }
1010 } /* void cf_unregister */
1011
1012 void cf_unregister_complex (const char *type)
1013 {
1014         cf_complex_callback_t *this, *prev;
1015
1016         for (prev = NULL, this = complex_callback_head;
1017                         this != NULL;
1018                         prev = this, this = this->next)
1019                 if (strcasecmp (this->type, type) == 0)
1020                 {
1021                         if (prev == NULL)
1022                                 complex_callback_head = this->next;
1023                         else
1024                                 prev->next = this->next;
1025
1026                         sfree (this->type);
1027                         sfree (this);
1028                         break;
1029                 }
1030 } /* void cf_unregister */
1031
1032 void cf_register (const char *type,
1033                 int (*callback) (const char *, const char *),
1034                 const char **keys, int keys_num)
1035 {
1036         cf_callback_t *cf_cb;
1037
1038         /* Remove this module from the list, if it already exists */
1039         cf_unregister (type);
1040
1041         /* This pointer will be free'd in `cf_unregister' */
1042         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1043                 return;
1044
1045         cf_cb->type     = type;
1046         cf_cb->callback = callback;
1047         cf_cb->keys     = keys;
1048         cf_cb->keys_num = keys_num;
1049         cf_cb->ctx      = plugin_get_ctx ();
1050
1051         cf_cb->next = first_callback;
1052         first_callback = cf_cb;
1053 } /* void cf_register */
1054
1055 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1056 {
1057         cf_complex_callback_t *new;
1058
1059         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1060         if (new == NULL)
1061                 return (-1);
1062
1063         new->type = strdup (type);
1064         if (new->type == NULL)
1065         {
1066                 sfree (new);
1067                 return (-1);
1068         }
1069
1070         new->callback = callback;
1071         new->next = NULL;
1072
1073         new->ctx = plugin_get_ctx ();
1074
1075         if (complex_callback_head == NULL)
1076         {
1077                 complex_callback_head = new;
1078         }
1079         else
1080         {
1081                 cf_complex_callback_t *last = complex_callback_head;
1082                 while (last->next != NULL)
1083                         last = last->next;
1084                 last->next = new;
1085         }
1086
1087         return (0);
1088 } /* int cf_register_complex */
1089
1090 int cf_read (char *filename)
1091 {
1092         oconfig_item_t *conf;
1093         int i;
1094
1095         conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1096         if (conf == NULL)
1097         {
1098                 ERROR ("Unable to read config file %s.", filename);
1099                 return (-1);
1100         }
1101         else if (conf->children_num == 0)
1102         {
1103                 ERROR ("Configuration file %s is empty.", filename);
1104                 oconfig_free (conf);
1105                 return (-1);
1106         }
1107
1108         for (i = 0; i < conf->children_num; i++)
1109         {
1110                 if (conf->children[i].children == NULL)
1111                         dispatch_value (conf->children + i);
1112                 else
1113                         dispatch_block (conf->children + i);
1114         }
1115
1116         oconfig_free (conf);
1117
1118         /* Read the default types.db if no `TypesDB' option was given. */
1119         if (cf_default_typesdb)
1120                 read_types_list (PKGDATADIR"/types.db");
1121
1122         return (0);
1123 } /* int cf_read */
1124
1125 /* Assures the config option is a string, duplicates it and returns the copy in
1126  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1127  * success. */
1128 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1129 {
1130         char *string;
1131
1132         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1133         {
1134                 ERROR ("cf_util_get_string: The %s option requires "
1135                                 "exactly one string argument.", ci->key);
1136                 return (-1);
1137         }
1138
1139         string = strdup (ci->values[0].value.string);
1140         if (string == NULL)
1141                 return (-1);
1142
1143         if (*ret_string != NULL)
1144                 sfree (*ret_string);
1145         *ret_string = string;
1146
1147         return (0);
1148 } /* }}} int cf_util_get_string */
1149
1150 /* Assures the config option is a string and copies it to the provided buffer.
1151  * Assures null-termination. */
1152 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1153                 size_t buffer_size)
1154 {
1155         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1156                 return (EINVAL);
1157
1158         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1159         {
1160                 ERROR ("cf_util_get_string_buffer: The %s option requires "
1161                                 "exactly one string argument.", ci->key);
1162                 return (-1);
1163         }
1164
1165         strncpy (buffer, ci->values[0].value.string, buffer_size);
1166         buffer[buffer_size - 1] = 0;
1167
1168         return (0);
1169 } /* }}} int cf_util_get_string_buffer */
1170
1171 /* Assures the config option is a number and returns it as an int. */
1172 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1173 {
1174         if ((ci == NULL) || (ret_value == NULL))
1175                 return (EINVAL);
1176
1177         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1178         {
1179                 ERROR ("cf_util_get_int: The %s option requires "
1180                                 "exactly one numeric argument.", ci->key);
1181                 return (-1);
1182         }
1183
1184         *ret_value = (int) ci->values[0].value.number;
1185
1186         return (0);
1187 } /* }}} int cf_util_get_int */
1188
1189 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1190 {
1191         if ((ci == NULL) || (ret_value == NULL))
1192                 return (EINVAL);
1193
1194         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1195         {
1196                 ERROR ("cf_util_get_double: The %s option requires "
1197                                 "exactly one numeric argument.", ci->key);
1198                 return (-1);
1199         }
1200
1201         *ret_value = ci->values[0].value.number;
1202
1203         return (0);
1204 } /* }}} int cf_util_get_double */
1205
1206 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1207 {
1208         if ((ci == NULL) || (ret_bool == NULL))
1209                 return (EINVAL);
1210
1211         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1212         {
1213                 ERROR ("cf_util_get_boolean: The %s option requires "
1214                                 "exactly one boolean argument.", ci->key);
1215                 return (-1);
1216         }
1217
1218         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1219
1220         return (0);
1221 } /* }}} int cf_util_get_boolean */
1222
1223 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1224                 unsigned int *ret_value, unsigned int flag)
1225 {
1226         int status;
1227         _Bool b;
1228
1229         if (ret_value == NULL)
1230                 return (EINVAL);
1231
1232         b = 0;
1233         status = cf_util_get_boolean (ci, &b);
1234         if (status != 0)
1235                 return (status);
1236
1237         if (b)
1238         {
1239                 *ret_value |= flag;
1240         }
1241         else
1242         {
1243                 *ret_value &= ~flag;
1244         }
1245
1246         return (0);
1247 } /* }}} int cf_util_get_flag */
1248
1249 /* Assures that the config option is a string or a number if the correct range
1250  * of 1-65535. The string is then converted to a port number using
1251  * `service_name_to_port_number' and returned.
1252  * Returns the port number in the range [1-65535] or less than zero upon
1253  * failure. */
1254 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1255 {
1256         int tmp;
1257
1258         if ((ci->values_num != 1)
1259                         || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1260                                 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1261         {
1262                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1263                                 "exactly one string argument.", ci->key);
1264                 return (-1);
1265         }
1266
1267         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1268                 return (service_name_to_port_number (ci->values[0].value.string));
1269
1270         assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1271         tmp = (int) (ci->values[0].value.number + 0.5);
1272         if ((tmp < 1) || (tmp > 65535))
1273         {
1274                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1275                                 "a service name or a port number. The number "
1276                                 "you specified, %i, is not in the valid "
1277                                 "range of 1-65535.",
1278                                 ci->key, tmp);
1279                 return (-1);
1280         }
1281
1282         return (tmp);
1283 } /* }}} int cf_util_get_port_number */
1284
1285 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1286 {
1287         int port;
1288         char *service;
1289         int status;
1290
1291         if (ci->values_num != 1)
1292         {
1293                 ERROR ("cf_util_get_service: The %s option requires exactly "
1294                                 "one argument.", ci->key);
1295                 return (-1);
1296         }
1297
1298         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1299                 return (cf_util_get_string (ci, ret_string));
1300         if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1301         {
1302                 ERROR ("cf_util_get_service: The %s option requires "
1303                                 "exactly one string or numeric argument.",
1304                                 ci->key);
1305         }
1306
1307         port = 0;
1308         status = cf_util_get_int (ci, &port);
1309         if (status != 0)
1310                 return (status);
1311         else if ((port < 1) || (port > 65535))
1312         {
1313                 ERROR ("cf_util_get_service: The port number given "
1314                                 "for the %s option is out of "
1315                                 "range (%i).", ci->key, port);
1316                 return (-1);
1317         }
1318
1319         service = malloc (6);
1320         if (service == NULL)
1321         {
1322                 ERROR ("cf_util_get_service: Out of memory.");
1323                 return (-1);
1324         }
1325         ssnprintf (service, 6, "%i", port);
1326
1327         sfree (*ret_string);
1328         *ret_string = service;
1329
1330         return (0);
1331 } /* }}} int cf_util_get_service */
1332
1333 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1334 {
1335         if ((ci == NULL) || (ret_value == NULL))
1336                 return (EINVAL);
1337
1338         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1339         {
1340                 ERROR ("cf_util_get_cdtime: The %s option requires "
1341                                 "exactly one numeric argument.", ci->key);
1342                 return (-1);
1343         }
1344
1345         if (ci->values[0].value.number < 0.0)
1346         {
1347                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1348                                 "option must not be negative.", ci->key);
1349                 return (-1);
1350         }
1351
1352         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1353
1354         return (0);
1355 } /* }}} int cf_util_get_cdtime */
1356