2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <apr_general.h>
30 #include <apr_strings.h>
32 #include "hostinfod.h"
34 typedef int (*process_line_fn) (const char *path, int lineno,
35 const char *key, const char *value,
37 typedef int (*process_section_fn) (const char *path, int lineno,
40 static void process_conf_file (const char *path, int exit_if_not_exist,
41 process_line_fn process_line,
42 process_section_fn process_section,
44 static int get_bool (const char *str);
46 /* Read the main configuration file. */
47 static int main_process_line (const char *path, int lineno, const char *key, const char *value, void *data);
50 read_main_conf_file (void)
52 process_conf_file (conf_file, 0, main_process_line, NULL, NULL);
56 main_process_line (const char *path, int lineno,
57 const char *key, const char *value,
62 if (strcasecmp (key, "guests") == 0) {
64 error ("%s:%d: directive is empty: %s", path, lineno, key);
67 guests_file = apr_pstrdup (pool, value);
68 } else if (strcasecmp (key, "sockets") == 0) {
70 error ("%s:%d: directive is empty: %s", path, lineno, key);
73 socket_dir = apr_pstrdup (pool, value);
74 } else if (strcasecmp (key, "libvirt") == 0) {
76 error ("%s:%d: directive is empty: %s", path, lineno, key);
79 if (!libvirt_uri_set_on_cmdline)
80 libvirt_uri = apr_pstrdup (pool, value);
81 } else if (strcasecmp (key, "verbose") == 0) {
82 bool = get_bool (value);
84 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
87 if (!verbose_set_on_cmdline)
89 } else if (strcasecmp (key, "foreground") == 0) {
90 bool = get_bool (value);
92 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
95 if (!foreground_set_on_cmdline)
98 error ("%s:%d: unknown directive in configuration file: %s",
105 /* Check that 'cmd' is enabled for the named guest, and that
106 * it is not being called too frequently.
108 * XXX Rereading the configuration file each time is possibly
111 * Returns 0 = proceed, -1 = fail.
113 static int guests_process_line (const char *path, int lineno,
114 const char *key, const char *value,
116 static int guests_process_section (const char *path, int lineno,
121 const char *name; /* guest "driver-name" */
122 const char *cmd; /* command being tested */
123 int in_section; /* currently processing the right section? */
124 double interval; /* interval for this guest (0 = any) */
125 int enabled; /* is command enabled? */
129 check_guests_file (struct guest_description *hval, const char *cmd,
130 double *interval, int *enabled)
132 struct guests_data *data = apr_palloc (hval->rpool, sizeof *data);
134 data->name = hval->name;
136 data->in_section = 0;
137 data->interval = 60.; /* default */
138 data->enabled = 0; /* default */
140 process_conf_file (guests_file, 1,
141 guests_process_line, guests_process_section, data);
143 *interval = data->interval;
144 *enabled = data->enabled;
148 guests_process_line (const char *path, int lineno,
149 const char *key, const char *value,
152 struct guests_data *data = datav;
155 if (!data->in_section)
158 if (strcasecmp (key, "interval") == 0) {
159 if (strcasecmp (value, "any") == 0)
162 if (sscanf (value, "%lg", &data->interval) != 1) {
163 error ("%s:%d: %s: not a valid decimal number", path, lineno, key);
167 } else if (strcasecmp (key, data->cmd) == 0) {
168 bool = get_bool (value);
170 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
173 data->enabled = bool;
180 guests_process_section (const char *path, int lineno,
184 struct guests_data *data = datav;
188 flags |= FNM_CASEFOLD;
191 data->in_section = fnmatch (section, data->name, flags) == 0;
195 /* Configuration file parser. */
197 process_conf_file (const char *path, int exit_if_not_exist,
198 process_line_fn process_line,
199 process_section_fn process_section,
202 static const char *whitespace = " \t\n\v";
207 size_t real_len, key_len;
209 const char *key, *value;
211 debug ("begin processing configuration file %s", path);
213 fp = fopen (path, "r");
215 if (exit_if_not_exist) {
216 perrorf ("%s", path);
219 pwarningf ("%s", path);
223 while ((r = getline (&line, &len, fp)) != -1) {
226 /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/
228 /* Remove trailing \n */
229 real_len = strlen (line);
230 if (real_len > 0 && line[real_len-1] == '\n')
231 line[--real_len] = '\0';
233 /* Ignore blank lines and comments. */
236 if (line[0] == '#' || strspn (line, whitespace) == real_len)
239 if (line[0] == '[') { /* Section. */
240 if (line[real_len-1] == ']')
241 line[--real_len] = '\0';
243 error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
248 debug ("configuration file: section [%s]", &line[1]);
250 if (!process_section) {
251 error ("%s:%d: unexpected section header ([%s]) in file",
252 path, lineno, &line[1]);
256 if (process_section (path, lineno, &line[1], data) == -1)
259 else { /* Key value */
260 key_len = strcspn (line, whitespace);
261 line[key_len] = '\0';
263 value = key_len < real_len ? &line[key_len+1] : NULL;
265 value += strspn (value, whitespace);
266 if (value[0] == '\0')
270 debug ("configuration file: key '%s', value '%s'", key, value);
272 if (process_line && process_line (path, lineno, key, value, data) == -1)
280 error ("%s: error reading configuration file", path);
283 if (fclose (fp) == EOF) {
284 perrorf ("%s", path);
288 debug ("finished processing configuration file successfully");
292 get_bool (const char *str)
297 if (strcasecmp (str, "on") == 0)
299 if (strcasecmp (str, "off") == 0)
303 case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
305 case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':