2 #if defined(HAVE_CONFIG_H)
12 #include <waiter/waiter.h>
14 #include <pb-config/pb-config.h>
15 #include <process/process.h>
18 #include "user-event.h"
19 #include "discover-server.h"
20 #include "device-handler.h"
23 static void print_version(void)
25 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
28 static void print_usage(void)
32 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
33 " [-n, --dry-run] [-V, --version]\n");
37 * enum opt_value - Tri-state options variables.
40 enum opt_value {opt_undef = 0, opt_yes, opt_no};
43 * struct opts - Values from command line options.
47 enum opt_value no_autoboot;
48 enum opt_value show_help;
50 enum opt_value dry_run;
51 enum opt_value show_version;
55 * opts_parse - Parse the command line options.
58 static int opts_parse(struct opts *opts, int argc, char *argv[])
60 static const struct option long_options[] = {
61 {"no-autoboot", no_argument, NULL, 'a'},
62 {"help", no_argument, NULL, 'h'},
63 {"log", required_argument, NULL, 'l'},
64 {"dry-run", no_argument, NULL, 'n'},
65 {"version", no_argument, NULL, 'V'},
68 static const char short_options[] = "ahl:nV";
69 static const struct opts default_values = {
70 .no_autoboot = opt_no,
71 .log_file = "/var/log/petitboot/pb-discover.log",
75 *opts = default_values;
78 int c = getopt_long(argc, argv, short_options, long_options,
86 opts->no_autoboot = opt_yes;
89 opts->show_help = opt_yes;
92 opts->log_file = optarg;
95 opts->dry_run = opt_yes;
98 opts->show_version = opt_yes;
101 opts->show_help = opt_yes;
106 return optind != argc;
111 static void sigint_handler(int __attribute__((unused)) signum)
116 int main(int argc, char *argv[])
118 struct device_handler *handler;
119 struct discover_server *server;
120 struct network *network;
121 struct waitset *waitset;
122 struct procset *procset;
124 struct pb_udev *udev;
125 struct user_event *uev;
128 if (opts_parse(&opts, argc, argv)) {
133 if (opts.show_help == opt_yes) {
138 if (opts.show_version == opt_yes) {
144 if (strcmp(opts.log_file, "-")) {
145 log = fopen(opts.log_file, "a");
147 fprintf(stderr, "can't open log file %s, logging to "
148 "stderr\n", opts.log_file);
154 pb_log("--- pb-discover ---\n");
156 /* we look for closed sockets when we write, so ignore SIGPIPE */
157 signal(SIGPIPE, SIG_IGN);
159 signal(SIGINT, sigint_handler);
161 if (opts.no_autoboot == opt_yes)
162 config_set_autoboot(false);
164 waitset = waitset_create(NULL);
166 server = discover_server_init(waitset);
170 procset = process_init(server, waitset, opts.dry_run == opt_yes);
176 network = network_init(server, waitset, opts.dry_run == opt_yes);
180 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
184 discover_server_set_device_source(server, handler);
186 udev = udev_init(waitset, handler);
190 uev = user_event_init(waitset, handler);
194 for (running = 1; running;) {
195 if (waiter_poll(waitset))
199 device_handler_destroy(handler);
203 pb_log("--- end ---\n");