Changed `plugin_set_dir' to take a `const' argument.
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005  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 "libconfig/libconfig.h"
26
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_debug.h"
30
31 #define SHORTOPT_NONE 0
32
33 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
34 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
35 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
36 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
37
38 #ifdef HAVE_LIBRRD
39 extern int operating_mode;
40 #else
41 static int operating_mode = MODE_CLIENT;
42 #endif
43
44 typedef struct cf_callback
45 {
46         char  *type;
47         int  (*callback) (char *, char *);
48         char **keys;
49         int    keys_num;
50         struct cf_callback *next;
51 } cf_callback_t;
52
53 static cf_callback_t *first_callback = NULL;
54
55 typedef struct cf_mode_item
56 {
57         char *key;
58         char *value;
59         int   mode;
60 } cf_mode_item_t;
61
62 static cf_mode_item_t cf_mode_list[] =
63 {
64         {"Server",      NULL, MODE_CLIENT                           },
65         {"Port",        NULL, MODE_CLIENT | MODE_SERVER             },
66         {"PIDFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
67         {"DataDir",     NULL, MODE_SERVER |               MODE_LOCAL}
68 };
69 static int cf_mode_num = 4;
70
71 static int nesting_depth = 0;
72 static char *current_module = NULL;
73
74 /* `cf_register' needs this prototype */
75 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
76                 const char *, lc_flags_t, void *);
77
78 /*
79  * Functions to handle register/unregister, search, and other plugin related
80  * stuff
81  */
82 cf_callback_t *cf_search (char *type)
83 {
84         cf_callback_t *cf_cb;
85
86         if (type == NULL)
87                 return (NULL);
88
89         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
90                 if (strcasecmp (cf_cb->type, type) == 0)
91                         break;
92
93         return (cf_cb);
94 }
95
96 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
97 {
98         cf_callback_t *cf_cb;
99         char *key;
100         char *value;
101         int ret;
102         int i;
103
104         if ((cf_cb = cf_search (type)) == NULL)
105         {
106                 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
107                 return (-1);
108         }
109
110         if ((key = strdup (orig_key)) == NULL)
111                 return (1);
112         if ((value = strdup (orig_value)) == NULL)
113         {
114                 free (key);
115                 return (2);
116         }
117
118         ret = -1;
119
120         for (i = 0; i < cf_cb->keys_num; i++)
121         {
122                 if (strcasecmp (cf_cb->keys[i], key) == 0)
123                 {
124                         ret = (*cf_cb->callback) (key, value);
125                         break;
126                 }
127         }
128
129         if (i >= cf_cb->keys_num)
130                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
131
132         free (key);
133         free (value);
134
135         return (ret);
136 }
137
138 void cf_unregister (char *type)
139 {
140         cf_callback_t *this, *prev;
141
142         for (prev = NULL, this = first_callback;
143                         this != NULL;
144                         prev = this, this = this->next)
145                 if (strcasecmp (this->type, type) == 0)
146                 {
147                         if (prev == NULL)
148                                 first_callback = this->next;
149                         else
150                                 prev->next = this->next;
151
152                         free (this);
153                         break;
154                 }
155 }
156
157 void cf_register (char *type,
158                 int (*callback) (char *, char *),
159                 char **keys, int keys_num)
160 {
161         cf_callback_t *cf_cb;
162         char buf[64];
163         int i;
164
165         /* Remove this module from the list, if it already exists */
166         cf_unregister (type);
167
168         /* This pointer will be free'd in `cf_unregister' */
169         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
170                 return;
171
172         cf_cb->type     = type;
173         cf_cb->callback = callback;
174         cf_cb->keys     = keys;
175         cf_cb->keys_num = keys_num;
176
177         cf_cb->next = first_callback;
178         first_callback = cf_cb;
179
180         for (i = 0; i < keys_num; i++)
181         {
182                 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
183                 {
184                         /* This may be called multiple times for the same
185                          * `key', but apparently `lc_register_*' can handle
186                          * it.. */
187                         lc_register_callback (buf, SHORTOPT_NONE,
188                                         LC_VAR_STRING, cf_callback_plugin_dispatch,
189                                         NULL);
190                 }
191                 else
192                 {
193                         DBG ("Key was truncated: `%s'", keys[i]);
194                 }
195         }
196 }
197
198 /*
199  * Other query functions
200  */
201 char *cf_get_mode_option (const char *key)
202 {
203         int i;
204
205         for (i = 0; i < cf_mode_num; i++)
206         {
207                 if ((cf_mode_list[i].mode & operating_mode) == 0)
208                         continue;
209
210                 if (strcasecmp (cf_mode_list[i].key, key) == 0)
211                         return (cf_mode_list[i].value);
212         }
213
214         return (NULL);
215 }
216
217 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
218  * Functions for the actual parsing                                    *
219  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
220
221 /*
222  * `cf_callback_mode'
223  *   Start/end the `mode' section
224  *
225  * <Mode `arguments'>
226  *   ...
227  * </Mode>
228  */
229 int cf_callback_mode (const char *shortvar, const char *var,
230                 const char *arguments, const char *value, lc_flags_t flags,
231                 void *extra)
232 {
233         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
234                         shortvar, var, arguments, value);
235
236         if (flags == LC_FLAGS_SECTIONSTART)
237         {
238                 if (nesting_depth != 0)
239                 {
240                         fprintf (stderr, ERR_NOT_NESTED);
241                         return (LC_CBRET_ERROR);
242                 }
243
244                 if (arguments == NULL)
245                 {
246                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
247                         return (LC_CBRET_ERROR);
248                 }
249
250                 nesting_depth++;
251
252                 if (((operating_mode == MODE_CLIENT)
253                                         && (strcasecmp (arguments, "Client") == 0))
254                                 || ((operating_mode == MODE_SERVER)
255                                         && (strcasecmp (arguments, "Server") == 0))
256                                 || ((operating_mode == MODE_LOCAL)
257                                         && (strcasecmp (arguments, "Local") == 0)))
258                 {
259                         return (LC_CBRET_OKAY);
260                 }
261                 else
262                 {
263                         return (LC_CBRET_IGNORESECTION);
264                 }
265         }
266         else if (flags == LC_FLAGS_SECTIONEND)
267         {
268                 nesting_depth--;
269
270                 return (LC_CBRET_OKAY);
271         }
272         else
273         {
274                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
275                 return (LC_CBRET_ERROR);
276         }
277
278 }
279
280 /*
281  * `cf_callback_mode_plugindir'
282  *   Change the plugin directory
283  *
284  * <Mode xxx>
285  *   PluginDir `value'
286  * </Mode>
287  */
288 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
289                 const char *arguments, const char *value, lc_flags_t flags,
290                 void *extra)
291 {
292         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
293                         shortvar, var, arguments, value);
294
295         plugin_set_dir (value);
296
297         return (LC_CBRET_OKAY);
298 }
299
300 int cf_callback_mode_option (const char *shortvar, const char *var,
301                 const char *arguments, const char *value, lc_flags_t flags,
302                 void *extra)
303 {
304         cf_mode_item_t *item;
305
306         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
307                         shortvar, var, arguments, value);
308
309         if (extra == NULL)
310         {
311                 fprintf (stderr, "No extra..?\n");
312                 return (LC_CBRET_ERROR);
313         }
314
315         item = (cf_mode_item_t *) extra;
316
317         if (strcasecmp (item->key, shortvar))
318         {
319                 fprintf (stderr, "Wrong extra..\n");
320                 return (LC_CBRET_ERROR);
321         }
322
323         if ((operating_mode & item->mode) == 0)
324         {
325                 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
326                 return (LC_CBRET_ERROR);
327         }
328
329         if (item->value != NULL)
330         {
331                 free (item->value);
332                 item->value = NULL;
333         }
334
335         if ((item->value = strdup (value)) == NULL)
336         {
337                 perror ("strdup");
338                 return (LC_CBRET_ERROR);
339         }
340
341         return (LC_CBRET_OKAY);
342 }
343
344 /*
345  * `cf_callback_mode_loadmodule':
346  *   Load a plugin.
347  *
348  * <Mode xxx>
349  *   LoadPlugin `value'
350  * </Mode>
351  */
352 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
353                 const char *arguments, const char *value, lc_flags_t flags,
354                 void *extra)
355 {
356         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
357                         shortvar, var, arguments, value);
358
359         if (nesting_depth == 0)
360         {
361                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
362                 return (LC_CBRET_ERROR);
363         }
364
365         if (plugin_load (value))
366                 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
367
368         /* Return `okay' even if there was an error, because it's not a syntax
369          * problem.. */
370         return (LC_CBRET_OKAY);
371 }
372
373 /* XXX think about how to do the command line stuff */
374 int cf_callback_mode_switch (const char *shortvar, const char *var,
375                 const char *arguments, const char *value, lc_flags_t flags,
376                 void *extra)
377 {
378         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
379                         shortvar, var, arguments, value);
380
381         if (strcasecmp (shortvar, "Client") == 0)
382                 operating_mode = MODE_CLIENT;
383         else if (strcasecmp (shortvar, "Local") == 0)
384                 operating_mode = MODE_LOCAL;
385         else if (strcasecmp (shortvar, "Server") == 0)
386                 operating_mode = MODE_SERVER;
387         else
388         {
389                 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
390                 return (LC_CBRET_ERROR);
391         }
392
393         return (LC_CBRET_OKAY);
394 }
395
396 /*
397  * `cf_callback_plugin'
398  *   Start/end section `plugin'
399  *
400  * <Plugin `arguments'>
401  *   ...
402  * </Plugin>
403  */
404 int cf_callback_plugin (const char *shortvar, const char *var,
405                 const char *arguments, const char *value, lc_flags_t flags,
406                 void *extra)
407 {
408         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
409                         shortvar, var, arguments, value);
410
411         if (flags == LC_FLAGS_SECTIONSTART)
412         {
413                 if (nesting_depth != 0)
414                 {
415                         fprintf (stderr, ERR_NOT_NESTED);
416                         return (LC_CBRET_ERROR);
417                 }
418
419                 if (arguments == NULL)
420                 {
421                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
422                         return (LC_CBRET_ERROR);
423                 }
424
425                 if ((current_module = strdup (arguments)) == NULL)
426                 {
427                         perror ("strdup");
428                         return (LC_CBRET_ERROR);
429                 }
430
431                 nesting_depth++;
432
433                 if (cf_search (current_module) != NULL)
434                         return (LC_CBRET_OKAY);
435                 else
436                         return (LC_CBRET_IGNORESECTION);
437         }
438         else if (flags == LC_FLAGS_SECTIONEND)
439         {
440                 if (current_module != NULL)
441                 {
442                         free (current_module);
443                         current_module = NULL;
444                 }
445
446                 nesting_depth--;
447
448                 return (LC_CBRET_OKAY);
449         }
450         else
451         {
452                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
453                 return (LC_CBRET_ERROR);
454         }
455 }
456
457 /*
458  * `cf_callback_plugin_dispatch'
459  *   Send options within `plugin' sections to the plugin that requests it.
460  *
461  * <Plugin `current_module'>
462  *   `var' `value'
463  * </Plugin>
464  */
465 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
466                 const char *arguments, const char *value, lc_flags_t flags,
467                 void *extra)
468 {
469         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
470                         shortvar, var, arguments, value);
471
472         if ((nesting_depth == 0) || (current_module == NULL))
473         {
474                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
475                 return (LC_CBRET_ERROR);
476         }
477
478         /* Send the data to the plugin */
479         if (cf_dispatch (current_module, shortvar, value) < 0)
480                 return (LC_CBRET_ERROR);
481
482         return (LC_CBRET_OKAY);
483 }
484
485 int cf_read (char *filename)
486 {
487         int i;
488
489         if (filename == NULL)
490                 filename = CONFIGFILE;
491
492         lc_register_callback ("Client", 'c', LC_VAR_NONE,
493                         cf_callback_mode_switch, NULL);
494         lc_register_callback ("Local", 'l', LC_VAR_NONE,
495                         cf_callback_mode_switch, NULL);
496         lc_register_callback ("Server", 's', LC_VAR_NONE,
497                         cf_callback_mode_switch, NULL);
498
499         lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
500                         cf_callback_mode, NULL);
501         lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
502                         cf_callback_plugin, NULL);
503
504         lc_register_callback ("Mode.PluginDir", 'P',
505                         LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
506         lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
507                         LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
508
509         for (i = 0; i < cf_mode_num; i++)
510         {
511                 char            longvar[256];
512                 cf_mode_item_t *item;
513
514                 item = &cf_mode_list[i];
515
516                 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
517                         continue;
518
519                 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
520                                 cf_callback_mode_option, (void *) item);
521         }
522
523         if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
524         {
525                 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
526                 return (-1);
527         }
528
529         /* free memory and stuff */
530         lc_cleanup ();
531
532         return (0);
533 }