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