opnsense-src/tools/regression/bin/sh/execution/redir2.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

29 lines
419 B
Text

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