public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH v2] pasta: propagate exit code from child command
@ 2023-02-09 14:59 Paul Holzinger
  2023-02-13  1:16 ` Stefano Brivio
  2023-02-13  2:37 ` David Gibson
  0 siblings, 2 replies; 3+ messages in thread
From: Paul Holzinger @ 2023-02-09 14:59 UTC (permalink / raw)
  To: passt-dev; +Cc: Paul Holzinger

Exits codes are very useful for scripts, when the pasta child execvp()
call fails with ENOENT that parent should also exit with > 0. In short
the parent should always exit with the code from the child to make it
useful in scripts.

It is easy to test with: `pasta -- bash -c "exit 3"; echo $?`

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
---
Changes from v1:
 - Fixed comments from Stefano.

 pasta.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/pasta.c b/pasta.c
index 3f6477c..fe21c5b 100644
--- a/pasta.c
+++ b/pasta.c
@@ -64,9 +64,17 @@ void pasta_child_handler(int signal)
 
 	if (pasta_child_pid &&
 	    !waitid(P_PID, pasta_child_pid, &infop, WEXITED | WNOHANG)) {
-		if (infop.si_pid == pasta_child_pid)
-			exit(EXIT_SUCCESS);
+		if (infop.si_pid == pasta_child_pid) {
+			if (infop.si_code == CLD_EXITED)
+				exit(infop.si_status);
+
+			/* If killed by a signal, si_status is the number.
+			 * Follow common shell convention of returning it + 128.
+			 */
+			exit(infop.si_status + 128);
+
 			/* Nothing to do, detached PID namespace going away */
+		}
 	}
 
 	waitid(P_ALL, 0, NULL, WEXITED | WNOHANG);
-- 
@@ -64,9 +64,17 @@ void pasta_child_handler(int signal)
 
 	if (pasta_child_pid &&
 	    !waitid(P_PID, pasta_child_pid, &infop, WEXITED | WNOHANG)) {
-		if (infop.si_pid == pasta_child_pid)
-			exit(EXIT_SUCCESS);
+		if (infop.si_pid == pasta_child_pid) {
+			if (infop.si_code == CLD_EXITED)
+				exit(infop.si_status);
+
+			/* If killed by a signal, si_status is the number.
+			 * Follow common shell convention of returning it + 128.
+			 */
+			exit(infop.si_status + 128);
+
 			/* Nothing to do, detached PID namespace going away */
+		}
 	}
 
 	waitid(P_ALL, 0, NULL, WEXITED | WNOHANG);
-- 
2.39.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] pasta: propagate exit code from child command
  2023-02-09 14:59 [PATCH v2] pasta: propagate exit code from child command Paul Holzinger
@ 2023-02-13  1:16 ` Stefano Brivio
  2023-02-13  2:37 ` David Gibson
  1 sibling, 0 replies; 3+ messages in thread
From: Stefano Brivio @ 2023-02-13  1:16 UTC (permalink / raw)
  To: Paul Holzinger; +Cc: passt-dev

On Thu,  9 Feb 2023 15:59:49 +0100
Paul Holzinger <pholzing@redhat.com> wrote:

> Exits codes are very useful for scripts, when the pasta child execvp()
> call fails with ENOENT that parent should also exit with > 0. In short
> the parent should always exit with the code from the child to make it
> useful in scripts.
> 
> It is easy to test with: `pasta -- bash -c "exit 3"; echo $?`
> 
> Signed-off-by: Paul Holzinger <pholzing@redhat.com>

Applied, thanks for the follow-up.

-- 
Stefano


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] pasta: propagate exit code from child command
  2023-02-09 14:59 [PATCH v2] pasta: propagate exit code from child command Paul Holzinger
  2023-02-13  1:16 ` Stefano Brivio
@ 2023-02-13  2:37 ` David Gibson
  1 sibling, 0 replies; 3+ messages in thread
From: David Gibson @ 2023-02-13  2:37 UTC (permalink / raw)
  To: Paul Holzinger; +Cc: passt-dev

[-- Attachment #1: Type: text/plain, Size: 1670 bytes --]

On Thu, Feb 09, 2023 at 03:59:49PM +0100, Paul Holzinger wrote:
> Exits codes are very useful for scripts, when the pasta child execvp()
> call fails with ENOENT that parent should also exit with > 0. In short
> the parent should always exit with the code from the child to make it
> useful in scripts.
> 
> It is easy to test with: `pasta -- bash -c "exit 3"; echo $?`
> 
> Signed-off-by: Paul Holzinger <pholzing@redhat.com>

Oops, I'm a little embarrassed I didn't think of that before.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
> Changes from v1:
>  - Fixed comments from Stefano.
> 
>  pasta.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/pasta.c b/pasta.c
> index 3f6477c..fe21c5b 100644
> --- a/pasta.c
> +++ b/pasta.c
> @@ -64,9 +64,17 @@ void pasta_child_handler(int signal)
>  
>  	if (pasta_child_pid &&
>  	    !waitid(P_PID, pasta_child_pid, &infop, WEXITED | WNOHANG)) {
> -		if (infop.si_pid == pasta_child_pid)
> -			exit(EXIT_SUCCESS);
> +		if (infop.si_pid == pasta_child_pid) {
> +			if (infop.si_code == CLD_EXITED)
> +				exit(infop.si_status);
> +
> +			/* If killed by a signal, si_status is the number.
> +			 * Follow common shell convention of returning it + 128.
> +			 */
> +			exit(infop.si_status + 128);
> +
>  			/* Nothing to do, detached PID namespace going away */
> +		}
>  	}
>  
>  	waitid(P_ALL, 0, NULL, WEXITED | WNOHANG);

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-13  3:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 14:59 [PATCH v2] pasta: propagate exit code from child command Paul Holzinger
2023-02-13  1:16 ` Stefano Brivio
2023-02-13  2:37 ` David Gibson

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).