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 [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
32 " [-n, --dry-run] [-V, --version]\n");
36 * enum opt_value - Tri-state options variables.
39 enum opt_value {opt_undef = 0, opt_yes, opt_no};
42 * struct opts - Values from command line options.
46 enum opt_value no_autoboot;
47 enum opt_value show_help;
49 enum opt_value dry_run;
50 enum opt_value show_version;
54 * opts_parse - Parse the command line options.
57 static int opts_parse(struct opts *opts, int argc, char *argv[])
59 static const struct option long_options[] = {
60 {"no-autoboot", no_argument, NULL, 'a'},
61 {"help", no_argument, NULL, 'h'},
62 {"log", required_argument, NULL, 'l'},
63 {"dry-run", no_argument, NULL, 'n'},
64 {"version", no_argument, NULL, 'V'},
67 static const char short_options[] = "ahl:nV";
68 static const struct opts default_values = {
69 .no_autoboot = opt_no,
70 .log_file = "/var/log/petitboot/pb-discover.log",
74 *opts = default_values;
77 int c = getopt_long(argc, argv, short_options, long_options,
85 opts->no_autoboot = opt_yes;
88 opts->show_help = opt_yes;
91 opts->log_file = optarg;
94 opts->dry_run = opt_yes;
97 opts->show_version = opt_yes;
100 opts->show_help = opt_yes;
105 return optind != argc;
110 static void sigint_handler(int __attribute__((unused)) signum)
115 int main(int argc, char *argv[])
117 struct device_handler *handler;
118 struct discover_server *server;
119 struct network *network;
120 struct waitset *waitset;
122 struct pb_udev *udev;
123 struct user_event *uev;
125 if (opts_parse(&opts, argc, argv)) {
130 if (opts.show_help == opt_yes) {
135 if (opts.show_version == opt_yes) {
140 if (strcmp(opts.log_file, "-")) {
141 FILE *log = fopen(opts.log_file, "a");
144 pb_log_set_stream(log);
146 pb_log_set_stream(stderr);
149 pb_log_always_flush(1);
151 pb_log("--- pb-discover ---\n");
153 /* we look for closed sockets when we write, so ignore SIGPIPE */
154 signal(SIGPIPE, SIG_IGN);
156 signal(SIGINT, sigint_handler);
160 if (opts.no_autoboot == opt_yes)
161 config_set_autoboot(false);
163 waitset = waitset_create(NULL);
165 server = discover_server_init(waitset);
169 network = network_init(server, waitset, opts.dry_run == opt_yes);
173 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
177 discover_server_set_device_source(server, handler);
179 udev = udev_init(waitset, handler);
183 uev = user_event_init(waitset, handler);
187 user_event_trigger(uev);
189 for (running = 1; running;) {
190 if (waiter_poll(waitset))
194 device_handler_destroy(handler);
195 waitset_destroy(waitset);
199 pb_log("--- end ---\n");