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", PIDFILE, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
73 {"DataDir", PKGLOCALSTATEDIR, MODE_SERVER | MODE_LOCAL},
74 {"LogFile", LOGFILE, MODE_SERVER | MODE_SERVER | MODE_LOCAL},
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 *);
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_mode_option (const char *key)
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)
220 return (cf_mode_list[i].value);
226 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
227 * Functions for the actual parsing *
228 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
232 * Chose the `operating_mode'
236 int cf_callback_mode (const char *shortvar, const char *var,
237 const char *arguments, const char *value, lc_flags_t flags,
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;
251 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
252 return (LC_CBRET_ERROR);
255 return (LC_CBRET_OKAY);
259 * `cf_callback_mode_plugindir'
260 * Change the plugin directory
266 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
267 const char *arguments, const char *value, lc_flags_t flags,
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,
282 cf_mode_item_t *item;
284 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
285 shortvar, var, arguments, value);
289 fprintf (stderr, "No extra..?\n");
290 return (LC_CBRET_ERROR);
293 item = (cf_mode_item_t *) extra;
295 if (strcasecmp (item->key, shortvar))
297 fprintf (stderr, "Wrong extra..\n");
298 return (LC_CBRET_ERROR);
301 if ((operating_mode & item->mode) == 0)
303 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
304 return (LC_CBRET_ERROR);
307 if (item->value != NULL)
313 if ((item->value = strdup (value)) == NULL)
316 return (LC_CBRET_ERROR);
319 return (LC_CBRET_OKAY);
323 * `cf_callback_mode_loadmodule':
330 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
331 const char *arguments, const char *value, lc_flags_t flags,
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
342 return (LC_CBRET_OKAY);
346 * `cf_callback_mode_switch'
347 * Change the contents of the global variable `operating_mode'
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.
353 int cf_callback_mode_switch (const char *shortvar, const char *var,
354 const char *arguments, const char *value, lc_flags_t flags,
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;
368 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
369 return (LC_CBRET_ERROR);
372 return (LC_CBRET_OKAY);
376 * `cf_callback_plugin'
377 * Start/end section `plugin'
379 * <Plugin `arguments'>
383 int cf_callback_plugin (const char *shortvar, const char *var,
384 const char *arguments, const char *value, lc_flags_t flags,
387 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
388 shortvar, var, arguments, value);
390 if (flags == LC_FLAGS_SECTIONSTART)
392 if (nesting_depth != 0)
394 fprintf (stderr, ERR_NOT_NESTED);
395 return (LC_CBRET_ERROR);
398 if (arguments == NULL)
400 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
401 return (LC_CBRET_ERROR);
404 if ((current_module = strdup (arguments)) == NULL)
407 return (LC_CBRET_ERROR);
412 if (cf_search (current_module) != NULL)
413 return (LC_CBRET_OKAY);
415 return (LC_CBRET_IGNORESECTION);
417 else if (flags == LC_FLAGS_SECTIONEND)
419 if (current_module != NULL)
421 free (current_module);
422 current_module = NULL;
427 return (LC_CBRET_OKAY);
431 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
432 return (LC_CBRET_ERROR);
437 * `cf_callback_plugin_dispatch'
438 * Send options within `plugin' sections to the plugin that requests it.
440 * <Plugin `current_module'>
444 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
445 const char *arguments, const char *value, lc_flags_t flags,
448 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
449 shortvar, var, arguments, value);
451 if ((nesting_depth == 0) || (current_module == NULL))
453 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
454 return (LC_CBRET_ERROR);
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);
466 static int run_once = 0;
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++)
493 cf_mode_item_t *item;
495 item = &cf_mode_list[i];
497 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
500 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
501 cf_callback_mode_option, (void *) item);
505 int cf_read (char *filename)
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))
517 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
521 DBG ("Done parsing file `%s'", filename);
523 /* free memory and stuff */