lib/log: Cleanup log API
[petitboot] / discover / pb-discover.c
index 6966ba4408f45e4ff734fc2ca2f6cf8028ff77d3..c16d6903ed5c1da180dfd615beed1950c824b20b 100644 (file)
 
 #include <waiter/waiter.h>
 #include <log/log.h>
+#include <pb-config/pb-config.h>
+#include <process/process.h>
 
 #include "udev.h"
 #include "user-event.h"
 #include "discover-server.h"
 #include "device-handler.h"
+#include "network.h"
 
 static void print_version(void)
 {
@@ -26,7 +29,8 @@ static void print_usage(void)
 {
        print_version();
        printf(
-"Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
+"Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
+"                   [-n, --dry-run] [-V, --version]\n");
 }
 
 /**
@@ -40,10 +44,11 @@ enum opt_value {opt_undef = 0, opt_yes, opt_no};
  */
 
 struct opts {
+       enum opt_value no_autoboot;
        enum opt_value show_help;
        const char *log_file;
-       enum opt_value show_version;
        enum opt_value dry_run;
+       enum opt_value show_version;
 };
 
 /**
@@ -53,14 +58,16 @@ struct opts {
 static int opts_parse(struct opts *opts, int argc, char *argv[])
 {
        static const struct option long_options[] = {
+               {"no-autoboot",    no_argument,       NULL, 'a'},
                {"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:nV";
+       static const char short_options[] = "ahl:nV";
        static const struct opts default_values = {
+               .no_autoboot = opt_no,
                .log_file = "/var/log/petitboot/pb-discover.log",
                .dry_run = opt_no,
        };
@@ -75,6 +82,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                        break;
 
                switch (c) {
+               case 'a':
+                       opts->no_autoboot = opt_yes;
+                       break;
                case 'h':
                        opts->show_help = opt_yes;
                        break;
@@ -107,10 +117,13 @@ int main(int argc, char *argv[])
 {
        struct device_handler *handler;
        struct discover_server *server;
+       struct network *network;
        struct waitset *waitset;
+       struct procset *procset;
        struct opts opts;
        struct pb_udev *udev;
        struct user_event *uev;
+       FILE *log;
 
        if (opts_parse(&opts, argc, argv)) {
                print_usage();
@@ -127,17 +140,17 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
+       log = stderr;
        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);
+               log = fopen(opts.log_file, "a");
+               if (!log) {
+                       fprintf(stderr, "can't open log file %s, logging to "
+                                       "stderr\n", opts.log_file);
+                       log = stderr;
+               }
+       }
+       pb_log_init(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 */
@@ -145,12 +158,25 @@ int main(int argc, char *argv[])
 
        signal(SIGINT, sigint_handler);
 
+       config_init(NULL);
+
+       if (opts.no_autoboot == opt_yes)
+               config_set_autoboot(false);
+
        waitset = waitset_create(NULL);
 
        server = discover_server_init(waitset);
        if (!server)
                return EXIT_FAILURE;
 
+       procset = process_init(server, waitset, opts.dry_run == opt_yes);
+       if (!procset)
+               return EXIT_FAILURE;
+
+       network = network_init(server, waitset, opts.dry_run == opt_yes);
+       if (!network)
+               return EXIT_FAILURE;
+
        handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
        if (!handler)
                return EXIT_FAILURE;
@@ -165,16 +191,14 @@ int main(int argc, char *argv[])
        if (!uev)
                return EXIT_FAILURE;
 
-       user_event_trigger(uev);
-
        for (running = 1; running;) {
                if (waiter_poll(waitset))
                        break;
        }
 
        device_handler_destroy(handler);
-       waitset_destroy(waitset);
        udev_destroy(udev);
+       config_fini();
 
        pb_log("--- end ---\n");