2 #if defined(HAVE_CONFIG_H)
12 #include <waiter/waiter.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
18 #include "user-event.h"
19 #include "discover-server.h"
20 #include "device-handler.h"
25 static void print_version(void)
27 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
30 static void print_usage(void)
34 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
35 " [-n, --dry-run] [-V, --version]\n");
39 * enum opt_value - Tri-state options variables.
42 enum opt_value {opt_undef = 0, opt_yes, opt_no};
45 * struct opts - Values from command line options.
49 enum opt_value no_autoboot;
50 enum opt_value show_help;
52 enum opt_value dry_run;
53 enum opt_value show_version;
57 * opts_parse - Parse the command line options.
60 static int opts_parse(struct opts *opts, int argc, char *argv[])
62 static const struct option long_options[] = {
63 {"no-autoboot", no_argument, NULL, 'a'},
64 {"help", no_argument, NULL, 'h'},
65 {"log", required_argument, NULL, 'l'},
66 {"dry-run", no_argument, NULL, 'n'},
67 {"version", no_argument, NULL, 'V'},
70 static const char short_options[] = "ahl:nV";
71 static const struct opts default_values = {
72 .no_autoboot = opt_no,
73 .log_file = "/var/log/petitboot/pb-discover.log",
77 *opts = default_values;
80 int c = getopt_long(argc, argv, short_options, long_options,
88 opts->no_autoboot = opt_yes;
91 opts->show_help = opt_yes;
94 opts->log_file = optarg;
97 opts->dry_run = opt_yes;
100 opts->show_version = opt_yes;
103 opts->show_help = opt_yes;
108 return optind != argc;
113 static void sigint_handler(int __attribute__((unused)) signum)
118 int main(int argc, char *argv[])
120 struct device_handler *handler;
121 struct discover_server *server;
122 struct network *network;
123 struct waitset *waitset;
124 struct procset *procset;
126 struct pb_udev *udev;
127 struct user_event *uev;
130 if (opts_parse(&opts, argc, argv)) {
135 if (opts.show_help == opt_yes) {
140 if (opts.show_version == opt_yes) {
146 if (strcmp(opts.log_file, "-")) {
147 log = fopen(opts.log_file, "a");
149 fprintf(stderr, "can't open log file %s, logging to "
150 "stderr\n", opts.log_file);
156 pb_log("--- pb-discover ---\n");
158 /* we look for closed sockets when we write, so ignore SIGPIPE */
159 signal(SIGPIPE, SIG_IGN);
161 signal(SIGINT, sigint_handler);
163 waitset = waitset_create(NULL);
165 server = discover_server_init(waitset);
169 procset = process_init(server, waitset, opts.dry_run == opt_yes);
174 if (opts.no_autoboot == opt_yes)
175 config_set_autoboot(false);
177 system_info_init(server);
179 handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
183 discover_server_set_device_source(server, handler);
185 /* init our device sources: udev, network and user events */
186 udev = udev_init(waitset, handler);
190 network = network_init(handler, waitset, opts.dry_run == opt_yes);
194 uev = user_event_init(waitset, handler);
198 for (running = 1; running;) {
199 if (waiter_poll(waitset))
203 network_shutdown(network);
204 device_handler_destroy(handler);
205 user_event_destroy(uev);
207 discover_server_destroy(server);
209 talloc_free(waitset);
211 pb_log("--- end ---\n");