freebsd-dev/tools/regression/bin/sh/execution/redir1.0
Jilles Tjoelker e1ef314121 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.
2009-11-29 22:33:59 +00:00

28 lines
421 B
Plaintext

# $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}