Hostinfo day 1: Mainly documentation.
[virt-hostinfo.git] / hostinfod / main.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 <unistd.h>
26
27 #include <apr_general.h>
28 #include <apr_network_io.h>
29 #include <apr_getopt.h>
30
31 #include "hostinfod.h"
32
33 const char *conf_file = DEFAULT_CONF_FILE;
34 const char *socket_dir = DEFAULT_SOCKET_DIR;
35 char *guests_file = NULL;
36 int socket_dir_set_on_cmdline = 0;
37 int debug = 0;
38 int debug_set_on_cmdline = 0;
39 int verbose = 0;
40 int verbose_set_on_cmdline = 0;
41 int foreground = 0;
42 int foreground_set_on_cmdline = 0;
43 apr_pool_t *pool = NULL;
44
45 static void
46 usage (void)
47 {
48   printf ("hostinfod (virt-hostinfo daemon)\n"
49           "Copyright (C) 2009 Red Hat Inc.\n"
50           "\n"
51           "Usage:\n"
52           "  hostinfod [--options]\n"
53           "\n"
54           "Options:\n"
55           "  --help           Display full usage\n"
56           "  -c file | --config file\n"
57           "                   Configuration file (default: %s)\n"
58           "  -d | --debug     Enable debug messages to stderr (implies -v)\n"
59           "  -f | --foreground\n"
60           "                   Run in the foreground (don't fork)\n"
61           "  -s dir | --socketdir dir\n"
62           "                   Socket directory (default: %s)\n"
63           "  -v               Enable verbose messages (sent to syslog, and to\n"
64           "                     stderr if -d option is given)\n",
65           DEFAULT_CONF_FILE, DEFAULT_SOCKET_DIR);
66 }
67
68 int
69 main (int argc, char *argv[])
70 {
71   static const apr_getopt_option_t options[] = {
72     { "config", 'c', TRUE, "configuration file" },
73     { "debug", 'd', FALSE, "enable debug messages to stderr" },
74     { "foreground", 'f', FALSE, "run in foreground (don't fork)" },
75     { "socketdir", 's', TRUE, "socket directory" },
76     { "verbose", 'v', FALSE, "enable verbose messages" },
77     { "help", '?', FALSE, "display help" },
78     { NULL, 0, 0, NULL },
79   };
80   apr_status_t r;
81   apr_getopt_t *opt;
82   int c;
83   const char *optarg;
84
85   apr_initialize ();
86   apr_pool_create (&pool, NULL);
87
88   apr_getopt_init (&opt, pool, argc, argv);
89
90   while ((r = apr_getopt_long (opt, options, &c, &optarg)) == APR_SUCCESS) {
91     switch (c) {
92     case 'c':
93       conf_file = optarg;
94       /* If the user is specifying this on the command line, then
95        * it should exist.  They may have typo'd the name.
96        */
97       if (access (conf_file, R_OK) == -1) {
98         perror (conf_file);
99         exit (1);
100       }
101       break;
102     case 'd':
103       debug = verbose = 1;
104       debug_set_on_cmdline = verbose_set_on_cmdline = 1;
105       break;
106     case 'f':
107       foreground = 1;
108       foreground_set_on_cmdline = 1;
109       break;
110     case 's':
111       socket_dir = optarg;
112       socket_dir_set_on_cmdline = 1;
113       break;
114     case 'v':
115       verbose = 1;
116       verbose_set_on_cmdline = 1;
117       break;
118     case '?':
119       usage ();
120       exit (0);
121     default:
122       abort ();
123     }
124   }
125   if (r != APR_EOF) {
126     fprintf (stderr, "%s: unknown command line option\n", argv[0]);
127     exit (1);
128   }
129
130   read_main_conf_file ();
131
132
133
134
135
136
137
138
139   /* Daemonize. */
140   chdir ("/");
141   if (!foreground)
142     apr_proc_detach (1);
143
144
145
146   apr_terminate ();
147   return 0;
148 }