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