discover: Add platform-dummy
[petitboot] / discover / pb-discover.c
index 9330da6129e7ad6165fe29fb3899d897804a228f..c494eeb31ba5e595a15b7df387270c6e1d7b1781 100644 (file)
@@ -7,15 +7,19 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <locale.h>
 #include <string.h>
 
 #include <waiter/waiter.h>
 #include <log/log.h>
+#include <process/process.h>
+#include <talloc/talloc.h>
+#include <i18n/i18n.h>
 
-#include "udev.h"
-#include "user-event.h"
 #include "discover-server.h"
 #include "device-handler.h"
+#include "sysinfo.h"
+#include "platform.h"
 
 static void print_version(void)
 {
@@ -26,7 +30,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, --verbose] [-V, --version]\n");
 }
 
 /**
@@ -40,10 +45,12 @@ 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;
+       enum opt_value verbose;
 };
 
 /**
@@ -53,16 +60,20 @@ 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'},
+               {"verbose",        no_argument,       NULL, 'v'},
                {"version",        no_argument,       NULL, 'V'},
                { NULL, 0, NULL, 0},
        };
-       static const char short_options[] = "hl:nV";
+       static const char short_options[] = "ahl:nvV";
        static const struct opts default_values = {
+               .no_autoboot = opt_no,
                .log_file = "/var/log/petitboot/pb-discover.log",
                .dry_run = opt_no,
+               .verbose = opt_no,
        };
 
        *opts = default_values;
@@ -75,6 +86,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;
@@ -84,6 +98,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                case 'n':
                        opts->dry_run = opt_yes;
                        break;
+               case 'v':
+                       opts->verbose = opt_yes;
+                       break;
                case 'V':
                        opts->show_version = opt_yes;
                        break;
@@ -108,9 +125,13 @@ int main(int argc, char *argv[])
        struct device_handler *handler;
        struct discover_server *server;
        struct waitset *waitset;
+       struct procset *procset;
        struct opts opts;
-       struct pb_udev *udev;
-       struct user_event *uev;
+       FILE *log;
+
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
 
        if (opts_parse(&opts, argc, argv)) {
                print_usage();
@@ -127,17 +148,20 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
+       log = stderr;
        if (strcmp(opts.log_file, "-")) {
-               FILE *log = fopen(opts.log_file, "a");
+               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);
 
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
+       if (opts.verbose == opt_yes)
+               pb_log_set_debug(true);
 
-#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 */
@@ -151,21 +175,27 @@ int main(int argc, char *argv[])
        if (!server)
                return EXIT_FAILURE;
 
-       handler = device_handler_init(server, opts.dry_run == opt_yes);
-       if (!handler)
+       procset = process_init(server, waitset, opts.dry_run == opt_yes);
+       if (!procset)
                return EXIT_FAILURE;
 
-       discover_server_set_device_source(server, handler);
+       platform_init(NULL);
+       if (opts.no_autoboot == opt_yes)
+               config_set_autoboot(false);
 
-       udev = udev_init(waitset, handler);
-       if (!udev)
-               return EXIT_FAILURE;
+       if (config_get()->lang)
+               setlocale(LC_ALL, config_get()->lang);
+
+       if (config_get()->debug)
+               pb_log_set_debug(true);
 
-       uev = user_event_init(waitset, handler);
-       if (!uev)
+       system_info_init(server);
+
+       handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
+       if (!handler)
                return EXIT_FAILURE;
 
-       user_event_trigger(uev);
+       discover_server_set_device_source(server, handler);
 
        for (running = 1; running;) {
                if (waiter_poll(waitset))
@@ -173,10 +203,14 @@ int main(int argc, char *argv[])
        }
 
        device_handler_destroy(handler);
-       waitset_destroy(waitset);
-       udev_destroy(udev);
+       discover_server_destroy(server);
+       platform_fini();
+       talloc_free(waitset);
 
        pb_log("--- end ---\n");
 
+       if (log != stderr)
+               fclose(log);
+
        return EXIT_SUCCESS;
 }