From yar@FreeBSD.org  Tue Aug 11 07:22:49 2009
Return-Path: <yar@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 9240B1065670
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Aug 2009 07:22:49 +0000 (UTC)
	(envelope-from yar@FreeBSD.org)
Received: from ref8-amd64.freebsd.org (ref8-amd64.freebsd.org [IPv6:2001:4f8:fff6::5f])
	by mx1.freebsd.org (Postfix) with ESMTP id 8167B8FC31
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Aug 2009 07:22:49 +0000 (UTC)
Received: from ref8-amd64.freebsd.org (localhost [127.0.0.1])
	by ref8-amd64.freebsd.org (8.14.3/8.14.3) with ESMTP id n7B7MnrB042066
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 11 Aug 2009 07:22:49 GMT
	(envelope-from yar@ref8-amd64.freebsd.org)
Received: (from yar@localhost)
	by ref8-amd64.freebsd.org (8.14.3/8.14.3/Submit) id n7B7Mn2Q042065;
	Tue, 11 Aug 2009 07:22:49 GMT
	(envelope-from yar)
Message-Id: <200908110722.n7B7Mn2Q042065@ref8-amd64.freebsd.org>
Date: Tue, 11 Aug 2009 07:22:49 GMT
From: Yar Tikhiy <yar@freebsd.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: sh(1): /bin/sh fails to redirect stderr in backticks
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         137659
>Category:       bin
>Synopsis:       sh(1) does not redirect certain command not found messages
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 11 07:30:09 UTC 2009
>Closed-Date:    Fri Oct 23 15:12:33 UTC 2009
>Last-Modified:  Fri Oct 23 15:12:33 UTC 2009
>Originator:     Yar Tikhiy
>Release:        FreeBSD 8.0-BETA2 amd64
>Organization:
BarNet
>Environment:
System: FreeBSD ref8-amd64.freebsd.org 8.0-BETA2 FreeBSD 8.0-BETA2 #11 r195838: Fri Jul 24 09:52:42 UTC 2009 simon@ref8-amd64.freebsd.org:/scratch/src/sys/amd64/compile/REF8-AMD64 amd64

>Description:
	If the command to run in backticks has no path and the shell
	fails to run it, the stderr message cannot be redirected.

	For comparison, bash redirects OK in both cases.
>How-To-Repeat:

Compare the following two cases.  In case 1, the "not found" message
is properly redirected to stdout and assigned to a variable while in
case 2 it's just emitted immediately, defying redirection.

$ out=`/var/empty/foo 2>&1`
$ echo $out
/var/empty/foo: not found
$ out=`nosuchtool 2>&1`
nosuchtool: not found
$ echo $out

$ 

>Fix:
>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, yar@freebsd.org
Cc:  
Subject: Re: bin/137659: sh(1): /bin/sh fails to redirect stderr in
	backticks
Date: Tue, 8 Sep 2009 23:02:35 +0200

 --G4iJoqBmSsgzjUCe
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 There are indeed bugs with stderr output from builtins in backticks, but
 this is not one of them. Your bug also occurs outside backticks.
 
 $ nosuchtool 2>/dev/null
 nosuchtool: not found
 $ /nosuchtool 2>/dev/null
 $ (nosuchtool) 2>/dev/null
 
 The following patch fixes this, effectively by making the nosuchtool
 case like the /nosuchtool case (this involves forking while it is known
 that the command does not exist, but simplifies the code).
 
 There is a similar issue with the 'builtin' command but this is
 intertwined with some other stderr changes, so it will wait.
 
 -- 
 Jilles Tjoelker
 
 --G4iJoqBmSsgzjUCe
 Content-Type: text/x-diff; charset=us-ascii
 Content-Disposition: attachment; filename="unknowncmd-fd2.patch"
 
 Index: bin/sh/exec.c
 ===================================================================
 --- bin/sh/exec.c	(revision 196885)
 +++ bin/sh/exec.c	(working copy)
 @@ -429,6 +429,7 @@
  			outfmt(out2, "%s: %s\n", name, strerror(e));
  	}
  	entry->cmdtype = CMDUNKNOWN;
 +	entry->u.index = 0;
  	return;
  
  success:
 Index: bin/sh/eval.c
 ===================================================================
 --- bin/sh/eval.c	(revision 196885)
 +++ bin/sh/eval.c	(working copy)
 @@ -713,12 +713,7 @@
  				do_clearcmdentry = 1;
  			}
  
 -		find_command(argv[0], &cmdentry, 1, path);
 -		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
 -			exitstatus = 127;
 -			flushout(&errout);
 -			return;
 -		}
 +		find_command(argv[0], &cmdentry, 0, path);
  		/* implement the bltin builtin here */
  		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
  			for (;;) {
 @@ -740,7 +735,7 @@
  
  	/* Fork off a child process if necessary. */
  	if (cmd->ncmd.backgnd
 -	 || (cmdentry.cmdtype == CMDNORMAL
 +	 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
  	    && ((flags & EV_EXIT) == 0 || have_traps()))
  	 || ((flags & EV_BACKCMD) != 0
  	    && (cmdentry.cmdtype != CMDBUILTIN
 Index: tools/regression/bin/sh/execution/unknown1.0
 ===================================================================
 --- tools/regression/bin/sh/execution/unknown1.0	(revision 0)
 +++ tools/regression/bin/sh/execution/unknown1.0	(revision 0)
 @@ -0,0 +1,29 @@
 +# $FreeBSD$
 +
 +nosuchtool 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +/var/empty/nosuchtool 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +(nosuchtool) 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +(/var/empty/nosuchtool) 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +/ 2>/dev/null
 +[ $? -ne 126 ] && exit 1
 +PATH=/usr bin 2>/dev/null
 +[ $? -ne 126 ] && exit 1
 +
 +dummy=$(nosuchtool 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$(/var/empty/nosuchtool 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$( (nosuchtool) 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$( (/var/empty/nosuchtool) 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$(/ 2>/dev/null)
 +[ $? -ne 126 ] && exit 1
 +dummy=$(PATH=/usr bin 2>/dev/null)
 +[ $? -ne 126 ] && exit 1
 +
 +exit 0
 
 Property changes on: tools/regression/bin/sh/execution/unknown1.0
 ___________________________________________________________________
 Added: svn:keywords
    + FreeBSD=%H
 
 
 --G4iJoqBmSsgzjUCe--
Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Sun Sep 20 21:17:57 UTC 2009 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=137659 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/137659: commit references a PR
Date: Tue,  6 Oct 2009 22:00:30 +0000 (UTC)

 Author: jilles
 Date: Tue Oct  6 22:00:14 2009
 New Revision: 197820
 URL: http://svn.freebsd.org/changeset/base/197820
 
 Log:
   sh: Send the "xyz: not found" message to redirected fd 2.
   This also fixes that trying to execute a non-regular file with a command
   name without '/' returns 127 instead of 126.
   The fix is rather simplistic: treat CMDUNKNOWN as if the command were found
   as an external program. The resulting fork is a bit wasteful but executing
   unknown commands should not be very frequent.
   
   PR:		bin/137659
 
 Added:
   head/tools/regression/bin/sh/execution/unknown1.0   (contents, props changed)
 Modified:
   head/bin/sh/eval.c
   head/bin/sh/exec.c
 
 Modified: head/bin/sh/eval.c
 ==============================================================================
 --- head/bin/sh/eval.c	Tue Oct  6 21:49:13 2009	(r197819)
 +++ head/bin/sh/eval.c	Tue Oct  6 22:00:14 2009	(r197820)
 @@ -713,12 +713,7 @@ evalcommand(union node *cmd, int flags, 
  				do_clearcmdentry = 1;
  			}
  
 -		find_command(argv[0], &cmdentry, 1, path);
 -		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
 -			exitstatus = 127;
 -			flushout(&errout);
 -			return;
 -		}
 +		find_command(argv[0], &cmdentry, 0, path);
  		/* implement the bltin builtin here */
  		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
  			for (;;) {
 @@ -740,7 +735,7 @@ evalcommand(union node *cmd, int flags, 
  
  	/* Fork off a child process if necessary. */
  	if (cmd->ncmd.backgnd
 -	 || (cmdentry.cmdtype == CMDNORMAL
 +	 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
  	    && ((flags & EV_EXIT) == 0 || have_traps()))
  	 || ((flags & EV_BACKCMD) != 0
  	    && (cmdentry.cmdtype != CMDBUILTIN
 
 Modified: head/bin/sh/exec.c
 ==============================================================================
 --- head/bin/sh/exec.c	Tue Oct  6 21:49:13 2009	(r197819)
 +++ head/bin/sh/exec.c	Tue Oct  6 22:00:14 2009	(r197820)
 @@ -429,6 +429,7 @@ loop:
  			outfmt(out2, "%s: %s\n", name, strerror(e));
  	}
  	entry->cmdtype = CMDUNKNOWN;
 +	entry->u.index = 0;
  	return;
  
  success:
 
 Added: head/tools/regression/bin/sh/execution/unknown1.0
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/tools/regression/bin/sh/execution/unknown1.0	Tue Oct  6 22:00:14 2009	(r197820)
 @@ -0,0 +1,29 @@
 +# $FreeBSD$
 +
 +nosuchtool 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +/var/empty/nosuchtool 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +(nosuchtool) 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +(/var/empty/nosuchtool) 2>/dev/null
 +[ $? -ne 127 ] && exit 1
 +/ 2>/dev/null
 +[ $? -ne 126 ] && exit 1
 +PATH=/usr bin 2>/dev/null
 +[ $? -ne 126 ] && exit 1
 +
 +dummy=$(nosuchtool 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$(/var/empty/nosuchtool 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$( (nosuchtool) 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$( (/var/empty/nosuchtool) 2>/dev/null)
 +[ $? -ne 127 ] && exit 1
 +dummy=$(/ 2>/dev/null)
 +[ $? -ne 126 ] && exit 1
 +dummy=$(PATH=/usr bin 2>/dev/null)
 +[ $? -ne 126 ] && exit 1
 +
 +exit 0
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: jilles 
State-Changed-When: Fri Oct 23 15:10:59 UTC 2009 
State-Changed-Why:  
Fixed. No MFC planned. 

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