configfile, types_list: Improved error messages when handling "TypesDB".
[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 ((cf_cb->keys[i] != NULL)
159                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
160                 {
161                         ret = (*cf_cb->callback) (key, value);
162                         break;
163                 }
164         }
165
166         if (i >= cf_cb->keys_num)
167                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
168
169         free (key);
170         free (value);
171
172         DEBUG ("return (%i)", ret);
173
174         return (ret);
175 } /* int cf_dispatch */
176
177 static int dispatch_global_option (const oconfig_item_t *ci)
178 {
179         if (ci->values_num != 1)
180                 return (-1);
181         if (ci->values[0].type == OCONFIG_TYPE_STRING)
182                 return (global_option_set (ci->key, ci->values[0].value.string));
183         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
184         {
185                 char tmp[128];
186                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
187                 return (global_option_set (ci->key, tmp));
188         }
189         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
190         {
191                 if (ci->values[0].value.boolean)
192                         return (global_option_set (ci->key, "true"));
193                 else
194                         return (global_option_set (ci->key, "false"));
195         }
196
197         return (-1);
198 } /* int dispatch_global_option */
199
200 static int dispatch_value_typesdb (const oconfig_item_t *ci)
201 {
202         int i = 0;
203
204         assert (strcasecmp (ci->key, "TypesDB") == 0);
205
206         cf_default_typesdb = 0;
207
208         if (ci->values_num < 1) {
209                 ERROR ("configfile: `TypesDB' needs at least one argument.");
210                 return (-1);
211         }
212
213         for (i = 0; i < ci->values_num; ++i)
214         {
215                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
216                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
217                                         "is not a string.", i + 1);
218                         continue;
219                 }
220
221                 read_types_list (ci->values[i].value.string);
222         }
223         return (0);
224 } /* int dispatch_value_typesdb */
225
226 static int dispatch_value_plugindir (const oconfig_item_t *ci)
227 {
228         assert (strcasecmp (ci->key, "PluginDir") == 0);
229         
230         if (ci->values_num != 1)
231                 return (-1);
232         if (ci->values[0].type != OCONFIG_TYPE_STRING)
233                 return (-1);
234
235         plugin_set_dir (ci->values[0].value.string);
236         return (0);
237 }
238
239 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
240 {
241         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
242
243         if (ci->values_num != 1)
244                 return (-1);
245         if (ci->values[0].type != OCONFIG_TYPE_STRING)
246                 return (-1);
247
248         return (plugin_load (ci->values[0].value.string));
249 } /* int dispatch_value_loadplugin */
250
251 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
252 {
253         char  buffer[4096];
254         char *buffer_ptr;
255         int   buffer_free;
256         int i;
257
258         buffer_ptr = buffer;
259         buffer_free = sizeof (buffer);
260
261         for (i = 0; i < ci->values_num; i++)
262         {
263                 int status = -1;
264
265                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
266                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
267                                         ci->values[i].value.string);
268                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
269                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
270                                         ci->values[i].value.number);
271                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
272                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
273                                         ci->values[i].value.boolean
274                                         ? "true" : "false");
275
276                 if ((status < 0) || (status >= buffer_free))
277                         return (-1);
278                 buffer_free -= status;
279                 buffer_ptr  += status;
280         }
281         /* skip the initial space */
282         buffer_ptr = buffer + 1;
283
284         return (cf_dispatch (plugin, ci->key, buffer_ptr));
285 } /* int dispatch_value_plugin */
286
287 static int dispatch_value (const oconfig_item_t *ci)
288 {
289         int ret = -2;
290         int i;
291
292         for (i = 0; i < cf_value_map_num; i++)
293                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
294                 {
295                         ret = cf_value_map[i].func (ci);
296                         break;
297                 }
298
299         for (i = 0; i < cf_global_options_num; i++)
300                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
301                 {
302                         ret = dispatch_global_option (ci);
303                         break;
304                 }
305
306         return (ret);
307 } /* int dispatch_value */
308
309 static int dispatch_block_plugin (oconfig_item_t *ci)
310 {
311         int i;
312         char *name;
313
314         cf_complex_callback_t *cb;
315
316         if (strcasecmp (ci->key, "Plugin") != 0)
317                 return (-1);
318         if (ci->values_num < 1)
319                 return (-1);
320         if (ci->values[0].type != OCONFIG_TYPE_STRING)
321                 return (-1);
322
323         name = ci->values[0].value.string;
324
325         /* Check for a complex callback first */
326         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
327                 if (strcasecmp (name, cb->type) == 0)
328                         return (cb->callback (ci));
329
330         /* Hm, no complex plugin found. Dispatch the values one by one */
331         for (i = 0; i < ci->children_num; i++)
332         {
333                 if (ci->children[i].children == NULL)
334                         dispatch_value_plugin (name, ci->children + i);
335                 else
336                         {DEBUG ("No nested config blocks allowed for this plugin.");}
337         }
338
339         return (0);
340 }
341
342
343 static int dispatch_block (oconfig_item_t *ci)
344 {
345         if (strcasecmp (ci->key, "Plugin") == 0)
346                 return (dispatch_block_plugin (ci));
347         else if (strcasecmp (ci->key, "Threshold") == 0)
348                 return (ut_config (ci));
349
350         return (0);
351 }
352
353 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
354                 int offset)
355 {
356         oconfig_item_t *temp;
357         int i;
358
359         assert (offset >= 0);
360         assert (dst->children_num > offset);
361
362         /* Free the memory used by the replaced child. Usually that's the
363          * `Include "blah"' statement. */
364         temp = dst->children + offset;
365         for (i = 0; i < temp->values_num; i++)
366         {
367                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
368                 {
369                         sfree (temp->values[i].value.string);
370                 }
371         }
372         sfree (temp->values);
373         temp = NULL;
374
375         /* If (src->children_num == 0) the array size is decreased. If offset
376          * is _not_ the last element, (offset < (src->children_num - 1)), then
377          * we need to move the trailing elements before resizing the array. */
378         if ((src->children_num == 0) && (offset < (src->children_num - 1)))
379         {
380                 int nmemb = src->children_num - (offset + 1);
381                 memmove (src->children + offset, src->children + offset + 1,
382                                 sizeof (oconfig_item_t) * nmemb);
383         }
384
385         /* Resize the memory containing the children to be big enough to hold
386          * all children. */
387         temp = (oconfig_item_t *) realloc (dst->children,
388                         sizeof (oconfig_item_t)
389                         * (dst->children_num + src->children_num - 1));
390         if (temp == NULL)
391         {
392                 ERROR ("configfile: realloc failed.");
393                 return (-1);
394         }
395         dst->children = temp;
396
397         /* If there are children behind the include statement, and they have
398          * not yet been moved because (src->children_num == 0), then move them
399          * to the end of the list, so that the new children have room before
400          * them. */
401         if ((src->children_num > 0)
402                         && ((dst->children_num - (offset + 1)) > 0))
403         {
404                 int nmemb = dst->children_num - (offset + 1);
405                 int old_offset = offset + 1;
406                 int new_offset = offset + src->children_num;
407
408                 memmove (dst->children + new_offset,
409                                 dst->children + old_offset,
410                                 sizeof (oconfig_item_t) * nmemb);
411         }
412
413         /* Last but not least: If there are new childrem, copy them to the
414          * memory reserved for them. */
415         if (src->children_num > 0)
416         {
417                 memcpy (dst->children + offset,
418                                 src->children,
419                                 sizeof (oconfig_item_t) * src->children_num);
420         }
421
422         /* Update the number of children. */
423         dst->children_num += (src->children_num - 1);
424
425         return (0);
426 } /* int cf_ci_replace_child */
427
428 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
429 {
430         oconfig_item_t *temp;
431
432         if ((src == NULL) || (src->children_num == 0))
433                 return (0);
434
435         temp = (oconfig_item_t *) realloc (dst->children,
436                         sizeof (oconfig_item_t)
437                         * (dst->children_num + src->children_num));
438         if (temp == NULL)
439         {
440                 ERROR ("configfile: realloc failed.");
441                 return (-1);
442         }
443         dst->children = temp;
444
445         memcpy (dst->children + dst->children_num,
446                         src->children,
447                         sizeof (oconfig_item_t)
448                         * src->children_num);
449         dst->children_num += src->children_num;
450
451         return (0);
452 } /* int cf_ci_append_children */
453
454 #define CF_MAX_DEPTH 8
455 static oconfig_item_t *cf_read_generic (const char *path, int depth);
456
457 static int cf_include_all (oconfig_item_t *root, int depth)
458 {
459         int i;
460
461         for (i = 0; i < root->children_num; i++)
462         {
463                 oconfig_item_t *new;
464                 oconfig_item_t *old;
465
466                 /* Ignore all blocks, including `Include' blocks. */
467                 if (root->children[i].children_num != 0)
468                         continue;
469
470                 if (strcasecmp (root->children[i].key, "Include") != 0)
471                         continue;
472
473                 old = root->children + i;
474
475                 if ((old->values_num != 1)
476                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
477                 {
478                         ERROR ("configfile: `Include' needs exactly one string argument.");
479                         continue;
480                 }
481
482                 new = cf_read_generic (old->values[0].value.string, depth + 1);
483                 if (new == NULL)
484                         continue;
485
486                 /* Now replace the i'th child in `root' with `new'. */
487                 cf_ci_replace_child (root, new, i);
488
489                 sfree (new->values);
490                 sfree (new);
491         } /* for (i = 0; i < root->children_num; i++) */
492
493         return (0);
494 } /* int cf_include_all */
495
496 static oconfig_item_t *cf_read_file (const char *file, int depth)
497 {
498         oconfig_item_t *root;
499
500         assert (depth < CF_MAX_DEPTH);
501
502         root = oconfig_parse_file (file);
503         if (root == NULL)
504         {
505                 ERROR ("configfile: Cannot read file `%s'.", file);
506                 return (NULL);
507         }
508
509         cf_include_all (root, depth);
510
511         return (root);
512 } /* oconfig_item_t *cf_read_file */
513
514 static int cf_compare_string (const void *p1, const void *p2)
515 {
516         return strcmp (*(const char **) p1, *(const char **) p2);
517 }
518
519 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
520 {
521         oconfig_item_t *root = NULL;
522         DIR *dh;
523         struct dirent *de;
524         char **filenames = NULL;
525         int filenames_num = 0;
526         int status;
527         int i;
528
529         assert (depth < CF_MAX_DEPTH);
530
531         dh = opendir (dir);
532         if (dh == NULL)
533         {
534                 char errbuf[1024];
535                 ERROR ("configfile: opendir failed: %s",
536                                 sstrerror (errno, errbuf, sizeof (errbuf)));
537                 return (NULL);
538         }
539
540         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
541         if (root == NULL)
542         {
543                 ERROR ("configfile: malloc failed.");
544                 return (NULL);
545         }
546         memset (root, '\0', sizeof (oconfig_item_t));
547
548         while ((de = readdir (dh)) != NULL)
549         {
550                 char   name[1024];
551                 char **tmp;
552
553                 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
554                         continue;
555
556                 status = ssnprintf (name, sizeof (name), "%s/%s",
557                                 dir, de->d_name);
558                 if (status >= sizeof (name))
559                 {
560                         ERROR ("configfile: Not including `%s/%s' because its"
561                                         " name is too long.",
562                                         dir, de->d_name);
563                         for (i = 0; i < filenames_num; ++i)
564                                 free (filenames[i]);
565                         free (filenames);
566                         free (root);
567                         return (NULL);
568                 }
569
570                 ++filenames_num;
571                 tmp = (char **) realloc (filenames,
572                                 filenames_num * sizeof (*filenames));
573                 if (tmp == NULL) {
574                         ERROR ("configfile: realloc failed.");
575                         for (i = 0; i < filenames_num - 1; ++i)
576                                 free (filenames[i]);
577                         free (filenames);
578                         free (root);
579                         return (NULL);
580                 }
581                 filenames = tmp;
582
583                 filenames[filenames_num - 1] = sstrdup (name);
584         }
585
586         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
587                         cf_compare_string);
588
589         for (i = 0; i < filenames_num; ++i)
590         {
591                 oconfig_item_t *temp;
592                 char *name = filenames[i];
593
594                 temp = cf_read_generic (name, depth);
595                 if (temp == NULL) {
596                         int j;
597                         for (j = i; j < filenames_num; ++j)
598                                 free (filenames[j]);
599                         free (filenames);
600                         oconfig_free (root);
601                         return (NULL);
602                 }
603
604                 cf_ci_append_children (root, temp);
605                 sfree (temp->children);
606                 sfree (temp);
607
608                 free (name);
609         }
610
611         free(filenames);
612         return (root);
613 } /* oconfig_item_t *cf_read_dir */
614
615 /* 
616  * cf_read_generic
617  *
618  * Path is stat'ed and either cf_read_file or cf_read_dir is called
619  * accordingly.
620  *
621  * There are two versions of this function: If `wordexp' exists shell wildcards
622  * will be expanded and the function will include all matches found. If
623  * `wordexp' (or, more precisely, it's header file) is not available the
624  * simpler function is used which does not do any such expansion.
625  */
626 #if HAVE_WORDEXP_H
627 static oconfig_item_t *cf_read_generic (const char *path, int depth)
628 {
629         oconfig_item_t *root = NULL;
630         int status;
631         const char *path_ptr;
632         wordexp_t we;
633         int i;
634
635         if (depth >= CF_MAX_DEPTH)
636         {
637                 ERROR ("configfile: Not including `%s' because the maximum "
638                                 "nesting depth has been reached.", path);
639                 return (NULL);
640         }
641
642         status = wordexp (path, &we, WRDE_NOCMD);
643         if (status != 0)
644         {
645                 ERROR ("configfile: wordexp (%s) failed.", path);
646                 return (NULL);
647         }
648
649         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
650         if (root == NULL)
651         {
652                 ERROR ("configfile: malloc failed.");
653                 return (NULL);
654         }
655         memset (root, '\0', sizeof (oconfig_item_t));
656
657         /* wordexp() might return a sorted list already. That's not
658          * documented though, so let's make sure we get what we want. */
659         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
660                         cf_compare_string);
661
662         for (i = 0; i < we.we_wordc; i++)
663         {
664                 oconfig_item_t *temp;
665                 struct stat statbuf;
666
667                 path_ptr = we.we_wordv[i];
668
669                 status = stat (path_ptr, &statbuf);
670                 if (status != 0)
671                 {
672                         char errbuf[1024];
673                         ERROR ("configfile: stat (%s) failed: %s",
674                                         path_ptr,
675                                         sstrerror (errno, errbuf, sizeof (errbuf)));
676                         oconfig_free (root);
677                         return (NULL);
678                 }
679
680                 if (S_ISREG (statbuf.st_mode))
681                         temp = cf_read_file (path_ptr, depth);
682                 else if (S_ISDIR (statbuf.st_mode))
683                         temp = cf_read_dir (path_ptr, depth);
684                 else
685                 {
686                         ERROR ("configfile: %s is neither a file nor a "
687                                         "directory.", path);
688                         continue;
689                 }
690
691                 if (temp == NULL) {
692                         oconfig_free (root);
693                         return (NULL);
694                 }
695
696                 cf_ci_append_children (root, temp);
697                 sfree (temp->children);
698                 sfree (temp);
699         }
700
701         wordfree (&we);
702
703         return (root);
704 } /* oconfig_item_t *cf_read_generic */
705 /* #endif HAVE_WORDEXP_H */
706
707 #else /* if !HAVE_WORDEXP_H */
708 static oconfig_item_t *cf_read_generic (const char *path, int depth)
709 {
710         struct stat statbuf;
711         int status;
712
713         if (depth >= CF_MAX_DEPTH)
714         {
715                 ERROR ("configfile: Not including `%s' because the maximum "
716                                 "nesting depth has been reached.", path);
717                 return (NULL);
718         }
719
720         status = stat (path, &statbuf);
721         if (status != 0)
722         {
723                 char errbuf[1024];
724                 ERROR ("configfile: stat (%s) failed: %s",
725                                 path,
726                                 sstrerror (errno, errbuf, sizeof (errbuf)));
727                 return (NULL);
728         }
729
730         if (S_ISREG (statbuf.st_mode))
731                 return (cf_read_file (path, depth));
732         else if (S_ISDIR (statbuf.st_mode))
733                 return (cf_read_dir (path, depth));
734
735         ERROR ("configfile: %s is neither a file nor a directory.", path);
736         return (NULL);
737 } /* oconfig_item_t *cf_read_generic */
738 #endif /* !HAVE_WORDEXP_H */
739
740 /* 
741  * Public functions
742  */
743 int global_option_set (const char *option, const char *value)
744 {
745         int i;
746
747         DEBUG ("option = %s; value = %s;", option, value);
748
749         for (i = 0; i < cf_global_options_num; i++)
750                 if (strcasecmp (cf_global_options[i].key, option) == 0)
751                         break;
752
753         if (i >= cf_global_options_num)
754                 return (-1);
755
756         sfree (cf_global_options[i].value);
757
758         if (value != NULL)
759                 cf_global_options[i].value = strdup (value);
760         else
761                 cf_global_options[i].value = NULL;
762
763         return (0);
764 }
765
766 const char *global_option_get (const char *option)
767 {
768         int i;
769
770         for (i = 0; i < cf_global_options_num; i++)
771                 if (strcasecmp (cf_global_options[i].key, option) == 0)
772                         break;
773
774         if (i >= cf_global_options_num)
775                 return (NULL);
776         
777         return ((cf_global_options[i].value != NULL)
778                         ? cf_global_options[i].value
779                         : cf_global_options[i].def);
780 } /* char *global_option_get */
781
782 void cf_unregister (const char *type)
783 {
784         cf_callback_t *this, *prev;
785
786         for (prev = NULL, this = first_callback;
787                         this != NULL;
788                         prev = this, this = this->next)
789                 if (strcasecmp (this->type, type) == 0)
790                 {
791                         if (prev == NULL)
792                                 first_callback = this->next;
793                         else
794                                 prev->next = this->next;
795
796                         free (this);
797                         break;
798                 }
799 } /* void cf_unregister */
800
801 void cf_unregister_complex (const char *type)
802 {
803         cf_complex_callback_t *this, *prev;
804
805         for (prev = NULL, this = complex_callback_head;
806                         this != NULL;
807                         prev = this, this = this->next)
808                 if (strcasecmp (this->type, type) == 0)
809                 {
810                         if (prev == NULL)
811                                 complex_callback_head = this->next;
812                         else
813                                 prev->next = this->next;
814
815                         sfree (this->type);
816                         sfree (this);
817                         break;
818                 }
819 } /* void cf_unregister */
820
821 void cf_register (const char *type,
822                 int (*callback) (const char *, const char *),
823                 const char **keys, int keys_num)
824 {
825         cf_callback_t *cf_cb;
826
827         /* Remove this module from the list, if it already exists */
828         cf_unregister (type);
829
830         /* This pointer will be free'd in `cf_unregister' */
831         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
832                 return;
833
834         cf_cb->type     = type;
835         cf_cb->callback = callback;
836         cf_cb->keys     = keys;
837         cf_cb->keys_num = keys_num;
838
839         cf_cb->next = first_callback;
840         first_callback = cf_cb;
841 } /* void cf_register */
842
843 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
844 {
845         cf_complex_callback_t *new;
846
847         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
848         if (new == NULL)
849                 return (-1);
850
851         new->type = strdup (type);
852         if (new->type == NULL)
853         {
854                 sfree (new);
855                 return (-1);
856         }
857
858         new->callback = callback;
859         new->next = NULL;
860
861         if (complex_callback_head == NULL)
862         {
863                 complex_callback_head = new;
864         }
865         else
866         {
867                 cf_complex_callback_t *last = complex_callback_head;
868                 while (last->next != NULL)
869                         last = last->next;
870                 last->next = new;
871         }
872
873         return (0);
874 } /* int cf_register_complex */
875
876 int cf_read (char *filename)
877 {
878         oconfig_item_t *conf;
879         int i;
880
881         conf = cf_read_generic (filename, 0 /* depth */);
882         if (conf == NULL)
883         {
884                 ERROR ("Unable to read config file %s.", filename);
885                 return (-1);
886         }
887
888         for (i = 0; i < conf->children_num; i++)
889         {
890                 if (conf->children[i].children == NULL)
891                         dispatch_value (conf->children + i);
892                 else
893                         dispatch_block (conf->children + i);
894         }
895
896         oconfig_free (conf);
897
898         /* Read the default types.db if no `TypesDB' option was given. */
899         if (cf_default_typesdb)
900                 read_types_list (PLUGINDIR"/types.db");
901
902         return (0);
903 } /* int cf_read */