Code

Fixed a but in the mysql module, so it builds as write-only module, too.
[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  **/
23 /* TODO
24  * make internal-only functions `static' */
26 #include "collectd.h"
28 #include "libconfig/libconfig.h"
30 #include "plugin.h"
31 #include "configfile.h"
32 #include "utils_debug.h"
34 #define SHORTOPT_NONE 0
36 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
37 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
38 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
39 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
41 #ifdef HAVE_LIBRRD
42 extern int operating_mode;
43 #else
44 static int operating_mode = MODE_CLIENT;
45 #endif
47 typedef struct cf_callback
48 {
49         char  *type;
50         int  (*callback) (char *, char *);
51         char **keys;
52         int    keys_num;
53         struct cf_callback *next;
54 } cf_callback_t;
56 static cf_callback_t *first_callback = NULL;
58 typedef struct cf_mode_item
59 {
60         char *key;
61         char *value;
62         int   mode;
63 } cf_mode_item_t;
65 /* TODO
66  * - LogFile
67  */
68 static cf_mode_item_t cf_mode_list[] =
69 {
70         {"Server",      NULL,             MODE_CLIENT                           },
71         {"Port",        NULL,             MODE_CLIENT | MODE_SERVER             },
72         {"PIDFile",     PIDFILE,          MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
73         {"DataDir",     PKGLOCALSTATEDIR, MODE_SERVER |               MODE_LOCAL},
74         {"LogFile",     LOGFILE,          MODE_SERVER | MODE_SERVER | MODE_LOCAL},
75 };
76 static int cf_mode_num = 4;
78 static int nesting_depth = 0;
79 static char *current_module = NULL;
81 /* `cf_register' needs this prototype */
82 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
83                 const char *, lc_flags_t, void *);
85 /*
86  * Functions to handle register/unregister, search, and other plugin related
87  * stuff
88  */
89 cf_callback_t *cf_search (char *type)
90 {
91         cf_callback_t *cf_cb;
93         if (type == NULL)
94                 return (NULL);
96         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
97                 if (strcasecmp (cf_cb->type, type) == 0)
98                         break;
100         return (cf_cb);
103 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
105         cf_callback_t *cf_cb;
106         char *key;
107         char *value;
108         int ret;
109         int i;
111         DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
113         if ((cf_cb = cf_search (type)) == NULL)
114         {
115                 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
116                 return (-1);
117         }
119         if ((key = strdup (orig_key)) == NULL)
120                 return (1);
121         if ((value = strdup (orig_value)) == NULL)
122         {
123                 free (key);
124                 return (2);
125         }
127         ret = -1;
129         for (i = 0; i < cf_cb->keys_num; i++)
130         {
131                 if (strcasecmp (cf_cb->keys[i], key) == 0)
132                 {
133                         ret = (*cf_cb->callback) (key, value);
134                         break;
135                 }
136         }
138         if (i >= cf_cb->keys_num)
139                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
141         free (key);
142         free (value);
144         return (ret);
147 void cf_unregister (char *type)
149         cf_callback_t *this, *prev;
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;
161                         free (this);
162                         break;
163                 }
166 void cf_register (char *type,
167                 int (*callback) (char *, char *),
168                 char **keys, int keys_num)
170         cf_callback_t *cf_cb;
171         char buf[64];
172         int i;
174         /* Remove this module from the list, if it already exists */
175         cf_unregister (type);
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;
181         cf_cb->type     = type;
182         cf_cb->callback = callback;
183         cf_cb->keys     = keys;
184         cf_cb->keys_num = keys_num;
186         cf_cb->next = first_callback;
187         first_callback = cf_cb;
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         }
207 /*
208  * Other query functions
209  */
210 char *cf_get_mode_option (const char *key)
212         int i;
214         for (i = 0; i < cf_mode_num; i++)
215         {
216                 if ((cf_mode_list[i].mode & operating_mode) == 0)
217                         continue;
219                 if (strcasecmp (cf_mode_list[i].key, key) == 0)
220                         return (cf_mode_list[i].value);
221         }
223         return (NULL);
226 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
227  * Functions for the actual parsing                                    *
228  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
230 /*
231  * `cf_callback_mode'
232  *   Chose the `operating_mode'
233  *
234  * Mode `value'
235  */
236 int cf_callback_mode (const char *shortvar, const char *var,
237                 const char *arguments, const char *value, lc_flags_t flags,
238                 void *extra)
240         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
241                         shortvar, var, arguments, value);
243         if (strcasecmp (value, "Client") == 0)
244                 operating_mode = MODE_CLIENT;
245         else if (strcasecmp (value, "Server") == 0)
246                 operating_mode = MODE_SERVER;
247         else if (strcasecmp (value, "Local") == 0)
248                 operating_mode = MODE_LOCAL;
249         else
250         {
251                 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
252                 return (LC_CBRET_ERROR);
253         }
255         return (LC_CBRET_OKAY);
258 /*
259  * `cf_callback_mode_plugindir'
260  *   Change the plugin directory
261  *
262  * <Mode xxx>
263  *   PluginDir `value'
264  * </Mode>
265  */
266 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
267                 const char *arguments, const char *value, lc_flags_t flags,
268                 void *extra)
270         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
271                         shortvar, var, arguments, value);
273         plugin_set_dir (value);
275         return (LC_CBRET_OKAY);
278 int cf_callback_mode_option (const char *shortvar, const char *var,
279                 const char *arguments, const char *value, lc_flags_t flags,
280                 void *extra)
282         cf_mode_item_t *item;
284         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
285                         shortvar, var, arguments, value);
287         if (extra == NULL)
288         {
289                 fprintf (stderr, "No extra..?\n");
290                 return (LC_CBRET_ERROR);
291         }
293         item = (cf_mode_item_t *) extra;
295         if (strcasecmp (item->key, shortvar))
296         {
297                 fprintf (stderr, "Wrong extra..\n");
298                 return (LC_CBRET_ERROR);
299         }
301         if ((operating_mode & item->mode) == 0)
302         {
303                 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
304                 return (LC_CBRET_ERROR);
305         }
307         if (item->value != NULL)
308         {
309                 free (item->value);
310                 item->value = NULL;
311         }
313         if ((item->value = strdup (value)) == NULL)
314         {
315                 perror ("strdup");
316                 return (LC_CBRET_ERROR);
317         }
319         return (LC_CBRET_OKAY);
322 /*
323  * `cf_callback_mode_loadmodule':
324  *   Load a plugin.
325  *
326  * <Mode xxx>
327  *   LoadPlugin `value'
328  * </Mode>
329  */
330 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
331                 const char *arguments, const char *value, lc_flags_t flags,
332                 void *extra)
334         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
335                         shortvar, var, arguments, value);
337         if (plugin_load (value))
338                 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
340         /* Return `okay' even if there was an error, because it's not a syntax
341          * problem.. */
342         return (LC_CBRET_OKAY);
345 /*
346  * `cf_callback_mode_switch'
347  *   Change the contents of the global variable `operating_mode'
348  *
349  *   This should be command line options. One *can* do this in the config
350  *   files, but I will not document this. Don't whine abount it not working as
351  *   you expect if you do it anyways.
352  */
353 int cf_callback_mode_switch (const char *shortvar, const char *var,
354                 const char *arguments, const char *value, lc_flags_t flags,
355                 void *extra)
357         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
358                         shortvar, var, arguments, value);
360         if (strcasecmp (shortvar, "Client") == 0)
361                 operating_mode = MODE_CLIENT;
362         else if (strcasecmp (shortvar, "Local") == 0)
363                 operating_mode = MODE_LOCAL;
364         else if (strcasecmp (shortvar, "Server") == 0)
365                 operating_mode = MODE_SERVER;
366         else
367         {
368                 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
369                 return (LC_CBRET_ERROR);
370         }
372         return (LC_CBRET_OKAY);
375 /*
376  * `cf_callback_plugin'
377  *   Start/end section `plugin'
378  *
379  * <Plugin `arguments'>
380  *   ...
381  * </Plugin>
382  */
383 int cf_callback_plugin (const char *shortvar, const char *var,
384                 const char *arguments, const char *value, lc_flags_t flags,
385                 void *extra)
387         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
388                         shortvar, var, arguments, value);
390         if (flags == LC_FLAGS_SECTIONSTART)
391         {
392                 if (nesting_depth != 0)
393                 {
394                         fprintf (stderr, ERR_NOT_NESTED);
395                         return (LC_CBRET_ERROR);
396                 }
398                 if (arguments == NULL)
399                 {
400                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
401                         return (LC_CBRET_ERROR);
402                 }
404                 if ((current_module = strdup (arguments)) == NULL)
405                 {
406                         perror ("strdup");
407                         return (LC_CBRET_ERROR);
408                 }
410                 nesting_depth++;
412                 if (cf_search (current_module) != NULL)
413                         return (LC_CBRET_OKAY);
414                 else
415                         return (LC_CBRET_IGNORESECTION);
416         }
417         else if (flags == LC_FLAGS_SECTIONEND)
418         {
419                 if (current_module != NULL)
420                 {
421                         free (current_module);
422                         current_module = NULL;
423                 }
425                 nesting_depth--;
427                 return (LC_CBRET_OKAY);
428         }
429         else
430         {
431                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
432                 return (LC_CBRET_ERROR);
433         }
436 /*
437  * `cf_callback_plugin_dispatch'
438  *   Send options within `plugin' sections to the plugin that requests it.
439  *
440  * <Plugin `current_module'>
441  *   `var' `value'
442  * </Plugin>
443  */
444 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
445                 const char *arguments, const char *value, lc_flags_t flags,
446                 void *extra)
448         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
449                         shortvar, var, arguments, value);
451         if ((nesting_depth == 0) || (current_module == NULL))
452         {
453                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
454                 return (LC_CBRET_ERROR);
455         }
457         /* Send the data to the plugin */
458         if (cf_dispatch (current_module, shortvar, value) < 0)
459                 return (LC_CBRET_ERROR);
461         return (LC_CBRET_OKAY);
464 void cf_init (void)
466         static int run_once = 0;
467         int i;
469         if (run_once != 0)
470                 return;
471         run_once = 1;
473         lc_register_callback ("Client", SHORTOPT_NONE, LC_VAR_NONE,
474                         cf_callback_mode_switch, NULL);
475         lc_register_callback ("Local", SHORTOPT_NONE, LC_VAR_NONE,
476                         cf_callback_mode_switch, NULL);
477         lc_register_callback ("Server", SHORTOPT_NONE, LC_VAR_NONE,
478                         cf_callback_mode_switch, NULL);
480         lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
481                         cf_callback_mode, NULL);
482         lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
483                         cf_callback_plugin, NULL);
485         lc_register_callback ("PluginDir", SHORTOPT_NONE,
486                         LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
487         lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
488                         LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
490         for (i = 0; i < cf_mode_num; i++)
491         {
492                 char            longvar[256];
493                 cf_mode_item_t *item;
495                 item = &cf_mode_list[i];
497                 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
498                         continue;
500                 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
501                                 cf_callback_mode_option, (void *) item);
502         }
505 int cf_read (char *filename)
507         cf_init ();
509         if (filename == NULL)
510                 filename = CONFIGFILE;
512         DBG ("Starting to parse file `%s'", filename);
514         /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
515         if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
516         {
517                 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
518                 return (-1);
519         }
521         DBG ("Done parsing file `%s'", filename);
523         /* free memory and stuff */
524         lc_cleanup ();
526         return (0);