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 "utils_threshold.h"
33 #include "filter_chain.h"
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
41 /*
42 * Private types
43 */
44 typedef struct cf_callback
45 {
46 const char *type;
47 int (*callback) (const char *, const char *);
48 const char **keys;
49 int keys_num;
50 struct cf_callback *next;
51 } cf_callback_t;
53 typedef struct cf_complex_callback_s
54 {
55 char *type;
56 int (*callback) (oconfig_item_t *);
57 struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
60 typedef struct cf_value_map_s
61 {
62 char *key;
63 int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
66 typedef struct cf_global_option_s
67 {
68 char *key;
69 char *value;
70 char *def;
71 } cf_global_option_t;
73 /*
74 * Prototypes of callback functions
75 */
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_loadplugin (const oconfig_item_t *ci);
80 /*
81 * Private variables
82 */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
86 static cf_value_map_t cf_value_map[] =
87 {
88 {"TypesDB", dispatch_value_typesdb},
89 {"PluginDir", dispatch_value_plugindir},
90 {"LoadPlugin", dispatch_loadplugin}
91 };
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
94 static cf_global_option_t cf_global_options[] =
95 {
96 {"BaseDir", NULL, PKGLOCALSTATEDIR},
97 {"PIDFile", NULL, PIDFILE},
98 {"Hostname", NULL, NULL},
99 {"FQDNLookup", NULL, "true"},
100 {"Interval", NULL, "10"},
101 {"ReadThreads", NULL, "5"},
102 {"Timeout", NULL, "2"},
103 {"PreCacheChain", NULL, "PreCache"},
104 {"PostCacheChain", NULL, "PostCache"}
105 };
106 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
108 static int cf_default_typesdb = 1;
110 /*
111 * Functions to handle register/unregister, search, and other plugin related
112 * stuff
113 */
114 static cf_callback_t *cf_search (const char *type)
115 {
116 cf_callback_t *cf_cb;
118 if (type == NULL)
119 return (NULL);
121 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
122 if (strcasecmp (cf_cb->type, type) == 0)
123 break;
125 return (cf_cb);
126 }
128 static int cf_dispatch (const char *type, const char *orig_key,
129 const char *orig_value)
130 {
131 cf_callback_t *cf_cb;
132 char *key;
133 char *value;
134 int ret;
135 int i;
137 DEBUG ("type = %s, key = %s, value = %s",
138 ESCAPE_NULL(type),
139 ESCAPE_NULL(orig_key),
140 ESCAPE_NULL(orig_value));
142 if ((cf_cb = cf_search (type)) == NULL)
143 {
144 WARNING ("Found a configuration for the `%s' plugin, but "
145 "the plugin isn't loaded or didn't register "
146 "a configuration callback.", type);
147 return (-1);
148 }
150 if ((key = strdup (orig_key)) == NULL)
151 return (1);
152 if ((value = strdup (orig_value)) == NULL)
153 {
154 free (key);
155 return (2);
156 }
158 ret = -1;
160 for (i = 0; i < cf_cb->keys_num; i++)
161 {
162 if ((cf_cb->keys[i] != NULL)
163 && (strcasecmp (cf_cb->keys[i], key) == 0))
164 {
165 ret = (*cf_cb->callback) (key, value);
166 break;
167 }
168 }
170 if (i >= cf_cb->keys_num)
171 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
173 free (key);
174 free (value);
176 DEBUG ("cf_dispatch: return (%i)", ret);
178 return (ret);
179 } /* int cf_dispatch */
181 static int dispatch_global_option (const oconfig_item_t *ci)
182 {
183 if (ci->values_num != 1)
184 return (-1);
185 if (ci->values[0].type == OCONFIG_TYPE_STRING)
186 return (global_option_set (ci->key, ci->values[0].value.string));
187 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
188 {
189 char tmp[128];
190 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
191 return (global_option_set (ci->key, tmp));
192 }
193 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
194 {
195 if (ci->values[0].value.boolean)
196 return (global_option_set (ci->key, "true"));
197 else
198 return (global_option_set (ci->key, "false"));
199 }
201 return (-1);
202 } /* int dispatch_global_option */
204 static int dispatch_value_typesdb (const oconfig_item_t *ci)
205 {
206 int i = 0;
208 assert (strcasecmp (ci->key, "TypesDB") == 0);
210 cf_default_typesdb = 0;
212 if (ci->values_num < 1) {
213 ERROR ("configfile: `TypesDB' needs at least one argument.");
214 return (-1);
215 }
217 for (i = 0; i < ci->values_num; ++i)
218 {
219 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
220 WARNING ("configfile: TypesDB: Skipping %i. argument which "
221 "is not a string.", i + 1);
222 continue;
223 }
225 read_types_list (ci->values[i].value.string);
226 }
227 return (0);
228 } /* int dispatch_value_typesdb */
230 static int dispatch_value_plugindir (const oconfig_item_t *ci)
231 {
232 assert (strcasecmp (ci->key, "PluginDir") == 0);
234 if (ci->values_num != 1)
235 return (-1);
236 if (ci->values[0].type != OCONFIG_TYPE_STRING)
237 return (-1);
239 plugin_set_dir (ci->values[0].value.string);
240 return (0);
241 }
243 static int dispatch_loadplugin (const oconfig_item_t *ci)
244 {
245 int i;
246 uint32_t flags = 0;
247 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
249 if (ci->values_num != 1)
250 return (-1);
251 if (ci->values[0].type != OCONFIG_TYPE_STRING)
252 return (-1);
254 for (i = 0; i < ci->children_num; ++i) {
255 if (ci->children[i].values_num != 1 ||
256 ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
257 WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
258 continue;
259 }
260 if (strcasecmp(ci->children[i].key, "globals") == 0) {
261 flags |= PLUGIN_FLAGS_GLOBAL;
262 } else {
263 WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
264 }
265 }
266 return (plugin_load (ci->values[0].value.string, flags));
267 } /* int dispatch_value_loadplugin */
269 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
270 {
271 char buffer[4096];
272 char *buffer_ptr;
273 int buffer_free;
274 int i;
276 buffer_ptr = buffer;
277 buffer_free = sizeof (buffer);
279 for (i = 0; i < ci->values_num; i++)
280 {
281 int status = -1;
283 if (ci->values[i].type == OCONFIG_TYPE_STRING)
284 status = ssnprintf (buffer_ptr, buffer_free, " %s",
285 ci->values[i].value.string);
286 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
287 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
288 ci->values[i].value.number);
289 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
290 status = ssnprintf (buffer_ptr, buffer_free, " %s",
291 ci->values[i].value.boolean
292 ? "true" : "false");
294 if ((status < 0) || (status >= buffer_free))
295 return (-1);
296 buffer_free -= status;
297 buffer_ptr += status;
298 }
299 /* skip the initial space */
300 buffer_ptr = buffer + 1;
302 return (cf_dispatch (plugin, ci->key, buffer_ptr));
303 } /* int dispatch_value_plugin */
305 static int dispatch_value (const oconfig_item_t *ci)
306 {
307 int ret = -2;
308 int i;
310 for (i = 0; i < cf_value_map_num; i++)
311 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
312 {
313 ret = cf_value_map[i].func (ci);
314 break;
315 }
317 for (i = 0; i < cf_global_options_num; i++)
318 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
319 {
320 ret = dispatch_global_option (ci);
321 break;
322 }
324 return (ret);
325 } /* int dispatch_value */
327 static int dispatch_block_plugin (oconfig_item_t *ci)
328 {
329 int i;
330 char *name;
332 cf_complex_callback_t *cb;
334 if (strcasecmp (ci->key, "Plugin") != 0)
335 return (-1);
336 if (ci->values_num < 1)
337 return (-1);
338 if (ci->values[0].type != OCONFIG_TYPE_STRING)
339 return (-1);
341 name = ci->values[0].value.string;
343 /* Check for a complex callback first */
344 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
345 if (strcasecmp (name, cb->type) == 0)
346 return (cb->callback (ci));
348 /* Hm, no complex plugin found. Dispatch the values one by one */
349 for (i = 0; i < ci->children_num; i++)
350 {
351 if (ci->children[i].children == NULL)
352 dispatch_value_plugin (name, ci->children + i);
353 else
354 {
355 WARNING ("There is a `%s' block within the "
356 "configuration for the %s plugin. "
357 "The plugin either only expects "
358 "\"simple\" configuration statements "
359 "or wasn't loaded using `LoadPlugin'."
360 " Please check your configuration.",
361 ci->children[i].key, name);
362 }
363 }
365 return (0);
366 }
369 static int dispatch_block (oconfig_item_t *ci)
370 {
371 if (strcasecmp (ci->key, "LoadPlugin") == 0)
372 return (dispatch_loadplugin (ci));
373 else if (strcasecmp (ci->key, "Plugin") == 0)
374 return (dispatch_block_plugin (ci));
375 else if (strcasecmp (ci->key, "Threshold") == 0)
376 return (ut_config (ci));
377 else if (strcasecmp (ci->key, "Chain") == 0)
378 return (fc_configure (ci));
380 return (0);
381 }
383 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
384 int offset)
385 {
386 oconfig_item_t *temp;
387 int i;
389 assert (offset >= 0);
390 assert (dst->children_num > offset);
392 /* Free the memory used by the replaced child. Usually that's the
393 * `Include "blah"' statement. */
394 temp = dst->children + offset;
395 for (i = 0; i < temp->values_num; i++)
396 {
397 if (temp->values[i].type == OCONFIG_TYPE_STRING)
398 {
399 sfree (temp->values[i].value.string);
400 }
401 }
402 sfree (temp->values);
403 temp = NULL;
405 /* If (src->children_num == 0) the array size is decreased. If offset
406 * is _not_ the last element, (offset < (dst->children_num - 1)), then
407 * we need to move the trailing elements before resizing the array. */
408 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
409 {
410 int nmemb = dst->children_num - (offset + 1);
411 memmove (dst->children + offset, dst->children + offset + 1,
412 sizeof (oconfig_item_t) * nmemb);
413 }
415 /* Resize the memory containing the children to be big enough to hold
416 * all children. */
417 temp = (oconfig_item_t *) realloc (dst->children,
418 sizeof (oconfig_item_t)
419 * (dst->children_num + src->children_num - 1));
420 if (temp == NULL)
421 {
422 ERROR ("configfile: realloc failed.");
423 return (-1);
424 }
425 dst->children = temp;
427 /* If there are children behind the include statement, and they have
428 * not yet been moved because (src->children_num == 0), then move them
429 * to the end of the list, so that the new children have room before
430 * them. */
431 if ((src->children_num > 0)
432 && ((dst->children_num - (offset + 1)) > 0))
433 {
434 int nmemb = dst->children_num - (offset + 1);
435 int old_offset = offset + 1;
436 int new_offset = offset + src->children_num;
438 memmove (dst->children + new_offset,
439 dst->children + old_offset,
440 sizeof (oconfig_item_t) * nmemb);
441 }
443 /* Last but not least: If there are new children, copy them to the
444 * memory reserved for them. */
445 if (src->children_num > 0)
446 {
447 memcpy (dst->children + offset,
448 src->children,
449 sizeof (oconfig_item_t) * src->children_num);
450 }
452 /* Update the number of children. */
453 dst->children_num += (src->children_num - 1);
455 return (0);
456 } /* int cf_ci_replace_child */
458 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
459 {
460 oconfig_item_t *temp;
462 if ((src == NULL) || (src->children_num == 0))
463 return (0);
465 temp = (oconfig_item_t *) realloc (dst->children,
466 sizeof (oconfig_item_t)
467 * (dst->children_num + src->children_num));
468 if (temp == NULL)
469 {
470 ERROR ("configfile: realloc failed.");
471 return (-1);
472 }
473 dst->children = temp;
475 memcpy (dst->children + dst->children_num,
476 src->children,
477 sizeof (oconfig_item_t)
478 * src->children_num);
479 dst->children_num += src->children_num;
481 return (0);
482 } /* int cf_ci_append_children */
484 #define CF_MAX_DEPTH 8
485 static oconfig_item_t *cf_read_generic (const char *path, int depth);
487 static int cf_include_all (oconfig_item_t *root, int depth)
488 {
489 int i;
491 for (i = 0; i < root->children_num; i++)
492 {
493 oconfig_item_t *new;
494 oconfig_item_t *old;
496 /* Ignore all blocks, including `Include' blocks. */
497 if (root->children[i].children_num != 0)
498 continue;
500 if (strcasecmp (root->children[i].key, "Include") != 0)
501 continue;
503 old = root->children + i;
505 if ((old->values_num != 1)
506 || (old->values[0].type != OCONFIG_TYPE_STRING))
507 {
508 ERROR ("configfile: `Include' needs exactly one string argument.");
509 continue;
510 }
512 new = cf_read_generic (old->values[0].value.string, depth + 1);
513 if (new == NULL)
514 continue;
516 /* Now replace the i'th child in `root' with `new'. */
517 cf_ci_replace_child (root, new, i);
519 /* ... and go back to the new i'th child. */
520 --i;
522 sfree (new->values);
523 sfree (new);
524 } /* for (i = 0; i < root->children_num; i++) */
526 return (0);
527 } /* int cf_include_all */
529 static oconfig_item_t *cf_read_file (const char *file, int depth)
530 {
531 oconfig_item_t *root;
533 assert (depth < CF_MAX_DEPTH);
535 root = oconfig_parse_file (file);
536 if (root == NULL)
537 {
538 ERROR ("configfile: Cannot read file `%s'.", file);
539 return (NULL);
540 }
542 cf_include_all (root, depth);
544 return (root);
545 } /* oconfig_item_t *cf_read_file */
547 static int cf_compare_string (const void *p1, const void *p2)
548 {
549 return strcmp (*(const char **) p1, *(const char **) p2);
550 }
552 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
553 {
554 oconfig_item_t *root = NULL;
555 DIR *dh;
556 struct dirent *de;
557 char **filenames = NULL;
558 int filenames_num = 0;
559 int status;
560 int i;
562 assert (depth < CF_MAX_DEPTH);
564 dh = opendir (dir);
565 if (dh == NULL)
566 {
567 char errbuf[1024];
568 ERROR ("configfile: opendir failed: %s",
569 sstrerror (errno, errbuf, sizeof (errbuf)));
570 return (NULL);
571 }
573 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
574 if (root == NULL)
575 {
576 ERROR ("configfile: malloc failed.");
577 return (NULL);
578 }
579 memset (root, 0, sizeof (oconfig_item_t));
581 while ((de = readdir (dh)) != NULL)
582 {
583 char name[1024];
584 char **tmp;
586 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
587 continue;
589 status = ssnprintf (name, sizeof (name), "%s/%s",
590 dir, de->d_name);
591 if ((status < 0) || ((size_t) status >= sizeof (name)))
592 {
593 ERROR ("configfile: Not including `%s/%s' because its"
594 " name is too long.",
595 dir, de->d_name);
596 for (i = 0; i < filenames_num; ++i)
597 free (filenames[i]);
598 free (filenames);
599 free (root);
600 return (NULL);
601 }
603 ++filenames_num;
604 tmp = (char **) realloc (filenames,
605 filenames_num * sizeof (*filenames));
606 if (tmp == NULL) {
607 ERROR ("configfile: realloc failed.");
608 for (i = 0; i < filenames_num - 1; ++i)
609 free (filenames[i]);
610 free (filenames);
611 free (root);
612 return (NULL);
613 }
614 filenames = tmp;
616 filenames[filenames_num - 1] = sstrdup (name);
617 }
619 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
620 cf_compare_string);
622 for (i = 0; i < filenames_num; ++i)
623 {
624 oconfig_item_t *temp;
625 char *name = filenames[i];
627 temp = cf_read_generic (name, depth);
628 if (temp == NULL)
629 {
630 /* An error should already have been reported. */
631 sfree (name);
632 continue;
633 }
635 cf_ci_append_children (root, temp);
636 sfree (temp->children);
637 sfree (temp);
639 free (name);
640 }
642 free(filenames);
643 return (root);
644 } /* oconfig_item_t *cf_read_dir */
646 /*
647 * cf_read_generic
648 *
649 * Path is stat'ed and either cf_read_file or cf_read_dir is called
650 * accordingly.
651 *
652 * There are two versions of this function: If `wordexp' exists shell wildcards
653 * will be expanded and the function will include all matches found. If
654 * `wordexp' (or, more precisely, it's header file) is not available the
655 * simpler function is used which does not do any such expansion.
656 */
657 #if HAVE_WORDEXP_H
658 static oconfig_item_t *cf_read_generic (const char *path, int depth)
659 {
660 oconfig_item_t *root = NULL;
661 int status;
662 const char *path_ptr;
663 wordexp_t we;
664 size_t i;
666 if (depth >= CF_MAX_DEPTH)
667 {
668 ERROR ("configfile: Not including `%s' because the maximum "
669 "nesting depth has been reached.", path);
670 return (NULL);
671 }
673 status = wordexp (path, &we, WRDE_NOCMD);
674 if (status != 0)
675 {
676 ERROR ("configfile: wordexp (%s) failed.", path);
677 return (NULL);
678 }
680 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
681 if (root == NULL)
682 {
683 ERROR ("configfile: malloc failed.");
684 return (NULL);
685 }
686 memset (root, '\0', sizeof (oconfig_item_t));
688 /* wordexp() might return a sorted list already. That's not
689 * documented though, so let's make sure we get what we want. */
690 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
691 cf_compare_string);
693 for (i = 0; i < we.we_wordc; i++)
694 {
695 oconfig_item_t *temp;
696 struct stat statbuf;
698 path_ptr = we.we_wordv[i];
700 status = stat (path_ptr, &statbuf);
701 if (status != 0)
702 {
703 char errbuf[1024];
704 WARNING ("configfile: stat (%s) failed: %s",
705 path_ptr,
706 sstrerror (errno, errbuf, sizeof (errbuf)));
707 continue;
708 }
710 if (S_ISREG (statbuf.st_mode))
711 temp = cf_read_file (path_ptr, depth);
712 else if (S_ISDIR (statbuf.st_mode))
713 temp = cf_read_dir (path_ptr, depth);
714 else
715 {
716 WARNING ("configfile: %s is neither a file nor a "
717 "directory.", path);
718 continue;
719 }
721 if (temp == NULL) {
722 oconfig_free (root);
723 return (NULL);
724 }
726 cf_ci_append_children (root, temp);
727 sfree (temp->children);
728 sfree (temp);
729 }
731 wordfree (&we);
733 if (root->children == NULL)
734 {
735 oconfig_free (root);
736 return (NULL);
737 }
739 return (root);
740 } /* oconfig_item_t *cf_read_generic */
741 /* #endif HAVE_WORDEXP_H */
743 #else /* if !HAVE_WORDEXP_H */
744 static oconfig_item_t *cf_read_generic (const char *path, int depth)
745 {
746 struct stat statbuf;
747 int status;
749 if (depth >= CF_MAX_DEPTH)
750 {
751 ERROR ("configfile: Not including `%s' because the maximum "
752 "nesting depth has been reached.", path);
753 return (NULL);
754 }
756 status = stat (path, &statbuf);
757 if (status != 0)
758 {
759 char errbuf[1024];
760 ERROR ("configfile: stat (%s) failed: %s",
761 path,
762 sstrerror (errno, errbuf, sizeof (errbuf)));
763 return (NULL);
764 }
766 if (S_ISREG (statbuf.st_mode))
767 return (cf_read_file (path, depth));
768 else if (S_ISDIR (statbuf.st_mode))
769 return (cf_read_dir (path, depth));
771 ERROR ("configfile: %s is neither a file nor a directory.", path);
772 return (NULL);
773 } /* oconfig_item_t *cf_read_generic */
774 #endif /* !HAVE_WORDEXP_H */
776 /*
777 * Public functions
778 */
779 int global_option_set (const char *option, const char *value)
780 {
781 int i;
783 DEBUG ("option = %s; value = %s;", option, value);
785 for (i = 0; i < cf_global_options_num; i++)
786 if (strcasecmp (cf_global_options[i].key, option) == 0)
787 break;
789 if (i >= cf_global_options_num)
790 return (-1);
792 sfree (cf_global_options[i].value);
794 if (value != NULL)
795 cf_global_options[i].value = strdup (value);
796 else
797 cf_global_options[i].value = NULL;
799 return (0);
800 }
802 const char *global_option_get (const char *option)
803 {
804 int i;
806 for (i = 0; i < cf_global_options_num; i++)
807 if (strcasecmp (cf_global_options[i].key, option) == 0)
808 break;
810 if (i >= cf_global_options_num)
811 return (NULL);
813 return ((cf_global_options[i].value != NULL)
814 ? cf_global_options[i].value
815 : cf_global_options[i].def);
816 } /* char *global_option_get */
818 void cf_unregister (const char *type)
819 {
820 cf_callback_t *this, *prev;
822 for (prev = NULL, this = first_callback;
823 this != NULL;
824 prev = this, this = this->next)
825 if (strcasecmp (this->type, type) == 0)
826 {
827 if (prev == NULL)
828 first_callback = this->next;
829 else
830 prev->next = this->next;
832 free (this);
833 break;
834 }
835 } /* void cf_unregister */
837 void cf_unregister_complex (const char *type)
838 {
839 cf_complex_callback_t *this, *prev;
841 for (prev = NULL, this = complex_callback_head;
842 this != NULL;
843 prev = this, this = this->next)
844 if (strcasecmp (this->type, type) == 0)
845 {
846 if (prev == NULL)
847 complex_callback_head = this->next;
848 else
849 prev->next = this->next;
851 sfree (this->type);
852 sfree (this);
853 break;
854 }
855 } /* void cf_unregister */
857 void cf_register (const char *type,
858 int (*callback) (const char *, const char *),
859 const char **keys, int keys_num)
860 {
861 cf_callback_t *cf_cb;
863 /* Remove this module from the list, if it already exists */
864 cf_unregister (type);
866 /* This pointer will be free'd in `cf_unregister' */
867 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
868 return;
870 cf_cb->type = type;
871 cf_cb->callback = callback;
872 cf_cb->keys = keys;
873 cf_cb->keys_num = keys_num;
875 cf_cb->next = first_callback;
876 first_callback = cf_cb;
877 } /* void cf_register */
879 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
880 {
881 cf_complex_callback_t *new;
883 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
884 if (new == NULL)
885 return (-1);
887 new->type = strdup (type);
888 if (new->type == NULL)
889 {
890 sfree (new);
891 return (-1);
892 }
894 new->callback = callback;
895 new->next = NULL;
897 if (complex_callback_head == NULL)
898 {
899 complex_callback_head = new;
900 }
901 else
902 {
903 cf_complex_callback_t *last = complex_callback_head;
904 while (last->next != NULL)
905 last = last->next;
906 last->next = new;
907 }
909 return (0);
910 } /* int cf_register_complex */
912 int cf_read (char *filename)
913 {
914 oconfig_item_t *conf;
915 int i;
917 conf = cf_read_generic (filename, 0 /* depth */);
918 if (conf == NULL)
919 {
920 ERROR ("Unable to read config file %s.", filename);
921 return (-1);
922 }
924 for (i = 0; i < conf->children_num; i++)
925 {
926 if (conf->children[i].children == NULL)
927 dispatch_value (conf->children + i);
928 else
929 dispatch_block (conf->children + i);
930 }
932 oconfig_free (conf);
934 /* Read the default types.db if no `TypesDB' option was given. */
935 if (cf_default_typesdb)
936 read_types_list (PKGDATADIR"/types.db");
938 return (0);
939 } /* int cf_read */
941 /* Assures the config option is a string, duplicates it and returns the copy in
942 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
943 * success. */
944 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
945 {
946 char *string;
948 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
949 {
950 ERROR ("cf_util_get_string: The %s option requires "
951 "exactly one string argument.", ci->key);
952 return (-1);
953 }
955 string = strdup (ci->values[0].value.string);
956 if (string == NULL)
957 return (-1);
959 if (*ret_string != NULL)
960 sfree (*ret_string);
961 *ret_string = string;
963 return (0);
964 } /* }}} int cf_util_get_string */
966 /* Assures the config option is a string and copies it to the provided buffer.
967 * Assures null-termination. */
968 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
969 size_t buffer_size)
970 {
971 if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
972 return (EINVAL);
974 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
975 {
976 ERROR ("cf_util_get_string_buffer: The %s option requires "
977 "exactly one string argument.", ci->key);
978 return (-1);
979 }
981 strncpy (buffer, ci->values[0].value.string, buffer_size);
982 buffer[buffer_size - 1] = 0;
984 return (0);
985 } /* }}} int cf_util_get_string_buffer */
987 /* Assures the config option is a number and returns it as an int. */
988 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
989 {
990 if ((ci == NULL) || (ret_value == NULL))
991 return (EINVAL);
993 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
994 {
995 ERROR ("cf_util_get_int: The %s option requires "
996 "exactly one numeric argument.", ci->key);
997 return (-1);
998 }
1000 *ret_value = (int) ci->values[0].value.number;
1002 return (0);
1003 } /* }}} int cf_util_get_int */
1005 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1006 {
1007 if ((ci == NULL) || (ret_bool == NULL))
1008 return (EINVAL);
1010 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1011 {
1012 ERROR ("cf_util_get_boolean: The %s option requires "
1013 "exactly one boolean argument.", ci->key);
1014 return (-1);
1015 }
1017 *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1019 return (0);
1020 } /* }}} int cf_util_get_boolean */
1022 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1023 unsigned int *ret_value, unsigned int flag)
1024 {
1025 int status;
1026 _Bool b;
1028 if (ret_value == NULL)
1029 return (EINVAL);
1031 b = 0;
1032 status = cf_util_get_boolean (ci, &b);
1033 if (status != 0)
1034 return (status);
1036 if (b)
1037 {
1038 *ret_value |= flag;
1039 }
1040 else
1041 {
1042 *ret_value &= ~flag;
1043 }
1045 return (0);
1046 } /* }}} int cf_util_get_flag */
1048 /* Assures that the config option is a string. The string is then converted to
1049 * a port number using `service_name_to_port_number' and returned. Returns the
1050 * port number in the range [1-65535] or less than zero upon failure. */
1051 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1052 {
1053 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1054 {
1055 ERROR ("cf_util_get_port_number: The %s option requires "
1056 "exactly one string argument.", ci->key);
1057 return (-1);
1058 }
1060 return (service_name_to_port_number (ci->values[0].value.string));
1061 } /* }}} int cf_util_get_port_number */