e895f47653945fcef31fc888e2473d632cc21000
[petitboot] / discover / pb-discover.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <getopt.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <string.h>
11
12 #include <waiter/waiter.h>
13 #include <log/log.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
16
17 #include "discover-server.h"
18 #include "device-handler.h"
19 #include "sysinfo.h"
20 #include "platform.h"
21
22 static void print_version(void)
23 {
24         printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
25 }
26
27 static void print_usage(void)
28 {
29         print_version();
30         printf(
31 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
32 "                   [-n, --dry-run] [-v, --verbose] [-V, --version]\n");
33 }
34
35 /**
36  * enum opt_value - Tri-state options variables.
37  */
38
39 enum opt_value {opt_undef = 0, opt_yes, opt_no};
40
41 /**
42  * struct opts - Values from command line options.
43  */
44
45 struct opts {
46         enum opt_value no_autoboot;
47         enum opt_value show_help;
48         const char *log_file;
49         enum opt_value dry_run;
50         enum opt_value show_version;
51         enum opt_value verbose;
52 };
53
54 /**
55  * opts_parse - Parse the command line options.
56  */
57
58 static int opts_parse(struct opts *opts, int argc, char *argv[])
59 {
60         static const struct option long_options[] = {
61                 {"no-autoboot",    no_argument,       NULL, 'a'},
62                 {"help",           no_argument,       NULL, 'h'},
63                 {"log",            required_argument, NULL, 'l'},
64                 {"dry-run",        no_argument,       NULL, 'n'},
65                 {"verbose",        no_argument,       NULL, 'v'},
66                 {"version",        no_argument,       NULL, 'V'},
67                 { NULL, 0, NULL, 0},
68         };
69         static const char short_options[] = "ahl:nvV";
70         static const struct opts default_values = {
71                 .no_autoboot = opt_no,
72                 .log_file = "/var/log/petitboot/pb-discover.log",
73                 .dry_run = opt_no,
74                 .verbose = opt_no,
75         };
76
77         *opts = default_values;
78
79         while (1) {
80                 int c = getopt_long(argc, argv, short_options, long_options,
81                         NULL);
82
83                 if (c == EOF)
84                         break;
85
86                 switch (c) {
87                 case 'a':
88                         opts->no_autoboot = opt_yes;
89                         break;
90                 case 'h':
91                         opts->show_help = opt_yes;
92                         break;
93                 case 'l':
94                         opts->log_file = optarg;
95                         break;
96                 case 'n':
97                         opts->dry_run = opt_yes;
98                         break;
99                 case 'v':
100                         opts->verbose = opt_yes;
101                         break;
102                 case 'V':
103                         opts->show_version = opt_yes;
104                         break;
105                 default:
106                         opts->show_help = opt_yes;
107                         return -1;
108                 }
109         }
110
111         return optind != argc;
112 }
113
114 static int running;
115
116 static void sigint_handler(int __attribute__((unused)) signum)
117 {
118         running = 0;
119 }
120
121 int main(int argc, char *argv[])
122 {
123         struct device_handler *handler;
124         struct discover_server *server;
125         struct waitset *waitset;
126         struct procset *procset;
127         struct opts opts;
128         FILE *log;
129
130         if (opts_parse(&opts, argc, argv)) {
131                 print_usage();
132                 return EXIT_FAILURE;
133         }
134
135         if (opts.show_help == opt_yes) {
136                 print_usage();
137                 return EXIT_SUCCESS;
138         }
139
140         if (opts.show_version == opt_yes) {
141                 print_version();
142                 return EXIT_SUCCESS;
143         }
144
145         log = stderr;
146         if (strcmp(opts.log_file, "-")) {
147                 log = fopen(opts.log_file, "a");
148                 if (!log) {
149                         fprintf(stderr, "can't open log file %s, logging to "
150                                         "stderr\n", opts.log_file);
151                         log = stderr;
152                 }
153         }
154         pb_log_init(log);
155
156         if (opts.verbose)
157                 pb_log_set_debug(true);
158
159         pb_log("--- pb-discover ---\n");
160
161         /* we look for closed sockets when we write, so ignore SIGPIPE */
162         signal(SIGPIPE, SIG_IGN);
163
164         signal(SIGINT, sigint_handler);
165
166         waitset = waitset_create(NULL);
167
168         server = discover_server_init(waitset);
169         if (!server)
170                 return EXIT_FAILURE;
171
172         procset = process_init(server, waitset, opts.dry_run == opt_yes);
173         if (!procset)
174                 return EXIT_FAILURE;
175
176         platform_init(NULL);
177         if (opts.no_autoboot == opt_yes)
178                 config_set_autoboot(false);
179
180         system_info_init(server);
181
182         handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
183         if (!handler)
184                 return EXIT_FAILURE;
185
186         discover_server_set_device_source(server, handler);
187
188         for (running = 1; running;) {
189                 if (waiter_poll(waitset))
190                         break;
191         }
192
193         device_handler_destroy(handler);
194         discover_server_destroy(server);
195         platform_fini();
196         talloc_free(waitset);
197
198         pb_log("--- end ---\n");
199
200         if (log != stderr)
201                 fclose(log);
202
203         return EXIT_SUCCESS;
204 }