Code

src/configfile.c: Let errors in included files propagate up to cf_read().
[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  **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "filter_chain.h"
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
38 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
40 /*
41  * Private types
42  */
43 typedef struct cf_callback
44 {
45         const char  *type;
46         int  (*callback) (const char *, const char *);
47         const char **keys;
48         int    keys_num;
49         plugin_ctx_t ctx;
50         struct cf_callback *next;
51 } cf_callback_t;
53 typedef struct cf_complex_callback_s
54 {
55         char *type;
56         int (*callback) (oconfig_item_t *);
57         plugin_ctx_t ctx;
58         struct cf_complex_callback_s *next;
59 } cf_complex_callback_t;
61 typedef struct cf_value_map_s
62 {
63         char *key;
64         int (*func) (const oconfig_item_t *);
65 } cf_value_map_t;
67 typedef struct cf_global_option_s
68 {
69         char *key;
70         char *value;
71         char *def;
72 } cf_global_option_t;
74 /*
75  * Prototypes of callback functions
76  */
77 static int dispatch_value_typesdb (const oconfig_item_t *ci);
78 static int dispatch_value_plugindir (const oconfig_item_t *ci);
79 static int dispatch_loadplugin (const oconfig_item_t *ci);
81 /*
82  * Private variables
83  */
84 static cf_callback_t *first_callback = NULL;
85 static cf_complex_callback_t *complex_callback_head = NULL;
87 static cf_value_map_t cf_value_map[] =
88 {
89         {"TypesDB",    dispatch_value_typesdb},
90         {"PluginDir",  dispatch_value_plugindir},
91         {"LoadPlugin", dispatch_loadplugin}
92 };
93 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
95 static cf_global_option_t cf_global_options[] =
96 {
97         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
98         {"PIDFile",     NULL, PIDFILE},
99         {"Hostname",    NULL, NULL},
100         {"FQDNLookup",  NULL, "true"},
101         {"Interval",    NULL, NULL},
102         {"ReadThreads", NULL, "5"},
103         {"Timeout",     NULL, "2"},
104         {"PreCacheChain",  NULL, "PreCache"},
105         {"PostCacheChain", NULL, "PostCache"}
106 };
107 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
109 static int cf_default_typesdb = 1;
111 /*
112  * Functions to handle register/unregister, search, and other plugin related
113  * stuff
114  */
115 static cf_callback_t *cf_search (const char *type)
117         cf_callback_t *cf_cb;
119         if (type == NULL)
120                 return (NULL);
122         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
123                 if (strcasecmp (cf_cb->type, type) == 0)
124                         break;
126         return (cf_cb);
129 static int cf_dispatch (const char *type, const char *orig_key,
130                 const char *orig_value)
132         cf_callback_t *cf_cb;
133         plugin_ctx_t old_ctx;
134         char *key;
135         char *value;
136         int ret;
137         int i;
139         DEBUG ("type = %s, key = %s, value = %s",
140                         ESCAPE_NULL(type),
141                         ESCAPE_NULL(orig_key),
142                         ESCAPE_NULL(orig_value));
144         if ((cf_cb = cf_search (type)) == NULL)
145         {
146                 WARNING ("Found a configuration for the `%s' plugin, but "
147                                 "the plugin isn't loaded or didn't register "
148                                 "a configuration callback.", type);
149                 return (-1);
150         }
152         if ((key = strdup (orig_key)) == NULL)
153                 return (1);
154         if ((value = strdup (orig_value)) == NULL)
155         {
156                 free (key);
157                 return (2);
158         }
160         ret = -1;
162         old_ctx = plugin_set_ctx (cf_cb->ctx);
164         for (i = 0; i < cf_cb->keys_num; i++)
165         {
166                 if ((cf_cb->keys[i] != NULL)
167                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
168                 {
169                         ret = (*cf_cb->callback) (key, value);
170                         break;
171                 }
172         }
174         plugin_set_ctx (old_ctx);
176         if (i >= cf_cb->keys_num)
177                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
179         free (key);
180         free (value);
182         DEBUG ("cf_dispatch: return (%i)", ret);
184         return (ret);
185 } /* int cf_dispatch */
187 static int dispatch_global_option (const oconfig_item_t *ci)
189         if (ci->values_num != 1)
190                 return (-1);
191         if (ci->values[0].type == OCONFIG_TYPE_STRING)
192                 return (global_option_set (ci->key, ci->values[0].value.string));
193         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
194         {
195                 char tmp[128];
196                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
197                 return (global_option_set (ci->key, tmp));
198         }
199         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
200         {
201                 if (ci->values[0].value.boolean)
202                         return (global_option_set (ci->key, "true"));
203                 else
204                         return (global_option_set (ci->key, "false"));
205         }
207         return (-1);
208 } /* int dispatch_global_option */
210 static int dispatch_value_typesdb (const oconfig_item_t *ci)
212         int i = 0;
214         assert (strcasecmp (ci->key, "TypesDB") == 0);
216         cf_default_typesdb = 0;
218         if (ci->values_num < 1) {
219                 ERROR ("configfile: `TypesDB' needs at least one argument.");
220                 return (-1);
221         }
223         for (i = 0; i < ci->values_num; ++i)
224         {
225                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
226                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
227                                         "is not a string.", i + 1);
228                         continue;
229                 }
231                 read_types_list (ci->values[i].value.string);
232         }
233         return (0);
234 } /* int dispatch_value_typesdb */
236 static int dispatch_value_plugindir (const oconfig_item_t *ci)
238         assert (strcasecmp (ci->key, "PluginDir") == 0);
239         
240         if (ci->values_num != 1)
241                 return (-1);
242         if (ci->values[0].type != OCONFIG_TYPE_STRING)
243                 return (-1);
245         plugin_set_dir (ci->values[0].value.string);
246         return (0);
249 static int dispatch_loadplugin (const oconfig_item_t *ci)
251         int i;
252         const char *name;
253         unsigned int flags = 0;
254         plugin_ctx_t ctx;
255         plugin_ctx_t old_ctx;
256         int ret_val;
258         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
260         if (ci->values_num != 1)
261                 return (-1);
262         if (ci->values[0].type != OCONFIG_TYPE_STRING)
263                 return (-1);
265         name = ci->values[0].value.string;
267         /* default to the global interval set before loading this plugin */
268         memset (&ctx, 0, sizeof (ctx));
269         ctx.interval = cf_get_default_interval ();
271         /*
272          * XXX: Magic at work:
273          *
274          * Some of the language bindings, for example the Python and Perl
275          * plugins, need to be able to export symbols to the scripts they run.
276          * For this to happen, the "Globals" flag needs to be set.
277          * Unfortunately, this technical detail is hard to explain to the
278          * average user and she shouldn't have to worry about this, ideally.
279          * So in order to save everyone's sanity use a different default for a
280          * handful of special plugins. --octo
281          */
282         if ((strcasecmp ("Perl", name) == 0)
283                         || (strcasecmp ("Python", name) == 0))
284                 flags |= PLUGIN_FLAGS_GLOBAL;
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;
292                         if (cf_util_get_double (ci->children + i, &interval) != 0) {
293                                 /* cf_util_get_double will log an error */
294                                 continue;
295                         }
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         }
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);
311         return (ret_val);
312 } /* int dispatch_value_loadplugin */
314 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
316         char  buffer[4096];
317         char *buffer_ptr;
318         int   buffer_free;
319         int i;
321         buffer_ptr = buffer;
322         buffer_free = sizeof (buffer);
324         for (i = 0; i < ci->values_num; i++)
325         {
326                 int status = -1;
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");
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;
347         return (cf_dispatch (plugin, ci->key, buffer_ptr));
348 } /* int dispatch_value_plugin */
350 static int dispatch_value (const oconfig_item_t *ci)
352         int ret = -2;
353         int i;
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                 }
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                 }
369         return (ret);
370 } /* int dispatch_value */
372 static int dispatch_block_plugin (oconfig_item_t *ci)
374         int i;
375         char *name;
377         cf_complex_callback_t *cb;
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);
386         name = ci->values[0].value.string;
388         /* Check for a complex callback first */
389         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
390         {
391                 if (strcasecmp (name, cb->type) == 0)
392                 {
393                         plugin_ctx_t old_ctx;
394                         int ret_val;
396                         old_ctx = plugin_set_ctx (cb->ctx);
397                         ret_val = (cb->callback (ci));
398                         plugin_set_ctx (old_ctx);
399                         return (ret_val);
400                 }
401         }
403         /* Hm, no complex plugin found. Dispatch the values one by one */
404         for (i = 0; i < ci->children_num; i++)
405         {
406                 if (ci->children[i].children == NULL)
407                         dispatch_value_plugin (name, ci->children + i);
408                 else
409                 {
410                         WARNING ("There is a `%s' block within the "
411                                         "configuration for the %s plugin. "
412                                         "The plugin either only expects "
413                                         "\"simple\" configuration statements "
414                                         "or wasn't loaded using `LoadPlugin'."
415                                         " Please check your configuration.",
416                                         ci->children[i].key, name);
417                 }
418         }
420         return (0);
424 static int dispatch_block (oconfig_item_t *ci)
426         if (strcasecmp (ci->key, "LoadPlugin") == 0)
427                 return (dispatch_loadplugin (ci));
428         else if (strcasecmp (ci->key, "Plugin") == 0)
429                 return (dispatch_block_plugin (ci));
430         else if (strcasecmp (ci->key, "Chain") == 0)
431                 return (fc_configure (ci));
433         return (0);
436 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
437                 int offset)
439         oconfig_item_t *temp;
440         int i;
442         assert (offset >= 0);
443         assert (dst->children_num > offset);
445         /* Free the memory used by the replaced child. Usually that's the
446          * `Include "blah"' statement. */
447         temp = dst->children + offset;
448         for (i = 0; i < temp->values_num; i++)
449         {
450                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
451                 {
452                         sfree (temp->values[i].value.string);
453                 }
454         }
455         sfree (temp->values);
456         temp = NULL;
458         /* If (src->children_num == 0) the array size is decreased. If offset
459          * is _not_ the last element, (offset < (dst->children_num - 1)), then
460          * we need to move the trailing elements before resizing the array. */
461         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
462         {
463                 int nmemb = dst->children_num - (offset + 1);
464                 memmove (dst->children + offset, dst->children + offset + 1,
465                                 sizeof (oconfig_item_t) * nmemb);
466         }
468         /* Resize the memory containing the children to be big enough to hold
469          * all children. */
470         temp = (oconfig_item_t *) realloc (dst->children,
471                         sizeof (oconfig_item_t)
472                         * (dst->children_num + src->children_num - 1));
473         if (temp == NULL)
474         {
475                 ERROR ("configfile: realloc failed.");
476                 return (-1);
477         }
478         dst->children = temp;
480         /* If there are children behind the include statement, and they have
481          * not yet been moved because (src->children_num == 0), then move them
482          * to the end of the list, so that the new children have room before
483          * them. */
484         if ((src->children_num > 0)
485                         && ((dst->children_num - (offset + 1)) > 0))
486         {
487                 int nmemb = dst->children_num - (offset + 1);
488                 int old_offset = offset + 1;
489                 int new_offset = offset + src->children_num;
491                 memmove (dst->children + new_offset,
492                                 dst->children + old_offset,
493                                 sizeof (oconfig_item_t) * nmemb);
494         }
496         /* Last but not least: If there are new children, copy them to the
497          * memory reserved for them. */
498         if (src->children_num > 0)
499         {
500                 memcpy (dst->children + offset,
501                                 src->children,
502                                 sizeof (oconfig_item_t) * src->children_num);
503         }
505         /* Update the number of children. */
506         dst->children_num += (src->children_num - 1);
508         return (0);
509 } /* int cf_ci_replace_child */
511 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
513         oconfig_item_t *temp;
515         if ((src == NULL) || (src->children_num == 0))
516                 return (0);
518         temp = (oconfig_item_t *) realloc (dst->children,
519                         sizeof (oconfig_item_t)
520                         * (dst->children_num + src->children_num));
521         if (temp == NULL)
522         {
523                 ERROR ("configfile: realloc failed.");
524                 return (-1);
525         }
526         dst->children = temp;
528         memcpy (dst->children + dst->children_num,
529                         src->children,
530                         sizeof (oconfig_item_t)
531                         * src->children_num);
532         dst->children_num += src->children_num;
534         return (0);
535 } /* int cf_ci_append_children */
537 #define CF_MAX_DEPTH 8
538 static oconfig_item_t *cf_read_generic (const char *path, int depth);
540 static int cf_include_all (oconfig_item_t *root, int depth)
542         int i;
544         for (i = 0; i < root->children_num; i++)
545         {
546                 oconfig_item_t *new;
547                 oconfig_item_t *old;
549                 /* Ignore all blocks, including `Include' blocks. */
550                 if (root->children[i].children_num != 0)
551                         continue;
553                 if (strcasecmp (root->children[i].key, "Include") != 0)
554                         continue;
556                 old = root->children + i;
558                 if ((old->values_num != 1)
559                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
560                 {
561                         ERROR ("configfile: `Include' needs exactly one string argument.");
562                         continue;
563                 }
565                 new = cf_read_generic (old->values[0].value.string, depth + 1);
566                 if (new == NULL)
567                         return (-1);
569                 /* Now replace the i'th child in `root' with `new'. */
570                 cf_ci_replace_child (root, new, i);
572                 /* ... and go back to the new i'th child. */
573                 --i;
575                 sfree (new->values);
576                 sfree (new);
577         } /* for (i = 0; i < root->children_num; i++) */
579         return (0);
580 } /* int cf_include_all */
582 static oconfig_item_t *cf_read_file (const char *file, int depth)
584         oconfig_item_t *root;
585         int status;
587         assert (depth < CF_MAX_DEPTH);
589         root = oconfig_parse_file (file);
590         if (root == NULL)
591         {
592                 ERROR ("configfile: Cannot read file `%s'.", file);
593                 return (NULL);
594         }
596         status = cf_include_all (root, depth);
597         if (status != 0)
598         {
599                 oconfig_free (root);
600                 return (NULL);
601         }
603         return (root);
604 } /* oconfig_item_t *cf_read_file */
606 static int cf_compare_string (const void *p1, const void *p2)
608         return strcmp (*(const char **) p1, *(const char **) p2);
611 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
613         oconfig_item_t *root = NULL;
614         DIR *dh;
615         struct dirent *de;
616         char **filenames = NULL;
617         int filenames_num = 0;
618         int status;
619         int i;
621         assert (depth < CF_MAX_DEPTH);
623         dh = opendir (dir);
624         if (dh == NULL)
625         {
626                 char errbuf[1024];
627                 ERROR ("configfile: opendir failed: %s",
628                                 sstrerror (errno, errbuf, sizeof (errbuf)));
629                 return (NULL);
630         }
632         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
633         if (root == NULL)
634         {
635                 ERROR ("configfile: malloc failed.");
636                 return (NULL);
637         }
638         memset (root, 0, sizeof (oconfig_item_t));
640         while ((de = readdir (dh)) != NULL)
641         {
642                 char   name[1024];
643                 char **tmp;
645                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
646                         continue;
648                 status = ssnprintf (name, sizeof (name), "%s/%s",
649                                 dir, de->d_name);
650                 if ((status < 0) || ((size_t) status >= sizeof (name)))
651                 {
652                         ERROR ("configfile: Not including `%s/%s' because its"
653                                         " name is too long.",
654                                         dir, de->d_name);
655                         for (i = 0; i < filenames_num; ++i)
656                                 free (filenames[i]);
657                         free (filenames);
658                         free (root);
659                         return (NULL);
660                 }
662                 ++filenames_num;
663                 tmp = (char **) realloc (filenames,
664                                 filenames_num * sizeof (*filenames));
665                 if (tmp == NULL) {
666                         ERROR ("configfile: realloc failed.");
667                         for (i = 0; i < filenames_num - 1; ++i)
668                                 free (filenames[i]);
669                         free (filenames);
670                         free (root);
671                         return (NULL);
672                 }
673                 filenames = tmp;
675                 filenames[filenames_num - 1] = sstrdup (name);
676         }
678         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
679                         cf_compare_string);
681         for (i = 0; i < filenames_num; ++i)
682         {
683                 oconfig_item_t *temp;
684                 char *name = filenames[i];
686                 temp = cf_read_generic (name, depth);
687                 if (temp == NULL)
688                 {
689                         /* An error should already have been reported. */
690                         sfree (name);
691                         continue;
692                 }
694                 cf_ci_append_children (root, temp);
695                 sfree (temp->children);
696                 sfree (temp);
698                 free (name);
699         }
701         free(filenames);
702         return (root);
703 } /* oconfig_item_t *cf_read_dir */
705 /* 
706  * cf_read_generic
707  *
708  * Path is stat'ed and either cf_read_file or cf_read_dir is called
709  * accordingly.
710  *
711  * There are two versions of this function: If `wordexp' exists shell wildcards
712  * will be expanded and the function will include all matches found. If
713  * `wordexp' (or, more precisely, it's header file) is not available the
714  * simpler function is used which does not do any such expansion.
715  */
716 #if HAVE_WORDEXP_H
717 static oconfig_item_t *cf_read_generic (const char *path, int depth)
719         oconfig_item_t *root = NULL;
720         int status;
721         const char *path_ptr;
722         wordexp_t we;
723         size_t i;
725         if (depth >= CF_MAX_DEPTH)
726         {
727                 ERROR ("configfile: Not including `%s' because the maximum "
728                                 "nesting depth has been reached.", path);
729                 return (NULL);
730         }
732         status = wordexp (path, &we, WRDE_NOCMD);
733         if (status != 0)
734         {
735                 ERROR ("configfile: wordexp (%s) failed.", path);
736                 return (NULL);
737         }
739         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
740         if (root == NULL)
741         {
742                 ERROR ("configfile: malloc failed.");
743                 return (NULL);
744         }
745         memset (root, '\0', sizeof (oconfig_item_t));
747         /* wordexp() might return a sorted list already. That's not
748          * documented though, so let's make sure we get what we want. */
749         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
750                         cf_compare_string);
752         for (i = 0; i < we.we_wordc; i++)
753         {
754                 oconfig_item_t *temp;
755                 struct stat statbuf;
757                 path_ptr = we.we_wordv[i];
759                 status = stat (path_ptr, &statbuf);
760                 if (status != 0)
761                 {
762                         char errbuf[1024];
763                         WARNING ("configfile: stat (%s) failed: %s",
764                                         path_ptr,
765                                         sstrerror (errno, errbuf, sizeof (errbuf)));
766                         continue;
767                 }
769                 if (S_ISREG (statbuf.st_mode))
770                         temp = cf_read_file (path_ptr, depth);
771                 else if (S_ISDIR (statbuf.st_mode))
772                         temp = cf_read_dir (path_ptr, depth);
773                 else
774                 {
775                         WARNING ("configfile: %s is neither a file nor a "
776                                         "directory.", path);
777                         continue;
778                 }
780                 if (temp == NULL) {
781                         oconfig_free (root);
782                         return (NULL);
783                 }
785                 cf_ci_append_children (root, temp);
786                 sfree (temp->children);
787                 sfree (temp);
788         }
790         wordfree (&we);
792         return (root);
793 } /* oconfig_item_t *cf_read_generic */
794 /* #endif HAVE_WORDEXP_H */
796 #else /* if !HAVE_WORDEXP_H */
797 static oconfig_item_t *cf_read_generic (const char *path, int depth)
799         struct stat statbuf;
800         int status;
802         if (depth >= CF_MAX_DEPTH)
803         {
804                 ERROR ("configfile: Not including `%s' because the maximum "
805                                 "nesting depth has been reached.", path);
806                 return (NULL);
807         }
809         status = stat (path, &statbuf);
810         if (status != 0)
811         {
812                 char errbuf[1024];
813                 ERROR ("configfile: stat (%s) failed: %s",
814                                 path,
815                                 sstrerror (errno, errbuf, sizeof (errbuf)));
816                 return (NULL);
817         }
819         if (S_ISREG (statbuf.st_mode))
820                 return (cf_read_file (path, depth));
821         else if (S_ISDIR (statbuf.st_mode))
822                 return (cf_read_dir (path, depth));
824         ERROR ("configfile: %s is neither a file nor a directory.", path);
825         return (NULL);
826 } /* oconfig_item_t *cf_read_generic */
827 #endif /* !HAVE_WORDEXP_H */
829 /* 
830  * Public functions
831  */
832 int global_option_set (const char *option, const char *value)
834         int i;
836         DEBUG ("option = %s; value = %s;", option, value);
838         for (i = 0; i < cf_global_options_num; i++)
839                 if (strcasecmp (cf_global_options[i].key, option) == 0)
840                         break;
842         if (i >= cf_global_options_num)
843                 return (-1);
845         sfree (cf_global_options[i].value);
847         if (value != NULL)
848                 cf_global_options[i].value = strdup (value);
849         else
850                 cf_global_options[i].value = NULL;
852         return (0);
855 const char *global_option_get (const char *option)
857         int i;
859         for (i = 0; i < cf_global_options_num; i++)
860                 if (strcasecmp (cf_global_options[i].key, option) == 0)
861                         break;
863         if (i >= cf_global_options_num)
864                 return (NULL);
865         
866         return ((cf_global_options[i].value != NULL)
867                         ? cf_global_options[i].value
868                         : cf_global_options[i].def);
869 } /* char *global_option_get */
871 cdtime_t cf_get_default_interval (void)
873   char const *str = global_option_get ("Interval");
874   double interval_double = COLLECTD_DEFAULT_INTERVAL;
876   if (str != NULL)
877   {
878     char *endptr = NULL;
879     double tmp = strtod (str, &endptr);
881     if ((endptr == NULL) || (endptr == str) || (*endptr != 0))
882       ERROR ("cf_get_default_interval: Unable to parse string \"%s\" "
883           "as number.", str);
884     else if (tmp <= 0.0)
885       ERROR ("cf_get_default_interval: Interval must be a positive number. "
886           "The current number is %g.", tmp);
887     else
888       interval_double = tmp;
889   }
891   return (DOUBLE_TO_CDTIME_T (interval_double));
892 } /* }}} cdtime_t cf_get_default_interval */
894 void cf_unregister (const char *type)
896         cf_callback_t *this, *prev;
898         for (prev = NULL, this = first_callback;
899                         this != NULL;
900                         prev = this, this = this->next)
901                 if (strcasecmp (this->type, type) == 0)
902                 {
903                         if (prev == NULL)
904                                 first_callback = this->next;
905                         else
906                                 prev->next = this->next;
908                         free (this);
909                         break;
910                 }
911 } /* void cf_unregister */
913 void cf_unregister_complex (const char *type)
915         cf_complex_callback_t *this, *prev;
917         for (prev = NULL, this = complex_callback_head;
918                         this != NULL;
919                         prev = this, this = this->next)
920                 if (strcasecmp (this->type, type) == 0)
921                 {
922                         if (prev == NULL)
923                                 complex_callback_head = this->next;
924                         else
925                                 prev->next = this->next;
927                         sfree (this->type);
928                         sfree (this);
929                         break;
930                 }
931 } /* void cf_unregister */
933 void cf_register (const char *type,
934                 int (*callback) (const char *, const char *),
935                 const char **keys, int keys_num)
937         cf_callback_t *cf_cb;
939         /* Remove this module from the list, if it already exists */
940         cf_unregister (type);
942         /* This pointer will be free'd in `cf_unregister' */
943         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
944                 return;
946         cf_cb->type     = type;
947         cf_cb->callback = callback;
948         cf_cb->keys     = keys;
949         cf_cb->keys_num = keys_num;
950         cf_cb->ctx      = plugin_get_ctx ();
952         cf_cb->next = first_callback;
953         first_callback = cf_cb;
954 } /* void cf_register */
956 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
958         cf_complex_callback_t *new;
960         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
961         if (new == NULL)
962                 return (-1);
964         new->type = strdup (type);
965         if (new->type == NULL)
966         {
967                 sfree (new);
968                 return (-1);
969         }
971         new->callback = callback;
972         new->next = NULL;
974         new->ctx = plugin_get_ctx ();
976         if (complex_callback_head == NULL)
977         {
978                 complex_callback_head = new;
979         }
980         else
981         {
982                 cf_complex_callback_t *last = complex_callback_head;
983                 while (last->next != NULL)
984                         last = last->next;
985                 last->next = new;
986         }
988         return (0);
989 } /* int cf_register_complex */
991 int cf_read (char *filename)
993         oconfig_item_t *conf;
994         int i;
996         conf = cf_read_generic (filename, 0 /* depth */);
997         if (conf == NULL)
998         {
999                 ERROR ("Unable to read config file %s.", filename);
1000                 return (-1);
1001         }
1002         else if (conf->children_num == 0)
1003         {
1004                 ERROR ("Configuration file %s is empty.", filename);
1005                 oconfig_free (conf);
1006                 return (-1);
1007         }
1009         for (i = 0; i < conf->children_num; i++)
1010         {
1011                 if (conf->children[i].children == NULL)
1012                         dispatch_value (conf->children + i);
1013                 else
1014                         dispatch_block (conf->children + i);
1015         }
1017         oconfig_free (conf);
1019         /* Read the default types.db if no `TypesDB' option was given. */
1020         if (cf_default_typesdb)
1021                 read_types_list (PKGDATADIR"/types.db");
1023         return (0);
1024 } /* int cf_read */
1026 /* Assures the config option is a string, duplicates it and returns the copy in
1027  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
1028  * success. */
1029 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1031         char *string;
1033         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1034         {
1035                 ERROR ("cf_util_get_string: The %s option requires "
1036                                 "exactly one string argument.", ci->key);
1037                 return (-1);
1038         }
1040         string = strdup (ci->values[0].value.string);
1041         if (string == NULL)
1042                 return (-1);
1044         if (*ret_string != NULL)
1045                 sfree (*ret_string);
1046         *ret_string = string;
1048         return (0);
1049 } /* }}} int cf_util_get_string */
1051 /* Assures the config option is a string and copies it to the provided buffer.
1052  * Assures null-termination. */
1053 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1054                 size_t buffer_size)
1056         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1057                 return (EINVAL);
1059         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1060         {
1061                 ERROR ("cf_util_get_string_buffer: The %s option requires "
1062                                 "exactly one string argument.", ci->key);
1063                 return (-1);
1064         }
1066         strncpy (buffer, ci->values[0].value.string, buffer_size);
1067         buffer[buffer_size - 1] = 0;
1069         return (0);
1070 } /* }}} int cf_util_get_string_buffer */
1072 /* Assures the config option is a number and returns it as an int. */
1073 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1075         if ((ci == NULL) || (ret_value == NULL))
1076                 return (EINVAL);
1078         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1079         {
1080                 ERROR ("cf_util_get_int: The %s option requires "
1081                                 "exactly one numeric argument.", ci->key);
1082                 return (-1);
1083         }
1085         *ret_value = (int) ci->values[0].value.number;
1087         return (0);
1088 } /* }}} int cf_util_get_int */
1090 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1092         if ((ci == NULL) || (ret_value == NULL))
1093                 return (EINVAL);
1095         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1096         {
1097                 ERROR ("cf_util_get_double: The %s option requires "
1098                                 "exactly one numeric argument.", ci->key);
1099                 return (-1);
1100         }
1102         *ret_value = ci->values[0].value.number;
1104         return (0);
1105 } /* }}} int cf_util_get_double */
1107 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1109         if ((ci == NULL) || (ret_bool == NULL))
1110                 return (EINVAL);
1112         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1113         {
1114                 ERROR ("cf_util_get_boolean: The %s option requires "
1115                                 "exactly one boolean argument.", ci->key);
1116                 return (-1);
1117         }
1119         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1121         return (0);
1122 } /* }}} int cf_util_get_boolean */
1124 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1125                 unsigned int *ret_value, unsigned int flag)
1127         int status;
1128         _Bool b;
1130         if (ret_value == NULL)
1131                 return (EINVAL);
1133         b = 0;
1134         status = cf_util_get_boolean (ci, &b);
1135         if (status != 0)
1136                 return (status);
1138         if (b)
1139         {
1140                 *ret_value |= flag;
1141         }
1142         else
1143         {
1144                 *ret_value &= ~flag;
1145         }
1147         return (0);
1148 } /* }}} int cf_util_get_flag */
1150 /* Assures that the config option is a string or a number if the correct range
1151  * of 1-65535. The string is then converted to a port number using
1152  * `service_name_to_port_number' and returned.
1153  * Returns the port number in the range [1-65535] or less than zero upon
1154  * failure. */
1155 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1157         int tmp;
1159         if ((ci->values_num != 1)
1160                         || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1161                                 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1162         {
1163                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1164                                 "exactly one string argument.", ci->key);
1165                 return (-1);
1166         }
1168         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1169                 return (service_name_to_port_number (ci->values[0].value.string));
1171         assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1172         tmp = (int) (ci->values[0].value.number + 0.5);
1173         if ((tmp < 1) || (tmp > 65535))
1174         {
1175                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1176                                 "a service name or a port number. The number "
1177                                 "you specified, %i, is not in the valid "
1178                                 "range of 1-65535.",
1179                                 ci->key, tmp);
1180                 return (-1);
1181         }
1183         return (tmp);
1184 } /* }}} int cf_util_get_port_number */
1186 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
1188         int port;
1189         char *service;
1190         int status;
1192         if (ci->values_num != 1)
1193         {
1194                 ERROR ("cf_util_get_service: The %s option requires exactly "
1195                                 "one argument.", ci->key);
1196                 return (-1);
1197         }
1199         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1200                 return (cf_util_get_string (ci, ret_string));
1201         if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
1202         {
1203                 ERROR ("cf_util_get_service: The %s option requires "
1204                                 "exactly one string or numeric argument.",
1205                                 ci->key);
1206         }
1208         port = 0;
1209         status = cf_util_get_int (ci, &port);
1210         if (status != 0)
1211                 return (status);
1212         else if ((port < 1) || (port > 65535))
1213         {
1214                 ERROR ("cf_util_get_service: The port number given "
1215                                 "for the %s option is out of "
1216                                 "range (%i).", ci->key, port);
1217                 return (-1);
1218         }
1220         service = malloc (6);
1221         if (service == NULL)
1222         {
1223                 ERROR ("cf_util_get_service: Out of memory.");
1224                 return (-1);
1225         }
1226         ssnprintf (service, 6, "%i", port);
1228         sfree (*ret_string);
1229         *ret_string = service;
1231         return (0);
1232 } /* }}} int cf_util_get_service */
1234 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1236         if ((ci == NULL) || (ret_value == NULL))
1237                 return (EINVAL);
1239         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1240         {
1241                 ERROR ("cf_util_get_cdtime: The %s option requires "
1242                                 "exactly one numeric argument.", ci->key);
1243                 return (-1);
1244         }
1246         if (ci->values[0].value.number < 0.0)
1247         {
1248                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1249                                 "option must not be negative.", ci->key);
1250                 return (-1);
1251         }
1253         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1255         return (0);
1256 } /* }}} int cf_util_get_cdtime */