Hostinfo day 1: Mainly documentation.
[virt-hostinfo.git] / hostinfod / main.c
index bb915ca..ad5b326 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <apr_general.h>
 #include <apr_network_io.h>
 #include <apr_getopt.h>
 
-static void usage (void);
+#include "hostinfod.h"
+
+const char *conf_file = DEFAULT_CONF_FILE;
+const char *socket_dir = DEFAULT_SOCKET_DIR;
+char *guests_file = NULL;
+int socket_dir_set_on_cmdline = 0;
+int debug = 0;
+int debug_set_on_cmdline = 0;
+int verbose = 0;
+int verbose_set_on_cmdline = 0;
+int foreground = 0;
+int foreground_set_on_cmdline = 0;
+apr_pool_t *pool = NULL;
+
+static void
+usage (void)
+{
+  printf ("hostinfod (virt-hostinfo daemon)\n"
+         "Copyright (C) 2009 Red Hat Inc.\n"
+         "\n"
+         "Usage:\n"
+         "  hostinfod [--options]\n"
+         "\n"
+         "Options:\n"
+         "  --help           Display full usage\n"
+         "  -c file | --config file\n"
+         "                   Configuration file (default: %s)\n"
+         "  -d | --debug     Enable debug messages to stderr (implies -v)\n"
+         "  -f | --foreground\n"
+         "                   Run in the foreground (don't fork)\n"
+         "  -s dir | --socketdir dir\n"
+         "                   Socket directory (default: %s)\n"
+         "  -v               Enable verbose messages (sent to syslog, and to\n"
+         "                     stderr if -d option is given)\n",
+         DEFAULT_CONF_FILE, DEFAULT_SOCKET_DIR);
+}
 
 int
 main (int argc, char *argv[])
 {
   static const apr_getopt_option_t options[] = {
+    { "config", 'c', TRUE, "configuration file" },
+    { "debug", 'd', FALSE, "enable debug messages to stderr" },
+    { "foreground", 'f', FALSE, "run in foreground (don't fork)" },
+    { "socketdir", 's', TRUE, "socket directory" },
+    { "verbose", 'v', FALSE, "enable verbose messages" },
     { "help", '?', FALSE, "display help" },
     { NULL, 0, 0, NULL },
   };
   apr_status_t r;
-  apr_pool_t *mp;
   apr_getopt_t *opt;
   int c;
   const char *optarg;
-        
+
   apr_initialize ();
-  apr_pool_create (&mp, NULL);
+  apr_pool_create (&pool, NULL);
 
-  apr_getopt_init (&opt, mp, argc, argv);
+  apr_getopt_init (&opt, pool, argc, argv);
 
   while ((r = apr_getopt_long (opt, options, &c, &optarg)) == APR_SUCCESS) {
     switch (c) {
+    case 'c':
+      conf_file = optarg;
+      /* If the user is specifying this on the command line, then
+       * it should exist.  They may have typo'd the name.
+       */
+      if (access (conf_file, R_OK) == -1) {
+       perror (conf_file);
+       exit (1);
+      }
+      break;
+    case 'd':
+      debug = verbose = 1;
+      debug_set_on_cmdline = verbose_set_on_cmdline = 1;
+      break;
+    case 'f':
+      foreground = 1;
+      foreground_set_on_cmdline = 1;
+      break;
+    case 's':
+      socket_dir = optarg;
+      socket_dir_set_on_cmdline = 1;
+      break;
+    case 'v':
+      verbose = 1;
+      verbose_set_on_cmdline = 1;
+      break;
     case '?':
       usage ();
       exit (0);
@@ -61,21 +127,22 @@ main (int argc, char *argv[])
     exit (1);
   }
 
+  read_main_conf_file ();
+
+
+
+
+
+
+
+
+  /* Daemonize. */
+  chdir ("/");
+  if (!foreground)
+    apr_proc_detach (1);
+
+
+
   apr_terminate ();
   return 0;
 }
-
-static void
-usage (void)
-{
-  printf ("hostinfod (virt-hostinfo daemon)\n"
-         "Copyright (C) 2009 Red Hat Inc.\n"
-         "\n"
-         "Usage:\n"
-         "  hostinfod [-d] [-f]\n"
-         "\n"
-         "Options:\n"
-         "  --help     Display full usage\n"
-         "  -d         Enable verbose debug messages\n"
-         "  -f         Run in the foreground (don't fork)\n");
-}