Why dup()?

Clark Wang dearvoid at gmail.com
Mon Oct 16 14:06:06 AEDT 2017


On Sun, Oct 15, 2017 at 1:36 AM, Jim Knoble <jmknoble at pobox.com> wrote:

>
> In particular, 'sh -c "cat >/dev/null" | (while read X; do echo $X; done;
> echo EOF )' does not produce immediate 'EOF'.
>

The shell does not handle "cat > /dev/null" and "exec cat > /dev/null" in
the same way. See the following strace output:

$ strace sh -c "cat > /dev/null"
[...]
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fcntl(1, F_DUPFD, 10)                   = 10
close(1)                                = 0
fcntl(10, F_SETFD, FD_CLOEXEC)          = 0
dup2(3, 1)                              = 1
close(3)                                = 0
clone(child_stack=NULL,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x7fd55c1dd9d0) = 27476
wait4(-1,

$ strace sh -c "exec cat > /dev/null"
[...]
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fcntl(1, F_DUPFD, 10)                   = 10
close(1)                                = 0
fcntl(10, F_SETFD, FD_CLOEXEC)          = 0
dup2(3, 1)                              = 1
close(3)                                = 0
execve("/bin/cat", ["cat"], [/* 32 vars */]) = 0
[...]


More information about the openssh-unix-dev mailing list