Move --dry-run option to discover server
[petitboot] / discover / pb-discover.c
index fa1c28ec75ca169f0690740d9358d4a394eff9b2..c83b5747ee0ded939f69df4bbe5976fe51f9be20 100644 (file)
@@ -7,6 +7,7 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <string.h>
 
 #include <waiter/waiter.h>
 #include <log/log.h>
@@ -42,6 +43,7 @@ struct opts {
        enum opt_value show_help;
        const char *log_file;
        enum opt_value show_version;
+       enum opt_value dry_run;
 };
 
 /**
@@ -53,12 +55,14 @@ 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'},
+               {"dry-run",        no_argument,       NULL, 'n'},
                {"version",        no_argument,       NULL, 'V'},
                { NULL, 0, NULL, 0},
        };
-       static const char short_options[] = "hl:V";
+       static const char short_options[] = "hl:nV";
        static const struct opts default_values = {
                .log_file = "/var/log/petitboot/pb-discover.log",
+               .dry_run = opt_no,
        };
 
        *opts = default_values;
@@ -77,6 +81,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                case 'l':
                        opts->log_file = optarg;
                        break;
+               case 'n':
+                       opts->dry_run = opt_yes;
+                       break;
                case 'V':
                        opts->show_version = opt_yes;
                        break;
@@ -100,10 +107,10 @@ int main(int argc, char *argv[])
 {
        struct device_handler *handler;
        struct discover_server *server;
+       struct waitset *waitset;
        struct opts opts;
        struct udev *udev;
        struct user_event *uev;
-       FILE *log;
 
        if (opts_parse(&opts, argc, argv)) {
                print_usage();
@@ -120,9 +127,13 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
-       log = fopen(opts.log_file, "a");
-       assert(log);
-       pb_log_set_stream(log);
+       if (strcmp(opts.log_file, "-")) {
+               FILE *log = fopen(opts.log_file, "a");
+
+               assert(log);
+               pb_log_set_stream(log);
+       } else
+               pb_log_set_stream(stderr);
 
 #if defined(DEBUG)
        pb_log_always_flush(1);
@@ -134,21 +145,23 @@ int main(int argc, char *argv[])
 
        signal(SIGINT, sigint_handler);
 
-       server = discover_server_init();
+       waitset = waitset_create(NULL);
+
+       server = discover_server_init(waitset);
        if (!server)
                return EXIT_FAILURE;
 
-       handler = device_handler_init(server);
+       handler = device_handler_init(server, opts.dry_run == opt_yes);
        if (!handler)
                return EXIT_FAILURE;
 
        discover_server_set_device_source(server, handler);
 
-       udev = udev_init(handler);
+       udev = udev_init(waitset, handler);
        if (!udev)
                return EXIT_FAILURE;
 
-       uev = user_event_init(handler);
+       uev = user_event_init(waitset, handler);
        if (!uev)
                return EXIT_FAILURE;
 
@@ -156,11 +169,12 @@ int main(int argc, char *argv[])
        user_event_trigger(uev);
 
        for (running = 1; running;) {
-               if (waiter_poll())
+               if (waiter_poll(waitset))
                        break;
        }
 
        device_handler_destroy(handler);
+       waitset_destroy(waitset);
 
        pb_log("--- end ---\n");