1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Sebastian tokkee Harl <sh at tokkee.org>
22 **/
24 #include "collectd.h"
26 #include "liboconfig/oconfig.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "utils_threshold.h"
33 #include "filter_chain.h"
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
41 /*
42 * Private types
43 */
44 typedef struct cf_callback
45 {
46 const char *type;
47 int (*callback) (const char *, const char *);
48 const char **keys;
49 int keys_num;
50 struct cf_callback *next;
51 } cf_callback_t;
53 typedef struct cf_complex_callback_s
54 {
55 char *type;
56 int (*callback) (oconfig_item_t *);
57 struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
60 typedef struct cf_value_map_s
61 {
62 char *key;
63 int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
66 typedef struct cf_global_option_s
67 {
68 char *key;
69 char *value;
70 char *def;
71 } cf_global_option_t;
73 /*
74 * Prototypes of callback functions
75 */
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
80 /*
81 * Private variables
82 */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
86 static cf_value_map_t cf_value_map[] =
87 {
88 {"TypesDB", dispatch_value_typesdb},
89 {"PluginDir", dispatch_value_plugindir},
90 {"LoadPlugin", dispatch_value_loadplugin}
91 };
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
94 static cf_global_option_t cf_global_options[] =
95 {
96 {"BaseDir", NULL, PKGLOCALSTATEDIR},
97 {"PIDFile", NULL, PIDFILE},
98 {"Hostname", NULL, NULL},
99 {"FQDNLookup", NULL, "false"},
100 {"Interval", NULL, "10"},
101 {"ReadThreads", NULL, "5"},
102 {"PreCacheChain", NULL, "PreCache"},
103 {"PostCacheChain", NULL, "PostCache"}
104 };
105 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
107 static int cf_default_typesdb = 1;
109 /*
110 * Functions to handle register/unregister, search, and other plugin related
111 * stuff
112 */
113 static cf_callback_t *cf_search (const char *type)
114 {
115 cf_callback_t *cf_cb;
117 if (type == NULL)
118 return (NULL);
120 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
121 if (strcasecmp (cf_cb->type, type) == 0)
122 break;
124 return (cf_cb);
125 }
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;
136 DEBUG ("type = %s, key = %s, value = %s",
137 ESCAPE_NULL(type),
138 ESCAPE_NULL(orig_key),
139 ESCAPE_NULL(orig_value));
141 if ((cf_cb = cf_search (type)) == NULL)
142 {
143 WARNING ("Found a configuration for the `%s' plugin, but "
144 "the plugin isn't loaded or didn't register "
145 "a configuration callback.", type);
146 return (-1);
147 }
149 if ((key = strdup (orig_key)) == NULL)
150 return (1);
151 if ((value = strdup (orig_value)) == NULL)
152 {
153 free (key);
154 return (2);
155 }
157 ret = -1;
159 for (i = 0; i < cf_cb->keys_num; i++)
160 {
161 if ((cf_cb->keys[i] != NULL)
162 && (strcasecmp (cf_cb->keys[i], key) == 0))
163 {
164 ret = (*cf_cb->callback) (key, value);
165 break;
166 }
167 }
169 if (i >= cf_cb->keys_num)
170 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
172 free (key);
173 free (value);
175 DEBUG ("cf_dispatch: return (%i)", ret);
177 return (ret);
178 } /* int cf_dispatch */
180 static int dispatch_global_option (const oconfig_item_t *ci)
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 }
200 return (-1);
201 } /* int dispatch_global_option */
203 static int dispatch_value_typesdb (const oconfig_item_t *ci)
204 {
205 int i = 0;
207 assert (strcasecmp (ci->key, "TypesDB") == 0);
209 cf_default_typesdb = 0;
211 if (ci->values_num < 1) {
212 ERROR ("configfile: `TypesDB' needs at least one argument.");
213 return (-1);
214 }
216 for (i = 0; i < ci->values_num; ++i)
217 {
218 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
219 WARNING ("configfile: TypesDB: Skipping %i. argument which "
220 "is not a string.", i + 1);
221 continue;
222 }
224 read_types_list (ci->values[i].value.string);
225 }
226 return (0);
227 } /* int dispatch_value_typesdb */
229 static int dispatch_value_plugindir (const oconfig_item_t *ci)
230 {
231 assert (strcasecmp (ci->key, "PluginDir") == 0);
233 if (ci->values_num != 1)
234 return (-1);
235 if (ci->values[0].type != OCONFIG_TYPE_STRING)
236 return (-1);
238 plugin_set_dir (ci->values[0].value.string);
239 return (0);
240 }
242 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
243 {
244 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
246 if (ci->values_num != 1)
247 return (-1);
248 if (ci->values[0].type != OCONFIG_TYPE_STRING)
249 return (-1);
251 return (plugin_load (ci->values[0].value.string));
252 } /* int dispatch_value_loadplugin */
254 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
255 {
256 char buffer[4096];
257 char *buffer_ptr;
258 int buffer_free;
259 int i;
261 buffer_ptr = buffer;
262 buffer_free = sizeof (buffer);
264 for (i = 0; i < ci->values_num; i++)
265 {
266 int status = -1;
268 if (ci->values[i].type == OCONFIG_TYPE_STRING)
269 status = ssnprintf (buffer_ptr, buffer_free, " %s",
270 ci->values[i].value.string);
271 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
272 status = ssnprintf (buffer_ptr, buffer_free, " %lf",
273 ci->values[i].value.number);
274 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
275 status = ssnprintf (buffer_ptr, buffer_free, " %s",
276 ci->values[i].value.boolean
277 ? "true" : "false");
279 if ((status < 0) || (status >= buffer_free))
280 return (-1);
281 buffer_free -= status;
282 buffer_ptr += status;
283 }
284 /* skip the initial space */
285 buffer_ptr = buffer + 1;
287 return (cf_dispatch (plugin, ci->key, buffer_ptr));
288 } /* int dispatch_value_plugin */
290 static int dispatch_value (const oconfig_item_t *ci)
291 {
292 int ret = -2;
293 int i;
295 for (i = 0; i < cf_value_map_num; i++)
296 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
297 {
298 ret = cf_value_map[i].func (ci);
299 break;
300 }
302 for (i = 0; i < cf_global_options_num; i++)
303 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
304 {
305 ret = dispatch_global_option (ci);
306 break;
307 }
309 return (ret);
310 } /* int dispatch_value */
312 static int dispatch_block_plugin (oconfig_item_t *ci)
313 {
314 int i;
315 char *name;
317 cf_complex_callback_t *cb;
319 if (strcasecmp (ci->key, "Plugin") != 0)
320 return (-1);
321 if (ci->values_num < 1)
322 return (-1);
323 if (ci->values[0].type != OCONFIG_TYPE_STRING)
324 return (-1);
326 name = ci->values[0].value.string;
328 /* Check for a complex callback first */
329 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
330 if (strcasecmp (name, cb->type) == 0)
331 return (cb->callback (ci));
333 /* Hm, no complex plugin found. Dispatch the values one by one */
334 for (i = 0; i < ci->children_num; i++)
335 {
336 if (ci->children[i].children == NULL)
337 dispatch_value_plugin (name, ci->children + i);
338 else
339 {
340 WARNING ("There is a `%s' block within the "
341 "configuration for the %s plugin. "
342 "The plugin either only expects "
343 "\"simple\" configuration statements "
344 "or wasn't loaded using `LoadPlugin'."
345 " Please check your configuration.",
346 ci->children[i].key, name);
347 }
348 }
350 return (0);
351 }
354 static int dispatch_block (oconfig_item_t *ci)
355 {
356 if (strcasecmp (ci->key, "Plugin") == 0)
357 return (dispatch_block_plugin (ci));
358 else if (strcasecmp (ci->key, "Threshold") == 0)
359 return (ut_config (ci));
360 else if (strcasecmp (ci->key, "Chain") == 0)
361 return (fc_configure (ci));
363 return (0);
364 }
366 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
367 int offset)
368 {
369 oconfig_item_t *temp;
370 int i;
372 assert (offset >= 0);
373 assert (dst->children_num > offset);
375 /* Free the memory used by the replaced child. Usually that's the
376 * `Include "blah"' statement. */
377 temp = dst->children + offset;
378 for (i = 0; i < temp->values_num; i++)
379 {
380 if (temp->values[i].type == OCONFIG_TYPE_STRING)
381 {
382 sfree (temp->values[i].value.string);
383 }
384 }
385 sfree (temp->values);
386 temp = NULL;
388 /* If (src->children_num == 0) the array size is decreased. If offset
389 * is _not_ the last element, (offset < (dst->children_num - 1)), then
390 * we need to move the trailing elements before resizing the array. */
391 if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
392 {
393 int nmemb = dst->children_num - (offset + 1);
394 memmove (dst->children + offset, dst->children + offset + 1,
395 sizeof (oconfig_item_t) * nmemb);
396 }
398 /* Resize the memory containing the children to be big enough to hold
399 * all children. */
400 temp = (oconfig_item_t *) realloc (dst->children,
401 sizeof (oconfig_item_t)
402 * (dst->children_num + src->children_num - 1));
403 if (temp == NULL)
404 {
405 ERROR ("configfile: realloc failed.");
406 return (-1);
407 }
408 dst->children = temp;
410 /* If there are children behind the include statement, and they have
411 * not yet been moved because (src->children_num == 0), then move them
412 * to the end of the list, so that the new children have room before
413 * them. */
414 if ((src->children_num > 0)
415 && ((dst->children_num - (offset + 1)) > 0))
416 {
417 int nmemb = dst->children_num - (offset + 1);
418 int old_offset = offset + 1;
419 int new_offset = offset + src->children_num;
421 memmove (dst->children + new_offset,
422 dst->children + old_offset,
423 sizeof (oconfig_item_t) * nmemb);
424 }
426 /* Last but not least: If there are new children, copy them to the
427 * memory reserved for them. */
428 if (src->children_num > 0)
429 {
430 memcpy (dst->children + offset,
431 src->children,
432 sizeof (oconfig_item_t) * src->children_num);
433 }
435 /* Update the number of children. */
436 dst->children_num += (src->children_num - 1);
438 return (0);
439 } /* int cf_ci_replace_child */
441 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
442 {
443 oconfig_item_t *temp;
445 if ((src == NULL) || (src->children_num == 0))
446 return (0);
448 temp = (oconfig_item_t *) realloc (dst->children,
449 sizeof (oconfig_item_t)
450 * (dst->children_num + src->children_num));
451 if (temp == NULL)
452 {
453 ERROR ("configfile: realloc failed.");
454 return (-1);
455 }
456 dst->children = temp;
458 memcpy (dst->children + dst->children_num,
459 src->children,
460 sizeof (oconfig_item_t)
461 * src->children_num);
462 dst->children_num += src->children_num;
464 return (0);
465 } /* int cf_ci_append_children */
467 #define CF_MAX_DEPTH 8
468 static oconfig_item_t *cf_read_generic (const char *path, int depth);
470 static int cf_include_all (oconfig_item_t *root, int depth)
471 {
472 int i;
474 for (i = 0; i < root->children_num; i++)
475 {
476 oconfig_item_t *new;
477 oconfig_item_t *old;
479 /* Ignore all blocks, including `Include' blocks. */
480 if (root->children[i].children_num != 0)
481 continue;
483 if (strcasecmp (root->children[i].key, "Include") != 0)
484 continue;
486 old = root->children + i;
488 if ((old->values_num != 1)
489 || (old->values[0].type != OCONFIG_TYPE_STRING))
490 {
491 ERROR ("configfile: `Include' needs exactly one string argument.");
492 continue;
493 }
495 new = cf_read_generic (old->values[0].value.string, depth + 1);
496 if (new == NULL)
497 continue;
499 /* Now replace the i'th child in `root' with `new'. */
500 cf_ci_replace_child (root, new, i);
502 /* ... and go back to the new i'th child. */
503 --i;
505 sfree (new->values);
506 sfree (new);
507 } /* for (i = 0; i < root->children_num; i++) */
509 return (0);
510 } /* int cf_include_all */
512 static oconfig_item_t *cf_read_file (const char *file, int depth)
513 {
514 oconfig_item_t *root;
516 assert (depth < CF_MAX_DEPTH);
518 root = oconfig_parse_file (file);
519 if (root == NULL)
520 {
521 ERROR ("configfile: Cannot read file `%s'.", file);
522 return (NULL);
523 }
525 cf_include_all (root, depth);
527 return (root);
528 } /* oconfig_item_t *cf_read_file */
530 static int cf_compare_string (const void *p1, const void *p2)
531 {
532 return strcmp (*(const char **) p1, *(const char **) p2);
533 }
535 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
536 {
537 oconfig_item_t *root = NULL;
538 DIR *dh;
539 struct dirent *de;
540 char **filenames = NULL;
541 int filenames_num = 0;
542 int status;
543 int i;
545 assert (depth < CF_MAX_DEPTH);
547 dh = opendir (dir);
548 if (dh == NULL)
549 {
550 char errbuf[1024];
551 ERROR ("configfile: opendir failed: %s",
552 sstrerror (errno, errbuf, sizeof (errbuf)));
553 return (NULL);
554 }
556 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
557 if (root == NULL)
558 {
559 ERROR ("configfile: malloc failed.");
560 return (NULL);
561 }
562 memset (root, '\0', sizeof (oconfig_item_t));
564 while ((de = readdir (dh)) != NULL)
565 {
566 char name[1024];
567 char **tmp;
569 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
570 continue;
572 status = ssnprintf (name, sizeof (name), "%s/%s",
573 dir, de->d_name);
574 if ((status < 0) || ((size_t) status >= sizeof (name)))
575 {
576 ERROR ("configfile: Not including `%s/%s' because its"
577 " name is too long.",
578 dir, de->d_name);
579 for (i = 0; i < filenames_num; ++i)
580 free (filenames[i]);
581 free (filenames);
582 free (root);
583 return (NULL);
584 }
586 ++filenames_num;
587 tmp = (char **) realloc (filenames,
588 filenames_num * sizeof (*filenames));
589 if (tmp == NULL) {
590 ERROR ("configfile: realloc failed.");
591 for (i = 0; i < filenames_num - 1; ++i)
592 free (filenames[i]);
593 free (filenames);
594 free (root);
595 return (NULL);
596 }
597 filenames = tmp;
599 filenames[filenames_num - 1] = sstrdup (name);
600 }
602 qsort ((void *) filenames, filenames_num, sizeof (*filenames),
603 cf_compare_string);
605 for (i = 0; i < filenames_num; ++i)
606 {
607 oconfig_item_t *temp;
608 char *name = filenames[i];
610 temp = cf_read_generic (name, depth);
611 if (temp == NULL) {
612 int j;
613 for (j = i; j < filenames_num; ++j)
614 free (filenames[j]);
615 free (filenames);
616 oconfig_free (root);
617 return (NULL);
618 }
620 cf_ci_append_children (root, temp);
621 sfree (temp->children);
622 sfree (temp);
624 free (name);
625 }
627 free(filenames);
628 return (root);
629 } /* oconfig_item_t *cf_read_dir */
631 /*
632 * cf_read_generic
633 *
634 * Path is stat'ed and either cf_read_file or cf_read_dir is called
635 * accordingly.
636 *
637 * There are two versions of this function: If `wordexp' exists shell wildcards
638 * will be expanded and the function will include all matches found. If
639 * `wordexp' (or, more precisely, it's header file) is not available the
640 * simpler function is used which does not do any such expansion.
641 */
642 #if HAVE_WORDEXP_H
643 static oconfig_item_t *cf_read_generic (const char *path, int depth)
644 {
645 oconfig_item_t *root = NULL;
646 int status;
647 const char *path_ptr;
648 wordexp_t we;
649 size_t i;
651 if (depth >= CF_MAX_DEPTH)
652 {
653 ERROR ("configfile: Not including `%s' because the maximum "
654 "nesting depth has been reached.", path);
655 return (NULL);
656 }
658 status = wordexp (path, &we, WRDE_NOCMD);
659 if (status != 0)
660 {
661 ERROR ("configfile: wordexp (%s) failed.", path);
662 return (NULL);
663 }
665 root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
666 if (root == NULL)
667 {
668 ERROR ("configfile: malloc failed.");
669 return (NULL);
670 }
671 memset (root, '\0', sizeof (oconfig_item_t));
673 /* wordexp() might return a sorted list already. That's not
674 * documented though, so let's make sure we get what we want. */
675 qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
676 cf_compare_string);
678 for (i = 0; i < we.we_wordc; i++)
679 {
680 oconfig_item_t *temp;
681 struct stat statbuf;
683 path_ptr = we.we_wordv[i];
685 status = stat (path_ptr, &statbuf);
686 if (status != 0)
687 {
688 char errbuf[1024];
689 ERROR ("configfile: stat (%s) failed: %s",
690 path_ptr,
691 sstrerror (errno, errbuf, sizeof (errbuf)));
692 oconfig_free (root);
693 return (NULL);
694 }
696 if (S_ISREG (statbuf.st_mode))
697 temp = cf_read_file (path_ptr, depth);
698 else if (S_ISDIR (statbuf.st_mode))
699 temp = cf_read_dir (path_ptr, depth);
700 else
701 {
702 ERROR ("configfile: %s is neither a file nor a "
703 "directory.", path);
704 continue;
705 }
707 if (temp == NULL) {
708 oconfig_free (root);
709 return (NULL);
710 }
712 cf_ci_append_children (root, temp);
713 sfree (temp->children);
714 sfree (temp);
715 }
717 wordfree (&we);
719 return (root);
720 } /* oconfig_item_t *cf_read_generic */
721 /* #endif HAVE_WORDEXP_H */
723 #else /* if !HAVE_WORDEXP_H */
724 static oconfig_item_t *cf_read_generic (const char *path, int depth)
725 {
726 struct stat statbuf;
727 int status;
729 if (depth >= CF_MAX_DEPTH)
730 {
731 ERROR ("configfile: Not including `%s' because the maximum "
732 "nesting depth has been reached.", path);
733 return (NULL);
734 }
736 status = stat (path, &statbuf);
737 if (status != 0)
738 {
739 char errbuf[1024];
740 ERROR ("configfile: stat (%s) failed: %s",
741 path,
742 sstrerror (errno, errbuf, sizeof (errbuf)));
743 return (NULL);
744 }
746 if (S_ISREG (statbuf.st_mode))
747 return (cf_read_file (path, depth));
748 else if (S_ISDIR (statbuf.st_mode))
749 return (cf_read_dir (path, depth));
751 ERROR ("configfile: %s is neither a file nor a directory.", path);
752 return (NULL);
753 } /* oconfig_item_t *cf_read_generic */
754 #endif /* !HAVE_WORDEXP_H */
756 /*
757 * Public functions
758 */
759 int global_option_set (const char *option, const char *value)
760 {
761 int i;
763 DEBUG ("option = %s; value = %s;", option, value);
765 for (i = 0; i < cf_global_options_num; i++)
766 if (strcasecmp (cf_global_options[i].key, option) == 0)
767 break;
769 if (i >= cf_global_options_num)
770 return (-1);
772 sfree (cf_global_options[i].value);
774 if (value != NULL)
775 cf_global_options[i].value = strdup (value);
776 else
777 cf_global_options[i].value = NULL;
779 return (0);
780 }
782 const char *global_option_get (const char *option)
783 {
784 int i;
786 for (i = 0; i < cf_global_options_num; i++)
787 if (strcasecmp (cf_global_options[i].key, option) == 0)
788 break;
790 if (i >= cf_global_options_num)
791 return (NULL);
793 return ((cf_global_options[i].value != NULL)
794 ? cf_global_options[i].value
795 : cf_global_options[i].def);
796 } /* char *global_option_get */
798 void cf_unregister (const char *type)
799 {
800 cf_callback_t *this, *prev;
802 for (prev = NULL, this = first_callback;
803 this != NULL;
804 prev = this, this = this->next)
805 if (strcasecmp (this->type, type) == 0)
806 {
807 if (prev == NULL)
808 first_callback = this->next;
809 else
810 prev->next = this->next;
812 free (this);
813 break;
814 }
815 } /* void cf_unregister */
817 void cf_unregister_complex (const char *type)
818 {
819 cf_complex_callback_t *this, *prev;
821 for (prev = NULL, this = complex_callback_head;
822 this != NULL;
823 prev = this, this = this->next)
824 if (strcasecmp (this->type, type) == 0)
825 {
826 if (prev == NULL)
827 complex_callback_head = this->next;
828 else
829 prev->next = this->next;
831 sfree (this->type);
832 sfree (this);
833 break;
834 }
835 } /* void cf_unregister */
837 void cf_register (const char *type,
838 int (*callback) (const char *, const char *),
839 const char **keys, int keys_num)
840 {
841 cf_callback_t *cf_cb;
843 /* Remove this module from the list, if it already exists */
844 cf_unregister (type);
846 /* This pointer will be free'd in `cf_unregister' */
847 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
848 return;
850 cf_cb->type = type;
851 cf_cb->callback = callback;
852 cf_cb->keys = keys;
853 cf_cb->keys_num = keys_num;
855 cf_cb->next = first_callback;
856 first_callback = cf_cb;
857 } /* void cf_register */
859 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
860 {
861 cf_complex_callback_t *new;
863 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
864 if (new == NULL)
865 return (-1);
867 new->type = strdup (type);
868 if (new->type == NULL)
869 {
870 sfree (new);
871 return (-1);
872 }
874 new->callback = callback;
875 new->next = NULL;
877 if (complex_callback_head == NULL)
878 {
879 complex_callback_head = new;
880 }
881 else
882 {
883 cf_complex_callback_t *last = complex_callback_head;
884 while (last->next != NULL)
885 last = last->next;
886 last->next = new;
887 }
889 return (0);
890 } /* int cf_register_complex */
892 int cf_read (char *filename)
893 {
894 oconfig_item_t *conf;
895 int i;
897 conf = cf_read_generic (filename, 0 /* depth */);
898 if (conf == NULL)
899 {
900 ERROR ("Unable to read config file %s.", filename);
901 return (-1);
902 }
904 for (i = 0; i < conf->children_num; i++)
905 {
906 if (conf->children[i].children == NULL)
907 dispatch_value (conf->children + i);
908 else
909 dispatch_block (conf->children + i);
910 }
912 oconfig_free (conf);
914 /* Read the default types.db if no `TypesDB' option was given. */
915 if (cf_default_typesdb)
916 read_types_list (PKGDATADIR"/types.db");
918 return (0);
919 } /* int cf_read */
921 /* Assures the config option is a string, duplicates it and returns the copy in
922 * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
923 * success. */
924 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
925 {
926 char *string;
928 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
929 {
930 ERROR ("cf_util_get_string: The %s plugin requires "
931 "exactly one string argument.", ci->key);
932 return (-1);
933 }
935 string = strdup (ci->values[0].value.string);
936 if (string == NULL)
937 return (-1);
939 if (*ret_string != NULL)
940 sfree (*ret_string);
941 *ret_string = string;
943 return (0);
944 } /* }}} int cf_util_get_string */
946 /* Assures that the config option is a string. The string is then converted to
947 * a port number using `service_name_to_port_number' and returned. Returns the
948 * port number in the range [1-65535] or less than zero upon failure. */
949 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
950 {
951 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
952 {
953 ERROR ("cf_util_get_port_number: The %s plugin requires "
954 "exactly one string argument.", ci->key);
955 return (-1);
956 }
958 return (service_name_to_port_number (ci->values[0].value.string));
959 } /* }}} int cf_util_get_port_number */