Code

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