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