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