In rrd synchronous file creation use lock_file
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2011  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at collectd.org>
21  *   Sebastian tokkee Harl <sh at tokkee.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "liboconfig/oconfig.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
33
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
37
38 #if HAVE_FNMATCH_H
39 # include <fnmatch.h>
40 #endif /* HAVE_FNMATCH_H */
41
42 #if HAVE_LIBGEN_H
43 # include <libgen.h>
44 #endif /* HAVE_LIBGEN_H */
45
46 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
47
48 /*
49  * Private types
50  */
51 typedef struct cf_callback
52 {
53         const char  *type;
54         int  (*callback) (const char *, const char *);
55         const char **keys;
56         int    keys_num;
57         plugin_ctx_t ctx;
58         struct cf_callback *next;
59 } cf_callback_t;
60
61 typedef struct cf_complex_callback_s
62 {
63         char *type;
64         int (*callback) (oconfig_item_t *);
65         plugin_ctx_t ctx;
66         struct cf_complex_callback_s *next;
67 } cf_complex_callback_t;
68
69 typedef struct cf_value_map_s
70 {
71         char *key;
72         int (*func) (oconfig_item_t *);
73 } cf_value_map_t;
74
75 typedef struct cf_global_option_s
76 {
77         char *key;
78         char *value;
79         char *def;
80 } cf_global_option_t;
81
82 /*
83  * Prototypes of callback functions
84  */
85 static int dispatch_value_typesdb (oconfig_item_t *ci);
86 static int dispatch_value_plugindir (oconfig_item_t *ci);
87 static int dispatch_loadplugin (oconfig_item_t *ci);
88 static int dispatch_block_plugin (oconfig_item_t *ci);
89
90 /*
91  * Private variables
92  */
93 static cf_callback_t *first_callback = NULL;
94 static cf_complex_callback_t *complex_callback_head = NULL;
95
96 static cf_value_map_t cf_value_map[] =
97 {
98         {"TypesDB",    dispatch_value_typesdb},
99         {"PluginDir",  dispatch_value_plugindir},
100         {"LoadPlugin", dispatch_loadplugin},
101         {"Plugin",     dispatch_block_plugin}
102 };
103 static int cf_value_map_num = STATIC_ARRAY_SIZE (cf_value_map);
104
105 static cf_global_option_t cf_global_options[] =
106 {
107         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
108         {"PIDFile",     NULL, PIDFILE},
109         {"Hostname",    NULL, NULL},
110         {"FQDNLookup",  NULL, "true"},
111         {"Interval",    NULL, NULL},
112         {"ReadThreads", NULL, "5"},
113         {"WriteThreads", NULL, "5"},
114         {"WriteQueueLimitHigh", NULL, NULL},
115         {"WriteQueueLimitLow", NULL, NULL},
116         {"Timeout",     NULL, "2"},
117         {"AutoLoadPlugin", NULL, "false"},
118         {"PreCacheChain",  NULL, "PreCache"},
119         {"PostCacheChain", NULL, "PostCache"}
120 };
121 static int cf_global_options_num = STATIC_ARRAY_SIZE (cf_global_options);
122
123 static int cf_default_typesdb = 1;
124
125 /*
126  * Functions to handle register/unregister, search, and other plugin related
127  * stuff
128  */
129 static cf_callback_t *cf_search (const char *type)
130 {
131         cf_callback_t *cf_cb;
132
133         if (type == NULL)
134                 return (NULL);
135
136         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
137                 if (strcasecmp (cf_cb->type, type) == 0)
138                         break;
139
140         return (cf_cb);
141 }
142
143 static int cf_dispatch (const char *type, const char *orig_key,
144                 const char *orig_value)
145 {
146         cf_callback_t *cf_cb;
147         plugin_ctx_t old_ctx;
148         char *key;
149         char *value;
150         int ret;
151         int i;
152
153         DEBUG ("type = %s, key = %s, value = %s",
154                         ESCAPE_NULL(type),
155                         ESCAPE_NULL(orig_key),
156                         ESCAPE_NULL(orig_value));
157
158         if ((cf_cb = cf_search (type)) == NULL)
159         {
160                 WARNING ("Found a configuration for the `%s' plugin, but "
161                                 "the plugin isn't loaded or didn't register "
162                                 "a configuration callback.", type);
163                 return (-1);
164         }
165
166         if ((key = strdup (orig_key)) == NULL)
167                 return (1);
168         if ((value = strdup (orig_value)) == NULL)
169         {
170                 free (key);
171                 return (2);
172         }
173
174         ret = -1;
175
176         old_ctx = plugin_set_ctx (cf_cb->ctx);
177
178         for (i = 0; i < cf_cb->keys_num; i++)
179         {
180                 if ((cf_cb->keys[i] != NULL)
181                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
182                 {
183                         ret = (*cf_cb->callback) (key, value);
184                         break;
185                 }
186         }
187
188         plugin_set_ctx (old_ctx);
189
190         if (i >= cf_cb->keys_num)
191                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
192
193         free (key);
194         free (value);
195
196         DEBUG ("cf_dispatch: return (%i)", ret);
197
198         return (ret);
199 } /* int cf_dispatch */
200
201 static int dispatch_global_option (const oconfig_item_t *ci)
202 {
203         if (ci->values_num != 1)
204                 return (-1);
205         if (ci->values[0].type == OCONFIG_TYPE_STRING)
206                 return (global_option_set (ci->key, ci->values[0].value.string));
207         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
208         {
209                 char tmp[128];
210                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
211                 return (global_option_set (ci->key, tmp));
212         }
213         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
214         {
215                 if (ci->values[0].value.boolean)
216                         return (global_option_set (ci->key, "true"));
217                 else
218                         return (global_option_set (ci->key, "false"));
219         }
220
221         return (-1);
222 } /* int dispatch_global_option */
223
224 static int dispatch_value_typesdb (oconfig_item_t *ci)
225 {
226         int i = 0;
227
228         assert (strcasecmp (ci->key, "TypesDB") == 0);
229
230         cf_default_typesdb = 0;
231
232         if (ci->values_num < 1) {
233                 ERROR ("configfile: `TypesDB' needs at least one argument.");
234                 return (-1);
235         }
236
237         for (i = 0; i < ci->values_num; ++i)
238         {
239                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
240                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
241                                         "is not a string.", i + 1);
242                         continue;
243                 }
244
245                 read_types_list (ci->values[i].value.string);
246         }
247         return (0);
248 } /* int dispatch_value_typesdb */
249
250 static int dispatch_value_plugindir (oconfig_item_t *ci)
251 {
252         assert (strcasecmp (ci->key, "PluginDir") == 0);
253         
254         if (ci->values_num != 1)
255                 return (-1);
256         if (ci->values[0].type != OCONFIG_TYPE_STRING)
257                 return (-1);
258
259         plugin_set_dir (ci->values[0].value.string);
260         return (0);
261 }
262
263 static int dispatch_loadplugin (oconfig_item_t *ci)
264 {
265         int i;
266         const char *name;
267         unsigned int flags = 0;
268         plugin_ctx_t ctx;
269         plugin_ctx_t old_ctx;
270         int ret_val;
271
272         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
273
274         if (ci->values_num != 1)
275                 return (-1);
276         if (ci->values[0].type != OCONFIG_TYPE_STRING)
277                 return (-1);
278
279         name = ci->values[0].value.string;
280
281         /* default to the global interval set before loading this plugin */
282         memset (&ctx, 0, sizeof (ctx));
283         ctx.interval = cf_get_default_interval ();
284
285         for (i = 0; i < ci->children_num; ++i) {
286                 if (strcasecmp("Globals", ci->children[i].key) == 0)
287                         cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
288                 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
289                         double interval = 0.0;
290
291                         if (cf_util_get_double (ci->children + i, &interval) != 0) {
292                                 /* cf_util_get_double will log an error */
293                                 continue;
294                         }
295
296                         ctx.interval = DOUBLE_TO_CDTIME_T (interval);
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 (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                 plugin_ctx_t ctx;
390                 plugin_ctx_t old_ctx;
391                 int status;
392
393                 /* default to the global interval set before loading this plugin */
394                 memset (&ctx, 0, sizeof (ctx));
395                 ctx.interval = cf_get_default_interval ();
396
397                 old_ctx = plugin_set_ctx (ctx);
398                 status = plugin_load (name, /* flags = */ 0);
399                 /* reset to the "global" context */
400                 plugin_set_ctx (old_ctx);
401
402                 if (status != 0)
403                 {
404                         ERROR ("Automatically loading plugin \"%s\" failed "
405                                         "with status %i.", name, status);
406                         return (status);
407                 }
408         }
409
410         /* Check for a complex callback first */
411         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
412         {
413                 if (strcasecmp (name, cb->type) == 0)
414                 {
415                         plugin_ctx_t old_ctx;
416                         int ret_val;
417
418                         old_ctx = plugin_set_ctx (cb->ctx);
419                         ret_val = (cb->callback (ci));
420                         plugin_set_ctx (old_ctx);
421                         return (ret_val);
422                 }
423         }
424
425         /* Hm, no complex plugin found. Dispatch the values one by one */
426         for (i = 0; i < ci->children_num; i++)
427         {
428                 if (ci->children[i].children == NULL)
429                         dispatch_value_plugin (name, ci->children + i);
430                 else
431                 {
432                         WARNING ("There is a `%s' block within the "
433                                         "configuration for the %s plugin. "
434                                         "The plugin either only expects "
435                                         "\"simple\" configuration statements "
436                                         "or wasn't loaded using `LoadPlugin'."
437                                         " Please check your configuration.",
438                                         ci->children[i].key, name);
439                 }
440         }
441
442         return (0);
443 }
444
445
446 static int dispatch_block (oconfig_item_t *ci)
447 {
448         if (strcasecmp (ci->key, "LoadPlugin") == 0)
449                 return (dispatch_loadplugin (ci));
450         else if (strcasecmp (ci->key, "Plugin") == 0)
451                 return (dispatch_block_plugin (ci));
452         else if (strcasecmp (ci->key, "Chain") == 0)
453                 return (fc_configure (ci));
454
455         return (0);
456 }
457
458 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
459                 int offset)
460 {
461         oconfig_item_t *temp;
462         int i;
463
464         assert (offset >= 0);
465         assert (dst->children_num > offset);
466
467         /* Free the memory used by the replaced child. Usually that's the
468          * `Include "blah"' statement. */
469         temp = dst->children + offset;
470         for (i = 0; i < temp->values_num; i++)
471         {
472                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
473                 {
474                         sfree (temp->values[i].value.string);
475                 }
476         }
477         sfree (temp->values);
478         temp = NULL;
479
480         /* If (src->children_num == 0) the array size is decreased. If offset
481          * is _not_ the last element, (offset < (dst->children_num - 1)), then
482          * we need to move the trailing elements before resizing the array. */
483         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
484         {
485                 int nmemb = dst->children_num - (offset + 1);
486                 memmove (dst->children + offset, dst->children + offset + 1,
487                                 sizeof (oconfig_item_t) * nmemb);
488         }
489
490         /* Resize the memory containing the children to be big enough to hold
491          * all children. */
492         if (dst->children_num + src->children_num - 1 == 0)
493         {
494                 dst->children_num = 0;
495                 return (0);
496         }
497
498         temp = (oconfig_item_t *) realloc (dst->children,
499                         sizeof (oconfig_item_t)
500                         * (dst->children_num + src->children_num - 1));
501         if (temp == NULL)
502         {
503                 ERROR ("configfile: realloc failed.");
504                 return (-1);
505         }
506         dst->children = temp;
507
508         /* If there are children behind the include statement, and they have
509          * not yet been moved because (src->children_num == 0), then move them
510          * to the end of the list, so that the new children have room before
511          * them. */
512         if ((src->children_num > 0)
513                         && ((dst->children_num - (offset + 1)) > 0))
514         {
515                 int nmemb = dst->children_num - (offset + 1);
516                 int old_offset = offset + 1;
517                 int new_offset = offset + src->children_num;
518
519                 memmove (dst->children + new_offset,
520                                 dst->children + old_offset,
521                                 sizeof (oconfig_item_t) * nmemb);
522         }
523
524         /* Last but not least: If there are new children, copy them to the
525          * memory reserved for them. */
526         if (src->children_num > 0)
527         {
528                 memcpy (dst->children + offset,
529                                 src->children,
530                                 sizeof (oconfig_item_t) * src->children_num);
531         }
532
533         /* Update the number of children. */
534         dst->children_num += (src->children_num - 1);
535
536         return (0);
537 } /* int cf_ci_replace_child */
538
539 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
540 {
541         oconfig_item_t *temp;
542
543         if ((src == NULL) || (src->children_num == 0))
544                 return (0);
545
546         temp = (oconfig_item_t *) realloc (dst->children,
547                         sizeof (oconfig_item_t)
548                         * (dst->children_num + src->children_num));
549         if (temp == NULL)
550         {
551                 ERROR ("configfile: realloc failed.");
552                 return (-1);
553         }
554         dst->children = temp;
555
556         memcpy (dst->children + dst->children_num,
557                         src->children,
558                         sizeof (oconfig_item_t)
559                         * src->children_num);
560         dst->children_num += src->children_num;
561
562         return (0);
563 } /* int cf_ci_append_children */
564
565 #define CF_MAX_DEPTH 8
566 static oconfig_item_t *cf_read_generic (const char *path,
567                 const char *pattern, int depth);
568
569 static int cf_include_all (oconfig_item_t *root, int depth)
570 {
571         int i;
572
573         for (i = 0; i < root->children_num; i++)
574         {
575                 oconfig_item_t *new;
576                 oconfig_item_t *old;
577
578                 char *pattern = NULL;
579
580                 int j;
581
582                 if (strcasecmp (root->children[i].key, "Include") != 0)
583                         continue;
584
585                 old = root->children + i;
586
587                 if ((old->values_num != 1)
588                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
589                 {
590                         ERROR ("configfile: `Include' needs exactly one string argument.");
591                         continue;
592                 }
593
594                 for (j = 0; j < old->children_num; ++j)
595                 {
596                         oconfig_item_t *child = old->children + j;
597
598                         if (strcasecmp (child->key, "Filter") == 0)
599                                 cf_util_get_string (child, &pattern);
600                         else
601                                 ERROR ("configfile: Option `%s' not allowed in <Include> block.",
602                                                 child->key);
603                 }
604
605                 new = cf_read_generic (old->values[0].value.string, pattern, depth + 1);
606                 sfree (pattern);
607
608                 if (new == NULL)
609                         return (-1);
610
611                 /* Now replace the i'th child in `root' with `new'. */
612                 if (cf_ci_replace_child (root, new, i) < 0)
613                         return (-1);
614
615                 /* ... and go back to the new i'th child. */
616                 --i;
617
618                 sfree (new->values);
619                 sfree (new);
620         } /* for (i = 0; i < root->children_num; i++) */
621
622         return (0);
623 } /* int cf_include_all */
624
625 static oconfig_item_t *cf_read_file (const char *file,
626                 const char *pattern, int depth)
627 {
628         oconfig_item_t *root;
629         int status;
630
631         assert (depth < CF_MAX_DEPTH);
632
633         if (pattern != NULL) {
634 #if HAVE_FNMATCH_H && HAVE_LIBGEN_H
635                 char *tmp = sstrdup (file);
636                 char *filename = basename (tmp);
637
638                 if ((filename != NULL) && (fnmatch (pattern, filename, 0) != 0)) {
639                         DEBUG ("configfile: Not including `%s' because it "
640                                         "does not match pattern `%s'.",
641                                         filename, pattern);
642                         free (tmp);
643                         return (NULL);
644                 }
645
646                 free (tmp);
647 #else
648                 ERROR ("configfile: Cannot apply pattern filter '%s' "
649                                 "to file '%s': functions basename() and / or "
650                                 "fnmatch() not available.", pattern, file);
651 #endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */
652         }
653
654         root = oconfig_parse_file (file);
655         if (root == NULL)
656         {
657                 ERROR ("configfile: Cannot read file `%s'.", file);
658                 return (NULL);
659         }
660
661         status = cf_include_all (root, depth);
662         if (status != 0)
663         {
664                 oconfig_free (root);
665                 return (NULL);
666         }
667
668         return (root);
669 } /* oconfig_item_t *cf_read_file */
670
671 static int cf_compare_string (const void *p1, const void *p2)
672 {
673         return strcmp (*(const char **) p1, *(const char **) p2);
674 }
675
676 static oconfig_item_t *cf_read_dir (const char *dir,
677                 const char *pattern, int depth)
678 {
679         oconfig_item_t *root = NULL;
680         DIR *dh;
681         struct dirent *de;
682         char **filenames = NULL;
683         int filenames_num = 0;
684         int status;
685         int i;
686
687         assert (depth < CF_MAX_DEPTH);
688
689         dh = opendir (dir);
690         if (dh == NULL)
691         {
692                 char errbuf[1024];
693                 ERROR ("configfile: opendir failed: %s",
694                                 sstrerror (errno, errbuf, sizeof (errbuf)));
695                 return (NULL);
696         }
697
698         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
699         if (root == NULL)
700         {
701                 ERROR ("configfile: malloc failed.");
702                 return (NULL);
703         }
704         memset (root, 0, sizeof (oconfig_item_t));
705
706         while ((de = readdir (dh)) != NULL)
707         {
708                 char   name[1024];
709                 char **tmp;
710
711                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
712                         continue;
713
714                 status = ssnprintf (name, sizeof (name), "%s/%s",
715                                 dir, de->d_name);
716                 if ((status < 0) || ((size_t) status >= sizeof (name)))
717                 {
718                         ERROR ("configfile: Not including `%s/%s' because its"
719                                         " name is too long.",
720                                         dir, de->d_name);
721                         for (i = 0; i < filenames_num; ++i)
722                                 free (filenames[i]);
723                         free (filenames);
724                         free (root);
725                         return (NULL);
726                 }
727
728                 ++filenames_num;
729                 tmp = (char **) realloc (filenames,
730                                 filenames_num * sizeof (*filenames));
731                 if (tmp == NULL) {
732                         ERROR ("configfile: realloc failed.");
733                         for (i = 0; i < filenames_num - 1; ++i)
734                                 free (filenames[i]);
735                         free (filenames);
736                         free (root);
737                         return (NULL);
738                 }
739                 filenames = tmp;
740
741                 filenames[filenames_num - 1] = sstrdup (name);
742         }
743
744         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
745                         cf_compare_string);
746
747         for (i = 0; i < filenames_num; ++i)
748         {
749                 oconfig_item_t *temp;
750                 char *name = filenames[i];
751
752                 temp = cf_read_generic (name, pattern, depth);
753                 if (temp == NULL)
754                 {
755                         /* An error should already have been reported. */
756                         sfree (name);
757                         continue;
758                 }
759
760                 cf_ci_append_children (root, temp);
761                 sfree (temp->children);
762                 sfree (temp);
763
764                 free (name);
765         }
766
767         free(filenames);
768         return (root);
769 } /* oconfig_item_t *cf_read_dir */
770
771 /* 
772  * cf_read_generic
773  *
774  * Path is stat'ed and either cf_read_file or cf_read_dir is called
775  * accordingly.
776  *
777  * There are two versions of this function: If `wordexp' exists shell wildcards
778  * will be expanded and the function will include all matches found. If
779  * `wordexp' (or, more precisely, it's header file) is not available the
780  * simpler function is used which does not do any such expansion.
781  */
782 #if HAVE_WORDEXP_H
783 static oconfig_item_t *cf_read_generic (const char *path,
784                 const char *pattern, int depth)
785 {
786         oconfig_item_t *root = NULL;
787         int status;
788         const char *path_ptr;
789         wordexp_t we;
790         size_t i;
791
792         if (depth >= CF_MAX_DEPTH)
793         {
794                 ERROR ("configfile: Not including `%s' because the maximum "
795                                 "nesting depth has been reached.", path);
796                 return (NULL);
797         }
798
799         status = wordexp (path, &we, WRDE_NOCMD);
800         if (status != 0)
801         {
802                 ERROR ("configfile: wordexp (%s) failed.", path);
803                 return (NULL);
804         }
805
806         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
807         if (root == NULL)
808         {
809                 ERROR ("configfile: malloc failed.");
810                 return (NULL);
811         }
812         memset (root, '\0', sizeof (oconfig_item_t));
813
814         /* wordexp() might return a sorted list already. That's not
815          * documented though, so let's make sure we get what we want. */
816         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
817                         cf_compare_string);
818
819         for (i = 0; i < we.we_wordc; i++)
820         {
821                 oconfig_item_t *temp;
822                 struct stat statbuf;
823
824                 path_ptr = we.we_wordv[i];
825
826                 status = stat (path_ptr, &statbuf);
827                 if (status != 0)
828                 {
829                         char errbuf[1024];
830                         WARNING ("configfile: stat (%s) failed: %s",
831                                         path_ptr,
832                                         sstrerror (errno, errbuf, sizeof (errbuf)));
833                         continue;
834                 }
835
836                 if (S_ISREG (statbuf.st_mode))
837                         temp = cf_read_file (path_ptr, pattern, depth);
838                 else if (S_ISDIR (statbuf.st_mode))
839                         temp = cf_read_dir (path_ptr, pattern, depth);
840                 else
841                 {
842                         WARNING ("configfile: %s is neither a file nor a "
843                                         "directory.", path);
844                         continue;
845                 }
846
847                 if (temp == NULL) {
848                         oconfig_free (root);
849                         return (NULL);
850                 }
851
852                 cf_ci_append_children (root, temp);
853                 sfree (temp->children);
854                 sfree (temp);
855         }
856
857         wordfree (&we);
858
859         return (root);
860 } /* oconfig_item_t *cf_read_generic */
861 /* #endif HAVE_WORDEXP_H */
862
863 #else /* if !HAVE_WORDEXP_H */
864 static oconfig_item_t *cf_read_generic (const char *path,
865                 const char *pattern, int depth)
866 {
867         struct stat statbuf;
868         int status;
869
870         if (depth >= CF_MAX_DEPTH)
871         {
872                 ERROR ("configfile: Not including `%s' because the maximum "
873                                 "nesting depth has been reached.", path);
874                 return (NULL);
875         }
876
877         status = stat (path, &statbuf);
878         if (status != 0)
879         {
880                 char errbuf[1024];
881                 ERROR ("configfile: stat (%s) failed: %s",
882                                 path,
883                                 sstrerror (errno, errbuf, sizeof (errbuf)));
884                 return (NULL);
885         }
886
887         if (S_ISREG (statbuf.st_mode))
888                 return (cf_read_file (path, pattern, depth));
889         else if (S_ISDIR (statbuf.st_mode))
890                 return (cf_read_dir (path, pattern, depth));
891
892         ERROR ("configfile: %s is neither a file nor a directory.", path);
893         return (NULL);
894 } /* oconfig_item_t *cf_read_generic */
895 #endif /* !HAVE_WORDEXP_H */
896
897 /* 
898  * Public functions
899  */
900 int global_option_set (const char *option, const char *value)
901 {
902         int i;
903
904         DEBUG ("option = %s; value = %s;", option, value);
905
906         for (i = 0; i < cf_global_options_num; i++)
907                 if (strcasecmp (cf_global_options[i].key, option) == 0)
908                         break;
909
910         if (i >= cf_global_options_num)
911                 return (-1);
912
913         sfree (cf_global_options[i].value);
914
915         if (value != NULL)
916                 cf_global_options[i].value = strdup (value);
917         else
918                 cf_global_options[i].value = NULL;
919
920         return (0);
921 }
922
923 const char *global_option_get (const char *option)
924 {
925         int i;
926
927         for (i = 0; i < cf_global_options_num; i++)
928                 if (strcasecmp (cf_global_options[i].key, option) == 0)
929                         break;
930
931         if (i >= cf_global_options_num)
932                 return (NULL);
933         
934         return ((cf_global_options[i].value != NULL)
935                         ? cf_global_options[i].value
936                         : cf_global_options[i].def);
937 } /* char *global_option_get */
938
939 long global_option_get_long (const char *option, long default_value)
940 {
941                 const char *str;
942                 long value;
943
944                 str = global_option_get (option);
945                 if (NULL == str)
946                         return (default_value);
947
948                 errno = 0;
949                 value = strtol (str, /* endptr = */ NULL, /* base = */ 0);
950                 if (errno != 0)
951                         return (default_value);
952
953                 return (value);
954 } /* char *global_option_get_long */
955
956 cdtime_t cf_get_default_interval (void)
957 {
958   char const *str = global_option_get ("Interval");
959   double interval_double = COLLECTD_DEFAULT_INTERVAL;
960
961   if (str != NULL)
962   {
963     char *endptr = NULL;
964     double tmp = strtod (str, &endptr);
965
966     if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
967       ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
968           "as number.", str);
969     else if (tmp <= 0.0)
970       ERROR ("cf_get_default_interval: Interval must be a positive number. "
971           "The current number is %g.", tmp);
972     else
973       interval_double = tmp;
974   }
975
976   return (DOUBLE_TO_CDTIME_T (interval_double));
977 } /* }}} cdtime_t cf_get_default_interval */
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