Merge branch 'sh/next'
[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  *   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 "utils_threshold.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         struct cf_callback *next;
50 } cf_callback_t;
51
52 typedef struct cf_complex_callback_s
53 {
54         char *type;
55         int (*callback) (oconfig_item_t *);
56         struct cf_complex_callback_s *next;
57 } cf_complex_callback_t;
58
59 typedef struct cf_value_map_s
60 {
61         char *key;
62         int (*func) (const oconfig_item_t *);
63 } cf_value_map_t;
64
65 typedef struct cf_global_option_s
66 {
67         char *key;
68         char *value;
69         char *def;
70 } cf_global_option_t;
71
72 /*
73  * Prototypes of callback functions
74  */
75 static int dispatch_value_typesdb (const oconfig_item_t *ci);
76 static int dispatch_value_plugindir (const oconfig_item_t *ci);
77 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
78
79 /*
80  * Private variables
81  */
82 static cf_callback_t *first_callback = NULL;
83 static cf_complex_callback_t *complex_callback_head = NULL;
84
85 static cf_value_map_t cf_value_map[] =
86 {
87         {"TypesDB",    dispatch_value_typesdb},
88         {"PluginDir",  dispatch_value_plugindir},
89         {"LoadPlugin", dispatch_value_loadplugin}
90 };
91 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
92
93 static cf_global_option_t cf_global_options[] =
94 {
95         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
96         {"PIDFile",     NULL, PIDFILE},
97         {"Hostname",    NULL, NULL},
98         {"FQDNLookup",  NULL, "false"},
99         {"Interval",    NULL, "10"},
100         {"ReadThreads", NULL, "5"}
101 };
102 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
103
104 static int cf_default_typesdb = 1;
105
106 /*
107  * Functions to handle register/unregister, search, and other plugin related
108  * stuff
109  */
110 static cf_callback_t *cf_search (const char *type)
111 {
112         cf_callback_t *cf_cb;
113
114         if (type == NULL)
115                 return (NULL);
116
117         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
118                 if (strcasecmp (cf_cb->type, type) == 0)
119                         break;
120
121         return (cf_cb);
122 }
123
124 static int cf_dispatch (const char *type, const char *orig_key,
125                 const char *orig_value)
126 {
127         cf_callback_t *cf_cb;
128         char *key;
129         char *value;
130         int ret;
131         int i;
132
133         DEBUG ("type = %s, key = %s, value = %s",
134                         ESCAPE_NULL(type),
135                         ESCAPE_NULL(orig_key),
136                         ESCAPE_NULL(orig_value));
137
138         if ((cf_cb = cf_search (type)) == NULL)
139         {
140                 WARNING ("Found a configuration for the `%s' plugin, but "
141                                 "the plugin isn't loaded or didn't register "
142                                 "a configuration callback.", type);
143                 return (-1);
144         }
145
146         if ((key = strdup (orig_key)) == NULL)
147                 return (1);
148         if ((value = strdup (orig_value)) == NULL)
149         {
150                 free (key);
151                 return (2);
152         }
153
154         ret = -1;
155
156         for (i = 0; i < cf_cb->keys_num; i++)
157         {
158                 if (strcasecmp (cf_cb->keys[i], key) == 0)
159                 {
160                         ret = (*cf_cb->callback) (key, value);
161                         break;
162                 }
163         }
164
165         if (i >= cf_cb->keys_num)
166                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
167
168         free (key);
169         free (value);
170
171         DEBUG ("return (%i)", ret);
172
173         return (ret);
174 } /* int cf_dispatch */
175
176 static int dispatch_global_option (const oconfig_item_t *ci)
177 {
178         if (ci->values_num != 1)
179                 return (-1);
180         if (ci->values[0].type == OCONFIG_TYPE_STRING)
181                 return (global_option_set (ci->key, ci->values[0].value.string));
182         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
183         {
184                 char tmp[128];
185                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
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 = ssnprintf (buffer_ptr, buffer_free, " %s",
261                                         ci->values[i].value.string);
262                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
263                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
264                                         ci->values[i].value.number);
265                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
266                         status = ssnprintf (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 dispatch_value_plugin */
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 allowed 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 int cf_compare_string (const void *p1, const void *p2)
509 {
510         return strcmp (*(const char **) p1, *(const char **) p2);
511 }
512
513 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
514 {
515         oconfig_item_t *root = NULL;
516         DIR *dh;
517         struct dirent *de;
518         char **filenames = NULL;
519         int filenames_num = 0;
520         int status;
521         int i;
522
523         assert (depth < CF_MAX_DEPTH);
524
525         dh = opendir (dir);
526         if (dh == NULL)
527         {
528                 char errbuf[1024];
529                 ERROR ("configfile: opendir failed: %s",
530                                 sstrerror (errno, errbuf, sizeof (errbuf)));
531                 return (NULL);
532         }
533
534         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
535         if (root == NULL)
536         {
537                 ERROR ("configfile: malloc failed.");
538                 return (NULL);
539         }
540         memset (root, '\0', sizeof (oconfig_item_t));
541
542         while ((de = readdir (dh)) != NULL)
543         {
544                 char   name[1024];
545                 char **tmp;
546
547                 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
548                         continue;
549
550                 status = ssnprintf (name, sizeof (name), "%s/%s",
551                                 dir, de->d_name);
552                 if (status >= sizeof (name))
553                 {
554                         ERROR ("configfile: Not including `%s/%s' because its"
555                                         " name is too long.",
556                                         dir, de->d_name);
557                         for (i = 0; i < filenames_num; ++i)
558                                 free (filenames[i]);
559                         free (filenames);
560                         free (root);
561                         return (NULL);
562                 }
563
564                 ++filenames_num;
565                 tmp = (char **) realloc (filenames,
566                                 filenames_num * sizeof (*filenames));
567                 if (tmp == NULL) {
568                         ERROR ("configfile: realloc failed.");
569                         for (i = 0; i < filenames_num - 1; ++i)
570                                 free (filenames[i]);
571                         free (filenames);
572                         free (root);
573                         return (NULL);
574                 }
575                 filenames = tmp;
576
577                 filenames[filenames_num - 1] = sstrdup (name);
578         }
579
580         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
581                         cf_compare_string);
582
583         for (i = 0; i < filenames_num; ++i)
584         {
585                 oconfig_item_t *temp;
586                 char *name = filenames[i];
587
588                 temp = cf_read_generic (name, depth);
589                 if (temp == NULL) {
590                         int j;
591                         for (j = i; j < filenames_num; ++j)
592                                 free (filenames[j]);
593                         free (filenames);
594                         oconfig_free (root);
595                         return (NULL);
596                 }
597
598                 cf_ci_append_children (root, temp);
599                 sfree (temp->children);
600                 sfree (temp);
601
602                 free (name);
603         }
604
605         free(filenames);
606         return (root);
607 } /* oconfig_item_t *cf_read_dir */
608
609 /* 
610  * cf_read_generic
611  *
612  * Path is stat'ed and either cf_read_file or cf_read_dir is called
613  * accordingly.
614  *
615  * There are two versions of this function: If `wordexp' exists shell wildcards
616  * will be expanded and the function will include all matches found. If
617  * `wordexp' (or, more precisely, it's header file) is not available the
618  * simpler function is used which does not do any such expansion.
619  */
620 #if HAVE_WORDEXP_H
621 static oconfig_item_t *cf_read_generic (const char *path, int depth)
622 {
623         oconfig_item_t *root = NULL;
624         int status;
625         const char *path_ptr;
626         wordexp_t we;
627         int i;
628
629         if (depth >= CF_MAX_DEPTH)
630         {
631                 ERROR ("configfile: Not including `%s' because the maximum "
632                                 "nesting depth has been reached.", path);
633                 return (NULL);
634         }
635
636         status = wordexp (path, &we, WRDE_NOCMD);
637         if (status != 0)
638         {
639                 ERROR ("configfile: wordexp (%s) failed.", path);
640                 return (NULL);
641         }
642
643         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
644         if (root == NULL)
645         {
646                 ERROR ("configfile: malloc failed.");
647                 return (NULL);
648         }
649         memset (root, '\0', sizeof (oconfig_item_t));
650
651         /* wordexp() might return a sorted list already. That's not
652          * documented though, so let's make sure we get what we want. */
653         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
654                         cf_compare_string);
655
656         for (i = 0; i < we.we_wordc; i++)
657         {
658                 oconfig_item_t *temp;
659                 struct stat statbuf;
660
661                 path_ptr = we.we_wordv[i];
662
663                 status = stat (path_ptr, &statbuf);
664                 if (status != 0)
665                 {
666                         char errbuf[1024];
667                         ERROR ("configfile: stat (%s) failed: %s",
668                                         path_ptr,
669                                         sstrerror (errno, errbuf, sizeof (errbuf)));
670                         oconfig_free (root);
671                         return (NULL);
672                 }
673
674                 if (S_ISREG (statbuf.st_mode))
675                         temp = cf_read_file (path_ptr, depth);
676                 else if (S_ISDIR (statbuf.st_mode))
677                         temp = cf_read_dir (path_ptr, depth);
678                 else
679                 {
680                         ERROR ("configfile: %s is neither a file nor a "
681                                         "directory.", path);
682                         continue;
683                 }
684
685                 if (temp == NULL) {
686                         oconfig_free (root);
687                         return (NULL);
688                 }
689
690                 cf_ci_append_children (root, temp);
691                 sfree (temp->children);
692                 sfree (temp);
693         }
694
695         wordfree (&we);
696
697         return (root);
698 } /* oconfig_item_t *cf_read_generic */
699 /* #endif HAVE_WORDEXP_H */
700
701 #else /* if !HAVE_WORDEXP_H */
702 static oconfig_item_t *cf_read_generic (const char *path, int depth)
703 {
704         struct stat statbuf;
705         int status;
706
707         if (depth >= CF_MAX_DEPTH)
708         {
709                 ERROR ("configfile: Not including `%s' because the maximum "
710                                 "nesting depth has been reached.", path);
711                 return (NULL);
712         }
713
714         status = stat (path, &statbuf);
715         if (status != 0)
716         {
717                 char errbuf[1024];
718                 ERROR ("configfile: stat (%s) failed: %s",
719                                 path,
720                                 sstrerror (errno, errbuf, sizeof (errbuf)));
721                 return (NULL);
722         }
723
724         if (S_ISREG (statbuf.st_mode))
725                 return (cf_read_file (path, depth));
726         else if (S_ISDIR (statbuf.st_mode))
727                 return (cf_read_dir (path, depth));
728
729         ERROR ("configfile: %s is neither a file nor a directory.", path);
730         return (NULL);
731 } /* oconfig_item_t *cf_read_generic */
732 #endif /* !HAVE_WORDEXP_H */
733
734 /* 
735  * Public functions
736  */
737 int global_option_set (const char *option, const char *value)
738 {
739         int i;
740
741         DEBUG ("option = %s; value = %s;", option, value);
742
743         for (i = 0; i < cf_global_options_num; i++)
744                 if (strcasecmp (cf_global_options[i].key, option) == 0)
745                         break;
746
747         if (i >= cf_global_options_num)
748                 return (-1);
749
750         sfree (cf_global_options[i].value);
751
752         if (value != NULL)
753                 cf_global_options[i].value = strdup (value);
754         else
755                 cf_global_options[i].value = NULL;
756
757         return (0);
758 }
759
760 const char *global_option_get (const char *option)
761 {
762         int i;
763
764         for (i = 0; i < cf_global_options_num; i++)
765                 if (strcasecmp (cf_global_options[i].key, option) == 0)
766                         break;
767
768         if (i >= cf_global_options_num)
769                 return (NULL);
770         
771         return ((cf_global_options[i].value != NULL)
772                         ? cf_global_options[i].value
773                         : cf_global_options[i].def);
774 } /* char *global_option_get */
775
776 void cf_unregister (const char *type)
777 {
778         cf_callback_t *this, *prev;
779
780         for (prev = NULL, this = first_callback;
781                         this != NULL;
782                         prev = this, this = this->next)
783                 if (strcasecmp (this->type, type) == 0)
784                 {
785                         if (prev == NULL)
786                                 first_callback = this->next;
787                         else
788                                 prev->next = this->next;
789
790                         free (this);
791                         break;
792                 }
793 } /* void cf_unregister */
794
795 void cf_unregister_complex (const char *type)
796 {
797         cf_complex_callback_t *this, *prev;
798
799         for (prev = NULL, this = complex_callback_head;
800                         this != NULL;
801                         prev = this, this = this->next)
802                 if (strcasecmp (this->type, type) == 0)
803                 {
804                         if (prev == NULL)
805                                 complex_callback_head = this->next;
806                         else
807                                 prev->next = this->next;
808
809                         sfree (this->type);
810                         sfree (this);
811                         break;
812                 }
813 } /* void cf_unregister */
814
815 void cf_register (const char *type,
816                 int (*callback) (const char *, const char *),
817                 const char **keys, int keys_num)
818 {
819         cf_callback_t *cf_cb;
820
821         /* Remove this module from the list, if it already exists */
822         cf_unregister (type);
823
824         /* This pointer will be free'd in `cf_unregister' */
825         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
826                 return;
827
828         cf_cb->type     = type;
829         cf_cb->callback = callback;
830         cf_cb->keys     = keys;
831         cf_cb->keys_num = keys_num;
832
833         cf_cb->next = first_callback;
834         first_callback = cf_cb;
835 } /* void cf_register */
836
837 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
838 {
839         cf_complex_callback_t *new;
840
841         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
842         if (new == NULL)
843                 return (-1);
844
845         new->type = strdup (type);
846         if (new->type == NULL)
847         {
848                 sfree (new);
849                 return (-1);
850         }
851
852         new->callback = callback;
853         new->next = NULL;
854
855         if (complex_callback_head == NULL)
856         {
857                 complex_callback_head = new;
858         }
859         else
860         {
861                 cf_complex_callback_t *last = complex_callback_head;
862                 while (last->next != NULL)
863                         last = last->next;
864                 last->next = new;
865         }
866
867         return (0);
868 } /* int cf_register_complex */
869
870 int cf_read (char *filename)
871 {
872         oconfig_item_t *conf;
873         int i;
874
875         conf = cf_read_generic (filename, 0 /* depth */);
876         if (conf == NULL)
877         {
878                 ERROR ("Unable to read config file %s.", filename);
879                 return (-1);
880         }
881
882         for (i = 0; i < conf->children_num; i++)
883         {
884                 if (conf->children[i].children == NULL)
885                         dispatch_value (conf->children + i);
886                 else
887                         dispatch_block (conf->children + i);
888         }
889
890         oconfig_free (conf);
891
892         /* Read the default types.db if no `TypesDB' option was given. */
893         if (cf_default_typesdb)
894                 read_types_list (PKGDATADIR"/types.db");
895
896         return (0);
897 } /* int cf_read */