public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Houder <houder@xs4all.nl>
To: cygwin@cygwin.com
Subject: Re: /dev/fd/N not synonymous with file descriptor N; it is on Linux
Date: Mon, 17 Dec 2018 03:41:00 -0000	[thread overview]
Message-ID: <65de2a8fbe9871597460a4fc762f59bc@xs4all.nl> (raw)
In-Reply-To: <20181216202847.GK28727@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 1603 bytes --]

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

[-- Attachment #2: disassembly.txt --]
[-- Type: text/plain, Size: 3201 bytes --]

...

 - 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 +++
@@ 

#=====

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
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

  parent reply	other threads:[~2018-12-17  3:41 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-16 16:31 Houder
2018-12-16 20:28 ` Corinna Vinschen
2018-12-16 20:31   ` Corinna Vinschen
2018-12-16 21:36   ` Wayne Davison
2018-12-16 21:55     ` Corinna Vinschen
2018-12-17  4:30       ` Houder
2018-12-17  9:25         ` Corinna Vinschen
2018-12-17 11:26           ` Corinna Vinschen
2019-01-02 13:56             ` Houder
2018-12-17  3:41   ` Houder [this message]
2019-01-06 20:19 ` Corinna Vinschen
2019-01-22  8:50   ` Houder
2019-01-22  8:57     ` Houder
2019-01-22  9:06       ` Corinna Vinschen
2019-01-22  9:25         ` Houder
2019-01-22  9:42           ` Corinna Vinschen
2019-01-22 10:20             ` Houder
2019-01-22 10:39               ` Corinna Vinschen
2019-01-27 18:39                 ` Houder
2019-01-27 21:57                   ` Corinna Vinschen
2019-01-27 22:12                     ` Corinna Vinschen
2019-01-28 14:15                     ` Houder
2019-01-28 16:51                       ` Corinna Vinschen
2019-01-22  9:02     ` Corinna Vinschen
2019-01-22  9:07       ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65de2a8fbe9871597460a4fc762f59bc@xs4all.nl \
    --to=houder@xs4all.nl \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).