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", NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
73 {"DataDir", NULL, MODE_SERVER | MODE_SERVER | MODE_LOCAL},
74 {"LogFile", NULL, MODE_SERVER | MODE_SERVER | MODE_LOCAL}
75 };
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 *);
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);
101 }
103 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
104 {
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);
145 }
147 void cf_unregister (char *type)
148 {
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 }
164 }
166 void cf_register (char *type,
167 int (*callback) (char *, char *),
168 char **keys, int keys_num)
169 {
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 }
205 }
207 /*
208 * Other query functions
209 */
210 char *cf_get_option (const char *key, char *def)
211 {
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 continue;
222 if (cf_mode_list[i].value != NULL)
223 return (cf_mode_list[i].value);
224 return (def);
225 }
227 return (NULL);
228 }
230 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
231 * Functions for the actual parsing *
232 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
234 /*
235 * `cf_callback_mode'
236 * Chose the `operating_mode'
237 *
238 * Mode `value'
239 */
240 int cf_callback_mode (const char *shortvar, const char *var,
241 const char *arguments, const char *value, lc_flags_t flags,
242 void *extra)
243 {
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;
253 else
254 {
255 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
256 return (LC_CBRET_ERROR);
257 }
259 return (LC_CBRET_OKAY);
260 }
262 /*
263 * `cf_callback_mode_plugindir'
264 * Change the plugin directory
265 *
266 * <Mode xxx>
267 * PluginDir `value'
268 * </Mode>
269 */
270 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
271 const char *arguments, const char *value, lc_flags_t flags,
272 void *extra)
273 {
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);
280 }
282 int cf_callback_mode_option (const char *shortvar, const char *var,
283 const char *arguments, const char *value, lc_flags_t flags,
284 void *extra)
285 {
286 cf_mode_item_t *item;
288 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
289 shortvar, var, arguments, value);
291 if (extra == NULL)
292 {
293 fprintf (stderr, "No extra..?\n");
294 return (LC_CBRET_ERROR);
295 }
297 item = (cf_mode_item_t *) extra;
299 if (strcasecmp (item->key, shortvar))
300 {
301 fprintf (stderr, "Wrong extra..\n");
302 return (LC_CBRET_ERROR);
303 }
305 if ((operating_mode & item->mode) == 0)
306 {
307 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
308 return (LC_CBRET_ERROR);
309 }
311 if (item->value != NULL)
312 {
313 free (item->value);
314 item->value = NULL;
315 }
317 if ((item->value = strdup (value)) == NULL)
318 {
319 perror ("strdup");
320 return (LC_CBRET_ERROR);
321 }
323 return (LC_CBRET_OKAY);
324 }
326 /*
327 * `cf_callback_mode_loadmodule':
328 * Load a plugin.
329 *
330 * <Mode xxx>
331 * LoadPlugin `value'
332 * </Mode>
333 */
334 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
335 const char *arguments, const char *value, lc_flags_t flags,
336 void *extra)
337 {
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
345 * problem.. */
346 return (LC_CBRET_OKAY);
347 }
349 /*
350 * `cf_callback_mode_switch'
351 * Change the contents of the global variable `operating_mode'
352 *
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.
356 */
357 int cf_callback_mode_switch (const char *shortvar, const char *var,
358 const char *arguments, const char *value, lc_flags_t flags,
359 void *extra)
360 {
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;
370 else
371 {
372 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
373 return (LC_CBRET_ERROR);
374 }
376 return (LC_CBRET_OKAY);
377 }
379 /*
380 * `cf_callback_plugin'
381 * Start/end section `plugin'
382 *
383 * <Plugin `arguments'>
384 * ...
385 * </Plugin>
386 */
387 int cf_callback_plugin (const char *shortvar, const char *var,
388 const char *arguments, const char *value, lc_flags_t flags,
389 void *extra)
390 {
391 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
392 shortvar, var, arguments, value);
394 if (flags == LC_FLAGS_SECTIONSTART)
395 {
396 if (nesting_depth != 0)
397 {
398 fprintf (stderr, ERR_NOT_NESTED);
399 return (LC_CBRET_ERROR);
400 }
402 if (arguments == NULL)
403 {
404 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
405 return (LC_CBRET_ERROR);
406 }
408 if ((current_module = strdup (arguments)) == NULL)
409 {
410 perror ("strdup");
411 return (LC_CBRET_ERROR);
412 }
414 nesting_depth++;
416 if (cf_search (current_module) != NULL)
417 return (LC_CBRET_OKAY);
418 else
419 return (LC_CBRET_IGNORESECTION);
420 }
421 else if (flags == LC_FLAGS_SECTIONEND)
422 {
423 if (current_module != NULL)
424 {
425 free (current_module);
426 current_module = NULL;
427 }
429 nesting_depth--;
431 return (LC_CBRET_OKAY);
432 }
433 else
434 {
435 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
436 return (LC_CBRET_ERROR);
437 }
438 }
440 /*
441 * `cf_callback_plugin_dispatch'
442 * Send options within `plugin' sections to the plugin that requests it.
443 *
444 * <Plugin `current_module'>
445 * `var' `value'
446 * </Plugin>
447 */
448 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
449 const char *arguments, const char *value, lc_flags_t flags,
450 void *extra)
451 {
452 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
453 shortvar, var, arguments, value);
455 if ((nesting_depth == 0) || (current_module == NULL))
456 {
457 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
458 return (LC_CBRET_ERROR);
459 }
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);
466 }
468 void cf_init (void)
469 {
470 static int run_once = 0;
471 int i;
473 if (run_once != 0)
474 return;
475 run_once = 1;
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++)
488 {
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);
495 }
496 }
498 int cf_read (char *filename)
499 {
500 cf_init ();
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))
509 {
510 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
511 return (-1);
512 }
514 DBG ("Done parsing file `%s'", filename);
516 /* free memory and stuff */
517 lc_cleanup ();
519 return (0);
520 }