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