/* virt-hostinfo * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include "hostinfod.h" typedef int (*process_line_fn) (const char *path, int lineno, const char *key, const char *value, void *data); typedef int (*process_section_fn) (const char *path, int lineno, const char *section, void *data); static void process_conf_file (const char *path, int exit_if_not_exist, process_line_fn process_line, process_section_fn process_section, void *data); static int get_bool (const char *str); /* Read the main configuration file. */ static int main_process_line (const char *path, int lineno, const char *key, const char *value, void *data); void read_main_conf_file (void) { process_conf_file (conf_file, 0, main_process_line, NULL, NULL); } static int main_process_line (const char *path, int lineno, const char *key, const char *value, void *data) { int bool; if (strcasecmp (key, "guests") == 0) { if (!value) { error ("%s:%d: directive is empty: %s", path, lineno, key); return -1; } guests_file = apr_pstrdup (pool, value); } else if (strcasecmp (key, "sockets") == 0) { if (!value) { error ("%s:%d: directive is empty: %s", path, lineno, key); return -1; } socket_dir = apr_pstrdup (pool, value); } else if (strcasecmp (key, "libvirt") == 0) { if (!value) { error ("%s:%d: directive is empty: %s", path, lineno, key); return -1; } if (!libvirt_uri_set_on_cmdline) libvirt_uri = apr_pstrdup (pool, value); } else if (strcasecmp (key, "verbose") == 0) { bool = get_bool (value); if (bool == -1) { error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key); return -1; } if (!verbose_set_on_cmdline) verbose = bool; } else if (strcasecmp (key, "foreground") == 0) { bool = get_bool (value); if (bool == -1) { error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key); return -1; } if (!foreground_set_on_cmdline) foreground = bool; } else { error ("%s:%d: unknown directive in configuration file: %s", path, lineno, key); return -1; } return 0; } /* Check that 'cmd' is enabled for the named guest, and that * it is not being called too frequently. * * XXX Rereading the configuration file each time is possibly * inefficient. * * Returns 0 = proceed, -1 = fail. */ static int guests_process_line (const char *path, int lineno, const char *key, const char *value, void *data); static int guests_process_section (const char *path, int lineno, const char *section, void *data); struct guests_data { const char *name; /* guest "driver-name" */ const char *cmd; /* command being tested */ int in_section; /* currently processing the right section? */ double interval; /* interval for this guest (0 = any) */ int interval_set; /* have we set interval yet? */ int enabled; /* is command enabled? */ int enabled_set; /* have we set enabled flag yet? */ }; void check_guests_file (struct guest_description *hval, const char *cmd, double *interval, int *enabled) { struct guests_data *data = apr_palloc (hval->rpool, sizeof *data); data->name = hval->name; data->cmd = cmd; data->in_section = 0; data->interval = 60.; /* default */ data->interval_set = 0; data->enabled = 0; /* default */ data->enabled_set = 0; process_conf_file (guests_file, 1, guests_process_line, guests_process_section, data); *interval = data->interval; *enabled = data->enabled; } static int guests_process_line (const char *path, int lineno, const char *key, const char *value, void *datav) { struct guests_data *data = datav; int bool; if (!data->in_section) return 0; if (strcasecmp (key, "interval") == 0) { if (!data->interval_set) { if (strcasecmp (value, "any") == 0) data->interval = 0; else { if (sscanf (value, "%lg", &data->interval) != 1) { error ("%s:%d: %s: not a valid decimal number", path, lineno, key); return -1; } } data->interval_set = 1; } } else if (strcasecmp (key, data->cmd) == 0) { if (!data->enabled_set) { bool = get_bool (value); if (bool == -1) { error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key); return -1; } data->enabled = bool; data->enabled_set = 1; } } return 0; } static int guests_process_section (const char *path, int lineno, const char *section, void *datav) { struct guests_data *data = datav; int flags = 0; #ifdef FNM_CASEFOLD flags |= FNM_CASEFOLD; #endif data->in_section = fnmatch (section, data->name, flags) == 0; return 0; } /* Configuration file parser. */ static void process_conf_file (const char *path, int exit_if_not_exist, process_line_fn process_line, process_section_fn process_section, void *data) { static const char *whitespace = " \t\n\v"; FILE *fp; char *line = NULL; int lineno = 0; size_t len = 0; size_t real_len, key_len; ssize_t r; const char *key, *value; debug ("begin processing configuration file %s", path); fp = fopen (path, "r"); if (!fp) { if (exit_if_not_exist) { perrorf ("%s", path); exit (1); } else pwarningf ("%s", path); return; } while ((r = getline (&line, &len, fp)) != -1) { lineno++; /*debug ("%s:%d: '%s' (len = %d)", path, lineno, line, len);*/ /* Remove trailing \n */ real_len = strlen (line); if (real_len > 0 && line[real_len-1] == '\n') line[--real_len] = '\0'; /* Ignore blank lines and comments. */ if (real_len == 0) continue; if (line[0] == '#' || strspn (line, whitespace) == real_len) continue; if (line[0] == '[') { /* Section. */ if (line[real_len-1] == ']') line[--real_len] = '\0'; else { error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s", path, lineno, line); exit (1); } debug ("configuration file: section [%s]", &line[1]); if (!process_section) { error ("%s:%d: unexpected section header ([%s]) in file", path, lineno, &line[1]); exit (1); } if (process_section (path, lineno, &line[1], data) == -1) exit (1); } else { /* Key value */ key_len = strcspn (line, whitespace); line[key_len] = '\0'; key = line; value = key_len < real_len ? &line[key_len+1] : NULL; if (value) { value += strspn (value, whitespace); if (value[0] == '\0') value = NULL; } debug ("configuration file: key '%s', value '%s'", key, value); if (process_line && process_line (path, lineno, key, value, data) == -1) exit (1); } } free (line); if (ferror (fp)) { error ("%s: error reading configuration file", path); exit (1); } if (fclose (fp) == EOF) { perrorf ("%s", path); exit (1); } debug ("finished processing configuration file successfully"); } static int get_bool (const char *str) { if (!str) return -1; if (strcasecmp (str, "on") == 0) return 1; if (strcasecmp (str, "off") == 0) return 0; switch (str[0]) { case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E': return 1; case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D': return 0; } return -1; }