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