2 #if defined(HAVE_CONFIG_H)
13 #include <waiter/waiter.h>
15 #include <process/process.h>
16 #include <talloc/talloc.h>
17 #include <i18n/i18n.h>
19 #include "discover-server.h"
20 #include "device-handler.h"
24 static void print_version(void)
26 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
29 static void print_usage(void)
33 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
34 " [-n, --dry-run] [-v, --verbose] [-V, --version]\n");
38 * enum opt_value - Tri-state options variables.
41 enum opt_value {opt_undef = 0, opt_yes, opt_no};
44 * struct opts - Values from command line options.
48 enum opt_value no_autoboot;
49 enum opt_value show_help;
51 enum opt_value dry_run;
52 enum opt_value show_version;
53 enum opt_value verbose;
57 * opts_parse - Parse the command line options.
60 static int opts_parse(struct opts *opts, int argc, char *argv[])
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'},
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",
79 *opts = default_values;
82 int c = getopt_long(argc, argv, short_options, long_options,
90 opts->no_autoboot = opt_yes;
93 opts->show_help = opt_yes;
96 opts->log_file = optarg;
99 opts->dry_run = opt_yes;
102 opts->verbose = opt_yes;
105 opts->show_version = opt_yes;
108 opts->show_help = opt_yes;
113 return optind != argc;
118 static void sigint_handler(int __attribute__((unused)) signum)
123 int main(int argc, char *argv[])
125 struct device_handler *handler;
126 struct discover_server *server;
127 struct waitset *waitset;
128 struct procset *procset;
132 setlocale(LC_ALL, "");
133 bindtextdomain(PACKAGE, LOCALEDIR);
136 if (opts_parse(&opts, argc, argv)) {
141 if (opts.show_help == opt_yes) {
146 if (opts.show_version == opt_yes) {
152 if (strcmp(opts.log_file, "-")) {
153 log = fopen(opts.log_file, "a");
155 fprintf(stderr, "can't open log file %s, logging to "
156 "stderr\n", opts.log_file);
162 if (opts.verbose == opt_yes)
163 pb_log_set_debug(true);
165 pb_log("--- pb-discover ---\n");
167 /* we look for closed sockets when we write, so ignore SIGPIPE */
168 signal(SIGPIPE, SIG_IGN);
170 signal(SIGINT, sigint_handler);
172 waitset = waitset_create(NULL);
174 server = discover_server_init(waitset);
178 procset = process_init(server, waitset, opts.dry_run == opt_yes);
183 if (opts.no_autoboot == opt_yes)
184 config_set_autoboot(false);
186 if (config_get()->lang)
187 setlocale(LC_ALL, config_get()->lang);
189 if (config_get()->debug)
190 pb_log_set_debug(true);
192 system_info_init(server);
194 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
198 discover_server_set_device_source(server, handler);
200 for (running = 1; running;) {
201 if (waiter_poll(waitset))
205 device_handler_destroy(handler);
206 discover_server_destroy(server);
208 talloc_free(waitset);
210 pb_log("--- end ---\n");