[prev in list] [next in list] [prev in thread] [next in thread] 

List:       9fans
Subject:    Re: [9fans] on cp /fd/1 /fd/0
From:       Charles Forsyth <charles.forsyth () gmail ! com>
Date:       2015-05-12 18:54:49
Message-ID: CAOw7k5jQFt0M1=REru9=vT7CBdVWH3aYj4jJPDqshtv4O1OyKA () mail ! gmail ! com
[Download RAW message or body]

On 12 May 2015 at 18:10, Daniel Bastos <dbastos@toledo.com> wrote:

> could never work because it requires opening /fd/1 for reading, which
> is not possible. Is this conclusion incorrect?
>

It's not right. I'll try again.

/fd/1 gives a name to file descriptor 1.
When opened, you get a new file descriptor (not 1) that refers to the same
open file instance as file descriptor 1.
If file descriptor 1 was opened with a given mode (M, say), then /fd/1 can
only
be opened with a mode compatible with mode M.
Opening for read (OREAD) works with existing mode M being OREAD or ORDWR;
opening for write (OWRITE) works with existing mode being OWRITE or ORDWR;
opening for read-write ( ORDWR) is compatible only with ORDWR.

If opening with /fd/1 returns a permission error (inappropriate use),
that's because
the mode used to open it wasn't compatible with the mode of the open or
create
that put an open file in file descriptor slot 1.

The file permission shown for /fd/N tells you the open mode of the
underlying file descriptor:
if only w, then only OWRITE, if only r, then OREAD, if rw, then ORDWR.

Finally, you can indeed open /fd/1 for reading if file descriptor 1 was
itself opened for read:

      h% rc <>[1]/dev/cons    # open /dev/cons read-write as file
descriptor 1
      h% ls -l /fd/1
--rw------- d 0 bootes bootes 0 Jul  2  2014 /fd/1   # it says it's
read-write
      h% echo hi >/fd/1    # i can write
hi
      h% read -n 1 /fd/1    # i can read
      hi yourself
hi yourself


Something I wrote earlier came out as the opposite of what I'd intended:

> (the new open can add OCEXEC, which will apply to both)

I don't know why I said that. I meant to write that it will "apply only to
the newly opened file".

[Attachment #3 (text/html)]

<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On 12 May 2015 \
at 18:10, Daniel Bastos <span dir="ltr">&lt;<a href="mailto:dbastos@toledo.com" \
target="_blank">dbastos@toledo.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div \
id=":4gs" class="" style="overflow:hidden">could never work because it requires \
opening /fd/1 for reading, which<br> is not possible. Is this conclusion \
incorrect?</div></blockquote></div><div class="gmail_extra"><br></div>It&#39;s not \
right. I&#39;ll try again.</div><div class="gmail_extra"><br>/fd/1 gives a name to \
file descriptor 1.</div><div class="gmail_extra">When opened, you get a new file \
descriptor (not 1) that refers to the same open file instance as file descriptor \
1.</div><div class="gmail_extra">If file descriptor 1 was opened with a given mode \
(M, say), then /fd/1 can only</div><div class="gmail_extra">be opened with a mode \
compatible with mode M.</div><div class="gmail_extra">Opening for read (OREAD) works \
with existing mode M being OREAD or ORDWR;</div><div class="gmail_extra">opening for \
write (OWRITE) works with existing mode being OWRITE or ORDWR;</div><div \
class="gmail_extra">opening for read-write ( ORDWR) is compatible only with \
ORDWR.</div><div class="gmail_extra"><br></div><div class="gmail_extra">If opening \
with /fd/1 returns a permission error (inappropriate use), that&#39;s \
because</div><div class="gmail_extra">the mode used to open it wasn&#39;t compatible \
with the mode of the open or create</div><div class="gmail_extra">that put an open \
file in file descriptor slot 1.</div><div class="gmail_extra"><br></div><div \
class="gmail_extra">The file permission shown for /fd/N tells you the open mode of \
the underlying file descriptor:</div><div class="gmail_extra">if only w, then only \
OWRITE, if only r, then OREAD, if rw, then ORDWR.</div><div \
class="gmail_extra"><br></div><div class="gmail_extra">Finally, you can indeed open \
/fd/1 for reading if file descriptor 1 was itself opened for read:</div><div \
class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">      \
h% rc &lt;&gt;[1]/dev/cons      # open /dev/cons read-write as file descriptor \
1</div><div class="gmail_extra">         h% ls -l /fd/1</div><div \
class="gmail_extra">--rw------- d 0 bootes bootes 0 Jul   2   2014 /fd/1    # it says \
it&#39;s read-write</div><div class="gmail_extra">         h% echo hi &gt;/fd/1      \
# i can write</div><div class="gmail_extra">hi</div><div class="gmail_extra">         \
h% read -n 1 /fd/1      # i can read</div><div class="gmail_extra">         hi \
yourself</div><div class="gmail_extra">hi yourself</div><div><br></div></div><div \
class="gmail_extra"><br></div><div class="gmail_extra">Something I wrote earlier came \
out as the opposite of what I&#39;d intended:</div><div \
class="gmail_extra"><br></div><div class="gmail_extra"><span \
style="color:rgb(80,0,80);font-size:12.8000001907349px">&gt; (the new open can add \
OCEXEC, which will apply to both)</span><br></div><div class="gmail_extra"><span \
style="color:rgb(80,0,80);font-size:12.8000001907349px"><br></span></div><div \
class="gmail_extra"><span style="color:rgb(80,0,80);font-size:12.8000001907349px">I \
don&#39;t know why I said that. I meant to write that it will &quot;apply only to the \
newly opened file&quot;.</span></div><div class="gmail_extra"><span \
style="color:rgb(80,0,80);font-size:12.8000001907349px"><br></span></div></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic