configfile.c: Abort if any config file could not be read.
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2008  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 verplant.org>
21  **/
22
23 #include "collectd.h"
24
25 #include "liboconfig/oconfig.h"
26
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30 #include "types_list.h"
31 #include "utils_threshold.h"
32
33 #if HAVE_WORDEXP_H
34 # include <wordexp.h>
35 #endif /* HAVE_WORDEXP_H */
36
37 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
38
39 /*
40  * Private types
41  */
42 typedef struct cf_callback
43 {
44         const char  *type;
45         int  (*callback) (const char *, const char *);
46         const char **keys;
47         int    keys_num;
48         struct cf_callback *next;
49 } cf_callback_t;
50
51 typedef struct cf_complex_callback_s
52 {
53         char *type;
54         int (*callback) (oconfig_item_t *);
55         struct cf_complex_callback_s *next;
56 } cf_complex_callback_t;
57
58 typedef struct cf_value_map_s
59 {
60         char *key;
61         int (*func) (const oconfig_item_t *);
62 } cf_value_map_t;
63
64 typedef struct cf_global_option_s
65 {
66         char *key;
67         char *value;
68         char *def;
69 } cf_global_option_t;
70
71 /*
72  * Prototypes of callback functions
73  */
74 static int dispatch_value_typesdb (const oconfig_item_t *ci);
75 static int dispatch_value_plugindir (const oconfig_item_t *ci);
76 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
77
78 /*
79  * Private variables
80  */
81 static cf_callback_t *first_callback = NULL;
82 static cf_complex_callback_t *complex_callback_head = NULL;
83
84 static cf_value_map_t cf_value_map[] =
85 {
86         {"TypesDB",    dispatch_value_typesdb},
87         {"PluginDir",  dispatch_value_plugindir},
88         {"LoadPlugin", dispatch_value_loadplugin}
89 };
90 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
91
92 static cf_global_option_t cf_global_options[] =
93 {
94         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
95         {"PIDFile",     NULL, PIDFILE},
96         {"Hostname",    NULL, NULL},
97         {"FQDNLookup",  NULL, "false"},
98         {"Interval",    NULL, "10"},
99         {"ReadThreads", NULL, "5"}
100 };
101 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
102
103 static int cf_default_typesdb = 1;
104
105 /*
106  * Functions to handle register/unregister, search, and other plugin related
107  * stuff
108  */
109 static cf_callback_t *cf_search (const char *type)
110 {
111         cf_callback_t *cf_cb;
112
113         if (type == NULL)
114                 return (NULL);
115
116         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
117                 if (strcasecmp (cf_cb->type, type) == 0)
118                         break;
119
120         return (cf_cb);
121 }
122
123 static int cf_dispatch (const char *type, const char *orig_key,
124                 const char *orig_value)
125 {
126         cf_callback_t *cf_cb;
127         char *key;
128         char *value;
129         int ret;
130         int i;
131
132         DEBUG ("type = %s, key = %s, value = %s",
133                         ESCAPE_NULL(type),
134                         ESCAPE_NULL(orig_key),
135                         ESCAPE_NULL(orig_value));
136
137         if ((cf_cb = cf_search (type)) == NULL)
138         {
139                 WARNING ("Found a configuration for the `%s' plugin, but "
140                                 "the plugin isn't loaded or didn't register "
141                                 "a configuration callback.", type);
142                 return (-1);
143         }
144
145         if ((key = strdup (orig_key)) == NULL)
146                 return (1);
147         if ((value = strdup (orig_value)) == NULL)
148         {
149                 free (key);
150                 return (2);
151         }
152
153         ret = -1;
154
155         for (i = 0; i < cf_cb->keys_num; i++)
156         {
157                 if (strcasecmp (cf_cb->keys[i], key) == 0)
158                 {
159                         ret = (*cf_cb->callback) (key, value);
160                         break;
161                 }
162         }
163
164         if (i >= cf_cb->keys_num)
165                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
166
167         free (key);
168         free (value);
169
170         DEBUG ("return (%i)", ret);
171
172         return (ret);
173 } /* int cf_dispatch */
174
175 static int dispatch_global_option (const oconfig_item_t *ci)
176 {
177         if (ci->values_num != 1)
178                 return (-1);
179         if (ci->values[0].type == OCONFIG_TYPE_STRING)
180                 return (global_option_set (ci->key, ci->values[0].value.string));
181         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
182         {
183                 char tmp[128];
184                 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
185                 tmp[127] = '\0';
186                 return (global_option_set (ci->key, tmp));
187         }
188         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
189         {
190                 if (ci->values[0].value.boolean)
191                         return (global_option_set (ci->key, "true"));
192                 else
193                         return (global_option_set (ci->key, "false"));
194         }
195
196         return (-1);
197 } /* int dispatch_global_option */
198
199 static int dispatch_value_typesdb (const oconfig_item_t *ci)
200 {
201         int i = 0;
202
203         assert (strcasecmp (ci->key, "TypesDB") == 0);
204
205         cf_default_typesdb = 0;
206
207         if (ci->values_num < 1)
208                 return (-1);
209
210         for (i = 0; i < ci->values_num; ++i)
211         {
212                 if (OCONFIG_TYPE_STRING != ci->values[i].type)
213                         continue;
214
215                 read_types_list (ci->values[i].value.string);
216         }
217         return (0);
218 } /* int dispatch_value_typesdb */
219
220 static int dispatch_value_plugindir (const oconfig_item_t *ci)
221 {
222         assert (strcasecmp (ci->key, "PluginDir") == 0);
223         
224         if (ci->values_num != 1)
225                 return (-1);
226         if (ci->values[0].type != OCONFIG_TYPE_STRING)
227                 return (-1);
228
229         plugin_set_dir (ci->values[0].value.string);
230         return (0);
231 }
232
233 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
234 {
235         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
236
237         if (ci->values_num != 1)
238                 return (-1);
239         if (ci->values[0].type != OCONFIG_TYPE_STRING)
240                 return (-1);
241
242         return (plugin_load (ci->values[0].value.string));
243 } /* int dispatch_value_loadplugin */
244
245 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
246 {
247         char  buffer[4096];
248         char *buffer_ptr;
249         int   buffer_free;
250         int i;
251
252         buffer_ptr = buffer;
253         buffer_free = sizeof (buffer);
254
255         for (i = 0; i < ci->values_num; i++)
256         {
257                 int status = -1;
258
259                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
260                         status = snprintf (buffer_ptr, buffer_free, " %s",
261                                         ci->values[i].value.string);
262                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
263                         status = snprintf (buffer_ptr, buffer_free, " %lf",
264                                         ci->values[i].value.number);
265                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
266                         status = snprintf (buffer_ptr, buffer_free, " %s",
267                                         ci->values[i].value.boolean
268                                         ? "true" : "false");
269
270                 if ((status < 0) || (status >= buffer_free))
271                         return (-1);
272                 buffer_free -= status;
273                 buffer_ptr  += status;
274         }
275         /* skip the initial space */
276         buffer_ptr = buffer + 1;
277
278         return (cf_dispatch (plugin, ci->key, buffer_ptr));
279 } /* int plugin_conf_dispatch */
280
281 static int dispatch_value (const oconfig_item_t *ci)
282 {
283         int ret = -2;
284         int i;
285
286         for (i = 0; i < cf_value_map_num; i++)
287                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
288                 {
289                         ret = cf_value_map[i].func (ci);
290                         break;
291                 }
292
293         for (i = 0; i < cf_global_options_num; i++)
294                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
295                 {
296                         ret = dispatch_global_option (ci);
297                         break;
298                 }
299
300         return (ret);
301 } /* int dispatch_value */
302
303 static int dispatch_block_plugin (oconfig_item_t *ci)
304 {
305         int i;
306         char *name;
307
308         cf_complex_callback_t *cb;
309
310         if (strcasecmp (ci->key, "Plugin") != 0)
311                 return (-1);
312         if (ci->values_num < 1)
313                 return (-1);
314         if (ci->values[0].type != OCONFIG_TYPE_STRING)
315                 return (-1);
316
317         name = ci->values[0].value.string;
318
319         /* Check for a complex callback first */
320         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
321                 if (strcasecmp (name, cb->type) == 0)
322                         return (cb->callback (ci));
323
324         /* Hm, no complex plugin found. Dispatch the values one by one */
325         for (i = 0; i < ci->children_num; i++)
326         {
327                 if (ci->children[i].children == NULL)
328                         dispatch_value_plugin (name, ci->children + i);
329                 else
330                         {DEBUG ("No nested config blocks allow for this plugin.");}
331         }
332
333         return (0);
334 }
335
336
337 static int dispatch_block (oconfig_item_t *ci)
338 {
339         if (strcasecmp (ci->key, "Plugin") == 0)
340                 return (dispatch_block_plugin (ci));
341         else if (strcasecmp (ci->key, "Threshold") == 0)
342                 return (ut_config (ci));
343
344         return (0);
345 }
346
347 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
348                 int offset)
349 {
350         oconfig_item_t *temp;
351         int i;
352
353         assert (offset >= 0);
354         assert (dst->children_num > offset);
355
356         /* Free the memory used by the replaced child. Usually that's the
357          * `Include "blah"' statement. */
358         temp = dst->children + offset;
359         for (i = 0; i < temp->values_num; i++)
360         {
361                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
362                 {
363                         sfree (temp->values[i].value.string);
364                 }
365         }
366         sfree (temp->values);
367         temp = NULL;
368
369         /* If (src->children_num == 0) the array size is decreased. If offset
370          * is _not_ the last element, (offset < (src->children_num - 1)), then
371          * we need to move the trailing elements before resizing the array. */
372         if ((src->children_num == 0) && (offset < (src->children_num - 1)))
373         {
374                 int nmemb = src->children_num - (offset + 1);
375                 memmove (src->children + offset, src->children + offset + 1,
376                                 sizeof (oconfig_item_t) * nmemb);
377         }
378
379         /* Resize the memory containing the children to be big enough to hold
380          * all children. */
381         temp = (oconfig_item_t *) realloc (dst->children,
382                         sizeof (oconfig_item_t)
383                         * (dst->children_num + src->children_num - 1));
384         if (temp == NULL)
385         {
386                 ERROR ("configfile: realloc failed.");
387                 return (-1);
388         }
389         dst->children = temp;
390
391         /* If there are children behind the include statement, and they have
392          * not yet been moved because (src->children_num == 0), then move them
393          * to the end of the list, so that the new children have room before
394          * them. */
395         if ((src->children_num > 0)
396                         && ((dst->children_num - (offset + 1)) > 0))
397         {
398                 int nmemb = dst->children_num - (offset + 1);
399                 int old_offset = offset + 1;
400                 int new_offset = offset + src->children_num;
401
402                 memmove (dst->children + new_offset,
403                                 dst->children + old_offset,
404                                 sizeof (oconfig_item_t) * nmemb);
405         }
406
407         /* Last but not least: If there are new childrem, copy them to the
408          * memory reserved for them. */
409         if (src->children_num > 0)
410         {
411                 memcpy (dst->children + offset,
412                                 src->children,
413                                 sizeof (oconfig_item_t) * src->children_num);
414         }
415
416         /* Update the number of children. */
417         dst->children_num += (src->children_num - 1);
418
419         return (0);
420 } /* int cf_ci_replace_child */
421
422 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
423 {
424         oconfig_item_t *temp;
425
426         if ((src == NULL) || (src->children_num == 0))
427                 return (0);
428
429         temp = (oconfig_item_t *) realloc (dst->children,
430                         sizeof (oconfig_item_t)
431                         * (dst->children_num + src->children_num));
432         if (temp == NULL)
433         {
434                 ERROR ("configfile: realloc failed.");
435                 return (-1);
436         }
437         dst->children = temp;
438
439         memcpy (dst->children + dst->children_num,
440                         src->children,
441                         sizeof (oconfig_item_t)
442                         * src->children_num);
443         dst->children_num += src->children_num;
444
445         return (0);
446 } /* int cf_ci_append_children */
447
448 #define CF_MAX_DEPTH 8
449 static oconfig_item_t *cf_read_generic (const char *path, int depth);
450
451 static int cf_include_all (oconfig_item_t *root, int depth)
452 {
453         int i;
454
455         for (i = 0; i < root->children_num; i++)
456         {
457                 oconfig_item_t *new;
458                 oconfig_item_t *old;
459
460                 /* Ignore all blocks, including `Include' blocks. */
461                 if (root->children[i].children_num != 0)
462                         continue;
463
464                 if (strcasecmp (root->children[i].key, "Include") != 0)
465                         continue;
466
467                 old = root->children + i;
468
469                 if ((old->values_num != 1)
470                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
471                 {
472                         ERROR ("configfile: `Include' needs exactly one string argument.");
473                         continue;
474                 }
475
476                 new = cf_read_generic (old->values[0].value.string, depth + 1);
477                 if (new == NULL)
478                         continue;
479
480                 /* Now replace the i'th child in `root' with `new'. */
481                 cf_ci_replace_child (root, new, i);
482
483                 sfree (new->values);
484                 sfree (new);
485         } /* for (i = 0; i < root->children_num; i++) */
486
487         return (0);
488 } /* int cf_include_all */
489
490 static oconfig_item_t *cf_read_file (const char *file, int depth)
491 {
492         oconfig_item_t *root;
493
494         assert (depth < CF_MAX_DEPTH);
495
496         root = oconfig_parse_file (file);
497         if (root == NULL)
498         {
499                 ERROR ("configfile: Cannot read file `%s'.", file);
500                 return (NULL);
501         }
502
503         cf_include_all (root, depth);
504
505         return (root);
506 } /* oconfig_item_t *cf_read_file */
507
508 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
509 {
510         oconfig_item_t *root = NULL;
511         DIR *dh;
512         struct dirent *de;
513         char name[1024];
514         int status;
515
516         assert (depth < CF_MAX_DEPTH);
517
518         dh = opendir (dir);
519         if (dh == NULL)
520         {
521                 char errbuf[1024];
522                 ERROR ("configfile: opendir failed: %s",
523                                 sstrerror (errno, errbuf, sizeof (errbuf)));
524                 return (NULL);
525         }
526
527         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
528         if (root == NULL)
529         {
530                 ERROR ("configfile: malloc failed.");
531                 return (NULL);
532         }
533         memset (root, '\0', sizeof (oconfig_item_t));
534
535         while ((de = readdir (dh)) != NULL)
536         {
537                 oconfig_item_t *temp;
538
539                 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
540                         continue;
541
542                 status = snprintf (name, sizeof (name), "%s/%s",
543                                 dir, de->d_name);
544                 if (status >= sizeof (name))
545                 {
546                         ERROR ("configfile: Not including `%s/%s' because its"
547                                         " name is too long.",
548                                         dir, de->d_name);
549                         oconfig_free (root);
550                         return (NULL);
551                 }
552
553                 temp = cf_read_generic (name, depth);
554                 if (temp == NULL) {
555                         oconfig_free (root);
556                         return (NULL);
557                 }
558
559                 cf_ci_append_children (root, temp);
560                 sfree (temp->children);
561                 sfree (temp);
562         }
563
564         return (root);
565 } /* oconfig_item_t *cf_read_dir */
566
567 /* 
568  * cf_read_generic
569  *
570  * Path is stat'ed and either cf_read_file or cf_read_dir is called
571  * accordingly.
572  *
573  * There are two versions of this function: If `wordexp' exists shell wildcards
574  * will be expanded and the function will include all matches found. If
575  * `wordexp' (or, more precisely, it's header file) is not available the
576  * simpler function is used which does not do any such expansion.
577  */
578 #if HAVE_WORDEXP_H
579 static oconfig_item_t *cf_read_generic (const char *path, int depth)
580 {
581         oconfig_item_t *root = NULL;
582         int status;
583         const char *path_ptr;
584         wordexp_t we;
585         int i;
586
587         if (depth >= CF_MAX_DEPTH)
588         {
589                 ERROR ("configfile: Not including `%s' because the maximum "
590                                 "nesting depth has been reached.", path);
591                 return (NULL);
592         }
593
594         status = wordexp (path, &we, WRDE_NOCMD);
595         if (status != 0)
596         {
597                 ERROR ("configfile: wordexp (%s) failed.", path);
598                 return (NULL);
599         }
600
601         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
602         if (root == NULL)
603         {
604                 ERROR ("configfile: malloc failed.");
605                 return (NULL);
606         }
607         memset (root, '\0', sizeof (oconfig_item_t));
608
609         for (i = 0; i < we.we_wordc; i++)
610         {
611                 oconfig_item_t *temp;
612                 struct stat statbuf;
613
614                 path_ptr = we.we_wordv[i];
615
616                 status = stat (path_ptr, &statbuf);
617                 if (status != 0)
618                 {
619                         char errbuf[1024];
620                         ERROR ("configfile: stat (%s) failed: %s",
621                                         path_ptr,
622                                         sstrerror (errno, errbuf, sizeof (errbuf)));
623                         oconfig_free (root);
624                         return (NULL);
625                 }
626
627                 if (S_ISREG (statbuf.st_mode))
628                         temp = cf_read_file (path_ptr, depth);
629                 else if (S_ISDIR (statbuf.st_mode))
630                         temp = cf_read_dir (path_ptr, depth);
631                 else
632                 {
633                         ERROR ("configfile: %s is neither a file nor a "
634                                         "directory.", path);
635                         continue;
636                 }
637
638                 if (temp == NULL) {
639                         oconfig_free (root);
640                         return (NULL);
641                 }
642
643                 cf_ci_append_children (root, temp);
644                 sfree (temp->children);
645                 sfree (temp);
646         }
647
648         wordfree (&we);
649
650         return (root);
651 } /* oconfig_item_t *cf_read_generic */
652 /* #endif HAVE_WORDEXP_H */
653
654 #else /* if !HAVE_WORDEXP_H */
655 static oconfig_item_t *cf_read_generic (const char *path, int depth)
656 {
657         struct stat statbuf;
658         int status;
659
660         if (depth >= CF_MAX_DEPTH)
661         {
662                 ERROR ("configfile: Not including `%s' because the maximum "
663                                 "nesting depth has been reached.", path);
664                 return (NULL);
665         }
666
667         status = stat (path, &statbuf);
668         if (status != 0)
669         {
670                 char errbuf[1024];
671                 ERROR ("configfile: stat (%s) failed: %s",
672                                 path,
673                                 sstrerror (errno, errbuf, sizeof (errbuf)));
674                 return (NULL);
675         }
676
677         if (S_ISREG (statbuf.st_mode))
678                 return (cf_read_file (path, depth));
679         else if (S_ISDIR (statbuf.st_mode))
680                 return (cf_read_dir (path, depth));
681
682         ERROR ("configfile: %s is neither a file nor a directory.", path);
683         return (NULL);
684 } /* oconfig_item_t *cf_read_generic */
685 #endif /* !HAVE_WORDEXP_H */
686
687 /* 
688  * Public functions
689  */
690 int global_option_set (const char *option, const char *value)
691 {
692         int i;
693
694         DEBUG ("option = %s; value = %s;", option, value);
695
696         for (i = 0; i < cf_global_options_num; i++)
697                 if (strcasecmp (cf_global_options[i].key, option) == 0)
698                         break;
699
700         if (i >= cf_global_options_num)
701                 return (-1);
702
703         sfree (cf_global_options[i].value);
704
705         if (value != NULL)
706                 cf_global_options[i].value = strdup (value);
707         else
708                 cf_global_options[i].value = NULL;
709
710         return (0);
711 }
712
713 const char *global_option_get (const char *option)
714 {
715         int i;
716
717         for (i = 0; i < cf_global_options_num; i++)
718                 if (strcasecmp (cf_global_options[i].key, option) == 0)
719                         break;
720
721         if (i >= cf_global_options_num)
722                 return (NULL);
723         
724         return ((cf_global_options[i].value != NULL)
725                         ? cf_global_options[i].value
726                         : cf_global_options[i].def);
727 } /* char *global_option_get */
728
729 void cf_unregister (const char *type)
730 {
731         cf_callback_t *this, *prev;
732
733         for (prev = NULL, this = first_callback;
734                         this != NULL;
735                         prev = this, this = this->next)
736                 if (strcasecmp (this->type, type) == 0)
737                 {
738                         if (prev == NULL)
739                                 first_callback = this->next;
740                         else
741                                 prev->next = this->next;
742
743                         free (this);
744                         break;
745                 }
746 } /* void cf_unregister */
747
748 void cf_unregister_complex (const char *type)
749 {
750         cf_complex_callback_t *this, *prev;
751
752         for (prev = NULL, this = complex_callback_head;
753                         this != NULL;
754                         prev = this, this = this->next)
755                 if (strcasecmp (this->type, type) == 0)
756                 {
757                         if (prev == NULL)
758                                 complex_callback_head = this->next;
759                         else
760                                 prev->next = this->next;
761
762                         sfree (this->type);
763                         sfree (this);
764                         break;
765                 }
766 } /* void cf_unregister */
767
768 void cf_register (const char *type,
769                 int (*callback) (const char *, const char *),
770                 const char **keys, int keys_num)
771 {
772         cf_callback_t *cf_cb;
773
774         /* Remove this module from the list, if it already exists */
775         cf_unregister (type);
776
777         /* This pointer will be free'd in `cf_unregister' */
778         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
779                 return;
780
781         cf_cb->type     = type;
782         cf_cb->callback = callback;
783         cf_cb->keys     = keys;
784         cf_cb->keys_num = keys_num;
785
786         cf_cb->next = first_callback;
787         first_callback = cf_cb;
788 } /* void cf_register */
789
790 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
791 {
792         cf_complex_callback_t *new;
793
794         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
795         if (new == NULL)
796                 return (-1);
797
798         new->type = strdup (type);
799         if (new->type == NULL)
800         {
801                 sfree (new);
802                 return (-1);
803         }
804
805         new->callback = callback;
806         new->next = NULL;
807
808         if (complex_callback_head == NULL)
809         {
810                 complex_callback_head = new;
811         }
812         else
813         {
814                 cf_complex_callback_t *last = complex_callback_head;
815                 while (last->next != NULL)
816                         last = last->next;
817                 last->next = new;
818         }
819
820         return (0);
821 } /* int cf_register_complex */
822
823 int cf_read (char *filename)
824 {
825         oconfig_item_t *conf;
826         int i;
827
828         conf = cf_read_generic (filename, 0 /* depth */);
829         if (conf == NULL)
830         {
831                 ERROR ("Unable to read config file %s.", filename);
832                 return (-1);
833         }
834
835         for (i = 0; i < conf->children_num; i++)
836         {
837                 if (conf->children[i].children == NULL)
838                         dispatch_value (conf->children + i);
839                 else
840                         dispatch_block (conf->children + i);
841         }
842
843         if (cf_default_typesdb)
844                 read_types_list (PLUGINDIR"/types.db"); /* FIXME: Configure path */
845         return (0);
846 } /* int cf_read */