cmd: exit: Fix return value propagation out of environment scripts
PATCH v2 1/2 cmd: exit: Fix return value propagation out of environment scripts
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
"
---
Cc: Adrian Vovk <[email protected]>
Cc: Hector Palacios <[email protected]>
Cc: Pantelis Antoniou <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
---
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(-)
cmd/exit.c +5 -2
@@ -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(
common/cli.c +4 -3
@@ -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 ; }
common/cli_hush.c +15 -6
@@ -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__
doc/usage/cmd/exit.rst +3 -1
@@ -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.
Signature · 1 line
2.35.1
PATCH v2 2/2 test: cmd: exit: Add unit test for exit and partly run commands
Add a test which validates that exit from environment script works as expected, including return value propagation and clipping to positive integers.
---
Cc: Adrian Vovk <[email protected]>
Cc: Hector Palacios <[email protected]>
Cc: Pantelis Antoniou <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
---
V2: Add RB from Simon
--- include/test/suites.h | 1 + test/cmd/Makefile | 2 +- test/cmd/exit.c | 135 ++++++++++++++++++++++++++++++++++++++++++ test/cmd_ut.c | 1 + 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 test/cmd/exit.c
include/test/suites.h +1 -0
@@ -38,6 +38,7 @@ int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);int do_ut_dm (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]);int do_ut_env (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]); +int do_ut_exit (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]);int do_ut_fdt (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]);int do_ut_font (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]);int do_ut_lib (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]);
test/cmd/Makefile +1 -1
@@ -8,7 +8,7 @@ endif ifdef CONFIG_CONSOLE_RECORD obj-$(CONFIG_CMD_PAUSE) += test_pause.o endif -obj-y += mem.o +obj-y += exit.o mem.o obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o obj-$(CONFIG_CMD_FDT) += fdt.o obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
test/cmd/exit.c new file +135 -0
135 lines not on this page · raw message
test/cmd_ut.c +1 -0
@@ -65,6 +65,7 @@ static struct cmd_tbl cmd_ut_sub[] = {#if defined(CONFIG_UT_ENV )U_BOOT_CMD_MKENT (env,CONFIG_SYS_MAXARGS ,1 , do_ut_env,"" ,"" ), #endif +U_BOOT_CMD_MKENT (exit,CONFIG_SYS_MAXARGS ,1 , do_ut_exit,"" ,"" ), #ifdef CONFIG_CMD_FDT U_BOOT_CMD_MKENT(fdt, CONFIG_SYS_MAXARGS, 1, do_ut_fdt, "", ""), #endif
Signature · 1 line
2.35.1
Re: [PATCH v2 2/2] test: cmd: exit: Add unit test for exit and partly run commands
Quoted 6 lines · Marek Vasut
Add a test which validates that exit from environment script works as expected, including return value propagation and clipping to positive integers.
Reviewed-by Simon Glass <[email protected]>Signed-off-by Marek Vasut <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
Quoted 40 lines · Marek Vasut
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
"
Quoted 3 lines
Fixes 8c4e3b79bd0 ("cmd: exit: Fix return value")Reviewed-by Hector Palacios <[email protected]>Signed-off-by Marek Vasut <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom