Merge branch '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         ctx.flush_interval = 0;
294         ctx.flush_timeout = 0;
295
296         for (i = 0; i < ci->children_num; ++i)
297         {
298                 oconfig_item_t *child = ci->children + i;
299
300                 if (strcasecmp("Globals", child->key) == 0)
301                         cf_util_get_flag (child, &flags, PLUGIN_FLAGS_GLOBAL);
302                 else if (strcasecmp ("Interval", child->key) == 0)
303                         cf_util_get_cdtime (child, &ctx.interval);
304                 else if (strcasecmp ("FlushInterval", child->key) == 0)
305                         cf_util_get_cdtime (child, &ctx.flush_interval);
306                 else if (strcasecmp ("FlushTimeout", child->key) == 0)
307                         cf_util_get_cdtime (child, &ctx.flush_timeout);
308                 else {
309                         WARNING("Ignoring unknown LoadPlugin option \"%s\" "
310                                         "for plugin \"%s\"",
311                                         child->key, ci->values[0].value.string);
312                 }
313         }
314
315         old_ctx = plugin_set_ctx (ctx);
316         ret_val = plugin_load (name, (uint32_t) flags);
317         /* reset to the "global" context */
318         plugin_set_ctx (old_ctx);
319
320         return (ret_val);
321 } /* int dispatch_value_loadplugin */
322
323 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
324 {
325         char  buffer[4096];
326         char *buffer_ptr;
327         int   buffer_free;
328         int i;
329
330         buffer_ptr = buffer;
331         buffer_free = sizeof (buffer);
332
333         for (i = 0; i < ci->values_num; i++)
334         {
335                 int status = -1;
336
337                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
338                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
339                                         ci->values[i].value.string);
340                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
341                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
342                                         ci->values[i].value.number);
343                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
344                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
345                                         ci->values[i].value.boolean
346                                         ? "true" : "false");
347
348                 if ((status < 0) || (status >= buffer_free))
349                         return (-1);
350                 buffer_free -= status;
351                 buffer_ptr  += status;
352         }
353         /* skip the initial space */
354         buffer_ptr = buffer + 1;
355
356         return (cf_dispatch (plugin, ci->key, buffer_ptr));
357 } /* int dispatch_value_plugin */
358
359 static int dispatch_value (oconfig_item_t *ci)
360 {
361         int ret = -2;
362         int i;
363
364         for (i = 0; i < cf_value_map_num; i++)
365                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
366                 {
367                         ret = cf_value_map[i].func (ci);
368                         break;
369                 }
370
371         for (i = 0; i < cf_global_options_num; i++)
372                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
373                 {
374                         ret = dispatch_global_option (ci);
375                         break;
376                 }
377
378         return (ret);
379 } /* int dispatch_value */
380
381 static int dispatch_block_plugin (oconfig_item_t *ci)
382 {
383         int i;
384         char *name;
385
386         cf_complex_callback_t *cb;
387
388         if (strcasecmp (ci->key, "Plugin") != 0)
389                 return (-1);
390         if (ci->values_num < 1)
391                 return (-1);
392         if (ci->values[0].type != OCONFIG_TYPE_STRING)
393                 return (-1);
394
395         name = ci->values[0].value.string;
396         if (strcmp ("libvirt", name) == 0)
397         {
398                 /* TODO(octo): Remove this legacy. */
399                 WARNING ("The \"libvirt\" plugin has been renamed to \"virt\" to avoid problems with the build system. "
400                                 "Your configuration is still using the old name. "
401                                 "Please change it to use \"virt\" as soon as possible. "
402                                 "This compatibility code will go away eventually.");
403                 name = "virt";
404         }
405
406         if (IS_TRUE (global_option_get ("AutoLoadPlugin")))
407         {
408                 plugin_ctx_t ctx;
409                 plugin_ctx_t old_ctx;
410                 int status;
411
412                 /* default to the global interval set before loading this plugin */
413                 memset (&ctx, 0, sizeof (ctx));
414                 ctx.interval = cf_get_default_interval ();
415
416                 old_ctx = plugin_set_ctx (ctx);
417                 status = plugin_load (name, /* flags = */ 0);
418                 /* reset to the "global" context */
419                 plugin_set_ctx (old_ctx);
420
421                 if (status != 0)
422                 {
423                         ERROR ("Automatically loading plugin \"%s\" failed "
424                                         "with status %i.", name, status);
425                         return (status);
426                 }
427         }
428
429         /* Check for a complex callback first */
430         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
431         {
432                 if (strcasecmp (name, cb->type) == 0)
433                 {
434                         plugin_ctx_t old_ctx;
435                         int ret_val;
436
437                         old_ctx = plugin_set_ctx (cb->ctx);
438                         ret_val = (cb->callback (ci));
439                         plugin_set_ctx (old_ctx);
440                         return (ret_val);
441                 }
442         }
443
444         /* Hm, no complex plugin found. Dispatch the values one by one */
445         for (i = 0; i < ci->children_num; i++)
446         {
447                 if (ci->children[i].children == NULL)
448                         dispatch_value_plugin (name, ci->children + i);
449                 else
450                 {
451                         WARNING ("There is a `%s' block within the "
452                                         "configuration for the %s plugin. "
453                                         "The plugin either only expects "
454                                         "\"simple\" configuration statements "
455                                         "or wasn't loaded using `LoadPlugin'."
456                                         " Please check your configuration.",
457                                         ci->children[i].key, name);
458                 }
459         }
460
461         return (0);
462 }
463
464
465 static int dispatch_block (oconfig_item_t *ci)
466 {
467         if (strcasecmp (ci->key, "LoadPlugin") == 0)
468                 return (dispatch_loadplugin (ci));
469         else if (strcasecmp (ci->key, "Plugin") == 0)
470                 return (dispatch_block_plugin (ci));
471         else if (strcasecmp (ci->key, "Chain") == 0)
472                 return (fc_configure (ci));
473
474         return (0);
475 }
476
477 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
478                 int offset)
479 {
480         oconfig_item_t *temp;
481         int i;
482
483         assert (offset >= 0);
484         assert (dst->children_num > offset);
485
486         /* Free the memory used by the replaced child. Usually that's the
487          * `Include "blah"' statement. */
488         temp = dst->children + offset;
489         for (i = 0; i < temp->values_num; i++)
490         {
491                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
492                 {
493                         sfree (temp->values[i].value.string);
494                 }
495         }
496         sfree (temp->values);
497         temp = NULL;
498
499         /* If (src->children_num == 0) the array size is decreased. If offset
500          * is _not_ the last element, (offset < (dst->children_num - 1)), then
501          * we need to move the trailing elements before resizing the array. */
502         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
503         {
504                 int nmemb = dst->children_num - (offset + 1);
505                 memmove (dst->children + offset, dst->children + offset + 1,
506                                 sizeof (oconfig_item_t) * nmemb);
507         }
508
509         /* Resize the memory containing the children to be big enough to hold
510          * all children. */
511         if (dst->children_num + src->children_num - 1 == 0)
512         {
513                 dst->children_num = 0;
514                 return (0);
515         }
516
517         temp = (oconfig_item_t *) realloc (dst->children,
518                         sizeof (oconfig_item_t)
519                         * (dst->children_num + src->children_num - 1));
520         if (temp == NULL)
521         {
522                 ERROR ("configfile: realloc failed.");
523                 return (-1);
524         }
525         dst->children = temp;
526
527         /* If there are children behind the include statement, and they have
528          * not yet been moved because (src->children_num == 0), then move them
529          * to the end of the list, so that the new children have room before
530          * them. */
531         if ((src->children_num > 0)
532                         && ((dst->children_num - (offset + 1)) > 0))
533         {
534                 int nmemb = dst->children_num - (offset + 1);
535                 int old_offset = offset + 1;
536                 int new_offset = offset + src->children_num;
537
538                 memmove (dst->children + new_offset,
539                                 dst->children + old_offset,
540                                 sizeof (oconfig_item_t) * nmemb);
541         }
542
543         /* Last but not least: If there are new children, copy them to the
544          * memory reserved for them. */
545         if (src->children_num > 0)
546         {
547                 memcpy (dst->children + offset,
548                                 src->children,
549                                 sizeof (oconfig_item_t) * src->children_num);
550         }
551
552         /* Update the number of children. */
553         dst->children_num += (src->children_num - 1);
554
555         return (0);
556 } /* int cf_ci_replace_child */
557
558 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
559 {
560         oconfig_item_t *temp;
561
562         if ((src == NULL) || (src->children_num == 0))
563                 return (0);
564
565         temp = (oconfig_item_t *) realloc (dst->children,
566                         sizeof (oconfig_item_t)
567                         * (dst->children_num + src->children_num));
568         if (temp == NULL)
569         {
570                 ERROR ("configfile: realloc failed.");
571                 return (-1);
572         }
573         dst->children = temp;
574
575         memcpy (dst->children + dst->children_num,
576                         src->children,
577                         sizeof (oconfig_item_t)
578                         * src->children_num);
579         dst->children_num += src->children_num;
580
581         return (0);
582 } /* int cf_ci_append_children */
583
584 #define CF_MAX_DEPTH 8
585 static oconfig_item_t *cf_read_generic (const char *path,
586                 const char *pattern, int depth);
587
588 static int cf_include_all (oconfig_item_t *root, int depth)
589 {
590         int i;
591
592         for (i = 0; i < root->children_num; i++)
593         {
594                 oconfig_item_t *new;
595                 oconfig_item_t *old;
596
597                 char *pattern = NULL;
598
599                 int j;
600
601                 if (strcasecmp (root->children[i].key, "Include") != 0)
602                         continue;
603
604                 old = root->children + i;
605
606                 if ((old->values_num != 1)
607                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
608                 {
609                         ERROR ("configfile: `Include' needs exactly one string argument.");
610                         continue;
611                 }
612
613                 for (j = 0; j < old->children_num; ++j)
614                 {
615                         oconfig_item_t *child = old->children + j;
616
617                         if (strcasecmp (child->key, "Filter") == 0)
618                                 cf_util_get_string (child, &pattern);
619                         else
620                                 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
621                                                 child->key);
622                 }
623
624                 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
625                 sfree (pattern);
626
627                 if (new == NULL)
628                         return (-1);
629
630                 /* Now replace the i'th child in `root' with `new'. */
631                 if (cf_ci_replace_child (root, new, i) < 0)
632                         return (-1);
633
634                 /* ... and go back to the new i'th child. */
635                 --i;
636
637                 sfree (new->values);
638                 sfree (new);
639         } /* for (i = 0; i < root->children_num; i++) */
640
641         return (0);
642 } /* int cf_include_all */
643
644 static oconfig_item_t *cf_read_file (const char *file,
645                 const char *pattern, int depth)
646 {
647         oconfig_item_t *root;
648         int status;
649
650         assert (depth < CF_MAX_DEPTH);
651
652         if (pattern != NULL) {
653 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
654                 char *tmp = sstrdup (file);
655                 char *filename = basename (tmp);
656
657                 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
658                         DEBUG ("configfile: Not including `%s' because it "
659                                         "does not match pattern `%s'.",
660                                         filename, pattern);
661                         free (tmp);
662                         return (NULL);
663                 }
664
665                 free (tmp);
666 #else
667                 ERROR ("configfile: Cannot apply pattern filter '%s' "
668                                 "to file '%s': functions basename() and / or "
669                                 "fnmatch() not available.", pattern, file);
670 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
671         }
672
673         root = oconfig_parse_file (file);
674         if (root == NULL)
675         {
676                 ERROR ("configfile: Cannot read file `%s'.", file);
677                 return (NULL);
678         }
679
680         status = cf_include_all (root, depth);
681         if (status != 0)
682         {
683                 oconfig_free (root);
684                 return (NULL);
685         }
686
687         return (root);
688 } /* oconfig_item_t *cf_read_file */
689
690 static int cf_compare_string (const void *p1, const void *p2)
691 {
692         return strcmp (*(const char **) p1, *(const char **) p2);
693 }
694
695 static oconfig_item_t *cf_read_dir (const char *dir,
696                 const char *pattern, int depth)
697 {
698         oconfig_item_t *root = NULL;
699         DIR *dh;
700         struct dirent *de;
701         char **filenames = NULL;
702         int filenames_num = 0;
703         int status;
704         int i;
705
706         assert (depth < CF_MAX_DEPTH);
707
708         dh = opendir (dir);
709         if (dh == NULL)
710         {
711                 char errbuf[1024];
712                 ERROR ("configfile: opendir failed: %s",
713                                 sstrerror (errno, errbuf, sizeof (errbuf)));
714                 return (NULL);
715         }
716
717         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
718         if (root == NULL)
719         {
720                 ERROR ("configfile: malloc failed.");
721                 return (NULL);
722         }
723         memset (root, 0, sizeof (oconfig_item_t));
724
725         while ((de = readdir (dh)) != NULL)
726         {
727                 char   name[1024];
728                 char **tmp;
729
730                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
731                         continue;
732
733                 status = ssnprintf (name, sizeof (name), "%s/%s",
734                                 dir, de->d_name);
735                 if ((status < 0) || ((size_t) status >= sizeof (name)))
736                 {
737                         ERROR ("configfile: Not including `%s/%s' because its"
738                                         " name is too long.",
739                                         dir, de->d_name);
740                         for (i = 0; i < filenames_num; ++i)
741                                 free (filenames[i]);
742                         free (filenames);
743                         free (root);
744                         return (NULL);
745                 }
746
747                 ++filenames_num;
748                 tmp = (char **) realloc (filenames,
749                                 filenames_num * sizeof (*filenames));
750                 if (tmp == NULL) {
751                         ERROR ("configfile: realloc failed.");
752                         for (i = 0; i < filenames_num - 1; ++i)
753                                 free (filenames[i]);
754                         free (filenames);
755                         free (root);
756                         return (NULL);
757                 }
758                 filenames = tmp;
759
760                 filenames[filenames_num - 1] = sstrdup (name);
761         }
762
763         if (filenames == NULL)
764                 return (root);
765
766         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
767                         cf_compare_string);
768
769         for (i = 0; i < filenames_num; ++i)
770         {
771                 oconfig_item_t *temp;
772                 char *name = filenames[i];
773
774                 temp = cf_read_generic (name, pattern, depth);
775                 if (temp == NULL)
776                 {
777                         /* An error should already have been reported. */
778                         sfree (name);
779                         continue;
780                 }
781
782                 cf_ci_append_children (root, temp);
783                 sfree (temp->children);
784                 sfree (temp);
785
786                 free (name);
787         }
788
789         free(filenames);
790         return (root);
791 } /* oconfig_item_t *cf_read_dir */
792
793 /* 
794  * cf_read_generic
795  *
796  * Path is stat'ed and either cf_read_file or cf_read_dir is called
797  * accordingly.
798  *
799  * There are two versions of this function: If `wordexp' exists shell wildcards
800  * will be expanded and the function will include all matches found. If
801  * `wordexp' (or, more precisely, it's header file) is not available the
802  * simpler function is used which does not do any such expansion.
803  */
804 #if HAVE_WORDEXP_H
805 static oconfig_item_t *cf_read_generic (const char *path,
806                 const char *pattern, int depth)
807 {
808         oconfig_item_t *root = NULL;
809         int status;
810         const char *path_ptr;
811         wordexp_t we;
812         size_t i;
813
814         if (depth >= CF_MAX_DEPTH)
815         {
816                 ERROR ("configfile: Not including `%s' because the maximum "
817                                 "nesting depth has been reached.", path);
818                 return (NULL);
819         }
820
821         status = wordexp (path, &we, WRDE_NOCMD);
822         if (status != 0)
823         {
824                 ERROR ("configfile: wordexp (%s) failed.", path);
825                 return (NULL);
826         }
827
828         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
829         if (root == NULL)
830         {
831                 ERROR ("configfile: malloc failed.");
832                 return (NULL);
833         }
834         memset (root, '\0', sizeof (oconfig_item_t));
835
836         /* wordexp() might return a sorted list already. That's not
837          * documented though, so let's make sure we get what we want. */
838         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
839                         cf_compare_string);
840
841         for (i = 0; i < we.we_wordc; i++)
842         {
843                 oconfig_item_t *temp;
844                 struct stat statbuf;
845
846                 path_ptr = we.we_wordv[i];
847
848                 status = stat (path_ptr, &statbuf);
849                 if (status != 0)
850                 {
851                         char errbuf[1024];
852                         WARNING ("configfile: stat (%s) failed: %s",
853                                         path_ptr,
854                                         sstrerror (errno, errbuf, sizeof (errbuf)));
855                         continue;
856                 }
857
858                 if (S_ISREG (statbuf.st_mode))
859                         temp = cf_read_file (path_ptr, pattern, depth);
860                 else if (S_ISDIR (statbuf.st_mode))
861                         temp = cf_read_dir (path_ptr, pattern, depth);
862                 else
863                 {
864                         WARNING ("configfile: %s is neither a file nor a "
865                                         "directory.", path);
866                         continue;
867                 }
868
869                 if (temp == NULL) {
870                         oconfig_free (root);
871                         return (NULL);
872                 }
873
874                 cf_ci_append_children (root, temp);
875                 sfree (temp->children);
876                 sfree (temp);
877         }
878
879         wordfree (&we);
880
881         return (root);
882 } /* oconfig_item_t *cf_read_generic */
883 /* #endif HAVE_WORDEXP_H */
884
885 #else /* if !HAVE_WORDEXP_H */
886 static oconfig_item_t *cf_read_generic (const char *path,
887                 const char *pattern, int depth)
888 {
889         struct stat statbuf;
890         int status;
891
892         if (depth >= CF_MAX_DEPTH)
893         {
894                 ERROR ("configfile: Not including `%s' because the maximum "
895                                 "nesting depth has been reached.", path);
896                 return (NULL);
897         }
898
899         status = stat (path, &statbuf);
900         if (status != 0)
901         {
902                 char errbuf[1024];
903                 ERROR ("configfile: stat (%s) failed: %s",
904                                 path,
905                                 sstrerror (errno, errbuf, sizeof (errbuf)));
906                 return (NULL);
907         }
908
909         if (S_ISREG (statbuf.st_mode))
910                 return (cf_read_file (path, pattern, depth));
911         else if (S_ISDIR (statbuf.st_mode))
912                 return (cf_read_dir (path, pattern, depth));
913
914         ERROR ("configfile: %s is neither a file nor a directory.", path);
915         return (NULL);
916 } /* oconfig_item_t *cf_read_generic */
917 #endif /* !HAVE_WORDEXP_H */
918
919 /* 
920  * Public functions
921  */
922 int global_option_set (const char *option, const char *value)
923 {
924         int i;
925
926         DEBUG ("option = %s; value = %s;", option, value);
927
928         for (i = 0; i < cf_global_options_num; i++)
929                 if (strcasecmp (cf_global_options[i].key, option) == 0)
930                         break;
931
932         if (i >= cf_global_options_num)
933                 return (-1);
934
935         if (strcasecmp (option, "PIDFile") == 0 && pidfile_from_cli == 1)
936         {
937                 DEBUG ("Configfile: Ignoring `PIDFILE' option because "
938                         "command-line option `-P' take precedence.");
939                 return (0);
940         }
941
942         sfree (cf_global_options[i].value);
943
944         if (value != NULL)
945                 cf_global_options[i].value = strdup (value);
946         else
947                 cf_global_options[i].value = NULL;
948
949         return (0);
950 }
951
952 const char *global_option_get (const char *option)
953 {
954         int i;
955
956         for (i = 0; i < cf_global_options_num; i++)
957                 if (strcasecmp (cf_global_options[i].key, option) == 0)
958                         break;
959
960         if (i >= cf_global_options_num)
961                 return (NULL);
962         
963         return ((cf_global_options[i].value != NULL)
964                         ? cf_global_options[i].value
965                         : cf_global_options[i].def);
966 } /* char *global_option_get */
967
968 long global_option_get_long (const char *option, long default_value)
969 {
970         const char *str;
971         long value;
972
973         str = global_option_get (option);
974         if (NULL == str)
975                 return (default_value);
976
977         errno = 0;
978         value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
979         if (errno != 0)
980                 return (default_value);
981
982         return (value);
983 } /* char *global_option_get_long */
984
985 cdtime_t global_option_get_time (const char *name, cdtime_t def) /* {{{ */
986 {
987         char const *optstr;
988         char *endptr = NULL;
989         double v;
990
991         optstr = global_option_get (name);
992         if (optstr == NULL)
993                 return (def);
994
995         errno = 0;
996         v = strtod (optstr, &endptr);
997         if ((endptr == NULL) || (*endptr != 0) || (errno != 0))
998                 return (def);
999         else if (v <= 0.0)
1000                 return (def);
1001
1002         return (DOUBLE_TO_CDTIME_T (v));
1003 } /* }}} cdtime_t global_option_get_time */
1004
1005 cdtime_t cf_get_default_interval (void)
1006 {
1007         return (global_option_get_time ("Interval",
1008                                DOUBLE_TO_CDTIME_T (COLLECTD_DEFAULT_INTERVAL)));
1009 }
1010
1011 void cf_unregister (const char *type)
1012 {
1013         cf_callback_t *this, *prev;
1014
1015         for (prev = NULL, this = first_callback;
1016                         this != NULL;
1017                         prev = this, this = this->next)
1018                 if (strcasecmp (this->type, type) == 0)
1019                 {
1020                         if (prev == NULL)
1021                                 first_callback = this->next;
1022                         else
1023                                 prev->next = this->next;
1024
1025                         free (this);
1026                         break;
1027                 }
1028 } /* void cf_unregister */
1029
1030 void cf_unregister_complex (const char *type)
1031 {
1032         cf_complex_callback_t *this, *prev;
1033
1034         for (prev = NULL, this = complex_callback_head;
1035                         this != NULL;
1036                         prev = this, this = this->next)
1037                 if (strcasecmp (this->type, type) == 0)
1038                 {
1039                         if (prev == NULL)
1040                                 complex_callback_head = this->next;
1041                         else
1042                                 prev->next = this->next;
1043
1044                         sfree (this->type);
1045                         sfree (this);
1046                         break;
1047                 }
1048 } /* void cf_unregister */
1049
1050 void cf_register (const char *type,
1051                 int (*callback) (const char *, const char *),
1052                 const char **keys, int keys_num)
1053 {
1054         cf_callback_t *cf_cb;
1055
1056         /* Remove this module from the list, if it already exists */
1057         cf_unregister (type);
1058
1059         /* This pointer will be free'd in `cf_unregister' */
1060         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
1061                 return;
1062
1063         cf_cb->type     = type;
1064         cf_cb->callback = callback;
1065         cf_cb->keys     = keys;
1066         cf_cb->keys_num = keys_num;
1067         cf_cb->ctx      = plugin_get_ctx ();
1068
1069         cf_cb->next = first_callback;
1070         first_callback = cf_cb;
1071 } /* void cf_register */
1072
1073 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
1074 {
1075         cf_complex_callback_t *new;
1076
1077         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
1078         if (new == NULL)
1079                 return (-1);
1080
1081         new->type = strdup (type);
1082         if (new->type == NULL)
1083         {
1084                 sfree (new);
1085                 return (-1);
1086         }
1087
1088         new->callback = callback;
1089         new->next = NULL;
1090
1091         new->ctx = plugin_get_ctx ();
1092
1093         if (complex_callback_head == NULL)
1094         {
1095                 complex_callback_head = new;
1096         }
1097         else
1098         {
1099                 cf_complex_callback_t *last = complex_callback_head;
1100                 while (last->next != NULL)
1101                         last = last->next;
1102                 last->next = new;
1103         }
1104
1105         return (0);
1106 } /* int cf_register_complex */
1107
1108 int cf_read (char *filename)
1109 {
1110         oconfig_item_t *conf;
1111         int i;
1112
1113         conf = cf_read_generic (filename, /* pattern = */ NULL, /* depth = */ 0);
1114         if (conf == NULL)
1115         {
1116                 ERROR ("Unable to read config file %s.", filename);
1117                 return (-1);
1118         }
1119         else if (conf->children_num == 0)
1120         {
1121                 ERROR ("Configuration file %s is empty.", filename);
1122                 oconfig_free (conf);
1123                 return (-1);
1124         }
1125
1126         for (i = 0; i < conf->children_num; i++)
1127         {
1128                 if (conf->children[i].children == NULL)
1129                         dispatch_value (conf->children + i);
1130                 else
1131                         dispatch_block (conf->children + i);
1132         }
1133
1134         oconfig_free (conf);
1135
1136         /* Read the default types.db if no `TypesDB' option was given. */
1137         if (cf_default_typesdb)
1138                 read_types_list (PKGDATADIR"/types.db");
1139
1140         return (0);
1141 } /* int cf_read */
1142
1143 /* Assures the config option is a string, duplicates it and returns the copy in
1144  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1145  * success. */
1146 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1147 {
1148         char *string;
1149
1150         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1151         {
1152                 ERROR ("cf_util_get_string: The %s option requires "
1153                                 "exactly one string argument.", ci->key);
1154                 return (-1);
1155         }
1156
1157         string = strdup (ci->values[0].value.string);
1158         if (string == NULL)
1159                 return (-1);
1160
1161         if (*ret_string != NULL)
1162                 sfree (*ret_string);
1163         *ret_string = string;
1164
1165         return (0);
1166 } /* }}} int cf_util_get_string */
1167
1168 /* Assures the config option is a string and copies it to the provided buffer.
1169  * Assures null-termination. */
1170 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1171                 size_t buffer_size)
1172 {
1173         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1174                 return (EINVAL);
1175
1176         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1177         {
1178                 ERROR ("cf_util_get_string_buffer: The %s option requires "
1179                                 "exactly one string argument.", ci->key);
1180                 return (-1);
1181         }
1182
1183         strncpy (buffer, ci->values[0].value.string, buffer_size);
1184         buffer[buffer_size - 1] = 0;
1185
1186         return (0);
1187 } /* }}} int cf_util_get_string_buffer */
1188
1189 /* Assures the config option is a number and returns it as an int. */
1190 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1191 {
1192         if ((ci == NULL) || (ret_value == NULL))
1193                 return (EINVAL);
1194
1195         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1196         {
1197                 ERROR ("cf_util_get_int: The %s option requires "
1198                                 "exactly one numeric argument.", ci->key);
1199                 return (-1);
1200         }
1201
1202         *ret_value = (int) ci->values[0].value.number;
1203
1204         return (0);
1205 } /* }}} int cf_util_get_int */
1206
1207 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1208 {
1209         if ((ci == NULL) || (ret_value == NULL))
1210                 return (EINVAL);
1211
1212         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1213         {
1214                 ERROR ("cf_util_get_double: The %s option requires "
1215                                 "exactly one numeric argument.", ci->key);
1216                 return (-1);
1217         }
1218
1219         *ret_value = ci->values[0].value.number;
1220
1221         return (0);
1222 } /* }}} int cf_util_get_double */
1223
1224 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1225 {
1226         if ((ci == NULL) || (ret_bool == NULL))
1227                 return (EINVAL);
1228
1229         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1230         {
1231                 ERROR ("cf_util_get_boolean: The %s option requires "
1232                                 "exactly one boolean argument.", ci->key);
1233                 return (-1);
1234         }
1235
1236         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1237
1238         return (0);
1239 } /* }}} int cf_util_get_boolean */
1240
1241 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1242                 unsigned int *ret_value, unsigned int flag)
1243 {
1244         int status;
1245         _Bool b;
1246
1247         if (ret_value == NULL)
1248                 return (EINVAL);
1249
1250         b = 0;
1251         status = cf_util_get_boolean (ci, &b);
1252         if (status != 0)
1253                 return (status);
1254
1255         if (b)
1256         {
1257                 *ret_value |= flag;
1258         }
1259         else
1260         {
1261                 *ret_value &= ~flag;
1262         }
1263
1264         return (0);
1265 } /* }}} int cf_util_get_flag */
1266
1267 /* Assures that the config option is a string or a number if the correct range
1268  * of 1-65535. The string is then converted to a port number using
1269  * `service_name_to_port_number' and returned.
1270  * Returns the port number in the range [1-65535] or less than zero upon
1271  * failure. */
1272 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1273 {
1274         int tmp;
1275
1276         if ((ci->values_num != 1)
1277                         || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1278                                 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1279         {
1280                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1281                                 "exactly one string argument.", ci->key);
1282                 return (-1);
1283         }
1284
1285         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1286                 return (service_name_to_port_number (ci->values[0].value.string));
1287
1288         assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1289         tmp = (int) (ci->values[0].value.number + 0.5);
1290         if ((tmp < 1) || (tmp > 65535))
1291         {
1292                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1293                                 "a service name or a port number. The number "
1294                                 "you specified, %i, is not in the valid "
1295                                 "range of 1-65535.",
1296                                 ci->key, tmp);
1297                 return (-1);
1298         }
1299
1300         return (tmp);
1301 } /* }}} int cf_util_get_port_number */
1302
1303 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1304 {
1305         int port;
1306         char *service;
1307         int status;
1308
1309         if (ci->values_num != 1)
1310         {
1311                 ERROR ("cf_util_get_service: The %s option requires exactly "
1312                                 "one argument.", ci->key);
1313                 return (-1);
1314         }
1315
1316         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1317                 return (cf_util_get_string (ci, ret_string));
1318         if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1319         {
1320                 ERROR ("cf_util_get_service: The %s option requires "
1321                                 "exactly one string or numeric argument.",
1322                                 ci->key);
1323         }
1324
1325         port = 0;
1326         status = cf_util_get_int (ci, &port);
1327         if (status != 0)
1328                 return (status);
1329         else if ((port < 1) || (port > 65535))
1330         {
1331                 ERROR ("cf_util_get_service: The port number given "
1332                                 "for the %s option is out of "
1333                                 "range (%i).", ci->key, port);
1334                 return (-1);
1335         }
1336
1337         service = malloc (6);
1338         if (service == NULL)
1339         {
1340                 ERROR ("cf_util_get_service: Out of memory.");
1341                 return (-1);
1342         }
1343         ssnprintf (service, 6, "%i", port);
1344
1345         sfree (*ret_string);
1346         *ret_string = service;
1347
1348         return (0);
1349 } /* }}} int cf_util_get_service */
1350
1351 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1352 {
1353         if ((ci == NULL) || (ret_value == NULL))
1354                 return (EINVAL);
1355
1356         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1357         {
1358                 ERROR ("cf_util_get_cdtime: The %s option requires "
1359                                 "exactly one numeric argument.", ci->key);
1360                 return (-1);
1361         }
1362
1363         if (ci->values[0].value.number < 0.0)
1364         {
1365                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1366                                 "option must not be negative.", ci->key);
1367                 return (-1);
1368         }
1369
1370         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1371
1372         return (0);
1373 } /* }}} int cf_util_get_cdtime */
1374