7

On Lubuntu 18.04, I run a shell in lxterminal. Its controlling terminal is the current pseudoterminal slave:

$ tty
/dev/pts/2

I would like to know what relations are between my current controlling terminal /dev/pts/2 and /dev/tty.

  1. /dev/tty acts like my current controlling terminal /dev/pts/2:

    $ echo hello > /dev/tty
    hello
    
    $ cat < /dev/tty
    world
    world
    ^C
    
  2. But they seem to be unrelated files, instead of one being a symlink or hardlink to the other:

    $ ls -lai /dev/tty /dev/pts/2
     5 crw--w---- 1 t    tty 136, 2 May 31 16:38 /dev/pts/2
    13 crw-rw-rw- 1 root tty   5, 0 May 31 16:36 /dev/tty
    

For different sessions with different controlling terminals, if /dev/tty is guaranteed to be their controlling terminals. How can it be different controlling terminals, without being a symlink or hardlink?

So what are their relations and differences? Any help is much appreciated!

This post is originated from an earlier one Do the output of command `tty` and the file `/dev/tty` both refer to the controlling terminal of the current bash process?

  • As mentioned at that Q&A you link to, tty doesn't report the controlling terminal, but the terminal open on stdin if any. See ps -o tty= -p "$$" for the controlling terminal (in practice, they are generally the same at the prompt of an interactive shell). – Stéphane Chazelas Jul 1 '18 at 5:30
  • @StéphaneChazelas Thanks. ps -o tty= -p "$$" works well! Is it not possible to get the controlling terminal of the current shell from /dev/tty? – Tim Dec 1 '18 at 2:59
10

This is documented in the tty manpage in section 4:

The file /dev/tty is a character file with major number 5 and minor number 0, usually of mode 0666 and owner.group root.tty. It is a synonym for the controlling terminal of a process, if any.

In addition to the ioctl(2) requests supported by the device that tty refers to, the ioctl(2) request TIOCNOTTY is supported.

TIOCNOTTY

Detach the calling process from its controlling terminal.

If the process is the session leader, then SIGHUP and SIGCONT signals are sent to the foreground process group and all processes in the current session lose their controlling tty.

This ioctl(2) call works only on file descriptors connected to /dev/tty. It is used by daemon processes when they are invoked by a user at a terminal. The process attempts to open /dev/tty. If the open succeeds, it detaches itself from the terminal by using TIOCNOTTY, while if the open fails, it is obviously not attached to a terminal and does not need to detach itself.

This explains in part why /dev/tty isn’t a symlink to the controlling terminal: it supports an additional ioctl, and there might not be a controlling terminal (but a process can always try to access /dev/tty).

/dev/tty can represent different controlling terminals, without being a link, because the driver which implements it determines what the calling process’ controlling terminal is, if any.

You can think of this as /dev/tty being the controlling terminal, and thus offering functionality which only makes sense for a controlling terminal, whereas /dev/pts/2 etc. are plain terminals, one of which might happen to be the controlling terminal for a given process.

  • Thanks. If a process doesn't have a controlling terminal, what does /dev/ttymean for the process? – Tim Dec 1 '18 at 2:05
  • @Tim Then it can't open it. Please read the manpage carefully. – 炸鱼薯条德里克 Dec 22 '18 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.