thanks for checking into this
the next release ksh93/tests/io.sh will take the conventions into account
---
set \
        /proc/self/fd   open    \
        /proc/$$/fd     open
fdfs=/dev/fd
semantics=dup
while   (( $# >= 2 ))
do      if      [[ -e $1 ]]
        then    fdfs=$1
                semantics=$2
                break
        fi
        shift 2
done

exec 3<> file1
if      command exec 4< $fdfs/3
then    read -u3 line
        read -u4 line
        exp=foo
        case $line in
        foo)    got=open ;;
        bar)    got=dup ;;
        *)      got=error ;;
        esac
        if      [[ $semantics != $got ]]
        then    err_exit "'4< $fdfs/3' uses $got(2) instead of $semantics(2) -- 
expected '$exp', got '$line'"
        fi
fi

# 2004-11-25 ancient /dev/fd/N redirection bug fix
x=$(
        {
                print -n 1
                print -n 2 > $fdfs/2
                print -n 3
                print -n 4 > $fdfs/2
        }  2>&1
)
case $x in
1234)   got=dup ;;
4)      got=open ;;
*)      got=error ;;
esac
case $semantics in
dup)    exp=1234 ;;
*)      exp=4 ;;
esac
[[ $x == $exp ]] || err_exit "$fdfs/N uses $got(2) instead of $semantics(2) -- 
expected '$exp', got '$x'"
---

On Fri, 12 Dec 2008 17:50:39 -0600 Nicolas Williams wrote:
> On Fri, Dec 12, 2008 at 05:36:36PM -0600, Nicolas Williams wrote:
> > So, yes, open(2) of /dev/fd/N is as a dup(2) of N.

> Also, every reference I can find says that open(2) of /dev/fd/N is
> supposed to be a dup(2) of file descriptor N, on Solaris, on Linux, on
> BSD, on Plan 9.

> And, apparently also the convention is that open(2) of /proc/self/fd/N
> is NOT equivalent to dup(2) of fd N (it opens the same file, device,
> whatever, but does not keep the offset from fildes N).

> % (read line <#((200)) ; print $line ; read line
> </proc/self/fd/9 ; print $line) <&9
> 1041
> 1001
> % 

> And that's supposed to be so for Linux and Solaris (haven't checked
> BSD).  An open(2) of /proc/self/fd/N is supposed to check file
> permissions too:

> % pfexec chown root /tmp/foo
> % pfexec chmod 600 /tmp/foo 
> % (read line <#((200)) ; print $line ; read line
> </proc/self/fd/9 ; print $line) <&9
> 1041
> ksh93: /proc/self/fd/9: cannot open [Permission denied]
> 1041
> % 


Reply via email to