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