Web lists-archives.com

Re: /dev/fd/N not synonymous with file descriptor N; it is on Linux




On 2018-12-16 21:28, Corinna Vinschen wrote:
On Dec 16 17:31, Houder wrote:
L.S.,

/dev/fd/N not synonymous with file descriptor N; it is on Linux

Yes, it is.  Most of the time.  Try this:

$ echo foo | cat /dev/fd/0

The problem is that some of the concepts don't work as desired:

64-@@ cat /dev/fd/0 <<\EOF

If you observe what happens in tcsh in this situation you see that it
doesn't even execute cat as long as you didn't type EOF.  What you type
is written to a tmpfile:

$ ls -l /proc/5980/fd
total 0
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 0 -> /tmp/sh.lVQq04
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 15 -> /dev/pty0
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 16 -> /dev/pty0
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 17 -> /dev/pty0
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 18 -> /dev/pty0
lrwxrwxrwx 1 corinna vinschen 0 Dec 16 21:15 19 -> /dev/pty0

However, this tmpfile has been unlinked already, so it has been moved to the
recycle bin:

$ ls -l /tmp/sh.lVQq04
ls: /tmp/sh.lVQq04: No such file or directory

So the path in the fd subdir doesn't reflect the actual file path.

But after starting cat, cat tries to open /proc/self/fd/0 which
is in fact the non-existing path /tmp/sh.lVQq04.  Bad luck.

Yes Corinna, I am aware of the above. I described it here:

    https://cygwin.com/ml/cygwin/2018-12/msg00040.html
    ( Re: Bash heredoc on FD 3 )

Sorry for NOT expressing myself more clearly.

The STC is, as I wrote, based on what I observed on Linux.

See attachment.

Regards,
Henri
...

 - recurring pattern: the file descriptor to the tmpfile is dup'ed to fd N, where N is equal
   to the one in /dev/fd/N, the device file specified to be opened by the child.

@@ strace -ff -e open,close,unlink,read,write,execve,dup2 -- bash ./here_doc_disx.sh                                                        
# here_doc_disx.sh:
# cat /dev/fd/0 <<\EOF
# Hello, world!
# EOF

execve("/usr/bin/bash", ["bash", "./here_doc_disx.sh"], 0x7ffcaa1b6388 /* 63 vars */) = 0
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3)                                = 0
open("/lib64/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\331\0\0\0\0\0\0"..., 832) = 832
close(3)                                = 0
open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\r\0\0\0\0\0\0"..., 832) = 832
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\10\2\0\0\0\0\0"..., 832) = 832
close(3)                                = 0
open("/dev/tty", O_RDWR|O_NONBLOCK)     = 3
close(3)                                = 0
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
close(3)                                = 0
open("/usr/lib64/gconv/gconv-modules.cache", O_RDONLY) = 3
close(3)                                = 0
open("./here_doc_disx.sh", O_RDONLY)    = 3
read(3, "cat /dev/fd/0 <<\\EOF\nHello, worl"..., 80) = 39
dup2(3, 255)                            = 255
close(3)                                = 0
read(255, "cat /dev/fd/0 <<\\EOF\nHello, worl"..., 39) = 39
strace: Process 1681 attached
[pid  1681] close(255)                  = 0
[pid  1681] open("/tmp/sh-thd-932991243", O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600) = 3
[pid  1681] write(3, "Hello, world!\n", 14) = 14
[pid  1681] open("/tmp/sh-thd-932991243", O_RDONLY) = 4
[pid  1681] close(3)                    = 0
[pid  1681] unlink("/tmp/sh-thd-932991243") = 0
[pid  1681] dup2(4, 0)                  = 0
[pid  1681] close(4)                    = 0
[pid  1681] execve("/usr/bin/cat", ["cat", "/dev/fd/0"], 0x75ebd70e90 /* 62 vars */) = 0
[pid  1681] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[pid  1681] close(3)                    = 0
[pid  1681] open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[pid  1681] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\10\2\0\0\0\0\0"..., 832) = 832
[pid  1681] close(3)                    = 0
[pid  1681] open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
[pid  1681] close(3)                    = 0
[pid  1681] open("/dev/fd/0", O_RDONLY) = 3
[pid  1681] read(3, "Hello, world!\n", 131072) = 14
[pid  1681] write(1, "Hello, world!\n", 14) = 14
Hello, world!
[pid  1681] read(3, "", 131072)         = 0
[pid  1681] close(3)                    = 0
[pid  1681] close(1)                    = 0
[pid  1681] close(2)                    = 0
[pid  1681] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=1681, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
read(255, "", 39)                       = 0
+++ exited with 0 +++
@@ 

#=====
--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple