/proc/$$/fd mystery - {t,}csh vs. bash

Dave Johnson dave-gnhlug-list at davej.org
Thu Jan 8 22:25:31 EST 2009


Michael ODonnell writes:
>  ######## e521:~ 514---> /bin/csh
>  e521:~> echo My PID is $$, contents of my /proc/$$/fd follow... ; ls -l /proc/$$/fd
>  My PID is 27244, contents of my /proc/27244/fd follow...
>  total 0
>  lrwx------ 1 mod mod 64 2009-01-08 20:47 15 -> /dev/pts/0
>  lrwx------ 1 mod mod 64 2009-01-08 20:47 16 -> /dev/pts/0
>  lrwx------ 1 mod mod 64 2009-01-08 20:47 17 -> /dev/pts/0
>  lrwx------ 1 mod mod 64 2009-01-08 20:47 18 -> /dev/pts/0
>  lrwx------ 1 mod mod 64 2009-01-08 20:47 19 -> /dev/pts/0


Ah the wonders of strace....

Because on startup /bin/csh dups stdin/out/err to higher fds...

execve("/bin/csh", ["/bin/csh"], [/* 31 vars */]) = 0
[... blah blah ...]
dup2(0, 16)                             = 16
fcntl64(16, F_SETFD, FD_CLOEXEC)        = 0
dup2(1, 17)                             = 17
fcntl64(17, F_SETFD, FD_CLOEXEC)        = 0
dup2(2, 18)                             = 18
fcntl64(18, F_SETFD, FD_CLOEXEC)        = 0
dup2(16, 19)                            = 19
fcntl64(19, F_SETFD, FD_CLOEXEC)        = 0
[... blah blah ...]
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0

this means /bin/csh uses FDs 16,17,18 as it's stdin/out/err instead of
0/1/2.

when running a program:

after clone(), it just dup()s back to stdin/out/err before exec():

clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7d266f8) = 11073
[... blah blah ...]
[pid 11073] close(0)                    = -1 EBADF (Bad file descriptor)
[pid 11073] dup(19)                     = 0
[pid 11073] fcntl64(0, F_SETFD, 0)      = 0
[pid 11073] close(1)                    = -1 EBADF (Bad file descriptor)
[pid 11073] dup(17)                     = 1
[pid 11073] fcntl64(1, F_SETFD, 0)      = 0
[pid 11073] close(2)                    = -1 EBADF (Bad file descriptor)
[pid 11073] dup(18)                     = 2
[pid 11073] fcntl64(2, F_SETFD, 0)      = 0
[... blah blah ...]
[pid 11073] execve("/bin/ls", ["ls", "-l", "/proc/11071/fd"], [/* 37 vars */]) = 0




-- 
Dave


More information about the gnhlug-discuss mailing list