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},
68 {"PluginDir", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
70 static int cf_mode_num = 5;
72 static int nesting_depth = 0;
73 static char *current_module = NULL;
75 /* cf_register needs this prototype */
76 int cf_callback_dispatch (const char *, const char *, const char *,
77 const char *, lc_flags_t, void *);
80 * Functions to handle register/unregister, search, and other plugin related
83 cf_callback_t *cf_search (char *type)
90 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
91 if (strcasecmp (cf_cb->type, type) == 0)
97 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
105 if ((cf_cb = cf_search (type)) == NULL)
107 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
111 if ((key = strdup (orig_key)) == NULL)
113 if ((value = strdup (orig_value)) == NULL)
121 for (i = 0; i < cf_cb->keys_num; i++)
123 if (strcasecmp (cf_cb->keys[i], key) == 0)
125 ret = (*cf_cb->callback) (key, value);
130 if (i >= cf_cb->keys_num)
131 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
139 void cf_unregister (char *type)
141 cf_callback_t *this, *prev;
143 for (prev = NULL, this = first_callback;
145 prev = this, this = this->next)
146 if (strcasecmp (this->type, type) == 0)
149 first_callback = this->next;
151 prev->next = this->next;
158 void cf_register (char *type,
159 int (*callback) (char *, char *),
160 char **keys, int keys_num)
162 cf_callback_t *cf_cb;
166 /* Remove this module from the list, if it already exists */
167 cf_unregister (type);
169 /* This pointer will be free'd in `cf_unregister' */
170 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
174 cf_cb->callback = callback;
176 cf_cb->keys_num = keys_num;
178 cf_cb->next = first_callback;
179 first_callback = cf_cb;
181 for (i = 0; i < keys_num; i++)
183 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
185 /* This may be called multiple times for the same
186 * `key', but apparently `lc_register_*' can handle
188 lc_register_callback (buf, SHORTOPT_NONE,
189 LC_VAR_STRING, cf_callback_dispatch,
194 DBG ("Key was truncated: `%s'", keys[i]);
200 * Other query functions
202 char *cf_get_mode_option (const char *key)
206 for (i = 0; i < cf_mode_num; i++)
208 if ((cf_mode_list[i].mode & operating_mode) == 0)
211 if (strcasecmp (cf_mode_list[i].key, key) == 0)
212 return (cf_mode_list[i].value);
219 * Functions for the actual parsing
221 int cf_callback_dispatch (const char *shortvar, const char *var,
222 const char *arguments, const char *value, lc_flags_t flags,
225 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
226 shortvar, var, arguments, value);
228 if ((nesting_depth == 0) || (current_module == NULL))
230 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
231 return (LC_CBRET_ERROR);
234 /* Send the data to the plugin */
235 if (cf_dispatch (current_module, shortvar, value) < 0)
236 return (LC_CBRET_ERROR);
238 return (LC_CBRET_OKAY);
241 int cf_callback_options_mode (const char *shortvar, const char *var,
242 const char *arguments, const char *value, lc_flags_t flags,
245 cf_mode_item_t *item;
249 fprintf (stderr, "No extra..?\n");
250 return (LC_CBRET_ERROR);
253 item = (cf_mode_item_t *) extra;
255 if (strcasecmp (item->key, shortvar))
257 fprintf (stderr, "Wrong extra..\n");
258 return (LC_CBRET_ERROR);
261 if ((operating_mode & item->mode) == 0)
263 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
264 return (LC_CBRET_ERROR);
267 if (item->value != NULL)
273 if ((item->value = strdup (value)) == NULL)
276 return (LC_CBRET_ERROR);
279 return (LC_CBRET_OKAY);
282 int cf_callback_section_mode (const char *shortvar, const char *var,
283 const char *arguments, const char *value, lc_flags_t flags,
286 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
287 shortvar, var, arguments, value);
289 if (flags == LC_FLAGS_SECTIONSTART)
291 if (nesting_depth != 0)
293 fprintf (stderr, ERR_NOT_NESTED);
294 return (LC_CBRET_ERROR);
297 if (arguments == NULL)
299 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
300 return (LC_CBRET_ERROR);
305 if (((operating_mode == MODE_CLIENT)
306 && (strcasecmp (arguments, "Client") == 0))
307 || ((operating_mode == MODE_SERVER)
308 && (strcasecmp (arguments, "Server") == 0))
309 || ((operating_mode == MODE_LOCAL)
310 && (strcasecmp (arguments, "Local") == 0)))
312 return (LC_CBRET_OKAY);
316 return (LC_CBRET_IGNORESECTION);
319 else if (flags == LC_FLAGS_SECTIONEND)
323 return (LC_CBRET_OKAY);
327 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
328 return (LC_CBRET_ERROR);
333 int cf_callback_section_module (const char *shortvar, const char *var,
334 const char *arguments, const char *value, lc_flags_t flags,
337 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
338 shortvar, var, arguments, value);
340 if (flags == LC_FLAGS_SECTIONSTART)
342 if (nesting_depth != 0)
344 fprintf (stderr, ERR_NOT_NESTED);
345 return (LC_CBRET_ERROR);
348 if (arguments == NULL)
350 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
351 return (LC_CBRET_ERROR);
354 if ((current_module = strdup (arguments)) == NULL)
357 return (LC_CBRET_ERROR);
362 if (cf_search (current_module) != NULL)
363 return (LC_CBRET_OKAY);
365 return (LC_CBRET_IGNORESECTION);
367 else if (flags == LC_FLAGS_SECTIONEND)
369 if (current_module != NULL)
371 free (current_module);
372 current_module = NULL;
377 return (LC_CBRET_OKAY);
381 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
382 return (LC_CBRET_ERROR);
386 int cf_callback_loadmodule (const char *shortvar, const char *var,
387 const char *arguments, const char *value, lc_flags_t flags,
390 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
391 shortvar, var, arguments, value);
393 if (nesting_depth == 0)
395 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
396 return (LC_CBRET_ERROR);
399 if (plugin_load (value))
400 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", shortvar);
402 /* Return `okay' even if there was an error, because it's not a syntax
404 return (LC_CBRET_OKAY);
407 int cf_read (char *filename)
411 if (filename == NULL)
412 filename = CONFIGFILE;
414 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
415 cf_callback_section_mode, NULL);
416 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
417 cf_callback_section_module, NULL);
419 lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
420 LC_VAR_STRING, cf_callback_loadmodule,
423 for (i = 0; i < cf_mode_num; i++)
426 cf_mode_item_t *item;
428 item = &cf_mode_list[i];
430 if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
433 lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
434 cf_callback_options_mode, (void *) item);
437 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
439 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
443 /* free memory and stuff */