From patchwork Thu Nov 23 03:03:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 840654 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yj40Q14YXz9s4s for ; Thu, 23 Nov 2017 14:03:50 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="RSSFFuVV"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TosD/p5V"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3yj40P43L6zDrKt for ; Thu, 23 Nov 2017 14:03:49 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="RSSFFuVV"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TosD/p5V"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.28; helo=out4-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="RSSFFuVV"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TosD/p5V"; dkim-atps=neutral Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3yj4090cRHzDrJB for ; Thu, 23 Nov 2017 14:03:34 +1100 (AEDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 0F9F820BFE; Wed, 22 Nov 2017 22:03:32 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute2.internal (MEProxy); Wed, 22 Nov 2017 22:03:32 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=q5wHSJG8h/3m1wJ19 4Bqif0GSM/zBXYG2DDZgdOBlN4=; b=RSSFFuVVsCTjRGE6+/ni1UBf45/zKa2pO oEtfF1r5AY2mkFZ3nPPwcEO6LyCd4ZTrrEOU3Pff0cIsFabe1mMJnYdc29bD5xTy 3FBEH4C7/PSqx9V8J061UASBrmj1WNC9UqAM2QVaYZyb3QYn3bMP6hyZ6YtEoNvW NY6FBvbu6piogrkcL1LFrApHFBuXbTMxhKxth4sKw7e7DTXCEnHN1vB8s+hgnhvc drEQ30hErYe+Bi27e6AqA7hpdjgUYLXONf8IltXuSZVicoPrqfH8VujXMeIaRRmT il0AHsKX/jDejecvHVzvfk1a0orY33OqEK6gGEB/5mnqjUE8WU1Jg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=q5wHSJG8h/3m1wJ19 4Bqif0GSM/zBXYG2DDZgdOBlN4=; b=TosD/p5Vha463fSEVaAjVjmHISMAgnD+e tkJ5519NIKu3S2xS9p1UNDTRTk2I+JH4Ctu/5f5SUgUMWma4cQmFVhvUsDegYunS SiNcqEWyQP4Rjagptz5+GuJpcvlagukBEu1La4Ei4CWimBjDDtETYC5eRMrw3MG/ +kbnXtrPACMxlYXMbVtZQJUNFwFJ9PSalxa2SOhXVAXb4PorZ/Mi0A+BuLAayJMd tixgmWrv9AE27qo0hW/S5YWPuTVKhsG76AGlrp6meFnD0s9lW0Lh8pF61ReBb183 3bVfV4Xnf5YgAuUBhtgJVqJ8E2cvB6PB6EDCL5kE+0x0gqvQX4zgg== X-ME-Sender: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id DF4A47EECF; Wed, 22 Nov 2017 22:03:30 -0500 (EST) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH] ui/ncurses: Always cancel autoboot on exit Date: Thu, 23 Nov 2017 14:03:18 +1100 Message-Id: <20171123030318.31910-1-sam@mendozajonas.com> X-Mailer: git-send-email 2.15.0 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.24 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" If the ncurses UI exits before it has contacted the server it is meant to fork a process to wait until the connection is made and cancel autoboot. This prevents users dropping to the shell and then having the machine boot out from underneath them. In e1e2ca68 "Spawn shell in exit handler" the UI doesn't actually exit on "exit", but it isn't listening to server events either while the shell is active. In this case make sure we still fork to notify the server. Signed-off-by: Samuel Mendoza-Jonas Tested-by: Stewart Smith --- ui/ncurses/nc-cui.c | 76 +++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 64b1f64..34d92bb 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -56,6 +56,8 @@ static bool cui_detached = false; 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 bool lockdown_active(void) { bool lockdown = false; @@ -166,6 +168,8 @@ 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\";\ @@ -1367,31 +1371,6 @@ 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. * @@ -1540,6 +1519,40 @@ fail_alloc: return NULL; } +/** + * cui_cancel_autoboot_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. A child is forked + * which will spin until the server is connected and told to cancel autoboot. + */ +static void cui_cancel_autoboot_on_exit(struct cui *cui) +{ + pid_t pid; + + if (!cui->client) { + /* Fork a child to tell the server to cancel autoboot */ + pid = fork(); + if (!pid) { + cui_detached = true; + + /* Loop until connection established */ + 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); + exit(EXIT_SUCCESS); + } + if (pid < 0) + pb_log("Failed to fork child on exit: %m\n"); + } +} + /** * cui_run - The main cui program loop. * @cui: The cui instance. @@ -1552,8 +1565,6 @@ fail_alloc: int cui_run(struct cui *cui) { - pid_t pid; - assert(main); cui->current = &cui->main->scr; @@ -1580,16 +1591,7 @@ 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"); - } + cui_cancel_autoboot_on_exit(cui); return cui->abort ? 0 : -1; }