Merge branch 'master' of git+ssh://g-rjones@et.redhat.com/git/virt-hostinfo
[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
28 #include <apr_general.h>
29 #include <apr_strings.h>
30
31 #include "hostinfod.h"
32
33 typedef int (*process_line_fn) (const char *path, int lineno,
34                                 const char *key, const char *value);
35 typedef int (*process_section_fn) (const char *path, int lineno,
36                                    const char *section);
37 static void process_conf_file (const char *path, int exit_if_not_exist,
38                                process_line_fn process_line,
39                                process_section_fn process_section);
40 static int get_bool (const char *str);
41
42 /* Read the main configuration file. */
43 static int main_process_line (const char *path, int lineno, const char *key, const char *value);
44
45 void
46 read_main_conf_file (void)
47 {
48   process_conf_file (conf_file, 0, main_process_line, NULL);
49 }
50
51 static int
52 main_process_line (const char *path, int lineno,
53                    const char *key, const char *value)
54 {
55   int bool;
56
57   if (strcasecmp (key, "guests") == 0) {
58     if (!value) {
59       error ("%s:%d: directive is empty: %s", path, lineno, key);
60       return -1;
61     }
62     guests_file = apr_pstrdup (pool, value);
63   } else if (strcasecmp (key, "sockets") == 0) {
64     if (!value) {
65       error ("%s:%d: directive is empty: %s", path, lineno, key);
66       return -1;
67     }
68     socket_dir = apr_pstrdup (pool, value);
69   } else if (strcasecmp (key, "verbose") == 0) {
70     bool = get_bool (value);
71     if (bool == -1) {
72       error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
73       return -1;
74     }
75     if (!verbose_set_on_cmdline)
76       verbose = bool;
77   } else if (strcasecmp (key, "foreground") == 0) {
78     bool = get_bool (value);
79     if (bool == -1) {
80       error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
81       return -1;
82     }
83     if (!foreground_set_on_cmdline)
84       foreground = bool;
85   } else {
86     error ("%s:%d: unknown directive in configuration file: %s",
87            path, lineno, key);
88     return -1;
89   }
90   return 0;
91 }
92
93 /* Configuration file parser. */
94 static void
95 process_conf_file (const char *path, int exit_if_not_exist,
96                    process_line_fn process_line,
97                    process_section_fn process_section)
98 {
99   static const char *whitespace = " \t\n\v";
100   FILE *fp;
101   char *line = NULL;
102   int lineno = 0;
103   size_t len = 0;
104   size_t real_len, key_len;
105   ssize_t r;
106   const char *key, *value;
107
108   debug ("begin processing configuration file %s", path);
109
110   fp = fopen (path, "r");
111   if (!fp) {
112     if (exit_if_not_exist) {
113       perrorf ("%s", path);
114       exit (1);
115     } else
116       pwarningf ("%s", path);
117     return;
118   }
119
120   while ((r = getline (&line, &len, fp)) != -1) {
121     lineno++;
122
123     /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/
124
125     /* Remove trailing \n */
126     real_len = strlen (line);
127     if (real_len > 0 && line[real_len-1] == '\n')
128       line[--real_len] = '\0';
129
130     /* Ignore blank lines and comments. */
131     if (real_len == 0)
132       continue;
133     if (line[0] == '#' || strspn (line, whitespace) == real_len)
134       continue;
135
136     if (line[0] == '[') {       /* Section. */
137       if (line[real_len-1] == ']')
138         line[--real_len] = '\0';
139       else {
140         error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
141                path, lineno, line);
142         exit (1);
143       }
144
145       debug ("configuration file: section [%s]", line);
146
147       if (!process_section) {
148         error ("%s:%d: unexpected section header ([%s]) in file",
149                path, lineno, line);
150         exit (1);
151       }
152
153       if (process_section (path, lineno, line) == -1)
154         exit (1);
155     }
156     else {                      /* Key value */
157       key_len = strcspn (line, whitespace);
158       line[key_len] = '\0';
159       key = line;
160       value = key_len < real_len ? &line[key_len+1] : NULL;
161       if (value) {
162         value += strspn (line, whitespace);
163         if (value[0] == '\0')
164           value = NULL;
165       }
166
167       debug ("configuration file: key '%s', value '%s'", key, value);
168
169       if (process_line && process_line (path, lineno, key, value) == -1)
170         exit (1);
171     }
172   }
173
174   free (line);
175
176   if (ferror (fp)) {
177     error ("%s: error reading configuration file", path);
178     exit (1);
179   }
180   if (fclose (fp) == EOF) {
181     perrorf ("%s", path);
182     exit (1);
183   }
184
185   debug ("finished processing configuration file successfully");
186 }
187
188 static int
189 get_bool (const char *str)
190 {
191   if (!str)
192     return -1;
193
194   if (strcasecmp (str, "on") == 0)
195     return 1;
196   if (strcasecmp (str, "off") == 0)
197     return 0;
198
199   switch (str[0]) {
200   case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
201     return 1;
202   case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':
203     return 0;
204   }
205
206   return -1;
207 }