src/configfile.c: Continue parsing config files if stat'ing 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  **/
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_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_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 ("cf_dispatch: 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_loadplugin (const oconfig_item_t *ci)
243 {
244         int i;
245         uint32_t flags = 0;
246         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
247
248         if (ci->values_num != 1)
249                 return (-1);
250         if (ci->values[0].type != OCONFIG_TYPE_STRING)
251                 return (-1);
252
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 */
267
268 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
269 {
270         char  buffer[4096];
271         char *buffer_ptr;
272         int   buffer_free;
273         int i;
274
275         buffer_ptr = buffer;
276         buffer_free = sizeof (buffer);
277
278         for (i = 0; i < ci->values_num; i++)
279         {
280                 int status = -1;
281
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");
292
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;
300
301         return (cf_dispatch (plugin, ci->key, buffer_ptr));
302 } /* int dispatch_value_plugin */
303
304 static int dispatch_value (const oconfig_item_t *ci)
305 {
306         int ret = -2;
307         int i;
308
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                 }
315
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                 }
322
323         return (ret);
324 } /* int dispatch_value */
325
326 static int dispatch_block_plugin (oconfig_item_t *ci)
327 {
328         int i;
329         char *name;
330
331         cf_complex_callback_t *cb;
332
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);
339
340         name = ci->values[0].value.string;
341
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));
346
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         }
363
364         return (0);
365 }
366
367
368 static int dispatch_block (oconfig_item_t *ci)
369 {
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, "Threshold") == 0)
375                 return (ut_config (ci));
376         else if (strcasecmp (ci->key, "Chain") == 0)
377                 return (fc_configure (ci));
378
379         return (0);
380 }
381
382 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
383                 int offset)
384 {
385         oconfig_item_t *temp;
386         int i;
387
388         assert (offset >= 0);
389         assert (dst->children_num > offset);
390
391         /* Free the memory used by the replaced child. Usually that's the
392          * `Include "blah"' statement. */
393         temp = dst->children + offset;
394         for (i = 0; i < temp->values_num; i++)
395         {
396                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
397                 {
398                         sfree (temp->values[i].value.string);
399                 }
400         }
401         sfree (temp->values);
402         temp = NULL;
403
404         /* If (src->children_num == 0) the array size is decreased. If offset
405          * is _not_ the last element, (offset < (dst->children_num - 1)), then
406          * we need to move the trailing elements before resizing the array. */
407         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
408         {
409                 int nmemb = dst->children_num - (offset + 1);
410                 memmove (dst->children + offset, dst->children + offset + 1,
411                                 sizeof (oconfig_item_t) * nmemb);
412         }
413
414         /* Resize the memory containing the children to be big enough to hold
415          * all children. */
416         temp = (oconfig_item_t *) realloc (dst->children,
417                         sizeof (oconfig_item_t)
418                         * (dst->children_num + src->children_num - 1));
419         if (temp == NULL)
420         {
421                 ERROR ("configfile: realloc failed.");
422                 return (-1);
423         }
424         dst->children = temp;
425
426         /* If there are children behind the include statement, and they have
427          * not yet been moved because (src->children_num == 0), then move them
428          * to the end of the list, so that the new children have room before
429          * them. */
430         if ((src->children_num > 0)
431                         && ((dst->children_num - (offset + 1)) > 0))
432         {
433                 int nmemb = dst->children_num - (offset + 1);
434                 int old_offset = offset + 1;
435                 int new_offset = offset + src->children_num;
436
437                 memmove (dst->children + new_offset,
438                                 dst->children + old_offset,
439                                 sizeof (oconfig_item_t) * nmemb);
440         }
441
442         /* Last but not least: If there are new children, copy them to the
443          * memory reserved for them. */
444         if (src->children_num > 0)
445         {
446                 memcpy (dst->children + offset,
447                                 src->children,
448                                 sizeof (oconfig_item_t) * src->children_num);
449         }
450
451         /* Update the number of children. */
452         dst->children_num += (src->children_num - 1);
453
454         return (0);
455 } /* int cf_ci_replace_child */
456
457 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
458 {
459         oconfig_item_t *temp;
460
461         if ((src == NULL) || (src->children_num == 0))
462                 return (0);
463
464         temp = (oconfig_item_t *) realloc (dst->children,
465                         sizeof (oconfig_item_t)
466                         * (dst->children_num + src->children_num));
467         if (temp == NULL)
468         {
469                 ERROR ("configfile: realloc failed.");
470                 return (-1);
471         }
472         dst->children = temp;
473
474         memcpy (dst->children + dst->children_num,
475                         src->children,
476                         sizeof (oconfig_item_t)
477                         * src->children_num);
478         dst->children_num += src->children_num;
479
480         return (0);
481 } /* int cf_ci_append_children */
482
483 #define CF_MAX_DEPTH 8
484 static oconfig_item_t *cf_read_generic (const char *path, int depth);
485
486 static int cf_include_all (oconfig_item_t *root, int depth)
487 {
488         int i;
489
490         for (i = 0; i < root->children_num; i++)
491         {
492                 oconfig_item_t *new;
493                 oconfig_item_t *old;
494
495                 /* Ignore all blocks, including `Include' blocks. */
496                 if (root->children[i].children_num != 0)
497                         continue;
498
499                 if (strcasecmp (root->children[i].key, "Include") != 0)
500                         continue;
501
502                 old = root->children + i;
503
504                 if ((old->values_num != 1)
505                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
506                 {
507                         ERROR ("configfile: `Include' needs exactly one string argument.");
508                         continue;
509                 }
510
511                 new = cf_read_generic (old->values[0].value.string, depth + 1);
512                 if (new == NULL)
513                         continue;
514
515                 /* Now replace the i'th child in `root' with `new'. */
516                 cf_ci_replace_child (root, new, i);
517
518                 /* ... and go back to the new i'th child. */
519                 --i;
520
521                 sfree (new->values);
522                 sfree (new);
523         } /* for (i = 0; i < root->children_num; i++) */
524
525         return (0);
526 } /* int cf_include_all */
527
528 static oconfig_item_t *cf_read_file (const char *file, int depth)
529 {
530         oconfig_item_t *root;
531
532         assert (depth < CF_MAX_DEPTH);
533
534         root = oconfig_parse_file (file);
535         if (root == NULL)
536         {
537                 ERROR ("configfile: Cannot read file `%s'.", file);
538                 return (NULL);
539         }
540
541         cf_include_all (root, depth);
542
543         return (root);
544 } /* oconfig_item_t *cf_read_file */
545
546 static int cf_compare_string (const void *p1, const void *p2)
547 {
548         return strcmp (*(const char **) p1, *(const char **) p2);
549 }
550
551 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
552 {
553         oconfig_item_t *root = NULL;
554         DIR *dh;
555         struct dirent *de;
556         char **filenames = NULL;
557         int filenames_num = 0;
558         int status;
559         int i;
560
561         assert (depth < CF_MAX_DEPTH);
562
563         dh = opendir (dir);
564         if (dh == NULL)
565         {
566                 char errbuf[1024];
567                 ERROR ("configfile: opendir failed: %s",
568                                 sstrerror (errno, errbuf, sizeof (errbuf)));
569                 return (NULL);
570         }
571
572         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
573         if (root == NULL)
574         {
575                 ERROR ("configfile: malloc failed.");
576                 return (NULL);
577         }
578         memset (root, 0, sizeof (oconfig_item_t));
579
580         while ((de = readdir (dh)) != NULL)
581         {
582                 char   name[1024];
583                 char **tmp;
584
585                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
586                         continue;
587
588                 status = ssnprintf (name, sizeof (name), "%s/%s",
589                                 dir, de->d_name);
590                 if ((status < 0) || ((size_t) status >= sizeof (name)))
591                 {
592                         ERROR ("configfile: Not including `%s/%s' because its"
593                                         " name is too long.",
594                                         dir, de->d_name);
595                         for (i = 0; i < filenames_num; ++i)
596                                 free (filenames[i]);
597                         free (filenames);
598                         free (root);
599                         return (NULL);
600                 }
601
602                 ++filenames_num;
603                 tmp = (char **) realloc (filenames,
604                                 filenames_num * sizeof (*filenames));
605                 if (tmp == NULL) {
606                         ERROR ("configfile: realloc failed.");
607                         for (i = 0; i < filenames_num - 1; ++i)
608                                 free (filenames[i]);
609                         free (filenames);
610                         free (root);
611                         return (NULL);
612                 }
613                 filenames = tmp;
614
615                 filenames[filenames_num - 1] = sstrdup (name);
616         }
617
618         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
619                         cf_compare_string);
620
621         for (i = 0; i < filenames_num; ++i)
622         {
623                 oconfig_item_t *temp;
624                 char *name = filenames[i];
625
626                 temp = cf_read_generic (name, depth);
627                 if (temp == NULL)
628                 {
629                         /* An error should already have been reported. */
630                         sfree (name);
631                         continue;
632                 }
633
634                 cf_ci_append_children (root, temp);
635                 sfree (temp->children);
636                 sfree (temp);
637
638                 free (name);
639         }
640
641         free(filenames);
642         return (root);
643 } /* oconfig_item_t *cf_read_dir */
644
645 /* 
646  * cf_read_generic
647  *
648  * Path is stat'ed and either cf_read_file or cf_read_dir is called
649  * accordingly.
650  *
651  * There are two versions of this function: If `wordexp' exists shell wildcards
652  * will be expanded and the function will include all matches found. If
653  * `wordexp' (or, more precisely, it's header file) is not available the
654  * simpler function is used which does not do any such expansion.
655  */
656 #if HAVE_WORDEXP_H
657 static oconfig_item_t *cf_read_generic (const char *path, int depth)
658 {
659         oconfig_item_t *root = NULL;
660         int status;
661         const char *path_ptr;
662         wordexp_t we;
663         size_t i;
664
665         if (depth >= CF_MAX_DEPTH)
666         {
667                 ERROR ("configfile: Not including `%s' because the maximum "
668                                 "nesting depth has been reached.", path);
669                 return (NULL);
670         }
671
672         status = wordexp (path, &we, WRDE_NOCMD);
673         if (status != 0)
674         {
675                 ERROR ("configfile: wordexp (%s) failed.", path);
676                 return (NULL);
677         }
678
679         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
680         if (root == NULL)
681         {
682                 ERROR ("configfile: malloc failed.");
683                 return (NULL);
684         }
685         memset (root, '\0', sizeof (oconfig_item_t));
686
687         /* wordexp() might return a sorted list already. That's not
688          * documented though, so let's make sure we get what we want. */
689         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
690                         cf_compare_string);
691
692         for (i = 0; i < we.we_wordc; i++)
693         {
694                 oconfig_item_t *temp;
695                 struct stat statbuf;
696
697                 path_ptr = we.we_wordv[i];
698
699                 status = stat (path_ptr, &statbuf);
700                 if (status != 0)
701                 {
702                         char errbuf[1024];
703                         WARNING ("configfile: stat (%s) failed: %s",
704                                         path_ptr,
705                                         sstrerror (errno, errbuf, sizeof (errbuf)));
706                         continue;
707                 }
708
709                 if (S_ISREG (statbuf.st_mode))
710                         temp = cf_read_file (path_ptr, depth);
711                 else if (S_ISDIR (statbuf.st_mode))
712                         temp = cf_read_dir (path_ptr, depth);
713                 else
714                 {
715                         WARNING ("configfile: %s is neither a file nor a "
716                                         "directory.", path);
717                         continue;
718                 }
719
720                 if (temp == NULL) {
721                         oconfig_free (root);
722                         return (NULL);
723                 }
724
725                 cf_ci_append_children (root, temp);
726                 sfree (temp->children);
727                 sfree (temp);
728         }
729
730         wordfree (&we);
731
732         if (root->children == NULL)
733         {
734                 oconfig_free (root);
735                 return (NULL);
736         }
737
738         return (root);
739 } /* oconfig_item_t *cf_read_generic */
740 /* #endif HAVE_WORDEXP_H */
741
742 #else /* if !HAVE_WORDEXP_H */
743 static oconfig_item_t *cf_read_generic (const char *path, int depth)
744 {
745         struct stat statbuf;
746         int status;
747
748         if (depth >= CF_MAX_DEPTH)
749         {
750                 ERROR ("configfile: Not including `%s' because the maximum "
751                                 "nesting depth has been reached.", path);
752                 return (NULL);
753         }
754
755         status = stat (path, &statbuf);
756         if (status != 0)
757         {
758                 char errbuf[1024];
759                 ERROR ("configfile: stat (%s) failed: %s",
760                                 path,
761                                 sstrerror (errno, errbuf, sizeof (errbuf)));
762                 return (NULL);
763         }
764
765         if (S_ISREG (statbuf.st_mode))
766                 return (cf_read_file (path, depth));
767         else if (S_ISDIR (statbuf.st_mode))
768                 return (cf_read_dir (path, depth));
769
770         ERROR ("configfile: %s is neither a file nor a directory.", path);
771         return (NULL);
772 } /* oconfig_item_t *cf_read_generic */
773 #endif /* !HAVE_WORDEXP_H */
774
775 /* 
776  * Public functions
777  */
778 int global_option_set (const char *option, const char *value)
779 {
780         int i;
781
782         DEBUG ("option = %s; value = %s;", option, value);
783
784         for (i = 0; i < cf_global_options_num; i++)
785                 if (strcasecmp (cf_global_options[i].key, option) == 0)
786                         break;
787
788         if (i >= cf_global_options_num)
789                 return (-1);
790
791         sfree (cf_global_options[i].value);
792
793         if (value != NULL)
794                 cf_global_options[i].value = strdup (value);
795         else
796                 cf_global_options[i].value = NULL;
797
798         return (0);
799 }
800
801 const char *global_option_get (const char *option)
802 {
803         int i;
804
805         for (i = 0; i < cf_global_options_num; i++)
806                 if (strcasecmp (cf_global_options[i].key, option) == 0)
807                         break;
808
809         if (i >= cf_global_options_num)
810                 return (NULL);
811         
812         return ((cf_global_options[i].value != NULL)
813                         ? cf_global_options[i].value
814                         : cf_global_options[i].def);
815 } /* char *global_option_get */
816
817 void cf_unregister (const char *type)
818 {
819         cf_callback_t *this, *prev;
820
821         for (prev = NULL, this = first_callback;
822                         this != NULL;
823                         prev = this, this = this->next)
824                 if (strcasecmp (this->type, type) == 0)
825                 {
826                         if (prev == NULL)
827                                 first_callback = this->next;
828                         else
829                                 prev->next = this->next;
830
831                         free (this);
832                         break;
833                 }
834 } /* void cf_unregister */
835
836 void cf_unregister_complex (const char *type)
837 {
838         cf_complex_callback_t *this, *prev;
839
840         for (prev = NULL, this = complex_callback_head;
841                         this != NULL;
842                         prev = this, this = this->next)
843                 if (strcasecmp (this->type, type) == 0)
844                 {
845                         if (prev == NULL)
846                                 complex_callback_head = this->next;
847                         else
848                                 prev->next = this->next;
849
850                         sfree (this->type);
851                         sfree (this);
852                         break;
853                 }
854 } /* void cf_unregister */
855
856 void cf_register (const char *type,
857                 int (*callback) (const char *, const char *),
858                 const char **keys, int keys_num)
859 {
860         cf_callback_t *cf_cb;
861
862         /* Remove this module from the list, if it already exists */
863         cf_unregister (type);
864
865         /* This pointer will be free'd in `cf_unregister' */
866         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
867                 return;
868
869         cf_cb->type     = type;
870         cf_cb->callback = callback;
871         cf_cb->keys     = keys;
872         cf_cb->keys_num = keys_num;
873
874         cf_cb->next = first_callback;
875         first_callback = cf_cb;
876 } /* void cf_register */
877
878 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
879 {
880         cf_complex_callback_t *new;
881
882         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
883         if (new == NULL)
884                 return (-1);
885
886         new->type = strdup (type);
887         if (new->type == NULL)
888         {
889                 sfree (new);
890                 return (-1);
891         }
892
893         new->callback = callback;
894         new->next = NULL;
895
896         if (complex_callback_head == NULL)
897         {
898                 complex_callback_head = new;
899         }
900         else
901         {
902                 cf_complex_callback_t *last = complex_callback_head;
903                 while (last->next != NULL)
904                         last = last->next;
905                 last->next = new;
906         }
907
908         return (0);
909 } /* int cf_register_complex */
910
911 int cf_read (char *filename)
912 {
913         oconfig_item_t *conf;
914         int i;
915
916         conf = cf_read_generic (filename, 0 /* depth */);
917         if (conf == NULL)
918         {
919                 ERROR ("Unable to read config file %s.", filename);
920                 return (-1);
921         }
922
923         for (i = 0; i < conf->children_num; i++)
924         {
925                 if (conf->children[i].children == NULL)
926                         dispatch_value (conf->children + i);
927                 else
928                         dispatch_block (conf->children + i);
929         }
930
931         oconfig_free (conf);
932
933         /* Read the default types.db if no `TypesDB' option was given. */
934         if (cf_default_typesdb)
935                 read_types_list (PKGDATADIR"/types.db");
936
937         return (0);
938 } /* int cf_read */
939
940 /* Assures the config option is a string, duplicates it and returns the copy in
941  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
942  * success. */
943 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
944 {
945         char *string;
946
947         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
948         {
949                 ERROR ("cf_util_get_string: The %s option requires "
950                                 "exactly one string argument.", ci->key);
951                 return (-1);
952         }
953
954         string = strdup (ci->values[0].value.string);
955         if (string == NULL)
956                 return (-1);
957
958         if (*ret_string != NULL)
959                 sfree (*ret_string);
960         *ret_string = string;
961
962         return (0);
963 } /* }}} int cf_util_get_string */
964
965 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
966 {
967         if ((ci == NULL) || (ret_bool == NULL))
968                 return (EINVAL);
969
970         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
971         {
972                 ERROR ("cf_util_get_boolean: The %s option requires "
973                                 "exactly one string argument.", ci->key);
974                 return (-1);
975         }
976
977         *ret_bool = ci->values[0].value.boolean ? true : false;
978
979         return (0);
980 } /* }}} int cf_util_get_boolean */
981
982 /* Assures that the config option is a string. The string is then converted to
983  * a port number using `service_name_to_port_number' and returned. Returns the
984  * port number in the range [1-65535] or less than zero upon failure. */
985 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
986 {
987         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
988         {
989                 ERROR ("cf_util_get_port_number: The %s option requires "
990                                 "exactly one string argument.", ci->key);
991                 return (-1);
992         }
993
994         return (service_name_to_port_number (ci->values[0].value.string));
995 } /* }}} int cf_util_get_port_number */