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"
21 static void print_version(void)
23 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
26 static void print_usage(void)
30 "Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
34 * enum opt_value - Tri-state options variables.
37 enum opt_value {opt_undef = 0, opt_yes, opt_no};
40 * struct opts - Values from command line options.
44 enum opt_value show_help;
46 enum opt_value show_version;
47 enum opt_value dry_run;
51 * opts_parse - Parse the command line options.
54 static int opts_parse(struct opts *opts, int argc, char *argv[])
56 static const struct option long_options[] = {
57 {"help", no_argument, NULL, 'h'},
58 {"log", required_argument, NULL, 'l'},
59 {"dry-run", no_argument, NULL, 'n'},
60 {"version", no_argument, NULL, 'V'},
63 static const char short_options[] = "hl:nV";
64 static const struct opts default_values = {
65 .log_file = "/var/log/petitboot/pb-discover.log",
69 *opts = default_values;
72 int c = getopt_long(argc, argv, short_options, long_options,
80 opts->show_help = opt_yes;
83 opts->log_file = optarg;
86 opts->dry_run = opt_yes;
89 opts->show_version = opt_yes;
92 opts->show_help = opt_yes;
97 return optind != argc;
102 static void sigint_handler(int __attribute__((unused)) signum)
107 int main(int argc, char *argv[])
109 struct device_handler *handler;
110 struct discover_server *server;
111 struct waitset *waitset;
113 struct pb_udev *udev;
114 struct user_event *uev;
116 if (opts_parse(&opts, argc, argv)) {
121 if (opts.show_help == opt_yes) {
126 if (opts.show_version == opt_yes) {
131 if (strcmp(opts.log_file, "-")) {
132 FILE *log = fopen(opts.log_file, "a");
135 pb_log_set_stream(log);
137 pb_log_set_stream(stderr);
140 pb_log_always_flush(1);
142 pb_log("--- pb-discover ---\n");
144 /* we look for closed sockets when we write, so ignore SIGPIPE */
145 signal(SIGPIPE, SIG_IGN);
147 signal(SIGINT, sigint_handler);
151 waitset = waitset_create(NULL);
153 server = discover_server_init(waitset);
157 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
161 discover_server_set_device_source(server, handler);
163 udev = udev_init(waitset, handler);
167 uev = user_event_init(waitset, handler);
171 user_event_trigger(uev);
173 for (running = 1; running;) {
174 if (waiter_poll(waitset))
178 device_handler_destroy(handler);
179 waitset_destroy(waitset);
183 pb_log("--- end ---\n");