summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2009-11-29 22:33:59 +0000
committerjilles <jilles@FreeBSD.org>2009-11-29 22:33:59 +0000
commit171b4944c1b7aaf979f3823e9bdfcdae155cb5ba (patch)
tree81617e0dd9ce9709a051aa522582d76eb94cb5a5 /tools
parent170d197358ca5cca91353b7adc3914268bbb9f69 (diff)
downloadFreeBSD-src-171b4944c1b7aaf979f3823e9bdfcdae155cb5ba.zip
FreeBSD-src-171b4944c1b7aaf979f3823e9bdfcdae155cb5ba.tar.gz
Fix some cases where file descriptors from redirections leak to programs.
- Redirecting fds that were not open before kept two copies of the redirected file. sh -c '{ :; } 7>/dev/null; fstat -p $$; true' (both fd 7 and 10 remained open) - File descriptors used to restore things after redirection were not set close-on-exec, instead they were explicitly closed before executing a program normally and before executing a shell procedure. The latter must remain but the former is replaced by close-on-exec. sh -c 'exec 7</; { exec fstat -p $$; } 7>/dev/null; true' (fd 10 remained open) The examples above are simpler than the testsuite because I do not want to use fstat or procstat in the testsuite.
Diffstat (limited to 'tools')
-rw-r--r--tools/regression/bin/sh/execution/redir1.027
-rw-r--r--tools/regression/bin/sh/execution/redir2.029
2 files changed, 56 insertions, 0 deletions
diff --git a/tools/regression/bin/sh/execution/redir1.0 b/tools/regression/bin/sh/execution/redir1.0
new file mode 100644
index 0000000..cc5cc49
--- /dev/null
+++ b/tools/regression/bin/sh/execution/redir1.0
@@ -0,0 +1,27 @@
+# $FreeBSD$
+trap ': $((brokenpipe+=1))' pipe
+
+P=${TMPDIR:-/tmp}
+cd $P
+T=$(mktemp -d sh-test.XXXXXX)
+cd $T
+
+brokenpipe=0
+mkfifo fifo1 fifo2
+read dummy >fifo2 <fifo1 &
+{
+ exec 4>fifo2
+} 3<fifo2 # Formerly, sh would keep fd 3 and a duplicate of it open.
+echo dummy >fifo1
+if [ $brokenpipe -ne 0 ]; then
+ rc=3
+fi
+wait
+echo dummy >&4
+if [ $brokenpipe -eq 1 ]; then
+ : ${rc:=0}
+fi
+
+rm fifo1 fifo2
+rmdir ${P}/${T}
+exit ${rc:-3}
diff --git a/tools/regression/bin/sh/execution/redir2.0 b/tools/regression/bin/sh/execution/redir2.0
new file mode 100644
index 0000000..079ee82
--- /dev/null
+++ b/tools/regression/bin/sh/execution/redir2.0
@@ -0,0 +1,29 @@
+# $FreeBSD$
+trap ': $((brokenpipe+=1))' pipe
+
+P=${TMPDIR:-/tmp}
+cd $P
+T=$(mktemp -d sh-test.XXXXXX)
+cd $T
+
+brokenpipe=0
+mkfifo fifo1 fifo2
+{
+ {
+ exec sh -c 'exec <fifo1; read dummy'
+ } 7<&- # fifo2 should be kept open, but not passed to programs
+ true
+} 7<fifo2 &
+
+exec 4>fifo2
+exec 3>fifo1
+echo dummy >&4
+if [ $brokenpipe -eq 1 ]; then
+ : ${rc:=0}
+fi
+echo dummy >&3
+wait
+
+rm fifo1 fifo2
+rmdir ${P}/${T}
+exit ${rc:-3}
OpenPOWER on IntegriCloud