From 52b9db95764fcdee9195113d7df225634a19c9f4 Mon Sep 17 00:00:00 2001 From: Geoff Levand Date: Fri, 30 Mar 2012 16:58:32 -0700 Subject: [PATCH] Cleanup --dry-run option code Signed-off-by: Geoff Levand --- discover/device-handler.c | 4 ++-- lib/system/system.c | 13 ++++++++++--- lib/system/system.h | 2 +- ui/common/loader.c | 10 +++++----- ui/common/ui-system.c | 9 ++++----- ui/ncurses/generic-main.c | 21 ++++++++++++--------- ui/ncurses/nc-cui.c | 7 ++++--- ui/ncurses/nc-cui.h | 3 ++- ui/ncurses/ps3-main.c | 2 +- ui/twin/pbt-client.c | 3 +-- 10 files changed, 42 insertions(+), 32 deletions(-) diff --git a/discover/device-handler.c b/discover/device-handler.c index 12bc40f..6e03ef3 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -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; } diff --git a/lib/system/system.c b/lib/system/system.c index 1b506d2..d77159d 100644 --- a/lib/system/system.c +++ b/lib/system/system.c @@ -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(); diff --git a/lib/system/system.h b/lib/system/system.h index d39280d..f8f18a3 100644 --- a/lib/system/system.h +++ b/lib/system/system.h @@ -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); diff --git a/ui/common/loader.c b/ui/common/loader.c index 009871d..775f211 100644 --- a/ui/common/loader.c +++ b/ui/common/loader.c @@ -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; diff --git a/ui/common/ui-system.c b/ui/common/ui-system.c index 4b0f144..b4ae8f8 100644 --- a/ui/common/ui-system.c +++ b/ui/common/ui-system.c @@ -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; diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c index 9a22265..dfeb1ba 100644 --- a/ui/ncurses/generic-main.c +++ b/ui/ncurses/generic-main.c @@ -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; diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 09e7a57..7db482b 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -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. */ diff --git a/ui/ncurses/nc-cui.h b/ui/ncurses/nc-cui.h index a4ac2e8..90eef1c 100644 --- a/ui/ncurses/nc-cui.h +++ b/ui/ncurses/nc-cui.h @@ -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); diff --git a/ui/ncurses/ps3-main.c b/ui/ncurses/ps3-main.c index 69913ed..26d2591 100644 --- a/ui/ncurses/ps3-main.c +++ b/ui/ncurses/ps3-main.c @@ -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); diff --git a/ui/twin/pbt-client.c b/ui/twin/pbt-client.c index 1170b34..8d7991a 100644 --- a/ui/twin/pbt-client.c +++ b/ui/twin/pbt-client.c @@ -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); -- 2.39.2