2 #if defined(HAVE_CONFIG_H)
12 #include <waiter/waiter.h>
16 #include "user-event.h"
17 #include "discover-server.h"
18 #include "device-handler.h"
20 static void print_version(void)
22 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
25 static void print_usage(void)
29 "Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
33 * enum opt_value - Tri-state options variables.
36 enum opt_value {opt_undef = 0, opt_yes, opt_no};
39 * struct opts - Values from command line options.
43 enum opt_value show_help;
45 enum opt_value show_version;
46 enum opt_value dry_run;
50 * opts_parse - Parse the command line options.
53 static int opts_parse(struct opts *opts, int argc, char *argv[])
55 static const struct option long_options[] = {
56 {"help", no_argument, NULL, 'h'},
57 {"log", required_argument, NULL, 'l'},
58 {"dry-run", no_argument, NULL, 'n'},
59 {"version", no_argument, NULL, 'V'},
62 static const char short_options[] = "hl:nV";
63 static const struct opts default_values = {
64 .log_file = "/var/log/petitboot/pb-discover.log",
68 *opts = default_values;
71 int c = getopt_long(argc, argv, short_options, long_options,
79 opts->show_help = opt_yes;
82 opts->log_file = optarg;
85 opts->dry_run = opt_yes;
88 opts->show_version = opt_yes;
91 opts->show_help = opt_yes;
96 return optind != argc;
101 static void sigint_handler(int __attribute__((unused)) signum)
106 int main(int argc, char *argv[])
108 struct device_handler *handler;
109 struct discover_server *server;
110 struct waitset *waitset;
113 struct user_event *uev;
115 if (opts_parse(&opts, argc, argv)) {
120 if (opts.show_help == opt_yes) {
125 if (opts.show_version == opt_yes) {
130 if (strcmp(opts.log_file, "-")) {
131 FILE *log = fopen(opts.log_file, "a");
134 pb_log_set_stream(log);
136 pb_log_set_stream(stderr);
139 pb_log_always_flush(1);
141 pb_log("--- pb-discover ---\n");
143 /* we look for closed sockets when we write, so ignore SIGPIPE */
144 signal(SIGPIPE, SIG_IGN);
146 signal(SIGINT, sigint_handler);
148 waitset = waitset_create(NULL);
150 server = discover_server_init(waitset);
154 handler = device_handler_init(server, opts.dry_run == opt_yes);
158 discover_server_set_device_source(server, handler);
160 udev = udev_init(waitset, handler);
164 uev = user_event_init(waitset, handler);
169 user_event_trigger(uev);
171 for (running = 1; running;) {
172 if (waiter_poll(waitset))
176 device_handler_destroy(handler);
177 waitset_destroy(waitset);
180 pb_log("--- end ---\n");