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 interval_set; /* have we set interval yet? */
126 int enabled; /* is command enabled? */
127 int enabled_set; /* have we set enabled flag yet? */
131 check_guests_file (struct guest_description *hval, const char *cmd,
132 double *interval, int *enabled)
134 struct guests_data *data = apr_palloc (hval->rpool, sizeof *data);
136 data->name = hval->name;
138 data->in_section = 0;
139 data->interval = 60.; /* default */
140 data->interval_set = 0;
141 data->enabled = 0; /* default */
142 data->enabled_set = 0;
144 process_conf_file (guests_file, 1,
145 guests_process_line, guests_process_section, data);
147 *interval = data->interval;
148 *enabled = data->enabled;
152 guests_process_line (const char *path, int lineno,
153 const char *key, const char *value,
156 struct guests_data *data = datav;
159 if (!data->in_section)
162 if (strcasecmp (key, "interval") == 0) {
163 if (!data->interval_set) {
164 if (strcasecmp (value, "any") == 0)
167 if (sscanf (value, "%lg", &data->interval) != 1) {
168 error ("%s:%d: %s: not a valid decimal number", path, lineno, key);
172 data->interval_set = 1;
174 } else if (strcasecmp (key, data->cmd) == 0) {
175 if (!data->enabled_set) {
176 bool = get_bool (value);
178 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
181 data->enabled = bool;
182 data->enabled_set = 1;
190 guests_process_section (const char *path, int lineno,
194 struct guests_data *data = datav;
198 flags |= FNM_CASEFOLD;
201 data->in_section = fnmatch (section, data->name, flags) == 0;
205 /* Configuration file parser. */
207 process_conf_file (const char *path, int exit_if_not_exist,
208 process_line_fn process_line,
209 process_section_fn process_section,
212 static const char *whitespace = " \t\n\v";
217 size_t real_len, key_len;
219 const char *key, *value;
221 debug ("begin processing configuration file %s", path);
223 fp = fopen (path, "r");
225 if (exit_if_not_exist) {
226 perrorf ("%s", path);
229 pwarningf ("%s", path);
233 while ((r = getline (&line, &len, fp)) != -1) {
236 /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/
238 /* Remove trailing \n */
239 real_len = strlen (line);
240 if (real_len > 0 && line[real_len-1] == '\n')
241 line[--real_len] = '\0';
243 /* Ignore blank lines and comments. */
246 if (line[0] == '#' || strspn (line, whitespace) == real_len)
249 if (line[0] == '[') { /* Section. */
250 if (line[real_len-1] == ']')
251 line[--real_len] = '\0';
253 error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
258 debug ("configuration file: section [%s]", &line[1]);
260 if (!process_section) {
261 error ("%s:%d: unexpected section header ([%s]) in file",
262 path, lineno, &line[1]);
266 if (process_section (path, lineno, &line[1], data) == -1)
269 else { /* Key value */
270 key_len = strcspn (line, whitespace);
271 line[key_len] = '\0';
273 value = key_len < real_len ? &line[key_len+1] : NULL;
275 value += strspn (value, whitespace);
276 if (value[0] == '\0')
280 debug ("configuration file: key '%s', value '%s'", key, value);
282 if (process_line && process_line (path, lineno, key, value, data) == -1)
290 error ("%s: error reading configuration file", path);
293 if (fclose (fp) == EOF) {
294 perrorf ("%s", path);
298 debug ("finished processing configuration file successfully");
302 get_bool (const char *str)
307 if (strcasecmp (str, "on") == 0)
309 if (strcasecmp (str, "off") == 0)
313 case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
315 case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':