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