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