NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: /bin/sh fd 12



    Date:        Mon, 13 Sep 2021 23:32:39 -0400
    From:        Jan Schaumann <jschauma%netmeister.org@localhost>
    Message-ID:  <20210914033238.GJ6164%netmeister.org@localhost>

  | 0, 1, and 2 are obvious, but fd 12 did not seem
  | obvious to me.
  |
  | Descriptor 12 being open to the current terminal means
  | I can do this:
  |
  | $ echo foo >&12
  | foo
  | $ 

That's a bug, I will fix it.

  | But I can also:
  |
  | $ echo foo >&13
  | foo
  | $

That's also a bug, a similar one, the same fix should apply.

  | even though fd 13 did not show up under /proc/$$/fd/.

No, it is created for the echo command.

  | Where does that fd come from, and why is not shown
  | under /proc/$$/fd?

When you redirect standard output of a built-in command, the
existing standard output needs to be moved somewhere else (saved)
before the new one can be opened (dup'd in this case).  13 is
the next available fd, so that's where it is moved to - just in
time for the dup() back to fd 1...   When echo is done, fd 13
is moved back to fd 1, so it is closed again before you get a
chance to look.

  | And what's the purpose of fds 12 and 13?

12 is the script input (which, when there is no script, is a
copy of stdin, which is the terminal ... strange as it seems,
when a tty is stdin/stdout/stderr it is open read/write on all
three fd's).

13 is as above.   But those numbers are not fixed, in various
circumstances others might be used.

  | When using /bin/ksh, I see a different extraneous fd, fd 10,

Same thing as fd 12 in /bin/sh - all Bourne shell clones will have
something similar.

  | but I can't write to it:

/bin/ksh only allows you to reference fd's 0..9 (so do many shells
incidentally, that's all that's guaranteed by POSIX).  That's why
the "illegal file descriptor name".

  | Is this documented anywhere?

No.   Aside from /proc/*/fd these things are supposed to be invisible
(an internal implementation detail) - you won't see them via the fdflags
sh built-in for example.

kre



Home | Main Index | Thread Index | Old Index