Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 66CBAC4332F for ; Tue, 20 Dec 2022 06:26:16 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5EB0584943; Tue, 20 Dec 2022 07:26:14 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1671517574; bh=JJ/AeAYKR/19Z8jgjbiEvztTIi8KWmOeNozRh9M7U7Y=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=OMem+X7dztGMhxvEVGC238ITDRI5u34ZGLSO8WxNF7BH1l1WuAF/I4rlLibHuY6m/ YO4m6S30yQD4yWJdjCZjYySQuU57Fa3+gKO1NgWVgLd16q8c1eTflPcJnaYmgOu3Lp GEjpaMQL01BWA7br3wrE6jvtO9uwv+6AoWfop6CgMfMPpGvyIx/GO/G9llJf2hNXgO /JvJ6q/QCulg2LAhwwPvFUDv0L0pGik0X3IfE+FVwNf0AlSEtJhCQ8jshkMQFLjsAi +WX4UkgyTEl1SV4HDWYGmdspJrk6gro/7ExugdzF1LIu2Oqn6wsvhkrNvb0H7JDOv4 iGT+S4wKEbdsA== Received: from tr.lan (ip-86-49-120-218.bb.vodafone.cz [86.49.120.218]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 0FE3583673; Tue, 20 Dec 2022 07:26:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1671517572; bh=JJ/AeAYKR/19Z8jgjbiEvztTIi8KWmOeNozRh9M7U7Y=; h=From:To:Cc:Subject:Date:From; b=bbBvTTw/trPj0KJjMMkO5mnQMJYfasL+pkEnkI2YcP5rZoo+ca1rCKiF8cgUOYf3b ZUGzmIVkALVIv2YWIbuWHJVPNYGnh1BNZP/+UTbz0aKGBi9RsUKCJN9WhkamNv+70M B7pm+ry9xBrmTSriVFcDpe3/QrFXfEbkZOnMUSbNAvufmzo2OzcDT8U44aJr358za6 CG/ybQb9q4gFs5mwQAr+fwVJrY1DZASDXr+AJTvJzvyLp+QlsQLBH98vcG9vQ0/8M9 fHgQgrFj3CRNgul0m+YRgZeJsgRvIxu01evFtLktT+f5ikGafsA9H0OiOYr9PTmmk1 P82JBEfKYLm+A== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Hector Palacios , Adrian Vovk , Pantelis Antoniou , Simon Glass , Tom Rini Subject: [PATCH v2 1/2] cmd: exit: Fix return value propagation out of environment scripts Date: Tue, 20 Dec 2022 07:25:59 +0100 Message-Id: <20221220062600.809064-1-marex@denx.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Make sure the 'exit' command as well as 'exit $val' command exits from environment scripts immediately and propagates return value out of those scripts fully. That means the following behavior is expected: " => setenv foo 'echo bar ; exit 1' ; run foo ; echo $? bar 1 => setenv foo 'echo bar ; exit 0' ; run foo ; echo $? bar 0 => setenv foo 'echo bar ; exit -2' ; run foo ; echo $? bar 0 " As well as the followin behavior: " => setenv foo 'echo bar ; exit 3 ; echo fail'; run foo; echo $? bar 3 => setenv foo 'echo bar ; exit 1 ; echo fail'; run foo; echo $? bar 1 => setenv foo 'echo bar ; exit 0 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit -1 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit -2 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit ; echo fail'; run foo; echo $? bar 0 " Fixes: 8c4e3b79bd0 ("cmd: exit: Fix return value") Reviewed-by: Hector Palacios Signed-off-by: Marek Vasut --- Cc: Adrian Vovk Cc: Hector Palacios Cc: Pantelis Antoniou Cc: Simon Glass Cc: Tom Rini --- V2: - Add RB from Hector - Update exit.rst --- cmd/exit.c | 7 +++++-- common/cli.c | 7 ++++--- common/cli_hush.c | 21 +++++++++++++++------ doc/usage/cmd/exit.rst | 4 +++- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/cmd/exit.c b/cmd/exit.c index 2c7132693ad..7bf241ec732 100644 --- a/cmd/exit.c +++ b/cmd/exit.c @@ -10,10 +10,13 @@ static int do_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { + int r; + + r = 0; if (argc > 1) - return dectoul(argv[1], NULL); + r = simple_strtoul(argv[1], NULL, 10); - return 0; + return -r - 2; } U_BOOT_CMD( diff --git a/common/cli.c b/common/cli.c index a47d6a3f2b4..ba45dad2db5 100644 --- a/common/cli.c +++ b/common/cli.c @@ -146,7 +146,7 @@ int run_commandf(const char *fmt, ...) #if defined(CONFIG_CMD_RUN) int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - int i; + int i, ret; if (argc < 2) return CMD_RET_USAGE; @@ -160,8 +160,9 @@ int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return 1; } - if (run_command(arg, flag | CMD_FLAG_ENV) != 0) - return 1; + ret = run_command(arg, flag | CMD_FLAG_ENV); + if (ret) + return ret; } return 0; } diff --git a/common/cli_hush.c b/common/cli_hush.c index 1467ff81b35..b8940b19735 100644 --- a/common/cli_hush.c +++ b/common/cli_hush.c @@ -1902,7 +1902,7 @@ static int run_list_real(struct pipe *pi) last_return_code = -rcode - 2; return -2; /* exit */ } - last_return_code=(rcode == 0) ? 0 : 1; + last_return_code = rcode; #endif #ifndef __U_BOOT__ pi->num_progs = save_num_progs; /* restore number of programs */ @@ -3212,7 +3212,15 @@ static int parse_stream_outer(struct in_str *inp, int flag) printf("exit not allowed from main input shell.\n"); continue; } - break; + /* + * DANGER + * Return code -2 is special in this context, + * it indicates exit from inner pipe instead + * of return code itself, the return code is + * stored in 'last_return_code' variable! + * DANGER + */ + return -2; } if (code == -1) flag_repeat = 0; @@ -3249,9 +3257,9 @@ int parse_string_outer(const char *s, int flag) #endif /* __U_BOOT__ */ { struct in_str input; + int rcode; #ifdef __U_BOOT__ char *p = NULL; - int rcode; if (!s) return 1; if (!*s) @@ -3263,11 +3271,12 @@ int parse_string_outer(const char *s, int flag) setup_string_in_str(&input, p); rcode = parse_stream_outer(&input, flag); free(p); - return rcode; + return rcode == -2 ? last_return_code : rcode; } else { #endif setup_string_in_str(&input, s); - return parse_stream_outer(&input, flag); + rcode = parse_stream_outer(&input, flag); + return rcode == -2 ? last_return_code : rcode; #ifdef __U_BOOT__ } #endif @@ -3287,7 +3296,7 @@ int parse_file_outer(void) setup_file_in_str(&input); #endif rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON); - return rcode; + return rcode == -2 ? last_return_code : rcode; } #ifdef __U_BOOT__ diff --git a/doc/usage/cmd/exit.rst b/doc/usage/cmd/exit.rst index 769223c4775..3edb12809ce 100644 --- a/doc/usage/cmd/exit.rst +++ b/doc/usage/cmd/exit.rst @@ -37,4 +37,6 @@ executed. Return value ------------ -$? is always set to 0 (true). +$? is default set to 0 (true). In case zero or positive integer parameter +is passed to the command, the return value is the parameter value. In case +negative integer parameter is passed to the command, the return value is 0. -- 2.35.1