The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: chet.ramey@case.edu (Chet Ramey)
Subject: [TUHS] UNIX of choice these days?
Date: Wed, 27 Sep 2017 09:50:10 -0400	[thread overview]
Message-ID: <4e49a311-704d-6b70-92b3-27c6565f8aa0@case.edu> (raw)
In-Reply-To: <20170927020214.GG28606@mcvoy.com>

On 9/26/17 10:02 PM, Larry McVoy wrote:

>>> Yeah, but still doesn't answer the question, application-wise.?? I'm gonna
>>> guess (without digging through the source) that it's a "dup2(0, 255)*" or
>>> something, to "save" a copy of stdin/out/err for some obscure reason.
>>
>> I already answered this.
> 
> OK, I'm gonna be that guy because I've learned when I ask, I learn.  
> 
> You answered but I didn't get any insight.  Why have an extra fd talking
> to the tty?  bash has 0, 1, 2 talking to it.   If it were redirected 
> would it have 255 pointing to some random tty?  I don't get the reason
> for the extra fd.

OK. The only way to guarantee you have a descriptor open to your
controlling terminal is to open /dev/tty yourself. You can run `bash -im'
and have yourself an interactive shell with job control enabled no matter
where stdin/stdout/stderr point, and they can be redirected at any point
during execution, so you can't count on them. But why do you have to have
the fd in the first place?

My initial message said you need it for "job control and other terminal
operations." Here's what that means.

You initially need a handle to your controlling terminal to determine
whether or not you have one and whether you're in the background. If you
don't have a controlling terminal, job control is impossible. You need the
terminal's process group to determine whether or not you're in the
foreground, and to get that you need a handle to the controlling tty. Every
shell does this.

Once you've determined the original tty pgrp, you need to become a process
group leader if you're not one already and set the terminal's process group
to your own so you can stay in the foreground. If you don't, you can't read
from the terminal. If you can't change the terminal's process group, you
can't enable job control. You need the handle to call tcsetpgrp(). Again,
this is something every shell does.

Now that you've set up job control, and you have a handle to your
controlling terminal, you might as well keep it around -- your controlling
terminal is not going to change throughout the life of the process.
You'll need that handle every time you want to set the terminal's process
group, and that's the essence of job control: setting the terminal's
process group to the process group ID of whatever you've determined is the
foreground job (which, most of the time, is yourself). So bash saves it
to an fd that is unlikely to collide with anything else, which is essential
because bash allows the file descriptor numbers used in redirections to
consist of multiple digits. (I explained the algorithm bash uses; other
shells do something different. dash and zsh use fd 10. mksh uses 24 (?).
ksh93 uses fd 2 no matter what, so you don't get job control if you
redirect stderr away from the terminal.)

There is another "terminal operation" for which the controlling terminal
is required: saving and restoring terminal state. Bash fetches the terminal
attributes before running an external command, and restores the attributes
if a process exits with a non-zero status or is stopped. The former is a
useful convenience; the latter is required. You need the handle for the
calls to tcgetattr()/tcsetattr().

You can get away without all of this stuff, of course. But that makes job
control harder and less efficient.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://cnswww.cns.cwru.edu/~chet/



  reply	other threads:[~2017-09-27 13:50 UTC|newest]

Thread overview: 110+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-25 12:07 Norman Wilson
2017-09-25 14:16 ` Clem Cole
2017-09-25 15:13   ` Warner Losh
2017-09-25 16:51     ` Warner Losh
2017-09-26  0:56       ` ron minnich
2017-09-25 15:18   ` Larry McVoy
2017-09-25 15:30     ` Warner Losh
2017-09-25 23:49     ` Dave Horsfall
2017-09-26  2:06       ` Chet Ramey
2017-09-26 14:53         ` Larry McVoy
2017-09-26 15:17           ` Chet Ramey
2017-09-26 21:23           ` Dave Horsfall
2017-09-26 21:43             ` Arthur Krewat
2017-09-26 21:45             ` Grant Taylor
2017-09-27  0:58               ` Dave Horsfall
2017-09-27  1:37                 ` Chet Ramey
2017-09-27  2:02                   ` Larry McVoy
2017-09-27 13:50                     ` Chet Ramey [this message]
2017-09-27 14:17                       ` Larry McVoy
2017-09-28  8:10                         ` Derek Fawcus
2017-09-28 12:34                           ` Chet Ramey
     [not found]                             ` <20170928174420.GA41732@accordion.employees.org>
2017-09-28 17:57                               ` Derek Fawcus
2017-09-28 18:04                                 ` Chet Ramey
2017-09-27  3:42                   ` Dave Horsfall
2017-09-27 14:35                     ` Chet Ramey
  -- strict thread matches above, loose matches on Subject: below --
2017-09-30 15:17 Norman Wilson
2017-09-30 20:29 ` Kevin Bowling
2017-09-30 21:56   ` Bakul Shah
2017-09-30 22:37     ` Kevin Bowling
2017-09-25 12:46 [TUHS] Unix " Doug McIlroy
2017-09-25 13:57 ` Clem Cole
2017-09-23 23:39 [TUHS] UNIX " Nelson H. F. Beebe
2017-09-21  2:28 Rudi Blom
2017-09-20  0:12 Arthur Krewat
2017-09-20  0:26 ` Larry McVoy
2017-09-20  0:39 ` Dave Horsfall
2017-09-20  1:03   ` Lyndon Nerenberg
2017-09-20 20:56     ` jason-tuhs
2017-09-23  9:17   ` Dario Niedermann
2017-09-23  9:36     ` Steve Mynott
2017-09-23 10:03       ` Dario Niedermann
2017-09-23 23:04         ` Dave Horsfall
2017-09-24  0:11           ` Random832
2017-09-24  1:19             ` Dave Horsfall
2017-09-24 13:46       ` Andy Kosela
2017-09-24 14:02         ` ron minnich
2017-09-24 14:06           ` Larry McVoy
2017-09-24 20:36             ` Kurt H Maier
2017-09-24 21:38               ` Bakul Shah
2017-09-24 23:36                 ` Dave Horsfall
2017-09-24 23:50                   ` Steve Nickolas
2017-09-25  0:03                     ` Wesley Parish
2017-09-25 15:36                       ` Tony Finch
2017-09-26  0:42                         ` Wesley Parish
2017-09-26  9:54                           ` Tony Finch
2017-09-26 14:41                           ` Larry McVoy
2017-09-26 17:34                             ` Bakul Shah
2017-09-26 17:39                               ` Warner Losh
2017-09-26 18:26                                 ` Bakul Shah
2017-09-26 17:43                               ` Larry McVoy
2017-09-26 19:44                                 ` Grant Taylor
2017-09-26 23:22                             ` Wesley Parish
2017-09-25  0:51                     ` Charles Anthony
2017-09-25  0:36                   ` Dan Cross
2017-09-25  0:44                     ` Grant Taylor
2017-09-25  0:56                   ` Bakul Shah
2017-09-25 15:45                     ` Tony Finch
2017-09-25 16:14                       ` Bakul Shah
2017-09-25  7:41                   ` Andy Kosela
2017-09-25  7:43                     ` Cory Smelosky
2017-09-25 10:14                       ` Andy Kosela
2017-09-25  9:58                     ` Steve Nickolas
2017-09-25 11:14                       ` Derek Fawcus
2017-09-25 11:48                       ` Andrew Warkentin
2017-09-24 15:26           ` Christian Barthel
2017-09-24 17:33             ` Clem Cole
2017-09-24 17:33           ` Clem Cole
2017-09-23 23:00     ` Dave Horsfall
2017-09-26 22:00     ` Christian Groessler
2017-09-20  4:42 ` Grant Taylor
2017-09-20  8:31   ` Mutiny 
2017-09-20  9:15 ` Steve Nickolas
2017-09-20 16:58   ` Arthur Krewat
2017-09-20 17:05     ` Steve Nickolas
2017-09-20 17:53     ` Henry Bent
2017-09-20 18:12       ` Arthur Krewat
2017-09-20 18:33         ` Brad Spencer
2017-09-20 19:20           ` Henry Bent
2017-09-20 19:37           ` Arthur Krewat
2017-09-20 19:58             ` Jacob Ritorto
2017-09-20 22:29               ` Ian Zimmerman
2017-09-20 22:31                 ` Warner Losh
2017-09-20 12:52 ` Chet Ramey
2017-09-20 13:33 ` Nemo
2017-09-20 15:39 ` Clem Cole
2017-09-20 15:42 ` Jon Steinhart
2017-09-20 16:58   ` Ian Zimmerman
2017-09-20 17:09     ` Jon Steinhart
2017-09-20 17:31     ` Arthur Krewat
2017-09-20 22:40 ` Steve Simon
2017-09-20 22:51   ` Erik Berls
2017-09-20 23:37 ` Robert Brockway
2017-09-21  1:47 ` Derrik Walker v2.0
2017-09-21  3:54 ` Gregg Levine
2017-09-21 14:33 ` Nicholas Chappell
2017-09-21 16:38   ` Mutiny 
2017-09-21 16:42     ` gilbertmm
2017-09-21 18:30     ` Grant Taylor
2017-09-21 23:34     ` Dave Horsfall
2017-09-25 10:36 ` Thomas Kellar

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=4e49a311-704d-6b70-92b3-27c6565f8aa0@case.edu \
    --to=chet.ramey@case.edu \
    /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).