2 * collectd - src/configfile.c
3 * Copyright (C) 2005 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
25 #include "libconfig/libconfig.h"
28 #include "configfile.h"
29 #include "utils_debug.h"
31 #define SHORTOPT_NONE 0
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"
39 extern int operating_mode;
41 static int operating_mode = MODE_CLIENT;
44 typedef struct cf_callback
47 int (*callback) (char *, char *);
50 struct cf_callback *next;
53 static cf_callback_t *first_callback = NULL;
55 typedef struct cf_mode_item
62 static cf_mode_item_t cf_mode_list[] =
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}
69 static int cf_mode_num = 4;
71 static int nesting_depth = 0;
72 static char *current_module = NULL;
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 *);
79 * Functions to handle register/unregister, search, and other plugin related
82 cf_callback_t *cf_search (char *type)
89 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
90 if (strcasecmp (cf_cb->type, type) == 0)
96 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
104 if ((cf_cb = cf_search (type)) == NULL)
106 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
110 if ((key = strdup (orig_key)) == NULL)
112 if ((value = strdup (orig_value)) == NULL)
120 for (i = 0; i < cf_cb->keys_num; i++)
122 if (strcasecmp (cf_cb->keys[i], key) == 0)
124 ret = (*cf_cb->callback) (key, value);
129 if (i >= cf_cb->keys_num)
130 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
138 void cf_unregister (char *type)
140 cf_callback_t *this, *prev;
142 for (prev = NULL, this = first_callback;
144 prev = this, this = this->next)
145 if (strcasecmp (this->type, type) == 0)
148 first_callback = this->next;
150 prev->next = this->next;
157 void cf_register (char *type,
158 int (*callback) (char *, char *),
159 char **keys, int keys_num)
161 cf_callback_t *cf_cb;
165 /* Remove this module from the list, if it already exists */
166 cf_unregister (type);
168 /* This pointer will be free'd in `cf_unregister' */
169 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
173 cf_cb->callback = callback;
175 cf_cb->keys_num = keys_num;
177 cf_cb->next = first_callback;
178 first_callback = cf_cb;
180 for (i = 0; i < keys_num; i++)
182 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
184 /* This may be called multiple times for the same
185 * `key', but apparently `lc_register_*' can handle
187 lc_register_callback (buf, SHORTOPT_NONE,
188 LC_VAR_STRING, cf_callback_plugin_dispatch,
193 DBG ("Key was truncated: `%s'", keys[i]);
199 * Other query functions
201 char *cf_get_mode_option (const char *key)
205 for (i = 0; i < cf_mode_num; i++)
207 if ((cf_mode_list[i].mode & operating_mode) == 0)
210 if (strcasecmp (cf_mode_list[i].key, key) == 0)
211 return (cf_mode_list[i].value);
217 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
218 * Functions for the actual parsing *
219 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
223 * Start/end the `mode' section
229 int cf_callback_mode (const char *shortvar, const char *var,
230 const char *arguments, const char *value, lc_flags_t flags,
233 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
234 shortvar, var, arguments, value);
236 if (flags == LC_FLAGS_SECTIONSTART)
238 if (nesting_depth != 0)
240 fprintf (stderr, ERR_NOT_NESTED);
241 return (LC_CBRET_ERROR);
244 if (arguments == NULL)
246 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
247 return (LC_CBRET_ERROR);
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)))
259 return (LC_CBRET_OKAY);
263 return (LC_CBRET_IGNORESECTION);
266 else if (flags == LC_FLAGS_SECTIONEND)
270 return (LC_CBRET_OKAY);
274 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
275 return (LC_CBRET_ERROR);
281 * `cf_callback_mode_plugindir'
282 * Change the plugin directory
288 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
289 const char *arguments, const char *value, lc_flags_t flags,
292 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
293 shortvar, var, arguments, value);
295 plugin_set_dir (value);
297 return (LC_CBRET_OKAY);
300 int cf_callback_mode_option (const char *shortvar, const char *var,
301 const char *arguments, const char *value, lc_flags_t flags,
304 cf_mode_item_t *item;
306 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
307 shortvar, var, arguments, value);
311 fprintf (stderr, "No extra..?\n");
312 return (LC_CBRET_ERROR);
315 item = (cf_mode_item_t *) extra;
317 if (strcasecmp (item->key, shortvar))
319 fprintf (stderr, "Wrong extra..\n");
320 return (LC_CBRET_ERROR);
323 if ((operating_mode & item->mode) == 0)
325 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
326 return (LC_CBRET_ERROR);
329 if (item->value != NULL)
335 if ((item->value = strdup (value)) == NULL)
338 return (LC_CBRET_ERROR);
341 return (LC_CBRET_OKAY);
345 * `cf_callback_mode_loadmodule':
352 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
353 const char *arguments, const char *value, lc_flags_t flags,
356 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
357 shortvar, var, arguments, value);
359 if (nesting_depth == 0)
361 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
362 return (LC_CBRET_ERROR);
365 if (plugin_load (value))
366 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
368 /* Return `okay' even if there was an error, because it's not a syntax
370 return (LC_CBRET_OKAY);
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,
378 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
379 shortvar, var, arguments, value);
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;
389 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
390 return (LC_CBRET_ERROR);
393 return (LC_CBRET_OKAY);
397 * `cf_callback_plugin'
398 * Start/end section `plugin'
400 * <Plugin `arguments'>
404 int cf_callback_plugin (const char *shortvar, const char *var,
405 const char *arguments, const char *value, lc_flags_t flags,
408 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
409 shortvar, var, arguments, value);
411 if (flags == LC_FLAGS_SECTIONSTART)
413 if (nesting_depth != 0)
415 fprintf (stderr, ERR_NOT_NESTED);
416 return (LC_CBRET_ERROR);
419 if (arguments == NULL)
421 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
422 return (LC_CBRET_ERROR);
425 if ((current_module = strdup (arguments)) == NULL)
428 return (LC_CBRET_ERROR);
433 if (cf_search (current_module) != NULL)
434 return (LC_CBRET_OKAY);
436 return (LC_CBRET_IGNORESECTION);
438 else if (flags == LC_FLAGS_SECTIONEND)
440 if (current_module != NULL)
442 free (current_module);
443 current_module = NULL;
448 return (LC_CBRET_OKAY);
452 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
453 return (LC_CBRET_ERROR);
458 * `cf_callback_plugin_dispatch'
459 * Send options within `plugin' sections to the plugin that requests it.
461 * <Plugin `current_module'>
465 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
466 const char *arguments, const char *value, lc_flags_t flags,
469 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
470 shortvar, var, arguments, value);
472 if ((nesting_depth == 0) || (current_module == NULL))
474 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
475 return (LC_CBRET_ERROR);
478 /* Send the data to the plugin */
479 if (cf_dispatch (current_module, shortvar, value) < 0)
480 return (LC_CBRET_ERROR);
482 return (LC_CBRET_OKAY);
485 int cf_read (char *filename)
489 if (filename == NULL)
490 filename = CONFIGFILE;
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);
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);
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);
509 for (i = 0; i < cf_mode_num; i++)
512 cf_mode_item_t *item;
514 item = &cf_mode_list[i];
516 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
519 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
520 cf_callback_mode_option, (void *) item);
523 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
525 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
529 /* free memory and stuff */