X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fncurses%2Fnc-cui.c;h=495f78599b7b6ef3f72e061cf8e0e3c7f793814d;hp=f9f82475166961659a232dedc305ba4411158643;hb=07f6db7fc29a93fae3213af18371cccb17e0276a;hpb=2dfbd9811d1e14911a6476850aada94b88a0dbdd diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index f9f8247..495f785 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ #include "nc-statuslog.h" #include "nc-subset.h" #include "nc-plugin.h" +#include "nc-auth.h" #include "console-codes.h" extern const struct help_text main_menu_help_text; @@ -57,25 +59,65 @@ static struct pmenu *main_menu_init(struct cui *cui); static struct pmenu *plugin_menu_init(struct cui *cui); static void cui_cancel_autoboot_on_exit(struct cui *cui); +static void cui_auth_exit(struct cui *cui); + +static struct { + int key; + struct autoboot_option opt; +} autoboot_override_keys[] = { + { KEY_F(10), { + .boot_type = BOOT_DEVICE_TYPE, + .type = DEVICE_TYPE_DISK, + }, + }, + { KEY_F(11), { + .boot_type = BOOT_DEVICE_TYPE, + .type = DEVICE_TYPE_USB, + }, + }, + { KEY_F(12), { + .boot_type = BOOT_DEVICE_TYPE, + .type = DEVICE_TYPE_NETWORK, + }, + }, +}; static bool lockdown_active(void) { +#if defined(SIGNED_BOOT) && defined(HARD_LOCKDOWN) + return true; +#else bool lockdown = false; if (access(LOCKDOWN_FILE, F_OK) != -1) lockdown = true; return lockdown; +#endif +} + +static void cui_set_curses_options(bool curses_mode) +{ + if (curses_mode) { + cbreak(); /* Disable line buffering. */ + noecho(); /* Disable getch() echo. */ + nonl(); /* Disable new-line translation. */ + intrflush(stdscr, FALSE); /* Disable interrupt flush. */ + curs_set(0); /* Make cursor invisible */ + nodelay(stdscr, TRUE); /* Enable non-blocking getch() */ + } else { + nocbreak(); /* Enable line buffering. */ + echo(); /* Enable getch() echo. */ + nl(); /* Enable new-line translation. */ + intrflush(stdscr, TRUE); /* Enable interrupt flush. */ + curs_set(1); /* Make cursor visible */ + nodelay(stdscr, FALSE); /* Disable non-blocking getch() */ + } } static void cui_start(void) { initscr(); /* Initialize ncurses. */ - cbreak(); /* Disable line buffering. */ - noecho(); /* Disable getch() echo. */ keypad(stdscr, TRUE); /* Enable num keypad keys. */ - nonl(); /* Disable new-line translation. */ - intrflush(stdscr, FALSE); /* Disable interrupt flush. */ - curs_set(0); /* Make cursor invisible */ - nodelay(stdscr, TRUE); /* Enable non-blocking getch() */ + cui_set_curses_options(true); /* We may be operating with an incorrect $TERM type; in this case * the keymappings will be slightly broken. We want at least @@ -143,7 +185,7 @@ static void cui_atexit(void) void cui_abort(struct cui *cui) { - pb_log("%s: exiting\n", __func__); + pb_log_fn("exiting\n"); cui->abort = 1; } @@ -166,31 +208,28 @@ void cui_resize(struct cui *cui) void cui_on_exit(struct pmenu *menu) { struct cui *cui = cui_from_pmenu(menu); - char *sh_cmd; cui_cancel_autoboot_on_exit(cui); - sh_cmd = talloc_asprintf(cui, - "echo \"Exiting petitboot. Type 'exit' to return.\";\ - echo \"You may run 'pb-sos' to gather diagnostic data\";\ - %s", pb_system_apps.sh); - - if (!sh_cmd) { - pb_log("Failed to allocate shell arguments\n"); - return; - } - const char *argv[] = { pb_system_apps.sh, - "-c", - sh_cmd, NULL }; cui_run_cmd(cui, argv); nc_scr_status_printf(cui->current, _("Returned from shell")); - talloc_free(sh_cmd); +} + +/** + * cui_abort_on_exit - Force an exit of the main loop on menu exit. + * This is mainly for lockdown situations where + * the exit then triggers an expected reboot. + */ +void cui_abort_on_exit(struct pmenu *menu) +{ + struct cui *cui = cui_from_pmenu(menu); + cui->abort = 1; } /** @@ -212,11 +251,9 @@ int cui_run_cmd(struct cui *cui, const char **cmd_argv) nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]); - nc_scr_unpost(cui->current); + def_prog_mode(); clear(); refresh(); - - def_prog_mode(); endwin(); result = process_run_sync(process); @@ -228,7 +265,7 @@ int cui_run_cmd(struct cui *cui, const char **cmd_argv) nc_scr_post(cui->current); if (result) { - pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]); + pb_log_fn("failed: '%s'\n", cmd_argv[0]); nc_scr_status_printf(cui->current, _("Failed: %s"), cmd_argv[0]); } @@ -272,6 +309,99 @@ static int cui_boot(struct pmenu_item *item) return 0; } +static int menu_sysinfo_execute(struct pmenu_item *item) +{ + cui_show_sysinfo(cui_from_item(item)); + return 0; +} + +static int menu_config_execute(struct pmenu_item *item) +{ + cui_show_config(cui_from_item(item)); + return 0; +} + +static int menu_lang_execute(struct pmenu_item *item) +{ + cui_show_lang(cui_from_item(item)); + return 0; +} + +static int menu_statuslog_execute(struct pmenu_item *item) +{ + cui_show_statuslog(cui_from_item(item)); + return 0; +} + +static void menu_reinit_cb(struct nc_scr *scr) +{ + struct pmenu *menu = pmenu_from_scr(scr); + + cui_send_reinit(cui_from_pmenu(menu)); +} + +static int menu_reinit_execute(struct pmenu_item *item) +{ + struct cui *cui = cui_from_item(item); + + if (!cui->client) + return 0; + + /* If we don't need to authenticate, send the reinit immediately */ + if (discover_client_authenticated(cui->client)) { + cui_send_reinit(cui); + return 0; + } + + if (!cui->current) + return 0; + + if (cui->auth_screen) + return 0; + + cui->auth_screen = auth_screen_init(cui, cui->current->main_ncw, + false, menu_reinit_cb, cui_auth_exit); + + if (cui->auth_screen) + cui_set_current(cui, auth_screen_scr(cui->auth_screen)); + + return 0; +} + +static int menu_add_url_execute(struct pmenu_item *item) +{ + if (cui_from_item(item)->client) + cui_show_add_url(cui_from_item(item)); + return 0; +} + +static int menu_plugin_execute(struct pmenu_item *item) +{ + if (cui_from_item(item)->client) + cui_show_plugin_menu(cui_from_item(item)); + return 0; +} + +static void cui_boot_cb(struct nc_scr *scr) +{ + struct pmenu *menu = pmenu_from_scr(scr); + + if (pmenu_find_selected(menu)) + cui_boot(pmenu_find_selected(menu)); +} + +static int cui_boot_check(struct pmenu_item *item) +{ + struct cui *cui = cui_from_item(item); + + if (discover_client_authenticated(cui->client)) + return cui_boot(item); + + cui_show_auth(cui, item->pmenu->scr.main_ncw, false, cui_boot_cb); + + return 0; +} + static void cui_boot_editor_on_exit(struct cui *cui, struct pmenu_item *item, struct pb_boot_data *bd) @@ -303,7 +433,7 @@ static void cui_boot_editor_on_exit(struct cui *cui, } item->on_edit = cui_item_edit; - item->on_execute = cui_boot; + item->on_execute = cui_boot_check; item->data = cod; talloc_steal(item, cod); @@ -362,6 +492,52 @@ void cui_item_new(struct pmenu *menu) cui_set_current(cui, boot_editor_scr(cui->boot_editor)); } + +/* Call pb-plugin to install a plugin specified by plugin_file */ +static int cui_install_plugin(struct pmenu_item *item) +{ + struct cui *cui = cui_from_item(item); + struct cui_opt_data *cod = cod_from_item(item); + int rc; + + rc = cui_send_plugin_install(cui, cod->pd->plugin_file); + + if (rc) { + pb_log("cui_send_plugin_install failed!\n"); + nc_scr_status_printf(cui->current, + _("Failed to send install request")); + } else { + nc_scr_status_printf(cui->current, _("Installing plugin %s"), + cod->pd->plugin_file); + pb_debug("cui_send_plugin_install sent!\n"); + } + + return rc; +} + +static void cui_plugin_install_cb(struct nc_scr *scr) +{ + struct pmenu *menu = pmenu_from_scr(scr); + + if (pmenu_find_selected(menu)) + cui_install_plugin(pmenu_find_selected(menu)); + else + pb_debug("%s: no current item\n", __func__); +} + +static int cui_plugin_install_check(struct pmenu_item *item) +{ + struct cui *cui = cui_from_item(item); + + if (discover_client_authenticated(cui->client)) + return cui_install_plugin(item); + + cui_show_auth(cui, item->pmenu->scr.main_ncw, false, + cui_plugin_install_cb); + + return 0; +} + static void cui_sysinfo_exit(struct cui *cui) { cui_set_current(cui, &cui->main->scr); @@ -500,6 +676,39 @@ void cui_show_subset(struct cui *cui, const char *title, cui_set_current(cui, subset_screen_scr(cui->subset_screen)); } +static void cui_auth_exit(struct cui *cui) +{ + struct nc_scr *return_scr = auth_screen_return_scr(cui->auth_screen); + + /* + * Destroy the auth screen first so that the subwindow is cleaned up + * before the return_scr posts. If we don't do this operations on the + * main_ncw can cause a blank screen at first (eg. status update). + */ + nc_scr_unpost(cui->current); + talloc_free(cui->auth_screen); + cui->auth_screen = NULL; + + cui->current = return_scr; + nc_scr_post(cui->current); +} + +void cui_show_auth(struct cui *cui, WINDOW *parent, bool set_password, + void (*callback)(struct nc_scr *)) +{ + if (!cui->current) + return; + + if (cui->auth_screen) + return; + + cui->auth_screen = auth_screen_init(cui, parent, set_password, + callback, cui_auth_exit); + + if (cui->auth_screen) + cui_set_current(cui, auth_screen_scr(cui->auth_screen)); +} + /** * cui_set_current - Set the currently active screen and redraw it. */ @@ -522,14 +731,50 @@ struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr) return old; } +static bool set_temp_autoboot_opt(struct cui *cui, struct autoboot_option *opt) +{ + cui->autoboot_opt = opt; + if (cui->client) + discover_client_send_temp_autoboot(cui->client, opt); + + return true; +} + +static bool key_cancels_boot(int key) +{ + unsigned int i; + + if (key == 0xc) + return false; + + for (i = 0; i < ARRAY_SIZE(autoboot_override_keys); i++) + if (key == autoboot_override_keys[i].key) + return false; + + return true; +} + static bool process_global_keys(struct cui *cui, int key) { + unsigned int i; + switch (key) { case 0xc: if (cui->current && cui->current->main_ncw) wrefresh(curscr); return true; } + + /* check for autoboot override keys */ + for (i = 0; i < ARRAY_SIZE(autoboot_override_keys); i++) { + if (key != autoboot_override_keys[i].key) + continue; + + pb_log("Sending temporary autoboot override\n"); + set_temp_autoboot_opt(cui, &autoboot_override_keys[i].opt); + return true; + } + return false; } @@ -577,7 +822,13 @@ static int cui_process_key(void *arg) } } - if (!cui->has_input) { + if (cui->preboot_mode) { + /* Turn curses options back on if the user interacts */ + cui->preboot_mode = false; + cui_set_curses_options(true); + } + + if (!cui->has_input && key_cancels_boot(c)) { cui->has_input = true; if (cui->client) { pb_log("UI input received (key = %d), aborting " @@ -626,7 +877,7 @@ static void cui_handle_resize(struct cui *cui) struct winsize ws; if (ioctl(1, TIOCGWINSZ, &ws) == -1) { - pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno)); + pb_log_fn("ioctl failed: %s\n", strerror(errno)); return; } @@ -692,10 +943,10 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, if (plugin_option) { i->on_edit = NULL; - i->on_execute = plugin_install_plugin; + i->on_execute = cui_plugin_install_check; } else { i->on_edit = cui_item_edit; - i->on_execute = cui_boot; + i->on_execute = cui_boot_check; } i->data = cod = talloc(i, struct cui_opt_data); @@ -721,7 +972,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, result = set_menu_items(menu->ncm, NULL); if (result) - pb_log("%s: set_menu_items failed: %d\n", __func__, result); + pb_log_fn("set_menu_items failed: %d\n", result); /* Insert new items at insert_pt. */ if (dev_hdr) { @@ -736,10 +987,10 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, } if (plugin_option) { - pb_log("%s: adding plugin '%s'\n", __func__, cod->name); + pb_log_fn("adding plugin '%s'\n", cod->name); pb_log(" file '%s'\n", cod->pd->plugin_file); } else { - pb_log("%s: adding opt '%s'\n", __func__, cod->name); + pb_log_fn("adding opt '%s'\n", cod->name); pb_log(" image '%s'\n", cod->bd->image); pb_log(" initrd '%s'\n", cod->bd->initrd); pb_log(" args '%s'\n", cod->bd->args); @@ -753,10 +1004,10 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, result = set_menu_items(cui->main->ncm, NULL); for (j = 0 ; j < cui->main->item_count; j++) { item = item_userptr(cui->main->items[j]); - if (strncmp(item->nci->name.str, "Plugins", strlen("Plugins"))) + if (item->on_execute != menu_plugin_execute) continue; cui->n_plugins++; - char *label = talloc_asprintf(item, "Plugins (%u)", + char *label = talloc_asprintf(item, _("Plugins (%u)"), cui->n_plugins); pmenu_item_update(item, label); talloc_free(label); @@ -764,14 +1015,14 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, } result = set_menu_items(cui->main->ncm, cui->main->items); if (result) - pb_log("%s: set_menu_items failed: %d\n", __func__, result); + pb_log_fn("set_menu_items failed: %d\n", result); } /* Re-attach the items array. */ result = set_menu_items(menu->ncm, menu->items); if (result) - pb_log("%s: set_menu_items failed: %d\n", __func__, result); + pb_log_fn("set_menu_items failed: %d\n", result); if (0) { pb_log("%s\n", __func__); @@ -816,7 +1067,7 @@ static void cui_device_remove(struct device *dev, void *arg) int rows, cols, top, last; int result; - pb_log("%s: %p %s\n", __func__, dev, dev->id); + pb_log_fn("%p %s\n", dev, dev->id); if (cui->current == &cui->main->scr) nc_scr_unpost(cui->current); @@ -829,7 +1080,7 @@ static void cui_device_remove(struct device *dev, void *arg) result |= set_menu_items(cui->plugin_menu->ncm, NULL); if (result) - pb_log("%s: set_menu_items failed: %d\n", __func__, result); + pb_log_fn("set_menu_items failed: %d\n", result); list_for_each_entry(&dev->boot_options, opt, list) { struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info); @@ -886,7 +1137,7 @@ static void cui_device_remove(struct device *dev, void *arg) } if (result) - pb_log("%s: set_menu_items failed: %d\n", __func__, result); + pb_log_fn("set_menu_items failed: %d\n", result); if (0) { pb_log("%s\n", __func__); @@ -907,8 +1158,21 @@ static void cui_update_status(struct status *status, void *arg) statuslog_append_steal(cui, cui->statuslog, status); /* Ignore status messages from the backlog */ - if (!status->backlog) - nc_scr_status_printf(cui->current, "%s", status->message); + if (status->backlog) + return; + + nc_scr_status_printf(cui->current, "%s", status->message); + + if (cui->preboot_mode && + (!status->boot_active || status->type == STATUS_ERROR)) { + cui_set_curses_options(true); + cui->preboot_mode = false; + } else { + cui->preboot_mode = status->boot_active && + status->type == STATUS_INFO; + if (cui->preboot_mode) + cui_set_curses_options(false); + } } /* @@ -963,8 +1227,8 @@ fallback: * If this option was faked above move the context under * the item so it is cleaned up later in cui_plugins_remove(). */ - if (strncmp(cod->opt->id, "dummy", strlen("dummy") == 0 && - cod->dev->type == DEVICE_TYPE_UNKNOWN)) { + if (strcmp(cod->opt->id, "dummy") == 0 && + cod->dev->type == DEVICE_TYPE_UNKNOWN) { talloc_steal(item, cod->dev); talloc_steal(item, cod->opt); } @@ -1041,10 +1305,10 @@ static int cui_plugins_remove(void *arg) set_menu_items(cui->main->ncm, NULL); for (i = 0; i < cui->main->item_count; i++) { item = item_userptr(cui->main->items[i]); - if (strncmp(item->nci->name.str, "Plugins", strlen("Plugins"))) + if (item->on_execute != menu_plugin_execute) continue; cui->n_plugins = 0; - pmenu_item_update(item, "Plugins (0)"); + pmenu_item_update(item, _("Plugins (0)")); break; } @@ -1112,7 +1376,7 @@ static void cui_update_sysinfo(struct system_info *sysinfo, void *arg) cui_update_mm_title(cui); } -static void cui_update_language(struct cui *cui, char *lang) +void cui_update_language(struct cui *cui, const char *lang) { bool repost_menu; char *cur_lang; @@ -1176,54 +1440,20 @@ int cui_send_plugin_install(struct cui *cui, char *file) return discover_client_send_plugin_install(cui->client, file); } -void cui_send_reinit(struct cui *cui) -{ - discover_client_send_reinit(cui->client); -} - -static int menu_sysinfo_execute(struct pmenu_item *item) -{ - cui_show_sysinfo(cui_from_item(item)); - return 0; -} - -static int menu_config_execute(struct pmenu_item *item) -{ - cui_show_config(cui_from_item(item)); - return 0; -} - -static int menu_lang_execute(struct pmenu_item *item) -{ - cui_show_lang(cui_from_item(item)); - return 0; -} - -static int menu_statuslog_execute(struct pmenu_item *item) +int cui_send_authenticate(struct cui *cui, char *password) { - cui_show_statuslog(cui_from_item(item)); - return 0; -} - -static int menu_reinit_execute(struct pmenu_item *item) -{ - if (cui_from_item(item)->client) - cui_send_reinit(cui_from_item(item)); - return 0; + return discover_client_send_authenticate(cui->client, password); } -static int menu_add_url_execute(struct pmenu_item *item) +int cui_send_set_password(struct cui *cui, char *password, char *new_password) { - if (cui_from_item(item)->client) - cui_show_add_url(cui_from_item(item)); - return 0; + return discover_client_send_set_password(cui->client, password, + new_password); } -static int menu_plugin_execute(struct pmenu_item *item) +void cui_send_reinit(struct cui *cui) { - if (cui_from_item(item)->client) - cui_show_plugin_menu(cui_from_item(item)); - return 0; + discover_client_send_reinit(cui->client); } /** @@ -1236,16 +1466,16 @@ static struct pmenu *main_menu_init(struct cui *cui) int result; bool lockdown = lockdown_active(); - m = pmenu_init(cui, 9, cui_on_exit); + m = pmenu_init(cui, 9, lockdown ? cui_abort_on_exit : cui_on_exit); if (!m) { - pb_log("%s: failed\n", __func__); + pb_log_fn("failed\n"); return NULL; } m->n_hot_keys = 1; m->hot_keys = talloc_array(m, hot_key_fn, m->n_hot_keys); if (!m->hot_keys) { - pb_log("%s: failed to allocate hot_keys\n", __func__); + pb_log_fn("failed to allocate hot_keys\n"); talloc_free(m); return NULL; } @@ -1334,7 +1564,7 @@ static struct pmenu *plugin_menu_init(struct cui *cui) m = pmenu_init(cui, 2, cui_plugin_menu_exit); m->scr.frame.ltitle = talloc_asprintf(m, _("Petitboot Plugins")); - m->scr.frame.rtitle = talloc_asprintf(m, NULL); + m->scr.frame.rtitle = talloc_asprintf(m, "%s", ""); m->scr.frame.help = talloc_strdup(m, _("Enter=install, e=details, x=exit, h=help")); m->scr.frame.status = talloc_asprintf(m, @@ -1415,6 +1645,12 @@ static int cui_server_wait(void *arg) pb_log("Aborting default boot on pb-discover connect\n"); discover_client_cancel_default(cui->client); } + + if (cui->autoboot_opt) { + pb_log("Sending autoboot override on pb-discover connect\n"); + discover_client_send_temp_autoboot(cui->client, + cui->autoboot_opt); + } } return 0; @@ -1438,7 +1674,7 @@ struct cui *cui_init(void* platform_info, cui = talloc_zero(NULL, struct cui); if (!cui) { - pb_log("%s: alloc cui failed.\n", __func__); + pb_log_fn("alloc cui failed.\n"); fprintf(stderr, _("%s: alloc cui failed.\n"), __func__); goto fail_alloc; } @@ -1458,7 +1694,7 @@ retry_start: &cui_client_ops, cui); if (cui->client || !i) break; - pb_log("%s: waiting for server %d\n", __func__, i); + pb_log_fn("waiting for server %d\n", i); sleep(1); } @@ -1472,7 +1708,7 @@ retry_start: if (!result) goto retry_start; - pb_log("%s: discover_client_init failed.\n", __func__); + pb_log_fn("discover_client_init failed.\n"); fprintf(stderr, _("%s: error: discover_client_init failed.\n"), __func__); fprintf(stderr, _("could not start pb-discover, the petitboot " @@ -1486,7 +1722,7 @@ retry_start: waiter_register_timeout(cui->waitset, 0, cui_server_wait, cui); } else if (!cui->client) { - pb_log("%s: discover_client_init failed.\n", __func__); + pb_log_fn("discover_client_init failed.\n"); fprintf(stderr, _("%s: error: discover_client_init failed.\n"), __func__); fprintf(stderr, _("check that pb-discover, " @@ -1573,7 +1809,8 @@ static void cui_cancel_autoboot_on_exit(struct cui *cui) int cui_run(struct cui *cui) { - assert(main); + assert(cui); + assert(cui->main); cui->current = &cui->main->scr; cui->default_item = 0; @@ -1584,7 +1821,7 @@ int cui_run(struct cui *cui) int result = waiter_poll(cui->waitset); if (result < 0) { - pb_log("%s: poll: %s\n", __func__, strerror(errno)); + pb_log_fn("poll: %s\n", strerror(errno)); break; }