Fixed various signedness issues identified by -Wextra.
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2009  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 #include "filter_chain.h"
34
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
38
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
40
41 /*
42  * Private types
43  */
44 typedef struct cf_callback
45 {
46         const char  *type;
47         int  (*callback) (const char *, const char *);
48         const char **keys;
49         int    keys_num;
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         struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
59
60 typedef struct cf_value_map_s
61 {
62         char *key;
63         int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
65
66 typedef struct cf_global_option_s
67 {
68         char *key;
69         char *value;
70         char *def;
71 } cf_global_option_t;
72
73 /*
74  * Prototypes of callback functions
75  */
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
79
80 /*
81  * Private variables
82  */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
85
86 static cf_value_map_t cf_value_map[] =
87 {
88         {"TypesDB",    dispatch_value_typesdb},
89         {"PluginDir",  dispatch_value_plugindir},
90         {"LoadPlugin", dispatch_value_loadplugin}
91 };
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
93
94 static cf_global_option_t cf_global_options[] =
95 {
96         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
97         {"PIDFile",     NULL, PIDFILE},
98         {"Hostname",    NULL, NULL},
99         {"FQDNLookup",  NULL, "false"},
100         {"Interval",    NULL, "10"},
101         {"ReadThreads", NULL, "5"},
102         {"PreCacheChain",  NULL, "PreCache"},
103         {"PostCacheChain", NULL, "PostCache"}
104 };
105 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
106
107 static int cf_default_typesdb = 1;
108
109 /*
110  * Functions to handle register/unregister, search, and other plugin related
111  * stuff
112  */
113 static cf_callback_t *cf_search (const char *type)
114 {
115         cf_callback_t *cf_cb;
116
117         if (type == NULL)
118                 return (NULL);
119
120         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
121                 if (strcasecmp (cf_cb->type, type) == 0)
122                         break;
123
124         return (cf_cb);
125 }
126
127 static int cf_dispatch (const char *type, const char *orig_key,
128                 const char *orig_value)
129 {
130         cf_callback_t *cf_cb;
131         char *key;
132         char *value;
133         int ret;
134         int i;
135
136         DEBUG ("type = %s, key = %s, value = %s",
137                         ESCAPE_NULL(type),
138                         ESCAPE_NULL(orig_key),
139                         ESCAPE_NULL(orig_value));
140
141         if ((cf_cb = cf_search (type)) == NULL)
142         {
143                 WARNING ("Found a configuration for the `%s' plugin, but "
144                                 "the plugin isn't loaded or didn't register "
145                                 "a configuration callback.", type);
146                 return (-1);
147         }
148
149         if ((key = strdup (orig_key)) == NULL)
150                 return (1);
151         if ((value = strdup (orig_value)) == NULL)
152         {
153                 free (key);
154                 return (2);
155         }
156
157         ret = -1;
158
159         for (i = 0; i < cf_cb->keys_num; i++)
160         {
161                 if ((cf_cb->keys[i] != NULL)
162                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
163                 {
164                         ret = (*cf_cb->callback) (key, value);
165                         break;
166                 }
167         }
168
169         if (i >= cf_cb->keys_num)
170                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
171
172         free (key);
173         free (value);
174
175         DEBUG ("return (%i)", ret);
176
177         return (ret);
178 } /* int cf_dispatch */
179
180 static int dispatch_global_option (const oconfig_item_t *ci)
181 {
182         if (ci->values_num != 1)
183                 return (-1);
184         if (ci->values[0].type == OCONFIG_TYPE_STRING)
185                 return (global_option_set (ci->key, ci->values[0].value.string));
186         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
187         {
188                 char tmp[128];
189                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
190                 return (global_option_set (ci->key, tmp));
191         }
192         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
193         {
194                 if (ci->values[0].value.boolean)
195                         return (global_option_set (ci->key, "true"));
196                 else
197                         return (global_option_set (ci->key, "false"));
198         }
199
200         return (-1);
201 } /* int dispatch_global_option */
202
203 static int dispatch_value_typesdb (const oconfig_item_t *ci)
204 {
205         int i = 0;
206
207         assert (strcasecmp (ci->key, "TypesDB") == 0);
208
209         cf_default_typesdb = 0;
210
211         if (ci->values_num < 1)
212                 return (-1);
213
214         for (i = 0; i < ci->values_num; ++i)
215         {
216                 if (OCONFIG_TYPE_STRING != ci->values[i].type)
217                         continue;
218
219                 read_types_list (ci->values[i].value.string);
220         }
221         return (0);
222 } /* int dispatch_value_typesdb */
223
224 static int dispatch_value_plugindir (const oconfig_item_t *ci)
225 {
226         assert (strcasecmp (ci->key, "PluginDir") == 0);
227         
228         if (ci->values_num != 1)
229                 return (-1);
230         if (ci->values[0].type != OCONFIG_TYPE_STRING)
231                 return (-1);
232
233         plugin_set_dir (ci->values[0].value.string);
234         return (0);
235 }
236
237 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
238 {
239         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
240
241         if (ci->values_num != 1)
242                 return (-1);
243         if (ci->values[0].type != OCONFIG_TYPE_STRING)
244                 return (-1);
245
246         return (plugin_load (ci->values[0].value.string));
247 } /* int dispatch_value_loadplugin */
248
249 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
250 {
251         char  buffer[4096];
252         char *buffer_ptr;
253         int   buffer_free;
254         int i;
255
256         buffer_ptr = buffer;
257         buffer_free = sizeof (buffer);
258
259         for (i = 0; i < ci->values_num; i++)
260         {
261                 int status = -1;
262
263                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
264                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
265                                         ci->values[i].value.string);
266                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
267                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
268                                         ci->values[i].value.number);
269                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
270                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
271                                         ci->values[i].value.boolean
272                                         ? "true" : "false");
273
274                 if ((status < 0) || (status >= buffer_free))
275                         return (-1);
276                 buffer_free -= status;
277                 buffer_ptr  += status;
278         }
279         /* skip the initial space */
280         buffer_ptr = buffer + 1;
281
282         return (cf_dispatch (plugin, ci->key, buffer_ptr));
283 } /* int dispatch_value_plugin */
284
285 static int dispatch_value (const oconfig_item_t *ci)
286 {
287         int ret = -2;
288         int i;
289
290         for (i = 0; i < cf_value_map_num; i++)
291                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
292                 {
293                         ret = cf_value_map[i].func (ci);
294                         break;
295                 }
296
297         for (i = 0; i < cf_global_options_num; i++)
298                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
299                 {
300                         ret = dispatch_global_option (ci);
301                         break;
302                 }
303
304         return (ret);
305 } /* int dispatch_value */
306
307 static int dispatch_block_plugin (oconfig_item_t *ci)
308 {
309         int i;
310         char *name;
311
312         cf_complex_callback_t *cb;
313
314         if (strcasecmp (ci->key, "Plugin") != 0)
315                 return (-1);
316         if (ci->values_num < 1)
317                 return (-1);
318         if (ci->values[0].type != OCONFIG_TYPE_STRING)
319                 return (-1);
320
321         name = ci->values[0].value.string;
322
323         /* Check for a complex callback first */
324         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
325                 if (strcasecmp (name, cb->type) == 0)
326                         return (cb->callback (ci));
327
328         /* Hm, no complex plugin found. Dispatch the values one by one */
329         for (i = 0; i < ci->children_num; i++)
330         {
331                 if (ci->children[i].children == NULL)
332                         dispatch_value_plugin (name, ci->children + i);
333                 else
334                         {DEBUG ("No nested config blocks allowed for this plugin.");}
335         }
336
337         return (0);
338 }
339
340
341 static int dispatch_block (oconfig_item_t *ci)
342 {
343         if (strcasecmp (ci->key, "Plugin") == 0)
344                 return (dispatch_block_plugin (ci));
345         else if (strcasecmp (ci->key, "Threshold") == 0)
346                 return (ut_config (ci));
347         else if (strcasecmp (ci->key, "Chain") == 0)
348                 return (fc_configure (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 < 0) || ((size_t) 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         size_t 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 (PKGDATADIR"/types.db");
901
902         return (0);
903 } /* int cf_read */