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