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>
24 * make internal-only functions `static' */
28 #include "libconfig/libconfig.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"
42 extern int operating_mode;
44 static int operating_mode = MODE_CLIENT;
47 typedef struct cf_callback
50 int (*callback) (char *, char *);
53 struct cf_callback *next;
56 static cf_callback_t *first_callback = NULL;
58 typedef struct cf_mode_item
68 static cf_mode_item_t cf_mode_list[] =
70 {"Server", NULL, MODE_CLIENT },
71 {"Port", NULL, MODE_CLIENT | MODE_SERVER },
72 {"PIDFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
73 {"DataDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
74 {"LogFile", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
76 static int cf_mode_num = 5;
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 *);
86 * Functions to handle register/unregister, search, and other plugin related
89 cf_callback_t *cf_search (char *type)
96 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
97 if (strcasecmp (cf_cb->type, type) == 0)
103 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
105 cf_callback_t *cf_cb;
111 DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
113 if ((cf_cb = cf_search (type)) == NULL)
115 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
119 if ((key = strdup (orig_key)) == NULL)
121 if ((value = strdup (orig_value)) == NULL)
129 for (i = 0; i < cf_cb->keys_num; i++)
131 if (strcasecmp (cf_cb->keys[i], key) == 0)
133 ret = (*cf_cb->callback) (key, value);
138 if (i >= cf_cb->keys_num)
139 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
147 void cf_unregister (char *type)
149 cf_callback_t *this, *prev;
151 for (prev = NULL, this = first_callback;
153 prev = this, this = this->next)
154 if (strcasecmp (this->type, type) == 0)
157 first_callback = this->next;
159 prev->next = this->next;
166 void cf_register (char *type,
167 int (*callback) (char *, char *),
168 char **keys, int keys_num)
170 cf_callback_t *cf_cb;
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)
182 cf_cb->callback = callback;
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++)
191 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
193 /* This may be called multiple times for the same
194 * `key', but apparently `lc_register_*' can handle
196 lc_register_callback (buf, SHORTOPT_NONE,
197 LC_VAR_STRING, cf_callback_plugin_dispatch,
202 DBG ("Key was truncated: `%s'", keys[i]);
208 * Other query functions
210 char *cf_get_option (const char *key, char *def)
214 for (i = 0; i < cf_mode_num; i++)
216 if ((cf_mode_list[i].mode & operating_mode) == 0)
219 if (strcasecmp (cf_mode_list[i].key, key) != 0)
222 if (cf_mode_list[i].value != NULL)
223 return (cf_mode_list[i].value);
230 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
231 * Functions for the actual parsing *
232 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
236 * Chose the `operating_mode'
240 int cf_callback_mode (const char *shortvar, const char *var,
241 const char *arguments, const char *value, lc_flags_t flags,
244 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
245 shortvar, var, arguments, value);
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;
255 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
256 return (LC_CBRET_ERROR);
259 return (LC_CBRET_OKAY);
263 * `cf_callback_mode_plugindir'
264 * Change the plugin directory
270 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
271 const char *arguments, const char *value, lc_flags_t flags,
274 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
275 shortvar, var, arguments, value);
277 plugin_set_dir (value);
279 return (LC_CBRET_OKAY);
282 int cf_callback_mode_option (const char *shortvar, const char *var,
283 const char *arguments, const char *value, lc_flags_t flags,
286 cf_mode_item_t *item;
288 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
289 shortvar, var, arguments, value);
293 fprintf (stderr, "No extra..?\n");
294 return (LC_CBRET_ERROR);
297 item = (cf_mode_item_t *) extra;
299 if (strcasecmp (item->key, shortvar))
301 fprintf (stderr, "Wrong extra..\n");
302 return (LC_CBRET_ERROR);
305 if ((operating_mode & item->mode) == 0)
307 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
308 return (LC_CBRET_ERROR);
311 if (item->value != NULL)
317 if ((item->value = strdup (value)) == NULL)
320 return (LC_CBRET_ERROR);
323 return (LC_CBRET_OKAY);
327 * `cf_callback_mode_loadmodule':
334 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
335 const char *arguments, const char *value, lc_flags_t flags,
338 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
339 shortvar, var, arguments, value);
341 if (plugin_load (value))
342 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
344 /* Return `okay' even if there was an error, because it's not a syntax
346 return (LC_CBRET_OKAY);
350 * `cf_callback_mode_switch'
351 * Change the contents of the global variable `operating_mode'
353 * This should be command line options. One *can* do this in the config
354 * files, but I will not document this. Don't whine abount it not working as
355 * you expect if you do it anyways.
357 int cf_callback_mode_switch (const char *shortvar, const char *var,
358 const char *arguments, const char *value, lc_flags_t flags,
361 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
362 shortvar, var, arguments, value);
364 if (strcasecmp (shortvar, "Client") == 0)
365 operating_mode = MODE_CLIENT;
366 else if (strcasecmp (shortvar, "Local") == 0)
367 operating_mode = MODE_LOCAL;
368 else if (strcasecmp (shortvar, "Server") == 0)
369 operating_mode = MODE_SERVER;
372 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
373 return (LC_CBRET_ERROR);
376 return (LC_CBRET_OKAY);
380 * `cf_callback_plugin'
381 * Start/end section `plugin'
383 * <Plugin `arguments'>
387 int cf_callback_plugin (const char *shortvar, const char *var,
388 const char *arguments, const char *value, lc_flags_t flags,
391 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
392 shortvar, var, arguments, value);
394 if (flags == LC_FLAGS_SECTIONSTART)
396 if (nesting_depth != 0)
398 fprintf (stderr, ERR_NOT_NESTED);
399 return (LC_CBRET_ERROR);
402 if (arguments == NULL)
404 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
405 return (LC_CBRET_ERROR);
408 if ((current_module = strdup (arguments)) == NULL)
411 return (LC_CBRET_ERROR);
416 if (cf_search (current_module) != NULL)
417 return (LC_CBRET_OKAY);
419 return (LC_CBRET_IGNORESECTION);
421 else if (flags == LC_FLAGS_SECTIONEND)
423 if (current_module != NULL)
425 free (current_module);
426 current_module = NULL;
431 return (LC_CBRET_OKAY);
435 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
436 return (LC_CBRET_ERROR);
441 * `cf_callback_plugin_dispatch'
442 * Send options within `plugin' sections to the plugin that requests it.
444 * <Plugin `current_module'>
448 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
449 const char *arguments, const char *value, lc_flags_t flags,
452 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
453 shortvar, var, arguments, value);
455 if ((nesting_depth == 0) || (current_module == NULL))
457 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
458 return (LC_CBRET_ERROR);
461 /* Send the data to the plugin */
462 if (cf_dispatch (current_module, shortvar, value) < 0)
463 return (LC_CBRET_ERROR);
465 return (LC_CBRET_OKAY);
470 static int run_once = 0;
477 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
478 cf_callback_mode, NULL);
479 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
480 cf_callback_plugin, NULL);
482 lc_register_callback ("PluginDir", SHORTOPT_NONE,
483 LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
484 lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
485 LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
487 for (i = 0; i < cf_mode_num; i++)
489 cf_mode_item_t *item;
491 item = &cf_mode_list[i];
493 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
494 cf_callback_mode_option, (void *) item);
498 int cf_read (char *filename)
502 if (filename == NULL)
503 filename = CONFIGFILE;
505 DBG ("Starting to parse file `%s'", filename);
507 /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
508 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
510 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
514 DBG ("Done parsing file `%s'", filename);
516 /* free memory and stuff */