X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fpb-discover.c;h=bd515e327cc9c94d75cd0c9ec3eb9de622a553ca;hp=d2ea7ae1e7b075691e708d4b72f8d94a6557050a;hb=402b446414b71e2052e08a7a879c88493f72c856;hpb=aff253f850e7391afc82d339c0e51ce31e87f307 diff --git a/discover/pb-discover.c b/discover/pb-discover.c index d2ea7ae..bd515e3 100644 --- a/discover/pb-discover.c +++ b/discover/pb-discover.c @@ -1,22 +1,139 @@ +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#include +#include #include #include +#include +#include + #include "udev.h" +#include "user-event.h" #include "discover-server.h" #include "device-handler.h" -#include "waiter.h" -#include "log.h" -int main(void) +static void print_version(void) +{ + printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"); +} + +static void print_usage(void) +{ + print_version(); + printf( +"Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n"); +} + +/** + * enum opt_value - Tri-state options variables. + */ + +enum opt_value {opt_undef = 0, opt_yes, opt_no}; + +/** + * struct opts - Values from command line options. + */ + +struct opts { + enum opt_value show_help; + const char *log_file; + enum opt_value show_version; +}; + +/** + * opts_parse - Parse the command line options. + */ + +static int opts_parse(struct opts *opts, int argc, char *argv[]) +{ + static const struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"log", required_argument, NULL, 'l'}, + {"version", no_argument, NULL, 'V'}, + { NULL, 0, NULL, 0}, + }; + static const char short_options[] = "hl:V"; + static const struct opts default_values = { + .log_file = "pb-discover.log", + }; + + *opts = default_values; + + while (1) { + int c = getopt_long(argc, argv, short_options, long_options, + NULL); + + if (c == EOF) + break; + + switch (c) { + case 'h': + opts->show_help = opt_yes; + break; + case 'l': + opts->log_file = optarg; + break; + case 'V': + opts->show_version = opt_yes; + break; + default: + opts->show_help = opt_yes; + return -1; + } + } + + return optind != argc; +} + +static int running; + +static void sigint_handler(int __attribute__((unused)) signum) +{ + running = 0; +} + +int main(int argc, char *argv[]) { struct device_handler *handler; struct discover_server *server; + struct opts opts; struct udev *udev; + struct user_event *uev; + FILE *log; + + if (opts_parse(&opts, argc, argv)) { + print_usage(); + return EXIT_FAILURE; + } + + if (opts.show_help == opt_yes) { + print_usage(); + return EXIT_SUCCESS; + } + + if (opts.show_version == opt_yes) { + print_version(); + return EXIT_SUCCESS; + } + + log = fopen(opts.log_file, "a"); + assert(log); + pb_log_set_stream(log); + +#if defined(DEBUG) + pb_log_always_flush(1); +#endif + pb_log("--- pb-discover ---\n"); /* we look for closed sockets when we write, so ignore SIGPIPE */ signal(SIGPIPE, SIG_IGN); + signal(SIGINT, sigint_handler); + server = discover_server_init(); if (!server) return EXIT_FAILURE; @@ -31,11 +148,21 @@ int main(void) if (!udev) return EXIT_FAILURE; - for (;;) { + uev = user_event_init(handler); + if (!uev) + return EXIT_FAILURE; + + udev_trigger(udev); + user_event_trigger(uev); + + for (running = 1; running;) { if (waiter_poll()) - return EXIT_FAILURE; + break; } + device_handler_destroy(handler); + + pb_log("--- end ---\n"); return EXIT_SUCCESS; }