Code

2a03035c6659ce4c85edc9e979b3f99ff440b206
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2011  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 collectd.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         plugin_ctx_t ctx;
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         plugin_ctx_t ctx;
58         struct cf_complex_callback_s *next;
59 } cf_complex_callback_t;
61 typedef struct cf_value_map_s
62 {
63         char *key;
64         int (*func) (const oconfig_item_t *);
65 } cf_value_map_t;
67 typedef struct cf_global_option_s
68 {
69         char *key;
70         char *value;
71         char *def;
72 } cf_global_option_t;
74 /*
75  * Prototypes of callback functions
76  */
77 static int dispatch_value_typesdb (const oconfig_item_t *ci);
78 static int dispatch_value_plugindir (const oconfig_item_t *ci);
79 static int dispatch_loadplugin (const oconfig_item_t *ci);
81 /*
82  * Private variables
83  */
84 static cf_callback_t *first_callback = NULL;
85 static cf_complex_callback_t *complex_callback_head = NULL;
87 static cf_value_map_t cf_value_map[] =
88 {
89         {"TypesDB",    dispatch_value_typesdb},
90         {"PluginDir",  dispatch_value_plugindir},
91         {"LoadPlugin", dispatch_loadplugin}
92 };
93 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
95 static cf_global_option_t cf_global_options[] =
96 {
97         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
98         {"PIDFile",     NULL, PIDFILE},
99         {"Hostname",    NULL, NULL},
100         {"FQDNLookup",  NULL, "true"},
101         {"Interval",    NULL, "10"},
102         {"ReadThreads", NULL, "5"},
103         {"Timeout",     NULL, "2"},
104         {"PreCacheChain",  NULL, "PreCache"},
105         {"PostCacheChain", NULL, "PostCache"}
106 };
107 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
109 static int cf_default_typesdb = 1;
111 /*
112  * Functions to handle register/unregister, search, and other plugin related
113  * stuff
114  */
115 static cf_callback_t *cf_search (const char *type)
117         cf_callback_t *cf_cb;
119         if (type == NULL)
120                 return (NULL);
122         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
123                 if (strcasecmp (cf_cb->type, type) == 0)
124                         break;
126         return (cf_cb);
129 static int cf_dispatch (const char *type, const char *orig_key,
130                 const char *orig_value)
132         cf_callback_t *cf_cb;
133         plugin_ctx_t old_ctx;
134         char *key;
135         char *value;
136         int ret;
137         int i;
139         DEBUG ("type = %s, key = %s, value = %s",
140                         ESCAPE_NULL(type),
141                         ESCAPE_NULL(orig_key),
142                         ESCAPE_NULL(orig_value));
144         if ((cf_cb = cf_search (type)) == NULL)
145         {
146                 WARNING ("Found a configuration for the `%s' plugin, but "
147                                 "the plugin isn't loaded or didn't register "
148                                 "a configuration callback.", type);
149                 return (-1);
150         }
152         if ((key = strdup (orig_key)) == NULL)
153                 return (1);
154         if ((value = strdup (orig_value)) == NULL)
155         {
156                 free (key);
157                 return (2);
158         }
160         ret = -1;
162         old_ctx = plugin_set_ctx (cf_cb->ctx);
164         for (i = 0; i < cf_cb->keys_num; i++)
165         {
166                 if ((cf_cb->keys[i] != NULL)
167                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
168                 {
169                         ret = (*cf_cb->callback) (key, value);
170                         break;
171                 }
172         }
174         plugin_set_ctx (old_ctx);
176         if (i >= cf_cb->keys_num)
177                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
179         free (key);
180         free (value);
182         DEBUG ("cf_dispatch: return (%i)", ret);
184         return (ret);
185 } /* int cf_dispatch */
187 static int dispatch_global_option (const oconfig_item_t *ci)
189         if (ci->values_num != 1)
190                 return (-1);
191         if (ci->values[0].type == OCONFIG_TYPE_STRING)
192                 return (global_option_set (ci->key, ci->values[0].value.string));
193         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
194         {
195                 char tmp[128];
196                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
197                 return (global_option_set (ci->key, tmp));
198         }
199         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
200         {
201                 if (ci->values[0].value.boolean)
202                         return (global_option_set (ci->key, "true"));
203                 else
204                         return (global_option_set (ci->key, "false"));
205         }
207         return (-1);
208 } /* int dispatch_global_option */
210 static int dispatch_value_typesdb (const oconfig_item_t *ci)
212         int i = 0;
214         assert (strcasecmp (ci->key, "TypesDB") == 0);
216         cf_default_typesdb = 0;
218         if (ci->values_num < 1) {
219                 ERROR ("configfile: `TypesDB' needs at least one argument.");
220                 return (-1);
221         }
223         for (i = 0; i < ci->values_num; ++i)
224         {
225                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
226                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
227                                         "is not a string.", i + 1);
228                         continue;
229                 }
231                 read_types_list (ci->values[i].value.string);
232         }
233         return (0);
234 } /* int dispatch_value_typesdb */
236 static int dispatch_value_plugindir (const oconfig_item_t *ci)
238         assert (strcasecmp (ci->key, "PluginDir") == 0);
239         
240         if (ci->values_num != 1)
241                 return (-1);
242         if (ci->values[0].type != OCONFIG_TYPE_STRING)
243                 return (-1);
245         plugin_set_dir (ci->values[0].value.string);
246         return (0);
249 static int dispatch_loadplugin (const oconfig_item_t *ci)
251         int i;
252         const char *name;
253         unsigned int flags = 0;
254         plugin_ctx_t ctx;
255         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
257         if (ci->values_num != 1)
258                 return (-1);
259         if (ci->values[0].type != OCONFIG_TYPE_STRING)
260                 return (-1);
262         name = ci->values[0].value.string;
263         ctx.interval = 0;
265         /*
266          * XXX: Magic at work:
267          *
268          * Some of the language bindings, for example the Python and Perl
269          * plugins, need to be able to export symbols to the scripts they run.
270          * For this to happen, the "Globals" flag needs to be set.
271          * Unfortunately, this technical detail is hard to explain to the
272          * average user and she shouldn't have to worry about this, ideally.
273          * So in order to save everyone's sanity use a different default for a
274          * handful of special plugins. --octo
275          */
276         if ((strcasecmp ("Perl", name) == 0)
277                         || (strcasecmp ("Python", name) == 0))
278                 flags |= PLUGIN_FLAGS_GLOBAL;
280         for (i = 0; i < ci->children_num; ++i) {
281                 if (strcasecmp("Globals", ci->children[i].key) == 0)
282                         cf_util_get_flag (ci->children + i, &flags, PLUGIN_FLAGS_GLOBAL);
283                 else if (strcasecmp ("Interval", ci->children[i].key) == 0) {
284                         double interval = 0.0;
286                         if (cf_util_get_double (ci->children + i, &interval) != 0) {
287                                 /* cf_util_get_double will log an error */
288                                 continue;
289                         }
291                         ctx.interval = DOUBLE_TO_CDTIME_T (interval);
292                 }
293                 else {
294                         WARNING("Ignoring unknown LoadPlugin option \"%s\" "
295                                         "for plugin \"%s\"",
296                                         ci->children[i].key, ci->values[0].value.string);
297                 }
298         }
300         plugin_set_ctx (ctx);
301         return (plugin_load (name, (uint32_t) flags));
302 } /* int dispatch_value_loadplugin */
304 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
306         char  buffer[4096];
307         char *buffer_ptr;
308         int   buffer_free;
309         int i;
311         buffer_ptr = buffer;
312         buffer_free = sizeof (buffer);
314         for (i = 0; i < ci->values_num; i++)
315         {
316                 int status = -1;
318                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
319                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
320                                         ci->values[i].value.string);
321                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
322                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
323                                         ci->values[i].value.number);
324                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
325                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
326                                         ci->values[i].value.boolean
327                                         ? "true" : "false");
329                 if ((status < 0) || (status >= buffer_free))
330                         return (-1);
331                 buffer_free -= status;
332                 buffer_ptr  += status;
333         }
334         /* skip the initial space */
335         buffer_ptr = buffer + 1;
337         return (cf_dispatch (plugin, ci->key, buffer_ptr));
338 } /* int dispatch_value_plugin */
340 static int dispatch_value (const oconfig_item_t *ci)
342         int ret = -2;
343         int i;
345         for (i = 0; i < cf_value_map_num; i++)
346                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
347                 {
348                         ret = cf_value_map[i].func (ci);
349                         break;
350                 }
352         for (i = 0; i < cf_global_options_num; i++)
353                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
354                 {
355                         ret = dispatch_global_option (ci);
356                         break;
357                 }
359         return (ret);
360 } /* int dispatch_value */
362 static int dispatch_block_plugin (oconfig_item_t *ci)
364         int i;
365         char *name;
367         cf_complex_callback_t *cb;
369         if (strcasecmp (ci->key, "Plugin") != 0)
370                 return (-1);
371         if (ci->values_num < 1)
372                 return (-1);
373         if (ci->values[0].type != OCONFIG_TYPE_STRING)
374                 return (-1);
376         name = ci->values[0].value.string;
378         /* Check for a complex callback first */
379         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
380         {
381                 if (strcasecmp (name, cb->type) == 0)
382                 {
383                         plugin_ctx_t old_ctx;
384                         int ret_val;
386                         old_ctx = plugin_set_ctx (cb->ctx);
387                         ret_val = (cb->callback (ci));
388                         plugin_set_ctx (old_ctx);
389                         return (ret_val);
390                 }
391         }
393         /* Hm, no complex plugin found. Dispatch the values one by one */
394         for (i = 0; i < ci->children_num; i++)
395         {
396                 if (ci->children[i].children == NULL)
397                         dispatch_value_plugin (name, ci->children + i);
398                 else
399                 {
400                         WARNING ("There is a `%s' block within the "
401                                         "configuration for the %s plugin. "
402                                         "The plugin either only expects "
403                                         "\"simple\" configuration statements "
404                                         "or wasn't loaded using `LoadPlugin'."
405                                         " Please check your configuration.",
406                                         ci->children[i].key, name);
407                 }
408         }
410         return (0);
414 static int dispatch_block (oconfig_item_t *ci)
416         if (strcasecmp (ci->key, "LoadPlugin") == 0)
417                 return (dispatch_loadplugin (ci));
418         else if (strcasecmp (ci->key, "Plugin") == 0)
419                 return (dispatch_block_plugin (ci));
420         else if (strcasecmp (ci->key, "Chain") == 0)
421                 return (fc_configure (ci));
423         return (0);
426 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
427                 int offset)
429         oconfig_item_t *temp;
430         int i;
432         assert (offset >= 0);
433         assert (dst->children_num > offset);
435         /* Free the memory used by the replaced child. Usually that's the
436          * `Include "blah"' statement. */
437         temp = dst->children + offset;
438         for (i = 0; i < temp->values_num; i++)
439         {
440                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
441                 {
442                         sfree (temp->values[i].value.string);
443                 }
444         }
445         sfree (temp->values);
446         temp = NULL;
448         /* If (src->children_num == 0) the array size is decreased. If offset
449          * is _not_ the last element, (offset < (dst->children_num - 1)), then
450          * we need to move the trailing elements before resizing the array. */
451         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
452         {
453                 int nmemb = dst->children_num - (offset + 1);
454                 memmove (dst->children + offset, dst->children + offset + 1,
455                                 sizeof (oconfig_item_t) * nmemb);
456         }
458         /* Resize the memory containing the children to be big enough to hold
459          * all children. */
460         temp = (oconfig_item_t *) realloc (dst->children,
461                         sizeof (oconfig_item_t)
462                         * (dst->children_num + src->children_num - 1));
463         if (temp == NULL)
464         {
465                 ERROR ("configfile: realloc failed.");
466                 return (-1);
467         }
468         dst->children = temp;
470         /* If there are children behind the include statement, and they have
471          * not yet been moved because (src->children_num == 0), then move them
472          * to the end of the list, so that the new children have room before
473          * them. */
474         if ((src->children_num > 0)
475                         && ((dst->children_num - (offset + 1)) > 0))
476         {
477                 int nmemb = dst->children_num - (offset + 1);
478                 int old_offset = offset + 1;
479                 int new_offset = offset + src->children_num;
481                 memmove (dst->children + new_offset,
482                                 dst->children + old_offset,
483                                 sizeof (oconfig_item_t) * nmemb);
484         }
486         /* Last but not least: If there are new children, copy them to the
487          * memory reserved for them. */
488         if (src->children_num > 0)
489         {
490                 memcpy (dst->children + offset,
491                                 src->children,
492                                 sizeof (oconfig_item_t) * src->children_num);
493         }
495         /* Update the number of children. */
496         dst->children_num += (src->children_num - 1);
498         return (0);
499 } /* int cf_ci_replace_child */
501 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
503         oconfig_item_t *temp;
505         if ((src == NULL) || (src->children_num == 0))
506                 return (0);
508         temp = (oconfig_item_t *) realloc (dst->children,
509                         sizeof (oconfig_item_t)
510                         * (dst->children_num + src->children_num));
511         if (temp == NULL)
512         {
513                 ERROR ("configfile: realloc failed.");
514                 return (-1);
515         }
516         dst->children = temp;
518         memcpy (dst->children + dst->children_num,
519                         src->children,
520                         sizeof (oconfig_item_t)
521                         * src->children_num);
522         dst->children_num += src->children_num;
524         return (0);
525 } /* int cf_ci_append_children */
527 #define CF_MAX_DEPTH 8
528 static oconfig_item_t *cf_read_generic (const char *path, int depth);
530 static int cf_include_all (oconfig_item_t *root, int depth)
532         int i;
534         for (i = 0; i < root->children_num; i++)
535         {
536                 oconfig_item_t *new;
537                 oconfig_item_t *old;
539                 /* Ignore all blocks, including `Include' blocks. */
540                 if (root->children[i].children_num != 0)
541                         continue;
543                 if (strcasecmp (root->children[i].key, "Include") != 0)
544                         continue;
546                 old = root->children + i;
548                 if ((old->values_num != 1)
549                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
550                 {
551                         ERROR ("configfile: `Include' needs exactly one string argument.");
552                         continue;
553                 }
555                 new = cf_read_generic (old->values[0].value.string, depth + 1);
556                 if (new == NULL)
557                         continue;
559                 /* Now replace the i'th child in `root' with `new'. */
560                 cf_ci_replace_child (root, new, i);
562                 /* ... and go back to the new i'th child. */
563                 --i;
565                 sfree (new->values);
566                 sfree (new);
567         } /* for (i = 0; i < root->children_num; i++) */
569         return (0);
570 } /* int cf_include_all */
572 static oconfig_item_t *cf_read_file (const char *file, int depth)
574         oconfig_item_t *root;
576         assert (depth < CF_MAX_DEPTH);
578         root = oconfig_parse_file (file);
579         if (root == NULL)
580         {
581                 ERROR ("configfile: Cannot read file `%s'.", file);
582                 return (NULL);
583         }
585         cf_include_all (root, depth);
587         return (root);
588 } /* oconfig_item_t *cf_read_file */
590 static int cf_compare_string (const void *p1, const void *p2)
592         return strcmp (*(const char **) p1, *(const char **) p2);
595 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
597         oconfig_item_t *root = NULL;
598         DIR *dh;
599         struct dirent *de;
600         char **filenames = NULL;
601         int filenames_num = 0;
602         int status;
603         int i;
605         assert (depth < CF_MAX_DEPTH);
607         dh = opendir (dir);
608         if (dh == NULL)
609         {
610                 char errbuf[1024];
611                 ERROR ("configfile: opendir failed: %s",
612                                 sstrerror (errno, errbuf, sizeof (errbuf)));
613                 return (NULL);
614         }
616         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
617         if (root == NULL)
618         {
619                 ERROR ("configfile: malloc failed.");
620                 return (NULL);
621         }
622         memset (root, 0, sizeof (oconfig_item_t));
624         while ((de = readdir (dh)) != NULL)
625         {
626                 char   name[1024];
627                 char **tmp;
629                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
630                         continue;
632                 status = ssnprintf (name, sizeof (name), "%s/%s",
633                                 dir, de->d_name);
634                 if ((status < 0) || ((size_t) status >= sizeof (name)))
635                 {
636                         ERROR ("configfile: Not including `%s/%s' because its"
637                                         " name is too long.",
638                                         dir, de->d_name);
639                         for (i = 0; i < filenames_num; ++i)
640                                 free (filenames[i]);
641                         free (filenames);
642                         free (root);
643                         return (NULL);
644                 }
646                 ++filenames_num;
647                 tmp = (char **) realloc (filenames,
648                                 filenames_num * sizeof (*filenames));
649                 if (tmp == NULL) {
650                         ERROR ("configfile: realloc failed.");
651                         for (i = 0; i < filenames_num - 1; ++i)
652                                 free (filenames[i]);
653                         free (filenames);
654                         free (root);
655                         return (NULL);
656                 }
657                 filenames = tmp;
659                 filenames[filenames_num - 1] = sstrdup (name);
660         }
662         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
663                         cf_compare_string);
665         for (i = 0; i < filenames_num; ++i)
666         {
667                 oconfig_item_t *temp;
668                 char *name = filenames[i];
670                 temp = cf_read_generic (name, depth);
671                 if (temp == NULL)
672                 {
673                         /* An error should already have been reported. */
674                         sfree (name);
675                         continue;
676                 }
678                 cf_ci_append_children (root, temp);
679                 sfree (temp->children);
680                 sfree (temp);
682                 free (name);
683         }
685         free(filenames);
686         return (root);
687 } /* oconfig_item_t *cf_read_dir */
689 /* 
690  * cf_read_generic
691  *
692  * Path is stat'ed and either cf_read_file or cf_read_dir is called
693  * accordingly.
694  *
695  * There are two versions of this function: If `wordexp' exists shell wildcards
696  * will be expanded and the function will include all matches found. If
697  * `wordexp' (or, more precisely, it's header file) is not available the
698  * simpler function is used which does not do any such expansion.
699  */
700 #if HAVE_WORDEXP_H
701 static oconfig_item_t *cf_read_generic (const char *path, int depth)
703         oconfig_item_t *root = NULL;
704         int status;
705         const char *path_ptr;
706         wordexp_t we;
707         size_t i;
709         if (depth >= CF_MAX_DEPTH)
710         {
711                 ERROR ("configfile: Not including `%s' because the maximum "
712                                 "nesting depth has been reached.", path);
713                 return (NULL);
714         }
716         status = wordexp (path, &we, WRDE_NOCMD);
717         if (status != 0)
718         {
719                 ERROR ("configfile: wordexp (%s) failed.", path);
720                 return (NULL);
721         }
723         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
724         if (root == NULL)
725         {
726                 ERROR ("configfile: malloc failed.");
727                 return (NULL);
728         }
729         memset (root, '\0', sizeof (oconfig_item_t));
731         /* wordexp() might return a sorted list already. That's not
732          * documented though, so let's make sure we get what we want. */
733         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
734                         cf_compare_string);
736         for (i = 0; i < we.we_wordc; i++)
737         {
738                 oconfig_item_t *temp;
739                 struct stat statbuf;
741                 path_ptr = we.we_wordv[i];
743                 status = stat (path_ptr, &statbuf);
744                 if (status != 0)
745                 {
746                         char errbuf[1024];
747                         WARNING ("configfile: stat (%s) failed: %s",
748                                         path_ptr,
749                                         sstrerror (errno, errbuf, sizeof (errbuf)));
750                         continue;
751                 }
753                 if (S_ISREG (statbuf.st_mode))
754                         temp = cf_read_file (path_ptr, depth);
755                 else if (S_ISDIR (statbuf.st_mode))
756                         temp = cf_read_dir (path_ptr, depth);
757                 else
758                 {
759                         WARNING ("configfile: %s is neither a file nor a "
760                                         "directory.", path);
761                         continue;
762                 }
764                 if (temp == NULL) {
765                         oconfig_free (root);
766                         return (NULL);
767                 }
769                 cf_ci_append_children (root, temp);
770                 sfree (temp->children);
771                 sfree (temp);
772         }
774         wordfree (&we);
776         if (root->children == NULL)
777         {
778                 oconfig_free (root);
779                 return (NULL);
780         }
782         return (root);
783 } /* oconfig_item_t *cf_read_generic */
784 /* #endif HAVE_WORDEXP_H */
786 #else /* if !HAVE_WORDEXP_H */
787 static oconfig_item_t *cf_read_generic (const char *path, int depth)
789         struct stat statbuf;
790         int status;
792         if (depth >= CF_MAX_DEPTH)
793         {
794                 ERROR ("configfile: Not including `%s' because the maximum "
795                                 "nesting depth has been reached.", path);
796                 return (NULL);
797         }
799         status = stat (path, &statbuf);
800         if (status != 0)
801         {
802                 char errbuf[1024];
803                 ERROR ("configfile: stat (%s) failed: %s",
804                                 path,
805                                 sstrerror (errno, errbuf, sizeof (errbuf)));
806                 return (NULL);
807         }
809         if (S_ISREG (statbuf.st_mode))
810                 return (cf_read_file (path, depth));
811         else if (S_ISDIR (statbuf.st_mode))
812                 return (cf_read_dir (path, depth));
814         ERROR ("configfile: %s is neither a file nor a directory.", path);
815         return (NULL);
816 } /* oconfig_item_t *cf_read_generic */
817 #endif /* !HAVE_WORDEXP_H */
819 /* 
820  * Public functions
821  */
822 int global_option_set (const char *option, const char *value)
824         int i;
826         DEBUG ("option = %s; value = %s;", option, value);
828         for (i = 0; i < cf_global_options_num; i++)
829                 if (strcasecmp (cf_global_options[i].key, option) == 0)
830                         break;
832         if (i >= cf_global_options_num)
833                 return (-1);
835         sfree (cf_global_options[i].value);
837         if (value != NULL)
838                 cf_global_options[i].value = strdup (value);
839         else
840                 cf_global_options[i].value = NULL;
842         return (0);
845 const char *global_option_get (const char *option)
847         int i;
849         for (i = 0; i < cf_global_options_num; i++)
850                 if (strcasecmp (cf_global_options[i].key, option) == 0)
851                         break;
853         if (i >= cf_global_options_num)
854                 return (NULL);
855         
856         return ((cf_global_options[i].value != NULL)
857                         ? cf_global_options[i].value
858                         : cf_global_options[i].def);
859 } /* char *global_option_get */
861 void cf_unregister (const char *type)
863         cf_callback_t *this, *prev;
865         for (prev = NULL, this = first_callback;
866                         this != NULL;
867                         prev = this, this = this->next)
868                 if (strcasecmp (this->type, type) == 0)
869                 {
870                         if (prev == NULL)
871                                 first_callback = this->next;
872                         else
873                                 prev->next = this->next;
875                         free (this);
876                         break;
877                 }
878 } /* void cf_unregister */
880 void cf_unregister_complex (const char *type)
882         cf_complex_callback_t *this, *prev;
884         for (prev = NULL, this = complex_callback_head;
885                         this != NULL;
886                         prev = this, this = this->next)
887                 if (strcasecmp (this->type, type) == 0)
888                 {
889                         if (prev == NULL)
890                                 complex_callback_head = this->next;
891                         else
892                                 prev->next = this->next;
894                         sfree (this->type);
895                         sfree (this);
896                         break;
897                 }
898 } /* void cf_unregister */
900 void cf_register (const char *type,
901                 int (*callback) (const char *, const char *),
902                 const char **keys, int keys_num)
904         cf_callback_t *cf_cb;
906         /* Remove this module from the list, if it already exists */
907         cf_unregister (type);
909         /* This pointer will be free'd in `cf_unregister' */
910         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
911                 return;
913         cf_cb->type     = type;
914         cf_cb->callback = callback;
915         cf_cb->keys     = keys;
916         cf_cb->keys_num = keys_num;
917         cf_cb->ctx      = plugin_get_ctx ();
919         cf_cb->next = first_callback;
920         first_callback = cf_cb;
921 } /* void cf_register */
923 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
925         cf_complex_callback_t *new;
927         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
928         if (new == NULL)
929                 return (-1);
931         new->type = strdup (type);
932         if (new->type == NULL)
933         {
934                 sfree (new);
935                 return (-1);
936         }
938         new->callback = callback;
939         new->next = NULL;
941         new->ctx = plugin_get_ctx ();
943         if (complex_callback_head == NULL)
944         {
945                 complex_callback_head = new;
946         }
947         else
948         {
949                 cf_complex_callback_t *last = complex_callback_head;
950                 while (last->next != NULL)
951                         last = last->next;
952                 last->next = new;
953         }
955         return (0);
956 } /* int cf_register_complex */
958 int cf_read (char *filename)
960         oconfig_item_t *conf;
961         int i;
963         conf = cf_read_generic (filename, 0 /* depth */);
964         if (conf == NULL)
965         {
966                 ERROR ("Unable to read config file %s.", filename);
967                 return (-1);
968         }
970         for (i = 0; i < conf->children_num; i++)
971         {
972                 if (conf->children[i].children == NULL)
973                         dispatch_value (conf->children + i);
974                 else
975                         dispatch_block (conf->children + i);
976         }
978         oconfig_free (conf);
980         /* Read the default types.db if no `TypesDB' option was given. */
981         if (cf_default_typesdb)
982                 read_types_list (PKGDATADIR"/types.db");
984         return (0);
985 } /* int cf_read */
987 /* Assures the config option is a string, duplicates it and returns the copy in
988  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
989  * success. */
990 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
992         char *string;
994         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
995         {
996                 ERROR ("cf_util_get_string: The %s option requires "
997                                 "exactly one string argument.", ci->key);
998                 return (-1);
999         }
1001         string = strdup (ci->values[0].value.string);
1002         if (string == NULL)
1003                 return (-1);
1005         if (*ret_string != NULL)
1006                 sfree (*ret_string);
1007         *ret_string = string;
1009         return (0);
1010 } /* }}} int cf_util_get_string */
1012 /* Assures the config option is a string and copies it to the provided buffer.
1013  * Assures null-termination. */
1014 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
1015                 size_t buffer_size)
1017         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
1018                 return (EINVAL);
1020         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1021         {
1022                 ERROR ("cf_util_get_string_buffer: The %s option requires "
1023                                 "exactly one string argument.", ci->key);
1024                 return (-1);
1025         }
1027         strncpy (buffer, ci->values[0].value.string, buffer_size);
1028         buffer[buffer_size - 1] = 0;
1030         return (0);
1031 } /* }}} int cf_util_get_string_buffer */
1033 /* Assures the config option is a number and returns it as an int. */
1034 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
1036         if ((ci == NULL) || (ret_value == NULL))
1037                 return (EINVAL);
1039         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1040         {
1041                 ERROR ("cf_util_get_int: The %s option requires "
1042                                 "exactly one numeric argument.", ci->key);
1043                 return (-1);
1044         }
1046         *ret_value = (int) ci->values[0].value.number;
1048         return (0);
1049 } /* }}} int cf_util_get_int */
1051 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value) /* {{{ */
1053         if ((ci == NULL) || (ret_value == NULL))
1054                 return (EINVAL);
1056         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1057         {
1058                 ERROR ("cf_util_get_double: The %s option requires "
1059                                 "exactly one numeric argument.", ci->key);
1060                 return (-1);
1061         }
1063         *ret_value = ci->values[0].value.number;
1065         return (0);
1066 } /* }}} int cf_util_get_double */
1068 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1070         if ((ci == NULL) || (ret_bool == NULL))
1071                 return (EINVAL);
1073         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1074         {
1075                 ERROR ("cf_util_get_boolean: The %s option requires "
1076                                 "exactly one boolean argument.", ci->key);
1077                 return (-1);
1078         }
1080         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1082         return (0);
1083 } /* }}} int cf_util_get_boolean */
1085 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1086                 unsigned int *ret_value, unsigned int flag)
1088         int status;
1089         _Bool b;
1091         if (ret_value == NULL)
1092                 return (EINVAL);
1094         b = 0;
1095         status = cf_util_get_boolean (ci, &b);
1096         if (status != 0)
1097                 return (status);
1099         if (b)
1100         {
1101                 *ret_value |= flag;
1102         }
1103         else
1104         {
1105                 *ret_value &= ~flag;
1106         }
1108         return (0);
1109 } /* }}} int cf_util_get_flag */
1111 /* Assures that the config option is a string or a number if the correct range
1112  * of 1-65535. The string is then converted to a port number using
1113  * `service_name_to_port_number' and returned.
1114  * Returns the port number in the range [1-65535] or less than zero upon
1115  * failure. */
1116 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1118         int tmp;
1120         if ((ci->values_num != 1)
1121                         || ((ci->values[0].type != OCONFIG_TYPE_STRING)
1122                                 && (ci->values[0].type != OCONFIG_TYPE_NUMBER)))
1123         {
1124                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1125                                 "exactly one string argument.", ci->key);
1126                 return (-1);
1127         }
1129         if (ci->values[0].type == OCONFIG_TYPE_STRING)
1130                 return (service_name_to_port_number (ci->values[0].value.string));
1132         assert (ci->values[0].type == OCONFIG_TYPE_NUMBER);
1133         tmp = (int) (ci->values[0].value.number + 0.5);
1134         if ((tmp < 1) || (tmp > 65535))
1135         {
1136                 ERROR ("cf_util_get_port_number: The \"%s\" option requires "
1137                                 "a service name or a port number. The number "
1138                                 "you specified, %i, is not in the valid "
1139                                 "range of 1-65535.",
1140                                 ci->key, tmp);
1141                 return (-1);
1142         }
1144         return (tmp);
1145 } /* }}} int cf_util_get_port_number */
1147 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1149         if ((ci == NULL) || (ret_value == NULL))
1150                 return (EINVAL);
1152         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1153         {
1154                 ERROR ("cf_util_get_cdtime: The %s option requires "
1155                                 "exactly one numeric argument.", ci->key);
1156                 return (-1);
1157         }
1159         if (ci->values[0].value.number < 0.0)
1160         {
1161                 ERROR ("cf_util_get_cdtime: The numeric argument of the %s "
1162                                 "option must not be negative.", ci->key);
1163                 return (-1);
1164         }
1166         *ret_value = DOUBLE_TO_CDTIME_T (ci->values[0].value.number);
1168         return (0);
1169 } /* }}} int cf_util_get_cdtime */