Hostinfo day 5: Implement first round of static host commands.
[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 enabled;                  /* is command enabled? */
126 };
127
128 void
129 check_guests_file (struct guest_description *hval, const char *cmd,
130                    double *interval, int *enabled)
131 {
132   struct guests_data *data = apr_palloc (hval->rpool, sizeof *data);
133
134   data->name = hval->name;
135   data->cmd = cmd;
136   data->in_section = 0;
137   data->interval = 60.;         /* default */
138   data->enabled = 0;            /* default */
139
140   process_conf_file (guests_file, 1,
141                      guests_process_line, guests_process_section, data);
142
143   *interval = data->interval;
144   *enabled = data->enabled;
145 }
146
147 static int
148 guests_process_line (const char *path, int lineno,
149                      const char *key, const char *value,
150                      void *datav)
151 {
152   struct guests_data *data = datav;
153   int bool;
154
155   if (!data->in_section)
156     return 0;
157
158   if (strcasecmp (key, "interval") == 0) {
159     if (strcasecmp (value, "any") == 0)
160       data->interval = 0;
161     else {
162       if (sscanf (value, "%lg", &data->interval) != 1) {
163         error ("%s:%d: %s: not a valid decimal number", path, lineno, key);
164         return -1;
165       }
166     }
167   } else if (strcasecmp (key, data->cmd) == 0) {
168     bool = get_bool (value);
169     if (bool == -1) {
170       error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
171       return -1;
172     }
173     data->enabled = bool;
174   }
175
176   return 0;
177 }
178
179 static int
180 guests_process_section (const char *path, int lineno,
181                         const char *section,
182                         void *datav)
183 {
184   struct guests_data *data = datav;
185   int flags = 0;
186
187 #ifdef FNM_CASEFOLD
188   flags |= FNM_CASEFOLD;
189 #endif
190
191   data->in_section = fnmatch (section, data->name, flags) == 0;
192   return 0;
193 }
194
195 /* Configuration file parser. */
196 static void
197 process_conf_file (const char *path, int exit_if_not_exist,
198                    process_line_fn process_line,
199                    process_section_fn process_section,
200                    void *data)
201 {
202   static const char *whitespace = " \t\n\v";
203   FILE *fp;
204   char *line = NULL;
205   int lineno = 0;
206   size_t len = 0;
207   size_t real_len, key_len;
208   ssize_t r;
209   const char *key, *value;
210
211   debug ("begin processing configuration file %s", path);
212
213   fp = fopen (path, "r");
214   if (!fp) {
215     if (exit_if_not_exist) {
216       perrorf ("%s", path);
217       exit (1);
218     } else
219       pwarningf ("%s", path);
220     return;
221   }
222
223   while ((r = getline (&line, &len, fp)) != -1) {
224     lineno++;
225
226     /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/
227
228     /* Remove trailing \n */
229     real_len = strlen (line);
230     if (real_len > 0 && line[real_len-1] == '\n')
231       line[--real_len] = '\0';
232
233     /* Ignore blank lines and comments. */
234     if (real_len == 0)
235       continue;
236     if (line[0] == '#' || strspn (line, whitespace) == real_len)
237       continue;
238
239     if (line[0] == '[') {       /* Section. */
240       if (line[real_len-1] == ']')
241         line[--real_len] = '\0';
242       else {
243         error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
244                path, lineno, line);
245         exit (1);
246       }
247
248       debug ("configuration file: section [%s]", &line[1]);
249
250       if (!process_section) {
251         error ("%s:%d: unexpected section header ([%s]) in file",
252                path, lineno, &line[1]);
253         exit (1);
254       }
255
256       if (process_section (path, lineno, &line[1], data) == -1)
257         exit (1);
258     }
259     else {                      /* Key value */
260       key_len = strcspn (line, whitespace);
261       line[key_len] = '\0';
262       key = line;
263       value = key_len < real_len ? &line[key_len+1] : NULL;
264       if (value) {
265         value += strspn (value, whitespace);
266         if (value[0] == '\0')
267           value = NULL;
268       }
269
270       debug ("configuration file: key '%s', value '%s'", key, value);
271
272       if (process_line && process_line (path, lineno, key, value, data) == -1)
273         exit (1);
274     }
275   }
276
277   free (line);
278
279   if (ferror (fp)) {
280     error ("%s: error reading configuration file", path);
281     exit (1);
282   }
283   if (fclose (fp) == EOF) {
284     perrorf ("%s", path);
285     exit (1);
286   }
287
288   debug ("finished processing configuration file successfully");
289 }
290
291 static int
292 get_bool (const char *str)
293 {
294   if (!str)
295     return -1;
296
297   if (strcasecmp (str, "on") == 0)
298     return 1;
299   if (strcasecmp (str, "off") == 0)
300     return 0;
301
302   switch (str[0]) {
303   case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
304     return 1;
305   case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':
306     return 0;
307   }
308
309   return -1;
310 }