Ubuntu

Comment 12 for bug 619269

Johan Kiviniemi (ion) wrote :

I must apologize: I managed to completely misread the strace output due to being too tired to think.

What *really* seems to be happening:

• The input file descriptor is duplicated and the original fd is closed.
• The duplicated fd is marked FD_CLOEXEC (dash, bash) or closed just before exec in children (zsh).
• The script *is* evaluated as it’s being read.
• The original half of the input pipe can be closed freely by the script as the shell is reading from a duplicate of the fd that won’t be seen by any children.

dash reading from a file:

open("/tmp/foo.sh", O_RDONLY) = 3
fcntl64(3, F_DUPFD, 10) = 10
close(3) = 0
fcntl64(10, F_SETFD, FD_CLOEXEC) = 0

read(10, "echo foo\n", 8192) = 9
write(1, "foo\n", 4) = 4

dash reading from a pipe:

open("/proc/self/fd/9", O_RDONLY) = 3
fcntl64(3, F_DUPFD, 10) = 10
close(3) = 0
fcntl64(10, F_SETFD, FD_CLOEXEC) = 0

read(10, "exec 9<&-; ls -l /proc/self/fd\n", 8192) = 31
fcntl64(9, F_DUPFD, 10) = 11
close(9) = 0
fcntl64(11, F_SETFD, FD_CLOEXEC) = 0
close(11) = 0

lr-x------ 1 ion ion 64 2011-03-16 22:26 0 -> /dev/null
lrwx------ 1 ion ion 64 2011-03-16 22:26 1 -> /dev/pts/5
lrwx------ 1 ion ion 64 2011-03-16 22:26 2 -> /dev/pts/5
lr-x------ 1 ion ion 64 2011-03-16 22:26 3 -> /proc/13746/fd

bash reading from a pipe:

open("/proc/self/fd/9", O_RDONLY|O_LARGEFILE) = 3

dup2(3, 255) = 255
close(3) = 0
fcntl64(255, F_SETFD, FD_CLOEXEC) = 0

read(255, "e", 1) = 1
read(255, "x", 1) = 1
read(255, "e", 1) = 1
read(255, "c", 1) = 1
read(255, " ", 1) = 1

fcntl64(9, F_DUPFD, 10) = 10

fcntl64(10, F_SETFD, FD_CLOEXEC) = 0
close(9) = 0
close(10) = 0

lr-x------ 1 ion ion 64 2011-03-16 22:26 0 -> /dev/null
lrwx------ 1 ion ion 64 2011-03-16 22:26 1 -> /dev/pts/5
lrwx------ 1 ion ion 64 2011-03-16 22:26 2 -> /dev/pts/5
lr-x------ 1 ion ion 64 2011-03-16 22:26 3 -> /proc/13789/fd

zsh reading from a pipe:

open("/proc/self/fd/9", O_RDONLY|O_NOCTTY|O_LARGEFILE) = 3
fcntl64(3, F_DUPFD, 10) = 11
close(3) = 0

read(11, "exec 9<&-; ls -l /proc/self/fd\n", 4096) = 31

fcntl64(9, F_DUPFD, 10) = 12
close(9) = 0

close(12) = 0

[pid 14022] close(11) = 0

[pid 14022] execve("/bin/ls", ["ls", "-l", "/proc/self/fd"], [/* 53 vars */]) = 0

lr-x------ 1 ion ion 64 2011-03-16 22:33 0 -> /dev/null
lrwx------ 1 ion ion 64 2011-03-16 22:33 1 -> /dev/pts/5
lrwx------ 1 ion ion 64 2011-03-16 22:33 2 -> /dev/pts/5
lr-x------ 1 ion ion 64 2011-03-16 22:33 3 -> /proc/14022/fd