git: 6c49b65d6da2 - stable/13 - xargs: fix exit code when using -P

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Thu, 10 Nov 2022 22:44:36 UTC
The branch stable/13 has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=6c49b65d6da24cc50409418b26a2df1bdbdb672e

commit 6c49b65d6da24cc50409418b26a2df1bdbdb672e
Author:     liu-du <duliujimmy@hotmail.com>
AuthorDate: 2022-10-16 03:41:54 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2022-11-10 22:43:58 +0000

    xargs: fix exit code when using -P
    
    currently when xargs runs in parallel mode (e.g. -P2), it somtimes
    incorrectly returns zero exit code.  this commit fix it and also adds
    tests.
    
    Reviewed by:    mjg
    PR:     267110
    
    (cherry picked from commit 0ca740d9a639ab635f5a28be9051d0124a9544a1)
---
 usr.bin/xargs/tests/regress.sh |  7 ++++++-
 usr.bin/xargs/xargs.c          | 12 ++++++++----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/usr.bin/xargs/tests/regress.sh b/usr.bin/xargs/tests/regress.sh
index 72458a42d774..e79bfd4352fa 100644
--- a/usr.bin/xargs/tests/regress.sh
+++ b/usr.bin/xargs/tests/regress.sh
@@ -1,6 +1,6 @@
 # $FreeBSD$
 
-echo 1..16
+echo 1..20
 
 REGRESSION_START($1)
 
@@ -21,4 +21,9 @@ REGRESSION_TEST(`0L', `xargs -0 -L2 echo <${SRCDIR}/regress.0.in')
 REGRESSION_TEST(`0P1', `xargs -0 -P1 echo <${SRCDIR}/regress.0.in')
 REGRESSION_TEST(`quotes', `xargs -n1 echo <${SRCDIR}/regress.quotes.in')
 
+REGRESSION_TEST_FREEFORM(`parallel1', `echo /var/empty       /var/empty       | xargs -n1 -P2 test -d; [ $? = 0 ]')
+REGRESSION_TEST_FREEFORM(`parallel2', `echo /var/empty       /var/empty/nodir | xargs -n1 -P2 test -d; [ $? = 1 ]')
+REGRESSION_TEST_FREEFORM(`parallel3', `echo /var/empty/nodir /var/empty       | xargs -n1 -P2 test -d; [ $? = 1 ]')
+REGRESSION_TEST_FREEFORM(`parallel4', `echo /var/empty/nodir /var/empty/nodir | xargs -n1 -P2 test -d; [ $? = 1 ]')
+
 REGRESSION_END()
diff --git a/usr.bin/xargs/xargs.c b/usr.bin/xargs/xargs.c
index 2825a26f4dfa..6520d43dae50 100644
--- a/usr.bin/xargs/xargs.c
+++ b/usr.bin/xargs/xargs.c
@@ -314,8 +314,10 @@ parse_input(int argc, char *argv[])
 	switch (ch = getchar()) {
 	case EOF:
 		/* No arguments since last exec. */
-		if (p == bbp)
-			xexit(*av, rval);
+		if (p == bbp) {
+			waitchildren(*av, 1);
+			exit(rval);
+		}
 		goto arg1;
 	case ' ':
 	case '\t':
@@ -405,8 +407,10 @@ arg2:
 					*xp++ = *avj;
 			}
 			prerun(argc, av);
-			if (ch == EOF || foundeof)
-				xexit(*av, rval);
+			if (ch == EOF || foundeof) {
+				waitchildren(*av, 1);
+				exit(rval);
+			}
 			p = bbp;
 			xp = bxp;
 			count = 0;