0

What explains the following inconsistency in the reported contents of /dev/fd?

erhannis@mnode6:/dev/fd$ ll /dev/fd/
total 0
dr-x------ 2 erhannis erhannis  0 Jan 12 22:10 .
dr-xr-xr-x 9 erhannis erhannis  0 Jan 12 22:10 ..
lrwx------ 1 erhannis erhannis 64 Jan 12 22:10 0 -> /dev/pts/8
lrwx------ 1 erhannis erhannis 64 Jan 12 22:10 1 -> /dev/pts/8
lrwx------ 1 erhannis erhannis 64 Jan 12 22:10 2 -> /dev/pts/8
lr-x------ 1 erhannis erhannis 64 Jan 12 22:10 3 -> /proc/24334/fd
erhannis@mnode6:/dev/fd$ ll
total 0
dr-x------ 2 erhannis erhannis  0 Jan 12 21:42 .
dr-xr-xr-x 9 erhannis erhannis  0 Jan 12 21:42 ..
lrwx------ 1 erhannis erhannis 64 Jan 12 21:42 0 -> /dev/pts/8
lrwx------ 1 erhannis erhannis 64 Jan 12 21:42 1 -> /dev/pts/8
lrwx------ 1 erhannis erhannis 64 Jan 12 21:42 2 -> /dev/pts/8
lrwx------ 1 erhannis erhannis 64 Jan 12 21:55 255 -> /dev/pts/8

Specifically, note that ll alone shows a file 255 linked to /dev/pts/8, but ll /dev/fd/ shows instead a file 3 linked to /proc/24334/fd. (The proc number changes each time; I suspect it to be the process number of ll itself.)

...I also note, now, that the dates are different - am I getting a different directory for . than for /dev/fd/? I further note that I can't create a file in /dev/fd (with either path).

Erhannis
  • 229

1 Answers1

2

In the first case, ls list the contents the /dev/fd -> /proc/self/fd -> /proc/<pid_of_ls>/fd, and in the second case, that of /proc/<pid_of_the_shell>/fd.

While /dev/fd is a (magical) symbolic link to /proc/self/fd, . (the current directory, as listed by ls when called without arguments) isn't, and it was already resolved to /proc/<pid_of_the_shell>/fd when you called cd /dev/fd before.

ll alone shows a file 255 linked to /dev/pts/8

That's a file descriptor used internally by the bash shell, and which being opened with the O_CLOEXEC flag, is automatically closed when the execve(2) system call is used to execute another program (like /bin/ls in your case).

I further note that I can't create a file in /dev/fd (with either path).

Creating files is not possible in the procfs filesystem on Linux.

You can however mount another filesystem over any directory under /proc -- or bind mount any file or directory over any path under /proc. Just like anywhere else.