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