Code

Merge branch 'collectd-4.10'
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2010  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 "filter_chain.h"
34 #if HAVE_WORDEXP_H
35 # include <wordexp.h>
36 #endif /* HAVE_WORDEXP_H */
38 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
40 /*
41  * Private types
42  */
43 typedef struct cf_callback
44 {
45         const char  *type;
46         int  (*callback) (const char *, const char *);
47         const char **keys;
48         int    keys_num;
49         struct cf_callback *next;
50 } cf_callback_t;
52 typedef struct cf_complex_callback_s
53 {
54         char *type;
55         int (*callback) (oconfig_item_t *);
56         struct cf_complex_callback_s *next;
57 } cf_complex_callback_t;
59 typedef struct cf_value_map_s
60 {
61         char *key;
62         int (*func) (const oconfig_item_t *);
63 } cf_value_map_t;
65 typedef struct cf_global_option_s
66 {
67         char *key;
68         char *value;
69         char *def;
70 } cf_global_option_t;
72 /*
73  * Prototypes of callback functions
74  */
75 static int dispatch_value_typesdb (const oconfig_item_t *ci);
76 static int dispatch_value_plugindir (const oconfig_item_t *ci);
77 static int dispatch_loadplugin (const oconfig_item_t *ci);
79 /*
80  * Private variables
81  */
82 static cf_callback_t *first_callback = NULL;
83 static cf_complex_callback_t *complex_callback_head = NULL;
85 static cf_value_map_t cf_value_map[] =
86 {
87         {"TypesDB",    dispatch_value_typesdb},
88         {"PluginDir",  dispatch_value_plugindir},
89         {"LoadPlugin", dispatch_loadplugin}
90 };
91 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
93 static cf_global_option_t cf_global_options[] =
94 {
95         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
96         {"PIDFile",     NULL, PIDFILE},
97         {"Hostname",    NULL, NULL},
98         {"FQDNLookup",  NULL, "true"},
99         {"Interval",    NULL, "10"},
100         {"ReadThreads", NULL, "5"},
101         {"Timeout",     NULL, "2"},
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_loadplugin (const oconfig_item_t *ci)
244         int i;
245         uint32_t flags = 0;
246         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
248         if (ci->values_num != 1)
249                 return (-1);
250         if (ci->values[0].type != OCONFIG_TYPE_STRING)
251                 return (-1);
253         for (i = 0; i < ci->children_num; ++i) {
254                 if (ci->children[i].values_num != 1 ||
255                                 ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
256                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
257                         continue;
258                 }
259                 if (strcasecmp(ci->children[i].key, "globals") == 0) {
260                         flags |= PLUGIN_FLAGS_GLOBAL;
261                 } else {
262                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
263                 }
264         }
265         return (plugin_load (ci->values[0].value.string, flags));
266 } /* int dispatch_value_loadplugin */
268 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
270         char  buffer[4096];
271         char *buffer_ptr;
272         int   buffer_free;
273         int i;
275         buffer_ptr = buffer;
276         buffer_free = sizeof (buffer);
278         for (i = 0; i < ci->values_num; i++)
279         {
280                 int status = -1;
282                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
283                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
284                                         ci->values[i].value.string);
285                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
286                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
287                                         ci->values[i].value.number);
288                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
289                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
290                                         ci->values[i].value.boolean
291                                         ? "true" : "false");
293                 if ((status < 0) || (status >= buffer_free))
294                         return (-1);
295                 buffer_free -= status;
296                 buffer_ptr  += status;
297         }
298         /* skip the initial space */
299         buffer_ptr = buffer + 1;
301         return (cf_dispatch (plugin, ci->key, buffer_ptr));
302 } /* int dispatch_value_plugin */
304 static int dispatch_value (const oconfig_item_t *ci)
306         int ret = -2;
307         int i;
309         for (i = 0; i < cf_value_map_num; i++)
310                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
311                 {
312                         ret = cf_value_map[i].func (ci);
313                         break;
314                 }
316         for (i = 0; i < cf_global_options_num; i++)
317                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
318                 {
319                         ret = dispatch_global_option (ci);
320                         break;
321                 }
323         return (ret);
324 } /* int dispatch_value */
326 static int dispatch_block_plugin (oconfig_item_t *ci)
328         int i;
329         char *name;
331         cf_complex_callback_t *cb;
333         if (strcasecmp (ci->key, "Plugin") != 0)
334                 return (-1);
335         if (ci->values_num < 1)
336                 return (-1);
337         if (ci->values[0].type != OCONFIG_TYPE_STRING)
338                 return (-1);
340         name = ci->values[0].value.string;
342         /* Check for a complex callback first */
343         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
344                 if (strcasecmp (name, cb->type) == 0)
345                         return (cb->callback (ci));
347         /* Hm, no complex plugin found. Dispatch the values one by one */
348         for (i = 0; i < ci->children_num; i++)
349         {
350                 if (ci->children[i].children == NULL)
351                         dispatch_value_plugin (name, ci->children + i);
352                 else
353                 {
354                         WARNING ("There is a `%s' block within the "
355                                         "configuration for the %s plugin. "
356                                         "The plugin either only expects "
357                                         "\"simple\" configuration statements "
358                                         "or wasn't loaded using `LoadPlugin'."
359                                         " Please check your configuration.",
360                                         ci->children[i].key, name);
361                 }
362         }
364         return (0);
368 static int dispatch_block (oconfig_item_t *ci)
370         if (strcasecmp (ci->key, "LoadPlugin") == 0)
371                 return (dispatch_loadplugin (ci));
372         else if (strcasecmp (ci->key, "Plugin") == 0)
373                 return (dispatch_block_plugin (ci));
374         else if (strcasecmp (ci->key, "Chain") == 0)
375                 return (fc_configure (ci));
377         return (0);
380 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
381                 int offset)
383         oconfig_item_t *temp;
384         int i;
386         assert (offset >= 0);
387         assert (dst->children_num > offset);
389         /* Free the memory used by the replaced child. Usually that's the
390          * `Include "blah"' statement. */
391         temp = dst->children + offset;
392         for (i = 0; i < temp->values_num; i++)
393         {
394                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
395                 {
396                         sfree (temp->values[i].value.string);
397                 }
398         }
399         sfree (temp->values);
400         temp = NULL;
402         /* If (src->children_num == 0) the array size is decreased. If offset
403          * is _not_ the last element, (offset < (dst->children_num - 1)), then
404          * we need to move the trailing elements before resizing the array. */
405         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
406         {
407                 int nmemb = dst->children_num - (offset + 1);
408                 memmove (dst->children + offset, dst->children + offset + 1,
409                                 sizeof (oconfig_item_t) * nmemb);
410         }
412         /* Resize the memory containing the children to be big enough to hold
413          * all children. */
414         temp = (oconfig_item_t *) realloc (dst->children,
415                         sizeof (oconfig_item_t)
416                         * (dst->children_num + src->children_num - 1));
417         if (temp == NULL)
418         {
419                 ERROR ("configfile: realloc failed.");
420                 return (-1);
421         }
422         dst->children = temp;
424         /* If there are children behind the include statement, and they have
425          * not yet been moved because (src->children_num == 0), then move them
426          * to the end of the list, so that the new children have room before
427          * them. */
428         if ((src->children_num > 0)
429                         && ((dst->children_num - (offset + 1)) > 0))
430         {
431                 int nmemb = dst->children_num - (offset + 1);
432                 int old_offset = offset + 1;
433                 int new_offset = offset + src->children_num;
435                 memmove (dst->children + new_offset,
436                                 dst->children + old_offset,
437                                 sizeof (oconfig_item_t) * nmemb);
438         }
440         /* Last but not least: If there are new children, copy them to the
441          * memory reserved for them. */
442         if (src->children_num > 0)
443         {
444                 memcpy (dst->children + offset,
445                                 src->children,
446                                 sizeof (oconfig_item_t) * src->children_num);
447         }
449         /* Update the number of children. */
450         dst->children_num += (src->children_num - 1);
452         return (0);
453 } /* int cf_ci_replace_child */
455 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
457         oconfig_item_t *temp;
459         if ((src == NULL) || (src->children_num == 0))
460                 return (0);
462         temp = (oconfig_item_t *) realloc (dst->children,
463                         sizeof (oconfig_item_t)
464                         * (dst->children_num + src->children_num));
465         if (temp == NULL)
466         {
467                 ERROR ("configfile: realloc failed.");
468                 return (-1);
469         }
470         dst->children = temp;
472         memcpy (dst->children + dst->children_num,
473                         src->children,
474                         sizeof (oconfig_item_t)
475                         * src->children_num);
476         dst->children_num += src->children_num;
478         return (0);
479 } /* int cf_ci_append_children */
481 #define CF_MAX_DEPTH 8
482 static oconfig_item_t *cf_read_generic (const char *path, int depth);
484 static int cf_include_all (oconfig_item_t *root, int depth)
486         int i;
488         for (i = 0; i < root->children_num; i++)
489         {
490                 oconfig_item_t *new;
491                 oconfig_item_t *old;
493                 /* Ignore all blocks, including `Include' blocks. */
494                 if (root->children[i].children_num != 0)
495                         continue;
497                 if (strcasecmp (root->children[i].key, "Include") != 0)
498                         continue;
500                 old = root->children + i;
502                 if ((old->values_num != 1)
503                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
504                 {
505                         ERROR ("configfile: `Include' needs exactly one string argument.");
506                         continue;
507                 }
509                 new = cf_read_generic (old->values[0].value.string, depth + 1);
510                 if (new == NULL)
511                         continue;
513                 /* Now replace the i'th child in `root' with `new'. */
514                 cf_ci_replace_child (root, new, i);
516                 /* ... and go back to the new i'th child. */
517                 --i;
519                 sfree (new->values);
520                 sfree (new);
521         } /* for (i = 0; i < root->children_num; i++) */
523         return (0);
524 } /* int cf_include_all */
526 static oconfig_item_t *cf_read_file (const char *file, int depth)
528         oconfig_item_t *root;
530         assert (depth < CF_MAX_DEPTH);
532         root = oconfig_parse_file (file);
533         if (root == NULL)
534         {
535                 ERROR ("configfile: Cannot read file `%s'.", file);
536                 return (NULL);
537         }
539         cf_include_all (root, depth);
541         return (root);
542 } /* oconfig_item_t *cf_read_file */
544 static int cf_compare_string (const void *p1, const void *p2)
546         return strcmp (*(const char **) p1, *(const char **) p2);
549 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
551         oconfig_item_t *root = NULL;
552         DIR *dh;
553         struct dirent *de;
554         char **filenames = NULL;
555         int filenames_num = 0;
556         int status;
557         int i;
559         assert (depth < CF_MAX_DEPTH);
561         dh = opendir (dir);
562         if (dh == NULL)
563         {
564                 char errbuf[1024];
565                 ERROR ("configfile: opendir failed: %s",
566                                 sstrerror (errno, errbuf, sizeof (errbuf)));
567                 return (NULL);
568         }
570         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
571         if (root == NULL)
572         {
573                 ERROR ("configfile: malloc failed.");
574                 return (NULL);
575         }
576         memset (root, 0, sizeof (oconfig_item_t));
578         while ((de = readdir (dh)) != NULL)
579         {
580                 char   name[1024];
581                 char **tmp;
583                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
584                         continue;
586                 status = ssnprintf (name, sizeof (name), "%s/%s",
587                                 dir, de->d_name);
588                 if ((status < 0) || ((size_t) status >= sizeof (name)))
589                 {
590                         ERROR ("configfile: Not including `%s/%s' because its"
591                                         " name is too long.",
592                                         dir, de->d_name);
593                         for (i = 0; i < filenames_num; ++i)
594                                 free (filenames[i]);
595                         free (filenames);
596                         free (root);
597                         return (NULL);
598                 }
600                 ++filenames_num;
601                 tmp = (char **) realloc (filenames,
602                                 filenames_num * sizeof (*filenames));
603                 if (tmp == NULL) {
604                         ERROR ("configfile: realloc failed.");
605                         for (i = 0; i < filenames_num - 1; ++i)
606                                 free (filenames[i]);
607                         free (filenames);
608                         free (root);
609                         return (NULL);
610                 }
611                 filenames = tmp;
613                 filenames[filenames_num - 1] = sstrdup (name);
614         }
616         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
617                         cf_compare_string);
619         for (i = 0; i < filenames_num; ++i)
620         {
621                 oconfig_item_t *temp;
622                 char *name = filenames[i];
624                 temp = cf_read_generic (name, depth);
625                 if (temp == NULL)
626                 {
627                         /* An error should already have been reported. */
628                         sfree (name);
629                         continue;
630                 }
632                 cf_ci_append_children (root, temp);
633                 sfree (temp->children);
634                 sfree (temp);
636                 free (name);
637         }
639         free(filenames);
640         return (root);
641 } /* oconfig_item_t *cf_read_dir */
643 /* 
644  * cf_read_generic
645  *
646  * Path is stat'ed and either cf_read_file or cf_read_dir is called
647  * accordingly.
648  *
649  * There are two versions of this function: If `wordexp' exists shell wildcards
650  * will be expanded and the function will include all matches found. If
651  * `wordexp' (or, more precisely, it's header file) is not available the
652  * simpler function is used which does not do any such expansion.
653  */
654 #if HAVE_WORDEXP_H
655 static oconfig_item_t *cf_read_generic (const char *path, int depth)
657         oconfig_item_t *root = NULL;
658         int status;
659         const char *path_ptr;
660         wordexp_t we;
661         size_t i;
663         if (depth >= CF_MAX_DEPTH)
664         {
665                 ERROR ("configfile: Not including `%s' because the maximum "
666                                 "nesting depth has been reached.", path);
667                 return (NULL);
668         }
670         status = wordexp (path, &we, WRDE_NOCMD);
671         if (status != 0)
672         {
673                 ERROR ("configfile: wordexp (%s) failed.", path);
674                 return (NULL);
675         }
677         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
678         if (root == NULL)
679         {
680                 ERROR ("configfile: malloc failed.");
681                 return (NULL);
682         }
683         memset (root, '\0', sizeof (oconfig_item_t));
685         /* wordexp() might return a sorted list already. That's not
686          * documented though, so let's make sure we get what we want. */
687         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
688                         cf_compare_string);
690         for (i = 0; i < we.we_wordc; i++)
691         {
692                 oconfig_item_t *temp;
693                 struct stat statbuf;
695                 path_ptr = we.we_wordv[i];
697                 status = stat (path_ptr, &statbuf);
698                 if (status != 0)
699                 {
700                         char errbuf[1024];
701                         WARNING ("configfile: stat (%s) failed: %s",
702                                         path_ptr,
703                                         sstrerror (errno, errbuf, sizeof (errbuf)));
704                         continue;
705                 }
707                 if (S_ISREG (statbuf.st_mode))
708                         temp = cf_read_file (path_ptr, depth);
709                 else if (S_ISDIR (statbuf.st_mode))
710                         temp = cf_read_dir (path_ptr, depth);
711                 else
712                 {
713                         WARNING ("configfile: %s is neither a file nor a "
714                                         "directory.", path);
715                         continue;
716                 }
718                 if (temp == NULL) {
719                         oconfig_free (root);
720                         return (NULL);
721                 }
723                 cf_ci_append_children (root, temp);
724                 sfree (temp->children);
725                 sfree (temp);
726         }
728         wordfree (&we);
730         if (root->children == NULL)
731         {
732                 oconfig_free (root);
733                 return (NULL);
734         }
736         return (root);
737 } /* oconfig_item_t *cf_read_generic */
738 /* #endif HAVE_WORDEXP_H */
740 #else /* if !HAVE_WORDEXP_H */
741 static oconfig_item_t *cf_read_generic (const char *path, int depth)
743         struct stat statbuf;
744         int status;
746         if (depth >= CF_MAX_DEPTH)
747         {
748                 ERROR ("configfile: Not including `%s' because the maximum "
749                                 "nesting depth has been reached.", path);
750                 return (NULL);
751         }
753         status = stat (path, &statbuf);
754         if (status != 0)
755         {
756                 char errbuf[1024];
757                 ERROR ("configfile: stat (%s) failed: %s",
758                                 path,
759                                 sstrerror (errno, errbuf, sizeof (errbuf)));
760                 return (NULL);
761         }
763         if (S_ISREG (statbuf.st_mode))
764                 return (cf_read_file (path, depth));
765         else if (S_ISDIR (statbuf.st_mode))
766                 return (cf_read_dir (path, depth));
768         ERROR ("configfile: %s is neither a file nor a directory.", path);
769         return (NULL);
770 } /* oconfig_item_t *cf_read_generic */
771 #endif /* !HAVE_WORDEXP_H */
773 /* 
774  * Public functions
775  */
776 int global_option_set (const char *option, const char *value)
778         int i;
780         DEBUG ("option = %s; value = %s;", option, value);
782         for (i = 0; i < cf_global_options_num; i++)
783                 if (strcasecmp (cf_global_options[i].key, option) == 0)
784                         break;
786         if (i >= cf_global_options_num)
787                 return (-1);
789         sfree (cf_global_options[i].value);
791         if (value != NULL)
792                 cf_global_options[i].value = strdup (value);
793         else
794                 cf_global_options[i].value = NULL;
796         return (0);
799 const char *global_option_get (const char *option)
801         int i;
803         for (i = 0; i < cf_global_options_num; i++)
804                 if (strcasecmp (cf_global_options[i].key, option) == 0)
805                         break;
807         if (i >= cf_global_options_num)
808                 return (NULL);
809         
810         return ((cf_global_options[i].value != NULL)
811                         ? cf_global_options[i].value
812                         : cf_global_options[i].def);
813 } /* char *global_option_get */
815 void cf_unregister (const char *type)
817         cf_callback_t *this, *prev;
819         for (prev = NULL, this = first_callback;
820                         this != NULL;
821                         prev = this, this = this->next)
822                 if (strcasecmp (this->type, type) == 0)
823                 {
824                         if (prev == NULL)
825                                 first_callback = this->next;
826                         else
827                                 prev->next = this->next;
829                         free (this);
830                         break;
831                 }
832 } /* void cf_unregister */
834 void cf_unregister_complex (const char *type)
836         cf_complex_callback_t *this, *prev;
838         for (prev = NULL, this = complex_callback_head;
839                         this != NULL;
840                         prev = this, this = this->next)
841                 if (strcasecmp (this->type, type) == 0)
842                 {
843                         if (prev == NULL)
844                                 complex_callback_head = this->next;
845                         else
846                                 prev->next = this->next;
848                         sfree (this->type);
849                         sfree (this);
850                         break;
851                 }
852 } /* void cf_unregister */
854 void cf_register (const char *type,
855                 int (*callback) (const char *, const char *),
856                 const char **keys, int keys_num)
858         cf_callback_t *cf_cb;
860         /* Remove this module from the list, if it already exists */
861         cf_unregister (type);
863         /* This pointer will be free'd in `cf_unregister' */
864         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
865                 return;
867         cf_cb->type     = type;
868         cf_cb->callback = callback;
869         cf_cb->keys     = keys;
870         cf_cb->keys_num = keys_num;
872         cf_cb->next = first_callback;
873         first_callback = cf_cb;
874 } /* void cf_register */
876 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
878         cf_complex_callback_t *new;
880         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
881         if (new == NULL)
882                 return (-1);
884         new->type = strdup (type);
885         if (new->type == NULL)
886         {
887                 sfree (new);
888                 return (-1);
889         }
891         new->callback = callback;
892         new->next = NULL;
894         if (complex_callback_head == NULL)
895         {
896                 complex_callback_head = new;
897         }
898         else
899         {
900                 cf_complex_callback_t *last = complex_callback_head;
901                 while (last->next != NULL)
902                         last = last->next;
903                 last->next = new;
904         }
906         return (0);
907 } /* int cf_register_complex */
909 int cf_read (char *filename)
911         oconfig_item_t *conf;
912         int i;
914         conf = cf_read_generic (filename, 0 /* depth */);
915         if (conf == NULL)
916         {
917                 ERROR ("Unable to read config file %s.", filename);
918                 return (-1);
919         }
921         for (i = 0; i < conf->children_num; i++)
922         {
923                 if (conf->children[i].children == NULL)
924                         dispatch_value (conf->children + i);
925                 else
926                         dispatch_block (conf->children + i);
927         }
929         oconfig_free (conf);
931         /* Read the default types.db if no `TypesDB' option was given. */
932         if (cf_default_typesdb)
933                 read_types_list (PKGDATADIR"/types.db");
935         return (0);
936 } /* int cf_read */
938 /* Assures the config option is a string, duplicates it and returns the copy in
939  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
940  * success. */
941 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
943         char *string;
945         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
946         {
947                 ERROR ("cf_util_get_string: The %s option requires "
948                                 "exactly one string argument.", ci->key);
949                 return (-1);
950         }
952         string = strdup (ci->values[0].value.string);
953         if (string == NULL)
954                 return (-1);
956         if (*ret_string != NULL)
957                 sfree (*ret_string);
958         *ret_string = string;
960         return (0);
961 } /* }}} int cf_util_get_string */
963 /* Assures the config option is a string and copies it to the provided buffer.
964  * Assures null-termination. */
965 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
966                 size_t buffer_size)
968         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
969                 return (EINVAL);
971         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
972         {
973                 ERROR ("cf_util_get_string_buffer: The %s option requires "
974                                 "exactly one string argument.", ci->key);
975                 return (-1);
976         }
978         strncpy (buffer, ci->values[0].value.string, buffer_size);
979         buffer[buffer_size - 1] = 0;
981         return (0);
982 } /* }}} int cf_util_get_string_buffer */
984 /* Assures the config option is a number and returns it as an int. */
985 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
987         if ((ci == NULL) || (ret_value == NULL))
988                 return (EINVAL);
990         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
991         {
992                 ERROR ("cf_util_get_int: The %s option requires "
993                                 "exactly one numeric argument.", ci->key);
994                 return (-1);
995         }
997         *ret_value = (int) ci->values[0].value.number;
999         return (0);
1000 } /* }}} int cf_util_get_int */
1002 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1004         if ((ci == NULL) || (ret_bool == NULL))
1005                 return (EINVAL);
1007         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1008         {
1009                 ERROR ("cf_util_get_boolean: The %s option requires "
1010                                 "exactly one boolean argument.", ci->key);
1011                 return (-1);
1012         }
1014         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1016         return (0);
1017 } /* }}} int cf_util_get_boolean */
1019 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1020                 unsigned int *ret_value, unsigned int flag)
1022         int status;
1023         _Bool b;
1025         if (ret_value == NULL)
1026                 return (EINVAL);
1028         b = 0;
1029         status = cf_util_get_boolean (ci, &b);
1030         if (status != 0)
1031                 return (status);
1033         if (b)
1034         {
1035                 *ret_value |= flag;
1036         }
1037         else
1038         {
1039                 *ret_value &= ~flag;
1040         }
1042         return (0);
1043 } /* }}} int cf_util_get_flag */
1045 /* Assures that the config option is a string. The string is then converted to
1046  * a port number using `service_name_to_port_number' and returned. Returns the
1047  * port number in the range [1-65535] or less than zero upon failure. */
1048 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1050         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1051         {
1052                 ERROR ("cf_util_get_port_number: The %s option requires "
1053                                 "exactly one string argument.", ci->key);
1054                 return (-1);
1055         }
1057         return (service_name_to_port_number (ci->values[0].value.string));
1058 } /* }}} int cf_util_get_port_number */
1060 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1062         if ((ci == NULL) || (ret_value == NULL))
1063                 return (EINVAL);
1065         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1066         {
1067                 ERROR ("cf_util_get_cdtime: The %s option requires "
1068                                 "exactly one numeric argument.", ci->key);
1069                 return (-1);
1070         }
1072         if (ci->values[0].value.number < 0.0)
1073         {
1074                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1075                                 "option must not be negative.", ci->key);
1076                 return (-1);
1077         }
1079         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1081         return (0);
1082 } /* }}} int cf_util_get_cdtime */