1 /**
2 * collectd - src/configfile.c
3 * Copyright (C) 2005,2006 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 **/
23 #include "collectd.h"
25 #include "liboconfig/oconfig.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
33 /*
34 * Private types
35 */
36 typedef struct cf_callback
37 {
38 const char *type;
39 int (*callback) (const char *, const char *);
40 const char **keys;
41 int keys_num;
42 struct cf_callback *next;
43 } cf_callback_t;
45 typedef struct cf_complex_callback_s
46 {
47 char *type;
48 int (*callback) (oconfig_item_t *);
49 struct cf_complex_callback_s *next;
50 } cf_complex_callback_t;
52 typedef struct cf_value_map_s
53 {
54 char *key;
55 int (*func) (const oconfig_item_t *);
56 } cf_value_map_t;
58 typedef struct cf_global_option_s
59 {
60 char *key;
61 char *value;
62 char *def;
63 } cf_global_option_t;
65 /*
66 * Prototypes of callback functions
67 */
68 static int dispatch_value_plugindir (const oconfig_item_t *ci);
69 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
71 /*
72 * Private variables
73 */
74 static cf_callback_t *first_callback = NULL;
75 static cf_complex_callback_t *complex_callback_head = NULL;
77 static cf_value_map_t cf_value_map[] =
78 {
79 {"PluginDir", dispatch_value_plugindir},
80 {"LoadPlugin", dispatch_value_loadplugin}
81 };
82 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
84 static cf_global_option_t cf_global_options[] =
85 {
86 {"BaseDir", NULL, PKGLOCALSTATEDIR},
87 {"PIDFile", NULL, PIDFILE},
88 {"Hostname", NULL, NULL},
89 {"FQDNLookup", NULL, "false"},
90 {"Interval", NULL, "10"},
91 {"ReadThreads", NULL, "5"},
92 {"TypesDB", NULL, PLUGINDIR"/types.db"} /* FIXME: Configure path */
93 };
94 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
96 /*
97 * Functions to handle register/unregister, search, and other plugin related
98 * stuff
99 */
100 static cf_callback_t *cf_search (const char *type)
101 {
102 cf_callback_t *cf_cb;
104 if (type == NULL)
105 return (NULL);
107 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
108 if (strcasecmp (cf_cb->type, type) == 0)
109 break;
111 return (cf_cb);
112 }
114 static int cf_dispatch (const char *type, const char *orig_key,
115 const char *orig_value)
116 {
117 cf_callback_t *cf_cb;
118 char *key;
119 char *value;
120 int ret;
121 int i;
123 DEBUG ("type = %s, key = %s, value = %s",
124 ESCAPE_NULL(type),
125 ESCAPE_NULL(orig_key),
126 ESCAPE_NULL(orig_value));
128 if ((cf_cb = cf_search (type)) == NULL)
129 {
130 WARNING ("Plugin `%s' did not register a callback.", type);
131 return (-1);
132 }
134 if ((key = strdup (orig_key)) == NULL)
135 return (1);
136 if ((value = strdup (orig_value)) == NULL)
137 {
138 free (key);
139 return (2);
140 }
142 ret = -1;
144 for (i = 0; i < cf_cb->keys_num; i++)
145 {
146 if (strcasecmp (cf_cb->keys[i], key) == 0)
147 {
148 ret = (*cf_cb->callback) (key, value);
149 break;
150 }
151 }
153 if (i >= cf_cb->keys_num)
154 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
156 free (key);
157 free (value);
159 DEBUG ("return (%i)", ret);
161 return (ret);
162 } /* int cf_dispatch */
164 static int dispatch_global_option (const oconfig_item_t *ci)
165 {
166 if (ci->values_num != 1)
167 return (-1);
168 if (ci->values[0].type == OCONFIG_TYPE_STRING)
169 return (global_option_set (ci->key, ci->values[0].value.string));
170 else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
171 {
172 char tmp[128];
173 snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
174 tmp[127] = '\0';
175 return (global_option_set (ci->key, tmp));
176 }
177 else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
178 {
179 if (ci->values[0].value.boolean)
180 return (global_option_set (ci->key, "true"));
181 else
182 return (global_option_set (ci->key, "false"));
183 }
185 return (-1);
186 } /* int dispatch_global_option */
188 static int dispatch_value_plugindir (const oconfig_item_t *ci)
189 {
190 assert (strcasecmp (ci->key, "PluginDir") == 0);
192 if (ci->values_num != 1)
193 return (-1);
194 if (ci->values[0].type != OCONFIG_TYPE_STRING)
195 return (-1);
197 plugin_set_dir (ci->values[0].value.string);
198 return (0);
199 }
201 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
202 {
203 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
205 if (ci->values_num != 1)
206 return (-1);
207 if (ci->values[0].type != OCONFIG_TYPE_STRING)
208 return (-1);
210 return (plugin_load (ci->values[0].value.string));
211 } /* int dispatch_value_loadplugin */
213 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
214 {
215 char buffer[4096];
216 char *buffer_ptr;
217 int buffer_free;
218 int i;
220 buffer_ptr = buffer;
221 buffer_free = sizeof (buffer);
223 for (i = 0; i < ci->values_num; i++)
224 {
225 int status = -1;
227 if (ci->values[i].type == OCONFIG_TYPE_STRING)
228 status = snprintf (buffer_ptr, buffer_free, " %s",
229 ci->values[i].value.string);
230 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
231 status = snprintf (buffer_ptr, buffer_free, " %lf",
232 ci->values[i].value.number);
233 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
234 status = snprintf (buffer_ptr, buffer_free, " %s",
235 ci->values[i].value.boolean
236 ? "true" : "false");
238 if ((status < 0) || (status >= buffer_free))
239 return (-1);
240 buffer_free -= status;
241 buffer_ptr += status;
242 }
243 /* skip the initial space */
244 buffer_ptr = buffer + 1;
246 return (cf_dispatch (plugin, ci->key, buffer_ptr));
247 } /* int plugin_conf_dispatch */
249 static int dispatch_value (const oconfig_item_t *ci)
250 {
251 int ret = -2;
252 int i;
254 for (i = 0; i < cf_value_map_num; i++)
255 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
256 {
257 ret = cf_value_map[i].func (ci);
258 break;
259 }
261 for (i = 0; i < cf_global_options_num; i++)
262 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
263 {
264 ret = dispatch_global_option (ci);
265 break;
266 }
268 return (ret);
269 } /* int dispatch_value */
271 static int dispatch_block_plugin (oconfig_item_t *ci)
272 {
273 int i;
274 char *name;
276 cf_complex_callback_t *cb;
278 if (strcasecmp (ci->key, "Plugin") != 0)
279 return (-1);
280 if (ci->values_num < 1)
281 return (-1);
282 if (ci->values[0].type != OCONFIG_TYPE_STRING)
283 return (-1);
285 name = ci->values[0].value.string;
287 /* Check for a complex callback first */
288 for (cb = complex_callback_head; cb != NULL; cb = cb->next)
289 if (strcasecmp (name, cb->type) == 0)
290 return (cb->callback (ci));
292 /* Hm, no complex plugin found. Dispatch the values one by one */
293 for (i = 0; i < ci->children_num; i++)
294 {
295 if (ci->children[i].children == NULL)
296 dispatch_value_plugin (name, ci->children + i);
297 else
298 {DEBUG ("No nested config blocks allow for this plugin.");}
299 }
301 return (0);
302 }
305 static int dispatch_block (oconfig_item_t *ci)
306 {
307 if (strcasecmp (ci->key, "Plugin") == 0)
308 return (dispatch_block_plugin (ci));
310 return (0);
311 }
313 #define CF_MAX_DEPTH 8
314 static oconfig_item_t *cf_read_file (const char *file, int depth);
316 static int cf_include_all (oconfig_item_t *root, int depth)
317 {
318 int i;
320 for (i = 0; i < root->children_num; i++)
321 {
322 oconfig_item_t *new;
323 oconfig_item_t *old;
325 /* Ignore all blocks, including `Include' blocks. */
326 if (root->children[i].children_num != 0)
327 continue;
329 if (strcasecmp (root->children[i].key, "Include") != 0)
330 continue;
332 old = root->children + i;
334 if ((old->values_num != 1)
335 || (old->values[0].type != OCONFIG_TYPE_STRING))
336 {
337 ERROR ("configfile: `Include' needs exactly one string argument.");
338 continue;
339 }
341 new = cf_read_file (old->values[0].value.string, depth + 1);
342 if (new == NULL)
343 continue;
345 /* There are more children now. We need to expand
346 * root->children. */
347 if (new->children_num > 1)
348 {
349 oconfig_item_t *temp;
351 DEBUG ("configfile: Resizing root-children from %i to %i elements.",
352 root->children_num,
353 root->children_num + new->children_num - 1);
355 temp = (oconfig_item_t *) realloc (root->children,
356 sizeof (oconfig_item_t)
357 * (root->children_num + new->children_num - 1));
358 if (temp == NULL)
359 {
360 ERROR ("configfile: realloc failed.");
361 oconfig_free (new);
362 continue;
363 }
364 root->children = temp;
365 }
367 /* Clean up the old include directive while we still have a
368 * valid pointer */
369 DEBUG ("configfile: Cleaning up `old'");
370 /* sfree (old->values[0].value.string); */
371 sfree (old->values);
373 /* If there are trailing children and the number of children
374 * changes, we need to move the trailing ones either one to the
375 * front or (new->num - 1) to the back */
376 if (((root->children_num - i) > 1)
377 && (new->children_num != 1))
378 {
379 DEBUG ("configfile: Moving trailing children.");
380 memmove (root->children + i + new->children_num,
381 root->children + i + 1,
382 sizeof (oconfig_item_t)
383 * (root->children_num - (i + 1)));
384 }
386 /* Now copy the new children to where the include statement was */
387 if (new->children_num > 0)
388 {
389 DEBUG ("configfile: Copying new children.");
390 memcpy (root->children + i,
391 new->children,
392 sizeof (oconfig_item_t)
393 * new->children_num);
394 }
396 /* Adjust the number of children and the position in the list. */
397 root->children_num = root->children_num + new->children_num - 1;
398 i = i + new->children_num - 1;
400 /* Clean up the `new' struct. We set `new->children' to NULL so
401 * the stuff we've just copied pointers to isn't freed by
402 * `oconfig_free' */
403 DEBUG ("configfile: Cleaning up `new'");
404 sfree (new->values); /* should be NULL anyway */
405 sfree (new);
406 new = NULL;
407 } /* for (i = 0; i < root->children_num; i++) */
409 return (0);
410 } /* int cf_include_all */
412 static oconfig_item_t *cf_read_file (const char *file, int depth)
413 {
414 oconfig_item_t *root;
416 if (depth >= CF_MAX_DEPTH)
417 {
418 ERROR ("configfile: Not including `%s' because the maximum nesting depth has been reached.",
419 file);
420 return (NULL);
421 }
423 root = oconfig_parse_file (file);
424 if (root == NULL)
425 {
426 ERROR ("configfile: Cannot read file `%s'.", file);
427 return (NULL);
428 }
430 cf_include_all (root, depth);
432 return (root);
433 } /* oconfig_item_t *cf_read_file */
435 /*
436 * Public functions
437 */
438 int global_option_set (const char *option, const char *value)
439 {
440 int i;
442 DEBUG ("option = %s; value = %s;", option, value);
444 for (i = 0; i < cf_global_options_num; i++)
445 if (strcasecmp (cf_global_options[i].key, option) == 0)
446 break;
448 if (i >= cf_global_options_num)
449 return (-1);
451 sfree (cf_global_options[i].value);
453 if (value != NULL)
454 cf_global_options[i].value = strdup (value);
455 else
456 cf_global_options[i].value = NULL;
458 return (0);
459 }
461 const char *global_option_get (const char *option)
462 {
463 int i;
465 for (i = 0; i < cf_global_options_num; i++)
466 if (strcasecmp (cf_global_options[i].key, option) == 0)
467 break;
469 if (i >= cf_global_options_num)
470 return (NULL);
472 return ((cf_global_options[i].value != NULL)
473 ? cf_global_options[i].value
474 : cf_global_options[i].def);
475 } /* char *global_option_get */
477 void cf_unregister (const char *type)
478 {
479 cf_callback_t *this, *prev;
481 for (prev = NULL, this = first_callback;
482 this != NULL;
483 prev = this, this = this->next)
484 if (strcasecmp (this->type, type) == 0)
485 {
486 if (prev == NULL)
487 first_callback = this->next;
488 else
489 prev->next = this->next;
491 free (this);
492 break;
493 }
494 } /* void cf_unregister */
496 void cf_unregister_complex (const char *type)
497 {
498 cf_complex_callback_t *this, *prev;
500 for (prev = NULL, this = complex_callback_head;
501 this != NULL;
502 prev = this, this = this->next)
503 if (strcasecmp (this->type, type) == 0)
504 {
505 if (prev == NULL)
506 complex_callback_head = this->next;
507 else
508 prev->next = this->next;
510 sfree (this->type);
511 sfree (this);
512 break;
513 }
514 } /* void cf_unregister */
516 void cf_register (const char *type,
517 int (*callback) (const char *, const char *),
518 const char **keys, int keys_num)
519 {
520 cf_callback_t *cf_cb;
522 /* Remove this module from the list, if it already exists */
523 cf_unregister (type);
525 /* This pointer will be free'd in `cf_unregister' */
526 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
527 return;
529 cf_cb->type = type;
530 cf_cb->callback = callback;
531 cf_cb->keys = keys;
532 cf_cb->keys_num = keys_num;
534 cf_cb->next = first_callback;
535 first_callback = cf_cb;
536 } /* void cf_register */
538 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
539 {
540 cf_complex_callback_t *new;
542 new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
543 if (new == NULL)
544 return (-1);
546 new->type = strdup (type);
547 if (new->type == NULL)
548 {
549 sfree (new);
550 return (-1);
551 }
553 new->callback = callback;
554 new->next = NULL;
556 if (complex_callback_head == NULL)
557 {
558 complex_callback_head = new;
559 }
560 else
561 {
562 cf_complex_callback_t *last = complex_callback_head;
563 while (last->next != NULL)
564 last = last->next;
565 last->next = new;
566 }
568 return (0);
569 } /* int cf_register_complex */
571 int cf_read (char *filename)
572 {
573 oconfig_item_t *conf;
574 int i;
576 conf = cf_read_file (filename, 0 /* depth */);
577 if (conf == NULL)
578 {
579 ERROR ("Unable to read config file %s.", filename);
580 return (-1);
581 }
583 for (i = 0; i < conf->children_num; i++)
584 {
585 if (conf->children[i].children == NULL)
586 dispatch_value (conf->children + i);
587 else
588 dispatch_block (conf->children + i);
589 }
591 return (0);
592 } /* int cf_read */