Skip to contentSkip to conversation

cli_hush: fix 'exit' cmd that was not exiting scripts

PATCH In review Hector Palacios 2 participants 5 messages Nov 18, 2022 – Nov 25, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cli_hush: fix 'exit' cmd that was not exiting scripts

Commit 8c4e3b79bd0bb76eea16869e9666e19047c0d005 supposedly passed one-level up the argument passed to 'exit' but it also broke 'exit' purpose of stopping a script.

In reality, even if 'do_exit()' is capable of returning any integer, the cli only admits '1' or '0' as return values.

This commit respects the current implementation to allow 'exit' to at least return '1' for future processing, but returns when the command being run is 'exit'.

Before this:

=> setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
should not see this
0

After this:

=> setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
1
=> setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
1
=> setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
0

---
 common/cli_hush.c | 4 ++++
 1 file changed, 4 insertions(+)
common/cli_hush.c +4 -0
index 1467ff81b35b..9fe8b87e02d7 100644
@@ -1902,6 +1902,10 @@ static int run_list_real(struct pipe *pi)
 			last_return_code = -rcode - 2;
 			return -2;	/* exit */
 		}
+		if (!strcmp(pi->progs->argv[0], "exit")) {
+			last_return_code = rcode;
+			return rcode;   /* exit */
+		}
 		last_return_code=(rcode == 0) ? 0 : 1;
 #endif
 #ifndef __U_BOOT__
#1 lore raw
Quoted 70 lines · Hector Palacios

Commit 8c4e3b79bd0bb76eea16869e9666e19047c0d005 supposedly passed one-level up the argument passed to 'exit' but it also broke 'exit' purpose of stopping a script.

In reality, even if 'do_exit()' is capable of returning any integer, the cli only admits '1' or '0' as return values.

This commit respects the current implementation to allow 'exit' to at least return '1' for future processing, but returns when the command being run is 'exit'.

Before this:

=> setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
should not see this
0
=> setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
should not see this
0

After this:

=> setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
1
=> setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
1
=> setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
0
=> setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
0

---
  common/cli_hush.c | 4 ++++
  1 file changed, 4 insertions(+)
common/cli_hush.c +0 -0
index 1467ff81b35b..9fe8b87e02d7 100644

common/cli_hush.c static int run_list_real(struct pipe *pi) · line 1911

Quoted 10 lines
common/cli_hush.c +4 -0
@@ -1902,6 +1902,10 @@ static int run_list_real(struct pipe *pi)
  			last_return_code = -rcode - 2;
  			return -2;	/* exit */
  		}
+		if (!strcmp(pi->progs->argv[0], "exit")) {
+			last_return_code = rcode;
+			return rcode;   /* exit */
+		}
  		last_return_code=(rcode == 0) ? 0 : 1;
  #endif
  #ifndef __U_BOOT__

Looking at the code just above this change 'if (rcode < -1) last_return_code = -rcode - 2', that explains the odd 'return -r - 2' in cmd/exit.c I think.

I wonder, can we somehow fix the return code handling in cmd/exit.c instead, so that it would cover both this behavior listed in this patch, and 8c4e3b79bd0 ("cmd: exit: Fix return value") ? The cmd/exit.c seems like the right place to fix it.

btw. it would be good to write a unit test for this, since it is becoming messy.

#2 lore raw

Hi Marek,

Quoted 77 lines · Marek Vasut
Quoted 76 lines · Hector Palacios

Commit 8c4e3b79bd0bb76eea16869e9666e19047c0d005 supposedly passed one-level up the argument passed to 'exit' but it also broke 'exit' purpose of stopping a script.

In reality, even if 'do_exit()' is capable of returning any integer, the cli only admits '1' or '0' as return values.

This commit respects the current implementation to allow 'exit' to at least return '1' for future processing, but returns when the command being run is 'exit'.

Before this:

      => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
      should not see this
      0

After this:

         => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
         0

---
  common/cli_hush.c | 4 ++++

  1 file changed, 4 insertions(+)
common/cli_hush.c +0 -0
index 1467ff81b35b..9fe8b87e02d7 100644
@@ -1902,6 +1902,10 @@ static int run_list_real(struct pipe *pi)
                       last_return_code = -rcode - 2;

                      return -2;      /* exit */ }
+             if (!strcmp(pi->progs->argv[0], "exit")) { +                     last_return_code = rcode; +                     return rcode;   /* exit */

Quoted 8 lines
Quoted 4 lines

+             }
              last_return_code=(rcode == 0) ? 0 : 1; #endif
  #ifndef __U_BOOT__

Looking at the code just above this change 'if (rcode < -1) last_return_code = -rcode - 2', that explains the odd 'return -r - 2' in cmd/exit.c I think.

That's what I thought, too. The cli captures a -2 as the number to exit a script, and with -rcode -2 was exiting and returning a 0. Instead of capturing a magic number, I'm suggesting to capture 'exit' command.

Quoted 4 lines

I wonder, can we somehow fix the return code handling in cmd/exit.c instead, so that it would cover both this behavior listed in this patch, and 8c4e3b79bd0 ("cmd: exit: Fix return value") ? The cmd/exit.c seems like the right place to fix it.

I didn't revert or touched 8c4e3b79bd0 but if what you wanted to do with that commit is to return any positive integer to the upper layers, I must say that just doesn't work because the cli_hush only processes 1 (failure) or 0 (success), so there's no way for something such as 'exit 3' to produce a $? of 3. I think the 'exit' command should only be used with this old U-Boot standard of considering 1 a failure and 0 a success.

I could remove the 'if (rcode < -1) last_return_code = -rcode - 2', which doesn't add much value now, but other than that I'm unsure of what you have in mind as to fix cmd/exit.c.

Quoted 3 lines

btw. it would be good to write a unit test for this, since it is becoming messy.

Regards

Signature · 1 line
Héctor Palacios
#3 lore raw
Quoted 98 lines · Hector Palacios

Hi Marek,

Quoted 77 lines · Marek Vasut
Quoted 76 lines · Hector Palacios

Commit 8c4e3b79bd0bb76eea16869e9666e19047c0d005 supposedly passed one-level up the argument passed to 'exit' but it also broke 'exit' purpose of stopping a script.

In reality, even if 'do_exit()' is capable of returning any integer, the cli only admits '1' or '0' as return values.

This commit respects the current implementation to allow 'exit' to at least return '1' for future processing, but returns when the command being run is 'exit'.

Before this:

      => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
      should not see this
      0

After this:

         => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
         0

---
  common/cli_hush.c | 4 ++++

  1 file changed, 4 insertions(+)
common/cli_hush.c +0 -0
index 1467ff81b35b..9fe8b87e02d7 100644
@@ -1902,6 +1902,10 @@ static int run_list_real(struct pipe *pi)
                       last_return_code = -rcode - 2;

                      return -2;      /* exit */ }
+             if (!strcmp(pi->progs->argv[0], "exit")) { +                     last_return_code = rcode; +                     return rcode;   /* exit */

Quoted 8 lines
Quoted 4 lines

+             }
              last_return_code=(rcode == 0) ? 0 : 1; #endif
  #ifndef __U_BOOT__

Looking at the code just above this change 'if (rcode < -1) last_return_code = -rcode - 2', that explains the odd 'return -r - 2' in cmd/exit.c I think.

That's what I thought, too. The cli captures a -2 as the number to exit a  script, and with -rcode -2 was exiting and returning a 0. Instead of capturing a magic number, I'm suggesting to capture 'exit' command.

Quoted 4 lines

I wonder, can we somehow fix the return code handling in cmd/exit.c instead, so that it would cover both this behavior listed in this patch, and 8c4e3b79bd0 ("cmd: exit: Fix return value") ? The cmd/exit.c seems like the right place to fix it.

Quoted 4 lines

I didn't revert or touched 8c4e3b79bd0 but if what you wanted to do with that commit is to return any positive integer to the upper layers, I must say that just doesn't work because the cli_hush only processes 1 (failure) or 0 (success), so there's no way for something such as 'exit 3' to produce a $? of 3. I think the 'exit' command should only be used with this old U-Boot standard of considering 1 a failure and 0 a success.

I could remove the 'if (rcode < -1)  last_return_code = -rcode - 2', which doesn't add much value now, but other than that I'm unsure of what you have in mind as to fix cmd/exit.c.

I just saw my patch causes a data abort on if conditionals, when accessing argv[0].

Maybe we'd rather simply revert 8c4e3b79bd0 ("cmd: exit: Fix return value") and let the exit command return 0 in all cases, as it is documented, at least until we find a proper solution.

Signature · 1 line
Héctor Palacios
#4 lore raw
Quoted 99 lines · Hector Palacios
Quoted 98 lines · Hector Palacios

Hi Marek,

Quoted 77 lines · Marek Vasut
Quoted 76 lines · Hector Palacios

Commit 8c4e3b79bd0bb76eea16869e9666e19047c0d005 supposedly passed one-level up the argument passed to 'exit' but it also broke 'exit' purpose of stopping a script.

In reality, even if 'do_exit()' is capable of returning any integer, the cli only admits '1' or '0' as return values.

This commit respects the current implementation to allow 'exit' to at least return '1' for future processing, but returns when the command being run is 'exit'.

Before this:

      => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
      should not see this
      0
      => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
      should not see this
      0

After this:

         => setenv foo 'echo bar ; exit 3 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 1 ; echo should not see this'; run foo; echo $? bar
         1
         => setenv foo 'echo bar ; exit 0 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -1 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit -2 ; echo should not see this'; run foo; echo $? bar
         0
         => setenv foo 'echo bar ; exit ; echo should not see this'; run foo; echo $? bar
         0

---
  common/cli_hush.c | 4 ++++

  1 file changed, 4 insertions(+)
common/cli_hush.c +0 -0
index 1467ff81b35b..9fe8b87e02d7 100644
@@ -1902,6 +1902,10 @@ static int run_list_real(struct pipe *pi)
                       last_return_code = -rcode - 2;

                      return -2;      /* exit */ }
+             if (!strcmp(pi->progs->argv[0], "exit")) { +                     last_return_code = rcode; +                     return rcode;   /* exit */

Quoted 8 lines
Quoted 4 lines

+             }
              last_return_code=(rcode == 0) ? 0 : 1; #endif
  #ifndef __U_BOOT__

Looking at the code just above this change 'if (rcode < -1) last_return_code = -rcode - 2', that explains the odd 'return -r - 2' in cmd/exit.c I think.

That's what I thought, too. The cli captures a -2 as the number to exit a  script, and with -rcode -2 was exiting and returning a 0. Instead of capturing a magic number, I'm suggesting to capture 'exit' command.

Quoted 4 lines

I wonder, can we somehow fix the return code handling in cmd/exit.c instead, so that it would cover both this behavior listed in this patch, and 8c4e3b79bd0 ("cmd: exit: Fix return value") ? The cmd/exit.c seems like the right place to fix it.

Quoted 8 lines
Quoted 4 lines

I didn't revert or touched 8c4e3b79bd0 but if what you wanted to do with that commit is to return any positive integer to the upper layers, I must say that just doesn't work because the cli_hush only processes 1 (failure) or 0 (success), so there's no way for something such as 'exit 3' to produce a $? of 3. I think the 'exit' command should only be used with this old U-Boot standard of considering 1 a failure and 0 a success.

I could remove the 'if (rcode < -1)  last_return_code = -rcode - 2', which doesn't add much value now, but other than that I'm unsure of what you have in mind as to fix cmd/exit.c.

I just saw my patch causes a data abort on if conditionals, when accessing argv[0].

Maybe we'd rather simply revert 8c4e3b79bd0 ("cmd: exit: Fix return value") and let the exit command return 0 in all cases, as it is documented, at least until we find a proper solution.

We need to find a proper fix, no half-baked solutions. ( I need to think about the other email in this thread a bit more, I'll get back to you in a few days ).