9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Charles Forsyth <charles.forsyth@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] on cp /fd/1 /fd/0
Date: Tue, 12 May 2015 19:54:49 +0100	[thread overview]
Message-ID: <CAOw7k5jQFt0M1=REru9=vT7CBdVWH3aYj4jJPDqshtv4O1OyKA@mail.gmail.com> (raw)
In-Reply-To: <CAB08-qqnU-mNTTZHekirNFGQ--NrELg=xsMen+tedT47TaMYWw@mail.gmail.com>

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

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 #2: Type: text/html, Size: 3559 bytes --]

  reply	other threads:[~2015-05-12 18:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-12 11:56 Daniel Bastos
2015-05-12 12:24 ` Iruatã Souza
2015-05-12 12:39   ` lucio
2015-05-12 13:20     ` erik quanstrom
2015-05-12 14:17   ` Daniel Bastos
2015-05-12 16:15     ` Charles Forsyth
2015-05-12 17:10       ` Daniel Bastos
2015-05-12 18:54         ` Charles Forsyth [this message]
2015-05-12 15:52 ` Charles Forsyth

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='CAOw7k5jQFt0M1=REru9=vT7CBdVWH3aYj4jJPDqshtv4O1OyKA@mail.gmail.com' \
    --to=charles.forsyth@gmail.com \
    --cc=9fans@9fans.net \
    /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 NNTP newsgroup(s).