From tim@robbins.dropbear.id.au  Tue Feb 12 21:38:02 2002
Return-Path: <tim@robbins.dropbear.id.au>
Received: from descent.robbins.dropbear.id.au (018.a.009.mel.iprimus.net.au [210.50.112.18])
	by hub.freebsd.org (Postfix) with ESMTP id 8A07E37B402
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Feb 2002 21:37:58 -0800 (PST)
Received: (from tim@localhost)
	by descent.robbins.dropbear.id.au (8.11.6/8.11.6) id g1D5bcd05187;
	Wed, 13 Feb 2002 16:37:38 +1100 (EST)
	(envelope-from tim)
Message-Id: <200202130537.g1D5bcd05187@descent.robbins.dropbear.id.au>
Date: Wed, 13 Feb 2002 16:37:38 +1100 (EST)
From: Tim Robbins <tim@robbins.dropbear.id.au>
Reply-To: Tim Robbins <tim@robbins.dropbear.id.au>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] rev 1.7 of xargs.c broke documented exit status
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         34898
>Category:       bin
>Synopsis:       [PATCH] rev 1.7 of xargs.c broke documented exit status
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 12 21:40:16 PST 2002
>Closed-Date:    Sun Mar 17 20:57:34 PST 2002
>Last-Modified:  Sun Mar 17 20:57:34 PST 2002
>Originator:     Tim Robbins
>Release:        FreeBSD 4.5-STABLE i386
>Organization:
>Environment:
System: FreeBSD descent.robbins.dropbear.id.au 4.5-STABLE FreeBSD 4.5-STABLE #3: Thu Feb 7 01:39:15 EST 2002 tim@descent.robbins.dropbear.id.au:/usr/obj/usr/src/sys/DESCENT i386


	
>Description:
Revision 1.7 of src/usr.bin/xargs/xargs.c, which changed the vfork() call
to a fork(), broke the exit status 127 documented in xargs(1).

It's broken in -STABLE and -CURRENT.

>How-To-Repeat:
tim@descent$ echo 'hi' | xargs /no
xargs: /no: No such file or directory
tim@descent$ echo $?
1

But xargs(1) says it should exit 127 instead of 1.

>Fix:

Index: xargs/xargs.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/xargs/xargs.c,v
retrieving revision 1.11
diff -u -r1.11 xargs.c
--- xargs/xargs.c	2001/12/11 22:36:26	1.11
+++ xargs/xargs.c	2002/02/13 05:32:54
@@ -296,7 +296,6 @@
 run(argv)
 	char **argv;
 {
-	volatile int noinvoke;
 	char **p;
 	pid_t pid;
 	int status;
@@ -308,21 +307,19 @@
 		(void)fprintf(stderr, "\n");
 		(void)fflush(stderr);
 	}
-	noinvoke = 0;
 	switch(pid = fork()) {
 	case -1:
 		err(1, "fork");
 	case 0:
 		execvp(argv[0], argv);
 		warn("%s", argv[0]);
-		noinvoke = 1;
-		_exit(1);
+		_exit(254);
 	}
 	pid = waitpid(pid, &status, 0);
 	if (pid == -1)
 		err(1, "waitpid");
 	/* If we couldn't invoke the utility, exit 127. */
-	if (noinvoke)
+	if (WEXITSTATUS(status) == 254)
 		exit(127);
 	/* If utility signaled or exited with a value of 255, exit 1-125. */
 	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: Tim Robbins <tim@robbins.dropbear.id.au>
Cc: <FreeBSD-gnats-submit@FreeBSD.ORG>
Subject: Re: bin/34898: [PATCH] rev 1.7 of xargs.c broke documented exit
 status
Date: Wed, 13 Feb 2002 21:44:16 +1100 (EST)

 On Wed, 13 Feb 2002, Tim Robbins wrote:
 
 > >Description:
 > Revision 1.7 of src/usr.bin/xargs/xargs.c, which changed the vfork() call
 > to a fork(), broke the exit status 127 documented in xargs(1).
 >
 > It's broken in -STABLE and -CURRENT.
 
 execvp() now uses alloca(), so it might be safe for xargs to use vfork()
 again.
 
 > >Fix:
 >
 > Index: xargs/xargs.c
 > ===================================================================
 > RCS file: /home/ncvs/src/usr.bin/xargs/xargs.c,v
 > retrieving revision 1.11
 > diff -u -r1.11 xargs.c
 > --- xargs/xargs.c	2001/12/11 22:36:26	1.11
 > +++ xargs/xargs.c	2002/02/13 05:32:54
 > @@ -296,7 +296,6 @@
 >  run(argv)
 >  	char **argv;
 >  {
 > -	volatile int noinvoke;
 >  	char **p;
 >  	pid_t pid;
 >  	int status;
 > @@ -308,21 +307,19 @@
 >  		(void)fprintf(stderr, "\n");
 >  		(void)fflush(stderr);
 >  	}
 > -	noinvoke = 0;
 >  	switch(pid = fork()) {
 >  	case -1:
 >  		err(1, "fork");
 >  	case 0:
 >  		execvp(argv[0], argv);
 >  		warn("%s", argv[0]);
 > -		noinvoke = 1;
 > -		_exit(1);
 > +		_exit(254);
 >  	}
 >  	pid = waitpid(pid, &status, 0);
 >  	if (pid == -1)
 >  		err(1, "waitpid");
 >  	/* If we couldn't invoke the utility, exit 127. */
 > -	if (noinvoke)
 > +	if (WEXITSTATUS(status) == 254)
 >  		exit(127);
 >  	/* If utility signaled or exited with a value of 255, exit 1-125. */
 >  	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
 
 The noinvoke hack has some advantages.  Status 254 is a bit more fragile
 than it used to be.  There is now a signal 126 and killing any process
 with this signal gives status 254.
 
 Bugs found while testing this:
 - /bin/kill doesn't support signals >= NSIG (32).
 - I use an old version of bash-1 for a shell.  It dumps core when a process
   is killed by a signal >= 32.  I think it was compiled when NSIG actually
   gave the number of signals.
 
 Bruce
 
State-Changed-From-To: open->feedback 
State-Changed-By: des 
State-Changed-When: Sat Mar 9 14:08:33 PST 2002 
State-Changed-Why:  
Fixed in -CURRENT, awaiting MFC. 


Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: des 
Responsible-Changed-When: Sat Mar 9 14:08:33 PST 2002 
Responsible-Changed-Why:  
I'll handle the MFC. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=34898 
State-Changed-From-To: feedback->closed 
State-Changed-By: des 
State-Changed-When: Sun Mar 17 20:57:32 PST 2002 
State-Changed-Why:  
MFCed, thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=34898 
>Unformatted:
