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