Hostinfo day 1: Mainly documentation.
[virt-hostinfo.git] / hostinfod / configuration.c
diff --git a/hostinfod/configuration.c b/hostinfod/configuration.c
new file mode 100644 (file)
index 0000000..14a2b80
--- /dev/null
@@ -0,0 +1,102 @@
+/* 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 <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <apr_general.h>
+
+#include "hostinfod.h"
+
+typedef int (*process_line_fn) (const char *key, const char *value);
+static void process_conf_file (const char *path, int exit_if_not_exist, process_line_fn process_line);
+
+/* Read the main configuration file.  This file is NOT reread if it
+ * changes, but the auxiliary guests.conf file IS reread (and so is
+ * the socket directory of course).
+ */
+void
+read_main_conf_file (void)
+{
+  process_conf_file (conf_file, 0, NULL);
+}
+
+static void
+process_conf_file (const char *path, int exit_if_not_exist,
+                  process_line_fn process_line)
+{
+  static const char *whitespace = " \t\n\v";
+  FILE *fp;
+  char *line = NULL;
+  size_t len = 0;
+  size_t real_len, key_len;
+  ssize_t r;
+  const char *key, *value;
+
+  fp = fopen (path, "r");
+  if (!fp) {
+    if (exit_if_not_exist) {
+      perror (path);
+      exit (1);
+    }
+    return;
+  }
+
+  while ((r = getline (&line, &len, fp)) != -1) {
+    /* Remove trailing \n */
+    real_len = len;
+    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;
+
+    /* Split into 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 (line, whitespace);
+      if (value[0] == '\0')
+       value = NULL;
+    }
+
+    if (process_line && process_line (key, value) == -1)
+      exit (1);
+  }
+
+  free (line);
+
+  if (ferror (fp)) {
+    fprintf (stderr, "error reading configuration file: %s\n", path);
+    exit (1);
+  }
+  if (fclose (fp) == EOF) {
+    perror (path);
+    exit (1);
+  }
+}