Hostinfo day 6: RPM packaging, initscripts, hostinfo-status, hostinfo-test.
[virt-hostinfo.git] / hostinfod / configuration.c
1 /* virt-hostinfo
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fnmatch.h>
28
29 #include <apr_general.h>
30 #include <apr_strings.h>
31
32 #include "hostinfod.h"
33
34 typedef int (*process_line_fn) (const char *path, int lineno,
35                                 const char *key, const char *value,
36                                 void *data);
37 typedef int (*process_section_fn) (const char *path, int lineno,
38                                    const char *section,
39                                    void *data);
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,
43                                void *data);
44 static int get_bool (const char *str);
45
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);
48
49 void
50 read_main_conf_file (void)
51 {
52   process_conf_file (conf_file, 0, main_process_line, NULL, NULL);
53 }
54
55 static int
56 main_process_line (const char *path, int lineno,
57                    const char *key, const char *value,
58                    void *data)
59 {
60   int bool;
61
62   if (strcasecmp (key, "guests") == 0) {
63     if (!value) {
64       error ("%s:%d: directive is empty: %s", path, lineno, key);
65       return -1;
66     }
67     guests_file = apr_pstrdup (pool, value);
68   } else if (strcasecmp (key, "sockets") == 0) {
69     if (!value) {
70       error ("%s:%d: directive is empty: %s", path, lineno, key);
71       return -1;
72     }
73     socket_dir = apr_pstrdup (pool, value);
74   } else if (strcasecmp (key, "libvirt") == 0) {
75     if (!value) {
76       error ("%s:%d: directive is empty: %s", path, lineno, key);
77       return -1;
78     }
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);
83     if (bool == -1) {
84       error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
85       return -1;
86     }
87     if (!verbose_set_on_cmdline)
88       verbose = bool;
89   } else if (strcasecmp (key, "foreground") == 0) {
90     bool = get_bool (value);
91     if (bool == -1) {
92       error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
93       return -1;
94     }
95     if (!foreground_set_on_cmdline)
96       foreground = bool;
97   } else {
98     error ("%s:%d: unknown directive in configuration file: %s",
99            path, lineno, key);
100     return -1;
101   }
102   return 0;
103 }
104
105 /* Check that 'cmd' is enabled for the named guest, and that
106  * it is not being called too frequently.
107  *
108  * XXX Rereading the configuration file each time is possibly
109  * inefficient.
110  *
111  * Returns 0 = proceed, -1 = fail.
112  */
113 static int guests_process_line (const char *path, int lineno,
114                                 const char *key, const char *value,
115                                 void *data);
116 static int guests_process_section (const char *path, int lineno,
117                                    const char *section,
118                                    void *data);
119
120 struct guests_data {
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? */
128 };
129
130 void
131 check_guests_file (struct guest_description *hval, const char *cmd,
132                    double *interval, int *enabled)
133 {
134   struct guests_data *data = apr_palloc (hval->rpool, sizeof *data);
135
136   data->name = hval->name;
137   data->cmd = cmd;
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;
143
144   process_conf_file (guests_file, 1,
145                      guests_process_line, guests_process_section, data);
146
147   *interval = data->interval;
148   *enabled = data->enabled;
149 }
150
151 static int
152 guests_process_line (const char *path, int lineno,
153                      const char *key, const char *value,
154                      void *datav)
155 {
156   struct guests_data *data = datav;
157   int bool;
158
159   if (!data->in_section)
160     return 0;
161
162   if (strcasecmp (key, "interval") == 0) {
163     if (!data->interval_set) {
164       if (strcasecmp (value, "any") == 0)
165         data->interval = 0;
166       else {
167         if (sscanf (value, "%lg", &data->interval) != 1) {
168           error ("%s:%d: %s: not a valid decimal number", path, lineno, key);
169           return -1;
170         }
171       }
172       data->interval_set = 1;
173     }
174   } else if (strcasecmp (key, data->cmd) == 0) {
175     if (!data->enabled_set) {
176       bool = get_bool (value);
177       if (bool == -1) {
178         error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
179         return -1;
180       }
181       data->enabled = bool;
182       data->enabled_set = 1;
183     }
184   }
185
186   return 0;
187 }
188
189 static int
190 guests_process_section (const char *path, int lineno,
191                         const char *section,
192                         void *datav)
193 {
194   struct guests_data *data = datav;
195   int flags = 0;
196
197 #ifdef FNM_CASEFOLD
198   flags |= FNM_CASEFOLD;
199 #endif
200
201   data->in_section = fnmatch (section, data->name, flags) == 0;
202   return 0;
203 }
204
205 /* Configuration file parser. */
206 static void
207 process_conf_file (const char *path, int exit_if_not_exist,
208                    process_line_fn process_line,
209                    process_section_fn process_section,
210                    void *data)
211 {
212   static const char *whitespace = " \t\n\v";
213   FILE *fp;
214   char *line = NULL;
215   int lineno = 0;
216   size_t len = 0;
217   size_t real_len, key_len;
218   ssize_t r;
219   const char *key, *value;
220
221   debug ("begin processing configuration file %s", path);
222
223   fp = fopen (path, "r");
224   if (!fp) {
225     if (exit_if_not_exist) {
226       perrorf ("%s", path);
227       exit (1);
228     } else
229       pwarningf ("%s", path);
230     return;
231   }
232
233   while ((r = getline (&line, &len, fp)) != -1) {
234     lineno++;
235
236     /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/
237
238     /* Remove trailing \n */
239     real_len = strlen (line);
240     if (real_len > 0 && line[real_len-1] == '\n')
241       line[--real_len] = '\0';
242
243     /* Ignore blank lines and comments. */
244     if (real_len == 0)
245       continue;
246     if (line[0] == '#' || strspn (line, whitespace) == real_len)
247       continue;
248
249     if (line[0] == '[') {       /* Section. */
250       if (line[real_len-1] == ']')
251         line[--real_len] = '\0';
252       else {
253         error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
254                path, lineno, line);
255         exit (1);
256       }
257
258       debug ("configuration file: section [%s]", &line[1]);
259
260       if (!process_section) {
261         error ("%s:%d: unexpected section header ([%s]) in file",
262                path, lineno, &line[1]);
263         exit (1);
264       }
265
266       if (process_section (path, lineno, &line[1], data) == -1)
267         exit (1);
268     }
269     else {                      /* Key value */
270       key_len = strcspn (line, whitespace);
271       line[key_len] = '\0';
272       key = line;
273       value = key_len < real_len ? &line[key_len+1] : NULL;
274       if (value) {
275         value += strspn (value, whitespace);
276         if (value[0] == '\0')
277           value = NULL;
278       }
279
280       debug ("configuration file: key '%s', value '%s'", key, value);
281
282       if (process_line && process_line (path, lineno, key, value, data) == -1)
283         exit (1);
284     }
285   }
286
287   free (line);
288
289   if (ferror (fp)) {
290     error ("%s: error reading configuration file", path);
291     exit (1);
292   }
293   if (fclose (fp) == EOF) {
294     perrorf ("%s", path);
295     exit (1);
296   }
297
298   debug ("finished processing configuration file successfully");
299 }
300
301 static int
302 get_bool (const char *str)
303 {
304   if (!str)
305     return -1;
306
307   if (strcasecmp (str, "on") == 0)
308     return 1;
309   if (strcasecmp (str, "off") == 0)
310     return 0;
311
312   switch (str[0]) {
313   case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
314     return 1;
315   case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':
316     return 0;
317   }
318
319   return -1;
320 }