]> git.ozlabs.org Git - petitboot/commitdiff
Cleanup --dry-run option code
authorGeoff Levand <geoff@infradead.org>
Fri, 30 Mar 2012 23:58:32 +0000 (16:58 -0700)
committerGeoff Levand <geoff@infradead.org>
Sat, 31 Mar 2012 01:08:57 +0000 (18:08 -0700)
Signed-off-by: Geoff Levand <geoff@infradead.org>
discover/device-handler.c
lib/system/system.c
lib/system/system.h
ui/common/loader.c
ui/common/ui-system.c
ui/ncurses/generic-main.c
ui/ncurses/nc-cui.c
ui/ncurses/nc-cui.h
ui/ncurses/ps3-main.c
ui/twin/pbt-client.c

index 12bc40f7283a83bf24dac77e3e4f0f353deb55af..6e03ef3fa159136b6015983a0054411cb7f126c8 100644 (file)
@@ -201,7 +201,7 @@ static int mount_device(struct discover_context *ctx)
        argv[4] = "ro";
        argv[5] = NULL;
 
-       if (pb_run_cmd(argv, 1)) {
+       if (pb_run_cmd(argv, 1, 0)) {
 
                /* Retry mount without ro option. */
 
@@ -210,7 +210,7 @@ static int mount_device(struct discover_context *ctx)
                argv[2] = ctx->mount_path;
                argv[3] = NULL;
 
-               if (pb_run_cmd(argv, 1))
+               if (pb_run_cmd(argv, 1, 0))
                        goto out_rmdir;
        }
 
index 1b506d2edaf6b5e84f9ea4211e0cac1b189f3d31..d77159d5a7b882470648c740aa301b0314fd67c6 100644 (file)
@@ -103,9 +103,11 @@ int pb_rmdir_recursive(const char *base, const char *dir)
 /**
  * pb_run_cmd - Run the supplied command.
  * @cmd_argv: An argument list array for execv.
+ * @wait: Wait for the child process to complete before returning.
+ * @dry_run: Don't actually fork and exec.
  */
 
-int pb_run_cmd(const char *const *cmd_argv, int wait)
+int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
 {
 #if defined(DEBUG)
        enum {do_debug = 1};
@@ -118,14 +120,19 @@ int pb_run_cmd(const char *const *cmd_argv, int wait)
        if (do_debug) {
                const char *const *p = cmd_argv;
 
-               pb_log("%s: ", __func__);
+               pb_log("%s: %s", __func__, (dry_run ? "(dry-run) " : ""));
+
                while (*p) {
                        pb_log("%s ", *p);
                        p++;
                }
                pb_log("\n");
        } else
-               pb_log("%s: %s\n", __func__, cmd_argv[0]);
+               pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
+                       cmd_argv[0]);
+
+       if (dry_run)
+               return 0;
 
        pid = fork();
 
index d39280d1bcb7d10f58bd92921a098907be8a08b4..f8f18a3cdbe0e8cbee8e5b12b7819b988e9617e1 100644 (file)
@@ -15,7 +15,7 @@ struct pb_system_apps {
 
 extern const struct pb_system_apps pb_system_apps;
 
-int pb_run_cmd(const char *const *cmd_argv, int wait);
+int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run);
 int pb_mkdir_recursive(const char *dir);
 int pb_rmdir_recursive(const char *base, const char *dir);
 
index 009871d3109ee348f5704bd530bc0e4422903382..775f2111c281b25381906dc95c8c79b88087c446 100644 (file)
@@ -94,7 +94,7 @@ static char *pb_load_nfs(void *ctx, struct pb_url *url)
        *p++ = local;                   /* 7 */
        *p++ = NULL;                    /* 8 */
 
-       result = pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, 0);
 
        talloc_free(opts);
 
@@ -138,7 +138,7 @@ static char *pb_load_sftp(void *ctx, struct pb_url __attribute__((unused)) *url)
        *p++ = local;                   /* 4 */
        *p++ = NULL;                    /* 5 */
 
-       result = pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, 0);
 
        if (result)
                goto fail;
@@ -183,7 +183,7 @@ static char *pb_load_tftp(void *ctx, struct pb_url *url)
                *p++ = url->port;       /* 8 */
        *p++ = NULL;                    /* 9 */
 
-       result = pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, 0);
 
        if (!result)
                return local;
@@ -203,7 +203,7 @@ static char *pb_load_tftp(void *ctx, struct pb_url *url)
        *p++ = local;                   /* 9 */
        *p++ = NULL;                    /* 10 */
 
-       result = pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, 0);
 
        if (!result)
                return local;
@@ -248,7 +248,7 @@ static char *pb_load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
                *p++ = "--no-check-certificate";        /* 6 */
        *p++ = NULL;                                    /* 7 */
 
-       result = pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, 0);
 
        if (result)
                goto fail;
index 4b0f144e41eb2d64cce7812e421236e4cb761042..b4ae8f8c101fd0cf1ea3b66dfe513656eb1e2691 100644 (file)
@@ -46,7 +46,7 @@ int pb_start_daemon(void)
        argv[0] = name;
        argv[1] =  NULL;
 
-       result = pb_run_cmd(argv, 0);
+       result = pb_run_cmd(argv, 0, 0);
 
        talloc_free(name);
 
@@ -91,7 +91,7 @@ static int kexec_load(const char *l_image, const char *l_initrd,
        *p++ = l_image;                 /* 5 */
        *p++ = NULL;                    /* 6 */
 
-       result = dry_run ? 0 : pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, dry_run);
 
        if (result)
                pb_log("%s: failed: (%d)\n", __func__, result);
@@ -122,7 +122,7 @@ static int kexec_reboot(int dry_run)
        *p++ =  "now";                  /* 3 */
        *p++ =  NULL;                   /* 4 */
 
-       result = dry_run ? 0 : pb_run_cmd(argv, 1);
+       result = pb_run_cmd(argv, 1, dry_run);
 
        /* On error, force a kexec with the -e option */
 
@@ -132,7 +132,7 @@ static int kexec_reboot(int dry_run)
                *p++ = "-e";                    /* 2 */
                *p++ = NULL;                    /* 3 */
 
-               result = pb_run_cmd(argv, 1);
+               result = pb_run_cmd(argv, 1, 0);
        }
 
        if (result)
@@ -156,7 +156,6 @@ int pb_run_kexec(const struct pb_kexec_data *kd, int dry_run)
        pb_log("%s: image:   '%s'\n", __func__, kd->image);
        pb_log("%s: initrd:  '%s'\n", __func__, kd->initrd);
        pb_log("%s: args:    '%s'\n", __func__, kd->args);
-       pb_log("%s: dry_run: '%d'\n", __func__, dry_run);
 
        result = -1;
 
index 9a22265811bd3c87cb49e4191a817160d24d2ff0..dfeb1ba6217a331dd3ce181d1b49282ea781e0b7 100644 (file)
@@ -129,7 +129,6 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
 struct pb_cui {
        struct pmenu *mm;
        struct cui *cui;
-       struct opts opts;
 };
 
 static struct pb_cui *pb_from_cui(struct cui *cui)
@@ -154,7 +153,7 @@ static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
 
        assert(pb->cui->current == &pb->cui->main->scr);
 
-       return pb_run_kexec(cod->kd, pb->opts.dry_run);
+       return pb_run_kexec(cod->kd, pb->cui->dry_run);
 }
 
 /**
@@ -176,7 +175,9 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
 
        m->on_open = cui_on_open;
 
-       m->scr.frame.title = talloc_strdup(m, "Petitboot");
+       m->scr.frame.title = talloc_asprintf(m,
+               "Petitboot (" PACKAGE_VERSION ")%s",
+               (pb_cui->cui->dry_run ? " (dry-run)" : ""));
        m->scr.frame.help = talloc_strdup(m,
                "ESC=exit, Enter=accept, e=edit, o=open");
        m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
@@ -239,26 +240,27 @@ int main(int argc, char *argv[])
        static struct sigaction sa;
        int result;
        int cui_result;
+       struct opts opts;
 
-       result = opts_parse(&pb.opts, argc, argv);
+       result = opts_parse(&opts, argc, argv);
 
        if (result) {
                print_usage();
                return EXIT_FAILURE;
        }
 
-       if (pb.opts.show_help == opt_yes) {
+       if (opts.show_help == opt_yes) {
                print_usage();
                return EXIT_SUCCESS;
        }
 
-       if (pb.opts.show_version == opt_yes) {
+       if (opts.show_version == opt_yes) {
                print_version();
                return EXIT_SUCCESS;
        }
 
-       if (strcmp(pb.opts.log_file, "-")) {
-               FILE *log = fopen(pb.opts.log_file, "a");
+       if (strcmp(opts.log_file, "-")) {
+               FILE *log = fopen(opts.log_file, "a");
 
                assert(log);
                pb_log_set_stream(log);
@@ -283,7 +285,8 @@ int main(int argc, char *argv[])
                return EXIT_FAILURE;
        }
 
-       pb.cui = cui_init(&pb, pb_kexec_cb, NULL, pb.opts.start_daemon);
+       pb.cui = cui_init(&pb, pb_kexec_cb, NULL, opts.start_daemon,
+               opts.dry_run);
 
        if (!pb.cui)
                return EXIT_FAILURE;
index 09e7a57fafe9454eb11cb24fa90fee11234718d1..7db482b0cfe040e646d9047198ddc0c3a7b8b1df 100644 (file)
@@ -111,7 +111,7 @@ int cui_run_cmd(struct pmenu_item *item)
 
        def_prog_mode();
 
-       result = pb_run_cmd(cmd_argv, 1);
+       result = pb_run_cmd(cmd_argv, 1, 0);
 
        reset_prog_mode();
        redrawwin(cui->current->main_ncw);
@@ -151,7 +151,7 @@ static int cui_run_kexec(struct pmenu_item *item)
                clear();
                mvaddstr(1, 0, "system is going down now...");
                refresh();
-               sleep(60);
+               sleep(cui->dry_run ? 1 : 60);
        }
 
        pb_log("%s: failed: %s\n", __func__, cod->kd->image);
@@ -534,7 +534,7 @@ static struct discover_client_ops cui_client_ops = {
 
 struct cui *cui_init(void* platform_info,
        int (*on_kexec)(struct cui *, struct cui_opt_data *),
-       int (*js_map)(const struct js_event *e), int start_deamon)
+       int (*js_map)(const struct js_event *e), int start_deamon, int dry_run)
 {
        struct cui *cui;
        struct discover_client *client;
@@ -552,6 +552,7 @@ struct cui *cui_init(void* platform_info,
        cui->platform_info = platform_info;
        cui->on_kexec = on_kexec;
        cui->timer.handle_timeout = cui_handle_timeout;
+       cui->dry_run = dry_run;
 
        /* Loop here for scripts that just started the server. */
 
index a4ac2e885a4341b2674b3838a6a5abe80786e2ad..90eef1cdfd7cd264cef80eebde5110fb202b9eb4 100644 (file)
@@ -51,6 +51,7 @@ struct cui {
        enum pb_nc_sig c_sig;
        sig_atomic_t abort;
        sig_atomic_t resize;
+       int dry_run;
        struct nc_scr *current;
        struct pmenu *main;
        struct ui_timer timer;
@@ -62,7 +63,7 @@ struct cui {
 
 struct cui *cui_init(void* platform_info,
        int (*on_kexec)(struct cui *, struct cui_opt_data *),
-       int (*js_map)(const struct js_event *e), int start_deamon);
+       int (*js_map)(const struct js_event *e), int start_deamon, int dry_run);
 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr);
 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item);
 int cui_ked_run(struct pmenu_item *item);
index 69913edc73910dcca30173649d6ceeb319b364e4..26d2591d467f7112fa63b7f351dde76f8d4224ba 100644 (file)
@@ -307,7 +307,7 @@ static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
        } else
                altered_args = 0;
 
-       result = pb_run_kexec(cod->kd);
+       result = pb_run_kexec(cod->kd, ps3->cui->dry_run);
 
        if (altered_args) {
                talloc_free(cod->kd->args);
index 1170b34555664c449fdbb12f0c3cdaf19541cc9b..8d7991a415d9370e42205d83fc328a7b0b50d6ee 100644 (file)
@@ -63,8 +63,7 @@ static int pbt_client_run_kexec(struct pbt_item *item)
        result = item->pbt_client->kexec_cb(item->pbt_client, opt_data);
 
        if (!result) {
-               //mvaddstr(1, 0, "system is going down now...");
-               sleep(60);
+               sleep(item->pbt_client->dry_run ? 1 : 60);
        }
 
        pb_log("%s: failed: %s\n", __func__, opt_data->kd->image);