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