2 #if defined(HAVE_CONFIG_H)
12 #include <waiter/waiter.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
16 #include <i18n/i18n.h>
18 #include "discover-server.h"
19 #include "device-handler.h"
23 static void print_version(void)
25 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
28 static void print_usage(void)
32 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
33 " [-n, --dry-run] [-v, --verbose] [-V, --version]\n");
37 * enum opt_value - Tri-state options variables.
40 enum opt_value {opt_undef = 0, opt_yes, opt_no};
43 * struct opts - Values from command line options.
47 enum opt_value no_autoboot;
48 enum opt_value show_help;
50 enum opt_value dry_run;
51 enum opt_value show_version;
52 enum opt_value verbose;
56 * opts_parse - Parse the command line options.
59 static int opts_parse(struct opts *opts, int argc, char *argv[])
61 static const struct option long_options[] = {
62 {"no-autoboot", no_argument, NULL, 'a'},
63 {"help", no_argument, NULL, 'h'},
64 {"log", required_argument, NULL, 'l'},
65 {"dry-run", no_argument, NULL, 'n'},
66 {"verbose", no_argument, NULL, 'v'},
67 {"version", no_argument, NULL, 'V'},
70 static const char short_options[] = "ahl:nvV";
71 static const struct opts default_values = {
72 .no_autoboot = opt_no,
73 .log_file = "/var/log/petitboot/pb-discover.log",
78 *opts = default_values;
81 int c = getopt_long(argc, argv, short_options, long_options,
89 opts->no_autoboot = opt_yes;
92 opts->show_help = opt_yes;
95 opts->log_file = optarg;
98 opts->dry_run = opt_yes;
101 opts->verbose = opt_yes;
104 opts->show_version = opt_yes;
107 opts->show_help = opt_yes;
112 return optind != argc;
117 static void sigint_handler(int __attribute__((unused)) signum)
122 int main(int argc, char *argv[])
124 struct device_handler *handler;
125 struct discover_server *server;
126 struct waitset *waitset;
127 struct procset *procset;
131 setlocale(LC_ALL, "");
132 bindtextdomain(PACKAGE, LOCALEDIR);
135 if (opts_parse(&opts, argc, argv)) {
140 if (opts.show_help == opt_yes) {
145 if (opts.show_version == opt_yes) {
151 if (strcmp(opts.log_file, "-")) {
152 log = fopen(opts.log_file, "a");
154 fprintf(stderr, "can't open log file %s, logging to "
155 "stderr\n", opts.log_file);
161 if (opts.verbose == opt_yes)
162 pb_log_set_debug(true);
164 pb_log("--- pb-discover ---\n");
166 /* we look for closed sockets when we write, so ignore SIGPIPE */
167 signal(SIGPIPE, SIG_IGN);
169 signal(SIGINT, sigint_handler);
171 waitset = waitset_create(NULL);
173 server = discover_server_init(waitset);
177 procset = process_init(server, waitset, opts.dry_run == opt_yes);
182 if (opts.no_autoboot == opt_yes)
183 config_set_autoboot(false);
185 if (config_get()->lang)
186 setlocale(LC_ALL, config_get()->lang);
188 if (config_get()->debug)
189 pb_log_set_debug(true);
191 system_info_init(server);
193 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
197 discover_server_set_device_source(server, handler);
199 for (running = 1; running;) {
200 if (waiter_poll(waitset))
204 device_handler_destroy(handler);
205 discover_server_destroy(server);
207 talloc_free(waitset);
209 pb_log("--- end ---\n");