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"
24 static void print_version(void)
26 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
29 static void print_usage(void)
33 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
34 " [-n, --dry-run] [-V, --version]\n");
38 * enum opt_value - Tri-state options variables.
41 enum opt_value {opt_undef = 0, opt_yes, opt_no};
44 * struct opts - Values from command line options.
48 enum opt_value no_autoboot;
49 enum opt_value show_help;
51 enum opt_value dry_run;
52 enum opt_value show_version;
56 * opts_parse - Parse the command line options.
59 static int opts_parse(struct opts *opts, int argc, char *argv[])
61 static const struct option long_options[] = {
62 {"no-autoboot", no_argument, NULL, 'a'},
63 {"help", no_argument, NULL, 'h'},
64 {"log", required_argument, NULL, 'l'},
65 {"dry-run", no_argument, NULL, 'n'},
66 {"version", no_argument, NULL, 'V'},
69 static const char short_options[] = "ahl:nV";
70 static const struct opts default_values = {
71 .no_autoboot = opt_no,
72 .log_file = "/var/log/petitboot/pb-discover.log",
76 *opts = default_values;
79 int c = getopt_long(argc, argv, short_options, long_options,
87 opts->no_autoboot = opt_yes;
90 opts->show_help = opt_yes;
93 opts->log_file = optarg;
96 opts->dry_run = opt_yes;
99 opts->show_version = opt_yes;
102 opts->show_help = opt_yes;
107 return optind != argc;
112 static void sigint_handler(int __attribute__((unused)) signum)
117 int main(int argc, char *argv[])
119 struct device_handler *handler;
120 struct discover_server *server;
121 struct network *network;
122 struct waitset *waitset;
123 struct procset *procset;
125 struct pb_udev *udev;
126 struct user_event *uev;
129 if (opts_parse(&opts, argc, argv)) {
134 if (opts.show_help == opt_yes) {
139 if (opts.show_version == opt_yes) {
145 if (strcmp(opts.log_file, "-")) {
146 log = fopen(opts.log_file, "a");
148 fprintf(stderr, "can't open log file %s, logging to "
149 "stderr\n", opts.log_file);
155 pb_log("--- pb-discover ---\n");
157 /* we look for closed sockets when we write, so ignore SIGPIPE */
158 signal(SIGPIPE, SIG_IGN);
160 signal(SIGINT, sigint_handler);
162 waitset = waitset_create(NULL);
164 server = discover_server_init(waitset);
168 procset = process_init(server, waitset, opts.dry_run == opt_yes);
173 if (opts.no_autoboot == opt_yes)
174 config_set_autoboot(false);
176 system_info_init(server);
178 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
182 discover_server_set_device_source(server, handler);
184 /* init our device sources: udev, network and user events */
185 udev = udev_init(waitset, handler);
189 network = network_init(handler, waitset, opts.dry_run == opt_yes);
193 uev = user_event_init(waitset, handler);
197 for (running = 1; running;) {
198 if (waiter_poll(waitset))
202 device_handler_destroy(handler);
206 pb_log("--- end ---\n");