Code

configfile: cf_read_dir: Don't abort reading a directory when reading one file fails.
[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  **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
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"
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
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;
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;
60 typedef struct cf_value_map_s
61 {
62         char *key;
63         int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
66 typedef struct cf_global_option_s
67 {
68         char *key;
69         char *value;
70         char *def;
71 } cf_global_option_t;
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);
80 /*
81  * Private variables
82  */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
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);
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);
107 static int cf_default_typesdb = 1;
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)
115         cf_callback_t *cf_cb;
117         if (type == NULL)
118                 return (NULL);
120         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
121                 if (strcasecmp (cf_cb->type, type) == 0)
122                         break;
124         return (cf_cb);
127 static int cf_dispatch (const char *type, const char *orig_key,
128                 const char *orig_value)
130         cf_callback_t *cf_cb;
131         char *key;
132         char *value;
133         int ret;
134         int i;
136         DEBUG ("type = %s, key = %s, value = %s",
137                         ESCAPE_NULL(type),
138                         ESCAPE_NULL(orig_key),
139                         ESCAPE_NULL(orig_value));
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         }
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         }
157         ret = -1;
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         }
169         if (i >= cf_cb->keys_num)
170                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
172         free (key);
173         free (value);
175         DEBUG ("cf_dispatch: return (%i)", ret);
177         return (ret);
178 } /* int cf_dispatch */
180 static int dispatch_global_option (const oconfig_item_t *ci)
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         }
200         return (-1);
201 } /* int dispatch_global_option */
203 static int dispatch_value_typesdb (const oconfig_item_t *ci)
205         int i = 0;
207         assert (strcasecmp (ci->key, "TypesDB") == 0);
209         cf_default_typesdb = 0;
211         if (ci->values_num < 1) {
212                 ERROR ("configfile: `TypesDB' needs at least one argument.");
213                 return (-1);
214         }
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                 }
224                 read_types_list (ci->values[i].value.string);
225         }
226         return (0);
227 } /* int dispatch_value_typesdb */
229 static int dispatch_value_plugindir (const oconfig_item_t *ci)
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);
238         plugin_set_dir (ci->values[0].value.string);
239         return (0);
242 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
244         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
246         if (ci->values_num != 1)
247                 return (-1);
248         if (ci->values[0].type != OCONFIG_TYPE_STRING)
249                 return (-1);
251         return (plugin_load (ci->values[0].value.string));
252 } /* int dispatch_value_loadplugin */
254 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
256         char  buffer[4096];
257         char *buffer_ptr;
258         int   buffer_free;
259         int i;
261         buffer_ptr = buffer;
262         buffer_free = sizeof (buffer);
264         for (i = 0; i < ci->values_num; i++)
265         {
266                 int status = -1;
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");
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;
287         return (cf_dispatch (plugin, ci->key, buffer_ptr));
288 } /* int dispatch_value_plugin */
290 static int dispatch_value (const oconfig_item_t *ci)
292         int ret = -2;
293         int i;
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                 }
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                 }
309         return (ret);
310 } /* int dispatch_value */
312 static int dispatch_block_plugin (oconfig_item_t *ci)
314         int i;
315         char *name;
317         cf_complex_callback_t *cb;
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);
326         name = ci->values[0].value.string;
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));
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                 {
340                         WARNING ("There is a `%s' block within the "
341                                         "configuration for the %s plugin. "
342                                         "The plugin either only expects "
343                                         "\"simple\" configuration statements "
344                                         "or wasn't loaded using `LoadPlugin'."
345                                         " Please check your configuration.",
346                                         ci->children[i].key, name);
347                 }
348         }
350         return (0);
354 static int dispatch_block (oconfig_item_t *ci)
356         if (strcasecmp (ci->key, "Plugin") == 0)
357                 return (dispatch_block_plugin (ci));
358         else if (strcasecmp (ci->key, "Threshold") == 0)
359                 return (ut_config (ci));
360         else if (strcasecmp (ci->key, "Chain") == 0)
361                 return (fc_configure (ci));
363         return (0);
366 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
367                 int offset)
369         oconfig_item_t *temp;
370         int i;
372         assert (offset >= 0);
373         assert (dst->children_num > offset);
375         /* Free the memory used by the replaced child. Usually that's the
376          * `Include "blah"' statement. */
377         temp = dst->children + offset;
378         for (i = 0; i < temp->values_num; i++)
379         {
380                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
381                 {
382                         sfree (temp->values[i].value.string);
383                 }
384         }
385         sfree (temp->values);
386         temp = NULL;
388         /* If (src->children_num == 0) the array size is decreased. If offset
389          * is _not_ the last element, (offset < (dst->children_num - 1)), then
390          * we need to move the trailing elements before resizing the array. */
391         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
392         {
393                 int nmemb = dst->children_num - (offset + 1);
394                 memmove (dst->children + offset, dst->children + offset + 1,
395                                 sizeof (oconfig_item_t) * nmemb);
396         }
398         /* Resize the memory containing the children to be big enough to hold
399          * all children. */
400         temp = (oconfig_item_t *) realloc (dst->children,
401                         sizeof (oconfig_item_t)
402                         * (dst->children_num + src->children_num - 1));
403         if (temp == NULL)
404         {
405                 ERROR ("configfile: realloc failed.");
406                 return (-1);
407         }
408         dst->children = temp;
410         /* If there are children behind the include statement, and they have
411          * not yet been moved because (src->children_num == 0), then move them
412          * to the end of the list, so that the new children have room before
413          * them. */
414         if ((src->children_num > 0)
415                         && ((dst->children_num - (offset + 1)) > 0))
416         {
417                 int nmemb = dst->children_num - (offset + 1);
418                 int old_offset = offset + 1;
419                 int new_offset = offset + src->children_num;
421                 memmove (dst->children + new_offset,
422                                 dst->children + old_offset,
423                                 sizeof (oconfig_item_t) * nmemb);
424         }
426         /* Last but not least: If there are new children, copy them to the
427          * memory reserved for them. */
428         if (src->children_num > 0)
429         {
430                 memcpy (dst->children + offset,
431                                 src->children,
432                                 sizeof (oconfig_item_t) * src->children_num);
433         }
435         /* Update the number of children. */
436         dst->children_num += (src->children_num - 1);
438         return (0);
439 } /* int cf_ci_replace_child */
441 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
443         oconfig_item_t *temp;
445         if ((src == NULL) || (src->children_num == 0))
446                 return (0);
448         temp = (oconfig_item_t *) realloc (dst->children,
449                         sizeof (oconfig_item_t)
450                         * (dst->children_num + src->children_num));
451         if (temp == NULL)
452         {
453                 ERROR ("configfile: realloc failed.");
454                 return (-1);
455         }
456         dst->children = temp;
458         memcpy (dst->children + dst->children_num,
459                         src->children,
460                         sizeof (oconfig_item_t)
461                         * src->children_num);
462         dst->children_num += src->children_num;
464         return (0);
465 } /* int cf_ci_append_children */
467 #define CF_MAX_DEPTH 8
468 static oconfig_item_t *cf_read_generic (const char *path, int depth);
470 static int cf_include_all (oconfig_item_t *root, int depth)
472         int i;
474         for (i = 0; i < root->children_num; i++)
475         {
476                 oconfig_item_t *new;
477                 oconfig_item_t *old;
479                 /* Ignore all blocks, including `Include' blocks. */
480                 if (root->children[i].children_num != 0)
481                         continue;
483                 if (strcasecmp (root->children[i].key, "Include") != 0)
484                         continue;
486                 old = root->children + i;
488                 if ((old->values_num != 1)
489                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
490                 {
491                         ERROR ("configfile: `Include' needs exactly one string argument.");
492                         continue;
493                 }
495                 new = cf_read_generic (old->values[0].value.string, depth + 1);
496                 if (new == NULL)
497                         continue;
499                 /* Now replace the i'th child in `root' with `new'. */
500                 cf_ci_replace_child (root, new, i);
502                 /* ... and go back to the new i'th child. */
503                 --i;
505                 sfree (new->values);
506                 sfree (new);
507         } /* for (i = 0; i < root->children_num; i++) */
509         return (0);
510 } /* int cf_include_all */
512 static oconfig_item_t *cf_read_file (const char *file, int depth)
514         oconfig_item_t *root;
516         assert (depth < CF_MAX_DEPTH);
518         root = oconfig_parse_file (file);
519         if (root == NULL)
520         {
521                 ERROR ("configfile: Cannot read file `%s'.", file);
522                 return (NULL);
523         }
525         cf_include_all (root, depth);
527         return (root);
528 } /* oconfig_item_t *cf_read_file */
530 static int cf_compare_string (const void *p1, const void *p2)
532         return strcmp (*(const char **) p1, *(const char **) p2);
535 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
537         oconfig_item_t *root = NULL;
538         DIR *dh;
539         struct dirent *de;
540         char **filenames = NULL;
541         int filenames_num = 0;
542         int status;
543         int i;
545         assert (depth < CF_MAX_DEPTH);
547         dh = opendir (dir);
548         if (dh == NULL)
549         {
550                 char errbuf[1024];
551                 ERROR ("configfile: opendir failed: %s",
552                                 sstrerror (errno, errbuf, sizeof (errbuf)));
553                 return (NULL);
554         }
556         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
557         if (root == NULL)
558         {
559                 ERROR ("configfile: malloc failed.");
560                 return (NULL);
561         }
562         memset (root, 0, sizeof (oconfig_item_t));
564         while ((de = readdir (dh)) != NULL)
565         {
566                 char   name[1024];
567                 char **tmp;
569                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
570                         continue;
572                 status = ssnprintf (name, sizeof (name), "%s/%s",
573                                 dir, de->d_name);
574                 if ((status < 0) || ((size_t) status >= sizeof (name)))
575                 {
576                         ERROR ("configfile: Not including `%s/%s' because its"
577                                         " name is too long.",
578                                         dir, de->d_name);
579                         for (i = 0; i < filenames_num; ++i)
580                                 free (filenames[i]);
581                         free (filenames);
582                         free (root);
583                         return (NULL);
584                 }
586                 ++filenames_num;
587                 tmp = (char **) realloc (filenames,
588                                 filenames_num * sizeof (*filenames));
589                 if (tmp == NULL) {
590                         ERROR ("configfile: realloc failed.");
591                         for (i = 0; i < filenames_num - 1; ++i)
592                                 free (filenames[i]);
593                         free (filenames);
594                         free (root);
595                         return (NULL);
596                 }
597                 filenames = tmp;
599                 filenames[filenames_num - 1] = sstrdup (name);
600         }
602         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
603                         cf_compare_string);
605         for (i = 0; i < filenames_num; ++i)
606         {
607                 oconfig_item_t *temp;
608                 char *name = filenames[i];
610                 temp = cf_read_generic (name, depth);
611                 if (temp == NULL)
612                 {
613                         /* An error should already have been reported. */
614                         sfree (name);
615                         continue;
616                 }
618                 cf_ci_append_children (root, temp);
619                 sfree (temp->children);
620                 sfree (temp);
622                 free (name);
623         }
625         free(filenames);
626         return (root);
627 } /* oconfig_item_t *cf_read_dir */
629 /* 
630  * cf_read_generic
631  *
632  * Path is stat'ed and either cf_read_file or cf_read_dir is called
633  * accordingly.
634  *
635  * There are two versions of this function: If `wordexp' exists shell wildcards
636  * will be expanded and the function will include all matches found. If
637  * `wordexp' (or, more precisely, it's header file) is not available the
638  * simpler function is used which does not do any such expansion.
639  */
640 #if HAVE_WORDEXP_H
641 static oconfig_item_t *cf_read_generic (const char *path, int depth)
643         oconfig_item_t *root = NULL;
644         int status;
645         const char *path_ptr;
646         wordexp_t we;
647         size_t i;
649         if (depth >= CF_MAX_DEPTH)
650         {
651                 ERROR ("configfile: Not including `%s' because the maximum "
652                                 "nesting depth has been reached.", path);
653                 return (NULL);
654         }
656         status = wordexp (path, &we, WRDE_NOCMD);
657         if (status != 0)
658         {
659                 ERROR ("configfile: wordexp (%s) failed.", path);
660                 return (NULL);
661         }
663         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
664         if (root == NULL)
665         {
666                 ERROR ("configfile: malloc failed.");
667                 return (NULL);
668         }
669         memset (root, '\0', sizeof (oconfig_item_t));
671         /* wordexp() might return a sorted list already. That's not
672          * documented though, so let's make sure we get what we want. */
673         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
674                         cf_compare_string);
676         for (i = 0; i < we.we_wordc; i++)
677         {
678                 oconfig_item_t *temp;
679                 struct stat statbuf;
681                 path_ptr = we.we_wordv[i];
683                 status = stat (path_ptr, &statbuf);
684                 if (status != 0)
685                 {
686                         char errbuf[1024];
687                         ERROR ("configfile: stat (%s) failed: %s",
688                                         path_ptr,
689                                         sstrerror (errno, errbuf, sizeof (errbuf)));
690                         oconfig_free (root);
691                         return (NULL);
692                 }
694                 if (S_ISREG (statbuf.st_mode))
695                         temp = cf_read_file (path_ptr, depth);
696                 else if (S_ISDIR (statbuf.st_mode))
697                         temp = cf_read_dir (path_ptr, depth);
698                 else
699                 {
700                         ERROR ("configfile: %s is neither a file nor a "
701                                         "directory.", path);
702                         continue;
703                 }
705                 if (temp == NULL) {
706                         oconfig_free (root);
707                         return (NULL);
708                 }
710                 cf_ci_append_children (root, temp);
711                 sfree (temp->children);
712                 sfree (temp);
713         }
715         wordfree (&we);
717         return (root);
718 } /* oconfig_item_t *cf_read_generic */
719 /* #endif HAVE_WORDEXP_H */
721 #else /* if !HAVE_WORDEXP_H */
722 static oconfig_item_t *cf_read_generic (const char *path, int depth)
724         struct stat statbuf;
725         int status;
727         if (depth >= CF_MAX_DEPTH)
728         {
729                 ERROR ("configfile: Not including `%s' because the maximum "
730                                 "nesting depth has been reached.", path);
731                 return (NULL);
732         }
734         status = stat (path, &statbuf);
735         if (status != 0)
736         {
737                 char errbuf[1024];
738                 ERROR ("configfile: stat (%s) failed: %s",
739                                 path,
740                                 sstrerror (errno, errbuf, sizeof (errbuf)));
741                 return (NULL);
742         }
744         if (S_ISREG (statbuf.st_mode))
745                 return (cf_read_file (path, depth));
746         else if (S_ISDIR (statbuf.st_mode))
747                 return (cf_read_dir (path, depth));
749         ERROR ("configfile: %s is neither a file nor a directory.", path);
750         return (NULL);
751 } /* oconfig_item_t *cf_read_generic */
752 #endif /* !HAVE_WORDEXP_H */
754 /* 
755  * Public functions
756  */
757 int global_option_set (const char *option, const char *value)
759         int i;
761         DEBUG ("option = %s; value = %s;", option, value);
763         for (i = 0; i < cf_global_options_num; i++)
764                 if (strcasecmp (cf_global_options[i].key, option) == 0)
765                         break;
767         if (i >= cf_global_options_num)
768                 return (-1);
770         sfree (cf_global_options[i].value);
772         if (value != NULL)
773                 cf_global_options[i].value = strdup (value);
774         else
775                 cf_global_options[i].value = NULL;
777         return (0);
780 const char *global_option_get (const char *option)
782         int i;
784         for (i = 0; i < cf_global_options_num; i++)
785                 if (strcasecmp (cf_global_options[i].key, option) == 0)
786                         break;
788         if (i >= cf_global_options_num)
789                 return (NULL);
790         
791         return ((cf_global_options[i].value != NULL)
792                         ? cf_global_options[i].value
793                         : cf_global_options[i].def);
794 } /* char *global_option_get */
796 void cf_unregister (const char *type)
798         cf_callback_t *this, *prev;
800         for (prev = NULL, this = first_callback;
801                         this != NULL;
802                         prev = this, this = this->next)
803                 if (strcasecmp (this->type, type) == 0)
804                 {
805                         if (prev == NULL)
806                                 first_callback = this->next;
807                         else
808                                 prev->next = this->next;
810                         free (this);
811                         break;
812                 }
813 } /* void cf_unregister */
815 void cf_unregister_complex (const char *type)
817         cf_complex_callback_t *this, *prev;
819         for (prev = NULL, this = complex_callback_head;
820                         this != NULL;
821                         prev = this, this = this->next)
822                 if (strcasecmp (this->type, type) == 0)
823                 {
824                         if (prev == NULL)
825                                 complex_callback_head = this->next;
826                         else
827                                 prev->next = this->next;
829                         sfree (this->type);
830                         sfree (this);
831                         break;
832                 }
833 } /* void cf_unregister */
835 void cf_register (const char *type,
836                 int (*callback) (const char *, const char *),
837                 const char **keys, int keys_num)
839         cf_callback_t *cf_cb;
841         /* Remove this module from the list, if it already exists */
842         cf_unregister (type);
844         /* This pointer will be free'd in `cf_unregister' */
845         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
846                 return;
848         cf_cb->type     = type;
849         cf_cb->callback = callback;
850         cf_cb->keys     = keys;
851         cf_cb->keys_num = keys_num;
853         cf_cb->next = first_callback;
854         first_callback = cf_cb;
855 } /* void cf_register */
857 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
859         cf_complex_callback_t *new;
861         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
862         if (new == NULL)
863                 return (-1);
865         new->type = strdup (type);
866         if (new->type == NULL)
867         {
868                 sfree (new);
869                 return (-1);
870         }
872         new->callback = callback;
873         new->next = NULL;
875         if (complex_callback_head == NULL)
876         {
877                 complex_callback_head = new;
878         }
879         else
880         {
881                 cf_complex_callback_t *last = complex_callback_head;
882                 while (last->next != NULL)
883                         last = last->next;
884                 last->next = new;
885         }
887         return (0);
888 } /* int cf_register_complex */
890 int cf_read (char *filename)
892         oconfig_item_t *conf;
893         int i;
895         conf = cf_read_generic (filename, 0 /* depth */);
896         if (conf == NULL)
897         {
898                 ERROR ("Unable to read config file %s.", filename);
899                 return (-1);
900         }
902         for (i = 0; i < conf->children_num; i++)
903         {
904                 if (conf->children[i].children == NULL)
905                         dispatch_value (conf->children + i);
906                 else
907                         dispatch_block (conf->children + i);
908         }
910         oconfig_free (conf);
912         /* Read the default types.db if no `TypesDB' option was given. */
913         if (cf_default_typesdb)
914                 read_types_list (PKGDATADIR"/types.db");
916         return (0);
917 } /* int cf_read */