2 #if defined(HAVE_CONFIG_H)
12 #include <waiter/waiter.h>
14 #include <pb-config/pb-config.h>
17 #include "user-event.h"
18 #include "discover-server.h"
19 #include "device-handler.h"
22 static void print_version(void)
24 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
27 static void print_usage(void)
31 "Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
35 * enum opt_value - Tri-state options variables.
38 enum opt_value {opt_undef = 0, opt_yes, opt_no};
41 * struct opts - Values from command line options.
45 enum opt_value show_help;
47 enum opt_value show_version;
48 enum opt_value dry_run;
52 * opts_parse - Parse the command line options.
55 static int opts_parse(struct opts *opts, int argc, char *argv[])
57 static const struct option long_options[] = {
58 {"help", no_argument, NULL, 'h'},
59 {"log", required_argument, NULL, 'l'},
60 {"dry-run", no_argument, NULL, 'n'},
61 {"version", no_argument, NULL, 'V'},
64 static const char short_options[] = "hl:nV";
65 static const struct opts default_values = {
66 .log_file = "/var/log/petitboot/pb-discover.log",
70 *opts = default_values;
73 int c = getopt_long(argc, argv, short_options, long_options,
81 opts->show_help = opt_yes;
84 opts->log_file = optarg;
87 opts->dry_run = opt_yes;
90 opts->show_version = opt_yes;
93 opts->show_help = opt_yes;
98 return optind != argc;
103 static void sigint_handler(int __attribute__((unused)) signum)
108 int main(int argc, char *argv[])
110 struct device_handler *handler;
111 struct discover_server *server;
112 struct network *network;
113 struct waitset *waitset;
115 struct pb_udev *udev;
116 struct user_event *uev;
118 if (opts_parse(&opts, argc, argv)) {
123 if (opts.show_help == opt_yes) {
128 if (opts.show_version == opt_yes) {
133 if (strcmp(opts.log_file, "-")) {
134 FILE *log = fopen(opts.log_file, "a");
137 pb_log_set_stream(log);
139 pb_log_set_stream(stderr);
142 pb_log_always_flush(1);
144 pb_log("--- pb-discover ---\n");
146 /* we look for closed sockets when we write, so ignore SIGPIPE */
147 signal(SIGPIPE, SIG_IGN);
149 signal(SIGINT, sigint_handler);
153 waitset = waitset_create(NULL);
155 server = discover_server_init(waitset);
159 network = network_init(server, waitset, opts.dry_run == opt_yes);
163 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
167 discover_server_set_device_source(server, handler);
169 udev = udev_init(waitset, handler);
173 uev = user_event_init(waitset, handler);
177 user_event_trigger(uev);
179 for (running = 1; running;) {
180 if (waiter_poll(waitset))
184 device_handler_destroy(handler);
185 waitset_destroy(waitset);
189 pb_log("--- end ---\n");