Code

Initialize `addrlen' before using. This might be the reason for `getnameinfo' failing..
[collectd.git] / src / configfile.c
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 "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "network.h"
34 #include "utils_debug.h"
36 #define SHORTOPT_NONE 0
38 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
39 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
40 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
41 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
43 #ifdef HAVE_LIBRRD
44 extern int operating_mode;
45 #else
46 static int operating_mode = MODE_CLIENT;
47 #endif
49 typedef struct cf_callback
50 {
51         char  *type;
52         int  (*callback) (char *, char *);
53         char **keys;
54         int    keys_num;
55         struct cf_callback *next;
56 } cf_callback_t;
58 static cf_callback_t *first_callback = NULL;
60 typedef struct cf_mode_item
61 {
62         char *key;
63         char *value;
64         int   mode;
65 } cf_mode_item_t;
67 /* TODO
68  * - LogFile
69  */
70 static cf_mode_item_t cf_mode_list[] =
71 {
72         /*
73         {"Server",      NULL, MODE_CLIENT                           },
74         {"Port",        NULL, MODE_CLIENT | MODE_SERVER             },
75         */
76         {"PIDFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
77         {"DataDir",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
78         {"LogFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
79 };
80 static int cf_mode_num = 3;
82 static int nesting_depth = 0;
83 static char *current_module = NULL;
85 /* `cf_register' needs this prototype */
86 int cf_callback_plugin_dispatch (const char *, const char *, const char *,
87                 const char *, lc_flags_t, void *);
89 /*
90  * Functions to handle register/unregister, search, and other plugin related
91  * stuff
92  */
93 cf_callback_t *cf_search (char *type)
94 {
95         cf_callback_t *cf_cb;
97         if (type == NULL)
98                 return (NULL);
100         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
101                 if (strcasecmp (cf_cb->type, type) == 0)
102                         break;
104         return (cf_cb);
107 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
109         cf_callback_t *cf_cb;
110         char *key;
111         char *value;
112         int ret;
113         int i;
115         DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
117         if ((cf_cb = cf_search (type)) == NULL)
118         {
119                 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
120                 return (-1);
121         }
123         if ((key = strdup (orig_key)) == NULL)
124                 return (1);
125         if ((value = strdup (orig_value)) == NULL)
126         {
127                 free (key);
128                 return (2);
129         }
131         ret = -1;
133         for (i = 0; i < cf_cb->keys_num; i++)
134         {
135                 if (strcasecmp (cf_cb->keys[i], key) == 0)
136                 {
137                         ret = (*cf_cb->callback) (key, value);
138                         break;
139                 }
140         }
142         if (i >= cf_cb->keys_num)
143                 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.\n", type, key);
145         free (key);
146         free (value);
148         return (ret);
151 void cf_unregister (char *type)
153         cf_callback_t *this, *prev;
155         for (prev = NULL, this = first_callback;
156                         this != NULL;
157                         prev = this, this = this->next)
158                 if (strcasecmp (this->type, type) == 0)
159                 {
160                         if (prev == NULL)
161                                 first_callback = this->next;
162                         else
163                                 prev->next = this->next;
165                         free (this);
166                         break;
167                 }
170 void cf_register (char *type,
171                 int (*callback) (char *, char *),
172                 char **keys, int keys_num)
174         cf_callback_t *cf_cb;
175         char buf[64];
176         int i;
178         /* Remove this module from the list, if it already exists */
179         cf_unregister (type);
181         /* This pointer will be free'd in `cf_unregister' */
182         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
183                 return;
185         cf_cb->type     = type;
186         cf_cb->callback = callback;
187         cf_cb->keys     = keys;
188         cf_cb->keys_num = keys_num;
190         cf_cb->next = first_callback;
191         first_callback = cf_cb;
193         for (i = 0; i < keys_num; i++)
194         {
195                 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
196                 {
197                         /* This may be called multiple times for the same
198                          * `key', but apparently `lc_register_*' can handle
199                          * it.. */
200                         lc_register_callback (buf, SHORTOPT_NONE,
201                                         LC_VAR_STRING, cf_callback_plugin_dispatch,
202                                         NULL);
203                 }
204                 else
205                 {
206                         DBG ("Key was truncated: `%s'", keys[i]);
207                 }
208         }
211 /*
212  * Other query functions
213  */
214 char *cf_get_option (const char *key, char *def)
216         int i;
218         for (i = 0; i < cf_mode_num; i++)
219         {
220                 if ((cf_mode_list[i].mode & operating_mode) == 0)
221                         continue;
223                 if (strcasecmp (cf_mode_list[i].key, key) != 0)
224                         continue;
226                 if (cf_mode_list[i].value != NULL)
227                         return (cf_mode_list[i].value);
228                 return (def);
229         }
231         return (NULL);
234 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
235  * Functions for the actual parsing                                    *
236  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
238 /*
239  * `cf_callback_mode'
240  *   Chose the `operating_mode'
241  *
242  * Mode `value'
243  */
244 int cf_callback_mode (const char *shortvar, const char *var,
245                 const char *arguments, const char *value, lc_flags_t flags,
246                 void *extra)
248         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
249                         shortvar, var, arguments, value);
251         if (strcasecmp (value, "Client") == 0)
252                 operating_mode = MODE_CLIENT;
253         else if (strcasecmp (value, "Server") == 0)
254                 operating_mode = MODE_SERVER;
255         else if (strcasecmp (value, "Local") == 0)
256                 operating_mode = MODE_LOCAL;
257         else
258         {
259                 syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
260                 return (LC_CBRET_ERROR);
261         }
263         return (LC_CBRET_OKAY);
266 /*
267  * `cf_callback_mode_plugindir'
268  *   Change the plugin directory
269  *
270  * <Mode xxx>
271  *   PluginDir `value'
272  * </Mode>
273  */
274 int cf_callback_mode_plugindir (const char *shortvar, const char *var,
275                 const char *arguments, const char *value, lc_flags_t flags,
276                 void *extra)
278         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
279                         shortvar, var, arguments, value);
281         plugin_set_dir (value);
283         return (LC_CBRET_OKAY);
286 int cf_callback_mode_option (const char *shortvar, const char *var,
287                 const char *arguments, const char *value, lc_flags_t flags,
288                 void *extra)
290         cf_mode_item_t *item;
292         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
293                         shortvar, var, arguments, value);
295         if (extra == NULL)
296         {
297                 fprintf (stderr, "No extra..?\n");
298                 return (LC_CBRET_ERROR);
299         }
301         item = (cf_mode_item_t *) extra;
303         if (strcasecmp (item->key, shortvar))
304         {
305                 fprintf (stderr, "Wrong extra..\n");
306                 return (LC_CBRET_ERROR);
307         }
309         if ((operating_mode & item->mode) == 0)
310         {
311                 fprintf (stderr, "Option `%s' is not valid in this mode!\n", shortvar);
312                 return (LC_CBRET_ERROR);
313         }
315         if (item->value != NULL)
316         {
317                 free (item->value);
318                 item->value = NULL;
319         }
321         if ((item->value = strdup (value)) == NULL)
322         {
323                 perror ("strdup");
324                 return (LC_CBRET_ERROR);
325         }
327         return (LC_CBRET_OKAY);
330 /*
331  * `cf_callback_mode_loadmodule':
332  *   Load a plugin.
333  *
334  * <Mode xxx>
335  *   LoadPlugin `value'
336  * </Mode>
337  */
338 int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
339                 const char *arguments, const char *value, lc_flags_t flags,
340                 void *extra)
342         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
343                         shortvar, var, arguments, value);
345         if (plugin_load (value))
346                 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
348         /* Return `okay' even if there was an error, because it's not a syntax
349          * problem.. */
350         return (LC_CBRET_OKAY);
353 /*
354  * `cf_callback_mode_switch'
355  *   Change the contents of the global variable `operating_mode'
356  *
357  *   This should be command line options. One *can* do this in the config
358  *   files, but I will not document this. Don't whine abount it not working as
359  *   you expect if you do it anyways.
360  */
361 int cf_callback_mode_switch (const char *shortvar, const char *var,
362                 const char *arguments, const char *value, lc_flags_t flags,
363                 void *extra)
365         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
366                         shortvar, var, arguments, value);
368         if (strcasecmp (shortvar, "Client") == 0)
369                 operating_mode = MODE_CLIENT;
370         else if (strcasecmp (shortvar, "Local") == 0)
371                 operating_mode = MODE_LOCAL;
372         else if (strcasecmp (shortvar, "Server") == 0)
373                 operating_mode = MODE_SERVER;
374         else
375         {
376                 fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
377                 return (LC_CBRET_ERROR);
378         }
380         return (LC_CBRET_OKAY);
383 int cf_callback_socket (const char *shortvar, const char *var,
384                 const char *arguments, const char *value, lc_flags_t flags,
385                 void *extra)
387         char *buffer;
389         char *fields[3];
390         int   numfields;
392         char *node;
393         char *service = NET_DEFAULT_PORT;
395         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
396                         shortvar, var, arguments, value);
398         buffer = strdup (value);
399         if (buffer == NULL)
400                 return (LC_CBRET_ERROR);
402         numfields = strsplit (buffer, fields, 3);
404         if ((numfields != 1) && (numfields != 2))
405         {
406                 syslog (LOG_ERR, "Invalid number of arguments to `%s'",
407                                 shortvar);
408                 free (buffer);
409                 return (LC_CBRET_ERROR);
410         }
412         node = fields[0];
413         if (numfields == 2)
414                 service = fields[1];
416         /* Still return `LC_CBRET_OKAY' because this is not an syntax error */
417         if (network_create_socket (node, service) < 1)
418                 syslog (LOG_ERR, "network_create_socket (%s, %s) failed",
419                                 node, service);
421         free (buffer);
423         return (LC_CBRET_OKAY);
426 /*
427  * `cf_callback_plugin'
428  *   Start/end section `plugin'
429  *
430  * <Plugin `arguments'>
431  *   ...
432  * </Plugin>
433  */
434 int cf_callback_plugin (const char *shortvar, const char *var,
435                 const char *arguments, const char *value, lc_flags_t flags,
436                 void *extra)
438         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
439                         shortvar, var, arguments, value);
441         if (flags == LC_FLAGS_SECTIONSTART)
442         {
443                 if (nesting_depth != 0)
444                 {
445                         fprintf (stderr, ERR_NOT_NESTED);
446                         return (LC_CBRET_ERROR);
447                 }
449                 if (arguments == NULL)
450                 {
451                         fprintf (stderr, ERR_NEEDS_ARG, shortvar);
452                         return (LC_CBRET_ERROR);
453                 }
455                 if ((current_module = strdup (arguments)) == NULL)
456                 {
457                         perror ("strdup");
458                         return (LC_CBRET_ERROR);
459                 }
461                 nesting_depth++;
463                 if (cf_search (current_module) != NULL)
464                         return (LC_CBRET_OKAY);
465                 else
466                         return (LC_CBRET_IGNORESECTION);
467         }
468         else if (flags == LC_FLAGS_SECTIONEND)
469         {
470                 if (current_module != NULL)
471                 {
472                         free (current_module);
473                         current_module = NULL;
474                 }
476                 nesting_depth--;
478                 return (LC_CBRET_OKAY);
479         }
480         else
481         {
482                 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
483                 return (LC_CBRET_ERROR);
484         }
487 /*
488  * `cf_callback_plugin_dispatch'
489  *   Send options within `plugin' sections to the plugin that requests it.
490  *
491  * <Plugin `current_module'>
492  *   `var' `value'
493  * </Plugin>
494  */
495 int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
496                 const char *arguments, const char *value, lc_flags_t flags,
497                 void *extra)
499         DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
500                         shortvar, var, arguments, value);
502         if ((nesting_depth == 0) || (current_module == NULL))
503         {
504                 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
505                 return (LC_CBRET_ERROR);
506         }
508         /* Send the data to the plugin */
509         if (cf_dispatch (current_module, shortvar, value) < 0)
510                 return (LC_CBRET_ERROR);
512         return (LC_CBRET_OKAY);
515 void cf_init (void)
517         static int run_once = 0;
518         int i;
520         if (run_once != 0)
521                 return;
522         run_once = 1;
524         lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
525                         cf_callback_mode, NULL);
526         lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
527                         cf_callback_plugin, NULL);
529         lc_register_callback ("PluginDir", SHORTOPT_NONE,
530                         LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
531         lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
532                         LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
534         lc_register_callback ("Listen", SHORTOPT_NONE,
535                         LC_VAR_STRING, cf_callback_socket, NULL);
536         lc_register_callback ("Server", SHORTOPT_NONE,
537                         LC_VAR_STRING, cf_callback_socket, NULL);
539         for (i = 0; i < cf_mode_num; i++)
540         {
541                 cf_mode_item_t *item;
543                 item = &cf_mode_list[i];
545                 lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
546                                 cf_callback_mode_option, (void *) item);
547         }
550 int cf_read (char *filename)
552         cf_init ();
554         if (filename == NULL)
555                 filename = CONFIGFILE;
557         DBG ("Starting to parse file `%s'", filename);
559         /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
560         if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
561         {
562                 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
563                 return (-1);
564         }
566         DBG ("Done parsing file `%s'", filename);
568         /* free memory and stuff */
569         lc_cleanup ();
571         return (0);