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