Bug 30247

Summary: sh cannot redirect to /dev/fd/1 or from /dev/fd/0
Product: Base System Reporter: marcolz <marcolz>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me CC: sys
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   

Description marcolz 2001-09-01 13:40:06 UTC
	FreeBSD's /bin/sh cannot redirect a filedescriptor to 'itself'.
	The only problem I have encountered throught this (this is how
	I found out), is that /usr/bin/mkdep cannot write to /dev/stdout.
	This affects FreeBSD 2.2 through 5.0 current.

Fix: 

I don't know why the open on redir.c:217 fails, so I don't
	have a fix.
How-To-Repeat: 	In /bin/sh enter:

	echo foo > /dev/fd/1

	cat < /dev/fd/0
Comment 1 marcolz 2001-09-01 15:56:48 UTC
It fails beacuse /dev/fd/1 is no longer connected because it is closed
on redir.c:148.

This seems to work:

--- redir.c.orig	Tue Oct  3 23:13:14 2000
+++ redir.c	Sat Sep  1 16:14:30 2001
@@ -143,13 +143,9 @@
 					break;
 				}
 			}
-			if (!try) {
+			if (!try)
 				sv->renamed[fd] = i;
-				close(fd);
-			}
 			INTON;
-		} else {
-			close(fd);
 		}
 		if (fd == 0)
 			fd0_redirected++;
@@ -186,6 +182,7 @@
 			error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
 movefd:
 		if (f != fd) {
+			close(fd);
 			copyfd(f, fd);
 			close(f);
 		}

Marc
Comment 2 Jonathan Chen freebsd_committer freebsd_triage 2002-07-07 17:25:19 UTC
State Changed
From-To: open->closed


Already fixed [src/bin/sh/redir.c 1.12.2.2, 1.16]