]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-cui.c
ui/ncurses: Make server connect message more clear
[petitboot] / ui / ncurses / nc-cui.c
index c2f1c83f1c406bcd698c9b9f782da25b431f3800..304371c926236f46d9b4d5c333906a54b58164dc 100644 (file)
@@ -46,6 +46,8 @@
 
 extern const struct help_text main_menu_help_text;
 
+static bool cui_detached = false;
+
 static struct pmenu *main_menu_init(struct cui *cui);
 
 static bool lockdown_active(void)
@@ -100,6 +102,9 @@ static void cui_start(void)
 
 static void cui_atexit(void)
 {
+       if (cui_detached)
+               return;
+
        clear();
        refresh();
        endwin();
@@ -924,6 +929,31 @@ static struct discover_client_ops cui_client_ops = {
        .update_config = cui_update_config,
 };
 
+/* cui_server_wait_on_exit - On exit spin until the server is available.
+ *
+ * If the program exits before connecting to the server autoboot won't be
+ * cancelled even though there has been keyboard activity. This function is
+ * called by a child process which will spin until the server is connected and
+ * told to cancel autoboot.
+ *
+ * Processes exiting from this function will not carry out the cui_atexit()
+ * steps.
+ */
+static void cui_server_wait_on_exit(struct cui *cui)
+{
+       cui_detached = true;
+
+       while (!cui->client) {
+               cui->client = discover_client_init(cui->waitset,
+                               &cui_client_ops, cui);
+               if (!cui->client)
+                       sleep(1);
+       }
+
+       talloc_steal(cui, cui->client);
+       discover_client_cancel_default(cui->client);
+}
+
 /* cui_server_wait - Connect to the discover server.
  * @arg: Pointer to the cui instance.
  *
@@ -951,13 +981,15 @@ static int cui_server_wait(void *arg)
        if (!cui->client) {
                waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
                                        cui);
-               nc_scr_status_printf(cui->current, "Info: Waiting for server");
+               nc_scr_status_printf(cui->current,
+                                    "Info: Waiting for device discovery");
        } else {
-               nc_scr_status_printf(cui->current, "Info: Connected to server!");
+               nc_scr_status_printf(cui->current,
+                                    "Info: Connected to pb-discover!");
                talloc_steal(cui, cui->client);
 
                if (cui->has_input) {
-                       pb_log("Aborting default boot on server connect\n");
+                       pb_log("Aborting default boot on pb-discover connect\n");
                        discover_client_cancel_default(cui->client);
                }
        }
@@ -1078,6 +1110,8 @@ fail_alloc:
 
 int cui_run(struct cui *cui)
 {
+       pid_t pid;
+
        assert(main);
 
        cui->current = &cui->main->scr;
@@ -1104,5 +1138,16 @@ int cui_run(struct cui *cui)
 
        cui_atexit();
 
+       if (!cui->client) {
+               /* Fork a child to tell the server to cancel autoboot */
+               pid = fork();
+               if (!pid) {
+                       cui_server_wait_on_exit(cui);
+                       exit(EXIT_SUCCESS);
+               }
+               if (pid < 0)
+                       pb_log("Failed to fork child on exit: %m\n");
+       }
+
        return cui->abort ? 0 : -1;
 }