2 #if defined(HAVE_CONFIG_H)
11 #include <waiter/waiter.h>
15 #include "user-event.h"
16 #include "discover-server.h"
17 #include "device-handler.h"
19 static void print_version(void)
21 printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
24 static void print_usage(void)
28 "Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
32 * enum opt_value - Tri-state options variables.
35 enum opt_value {opt_undef = 0, opt_yes, opt_no};
38 * struct opts - Values from command line options.
42 enum opt_value show_help;
44 enum opt_value show_version;
48 * opts_parse - Parse the command line options.
51 static int opts_parse(struct opts *opts, int argc, char *argv[])
53 static const struct option long_options[] = {
54 {"help", no_argument, NULL, 'h'},
55 {"log", required_argument, NULL, 'l'},
56 {"version", no_argument, NULL, 'V'},
59 static const char short_options[] = "hl:V";
60 static const struct opts default_values = {
61 .log_file = "/var/log/petitboot/pb-discover.log",
64 *opts = default_values;
67 int c = getopt_long(argc, argv, short_options, long_options,
75 opts->show_help = opt_yes;
78 opts->log_file = optarg;
81 opts->show_version = opt_yes;
84 opts->show_help = opt_yes;
89 return optind != argc;
94 static void sigint_handler(int __attribute__((unused)) signum)
99 int main(int argc, char *argv[])
101 struct device_handler *handler;
102 struct discover_server *server;
105 struct user_event *uev;
108 if (opts_parse(&opts, argc, argv)) {
113 if (opts.show_help == opt_yes) {
118 if (opts.show_version == opt_yes) {
123 log = fopen(opts.log_file, "a");
125 pb_log_set_stream(log);
128 pb_log_always_flush(1);
130 pb_log("--- pb-discover ---\n");
132 /* we look for closed sockets when we write, so ignore SIGPIPE */
133 signal(SIGPIPE, SIG_IGN);
135 signal(SIGINT, sigint_handler);
137 server = discover_server_init();
141 handler = device_handler_init(server);
145 discover_server_set_device_source(server, handler);
147 udev = udev_init(handler);
151 uev = user_event_init(handler);
156 user_event_trigger(uev);
158 for (running = 1; running;) {
163 device_handler_destroy(handler);
165 pb_log("--- end ---\n");