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