Linux TTY

From NovaOrdis Knowledge Base
Jump to: navigation, search

External

Internal

Overview

TTY is a Linux subsystem that provides conduits through which keyboard strokes captured while the focus is in a certain terminal such as iTerm2 or xterm, or a text mode console, are turned into the right characters and delivered to UNIX processes associated with that terminal. At the same time, the TTY subsystem funnels the output generated by those processes back to the terminal they are associated with. The "TTY" name comes from "teletype", which used to be electro-mechanical machines that read keyboard entires, turned them into electrical signals and sent them across the wire to to other teletypes.

TTY Device

The TTY subsystem has several kernel components, listed below. Instances of these components, connected together, form what is known as "TTY devices", sometimes referred to as just "TTY". Each TTY device has a corresponding character device under /dev, names similarly to /dev/ttys005 or /dev/pts/0. When a user logs in on a particular TTY device, that user must become the owner of the corresponding character device file. The actual ownership is set by the login programs that runs with root privileges. The ps -l command displays the TTY devices associated with current processes:

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  80   0 -  2912 -      pts/0    00:00:00 entrypoint
0 R     0     6     1 98  80   0 -  1082 -      pts/0    00:00:21 yes
4 S     0     7     0  0  80   0 -  2940 -      pts/1    00:00:00 bash
0 R     0    21     7  0  80   0 - 11333 -      pts/1    00:00:00 ps

Multitasking works differently depending on whether you're working from a text-based terminal or a graphical user interface. Linux from a text-based shell terminal supports just one foreground process per virtual terminal (TTY device). However, from the practical perspective of the user, a windowed environment supports several active windows that effectively serve as multiple simultaneous foreground processes.

TTY Device Components

TTYKernelTE.png

Line Discipline

A kernel-level component providing line editing capabilities. The line discipline attached to the corresponding TTY device is displayed by

stty -a -F <device>

Line Editing

TTY devices provide line editing services, where, for example, a backspace key actually removes the last typed character from an internal editing buffer, instead of being sent to the user process. This functionality is implemented by a component known as line discipline. Line discipline also contains options for character echoing, automatic conversion between CR and LF, flow control, etc. User processes have the choice of disabling this functionality, by putting the line discipline in "raw" mode, instead of the default "cooked" (or "canonical") mode. The kernel provides several line disciplines, but only one is attached to a given TTY device at a time.

The line discipline and line editing capabilities of the TTY device allow us to Ctrl-C a process that enters into an infinite loop and accidentally becomes too busy to be able to handle keyboard input.

TTY Driver

The TTY Driver is a kernel component that implements session management. The TTY driver does not have an execution context - it has data fields and methods, but the data gets updated only if the methods get called by a process or a kernel interrupt handler. As such, the driver maintains the foreground process group ID, but in a passive way, updated by the session leader. It also keeps track of the size of the connected terminal, passively, updated by the terminal emulator. The TTY driver may be associated with several processes but only the processes that are part of the foreground group will receive input from the TTY. Likewise, only the foreground job will be allowed to write to the TTY device.

The size of the terminal attached to the TTY device is displayed by:

stty -a -F <device>

Session Management

TTY session management allows one user to interact with multiple processes running simultaneously in user space, one at a time. This includes the capability to suspend, kill or send programs to background, bring programs into foreground, and direct the user input to the foreground process only. The TTY subsystem implements these features in the TTY driver.

Blocking I/O

The TTY maintains a kernel data buffer, and when the buffer is full, the write call executed by the processes that try to write will block, moving the writing process in an interruptible sleep state.

Terminal Emulator

https://en.wikipedia.org/wiki/Terminal_emulator

A terminal emulator can be implemented in kernel - as it is the case for the Linux console - or in user space, as it is the case for text-based user interfaces such as iTerm2 or xterm. The need to move the terminal emulation in user space, while still keeping the TTY subsystem that provides session management and line discipline, led to the development of pseudo terminals.

Pseudo Terminals

https://en.wikipedia.org/wiki/Pseudoterminal

A pseudoterminal ("pseudo TTY" or "PTY") is a pair of pseudo-devices (a master and a slave) that provide a communication channel. They emulate a physical computer text terminal.

TTYUserSpaceTE.png

TTY Operations

Determine the Controlling TTY for a Shell

tty command prints the name of TTY character device connected to standard input.

tty
/dev/pts/0

Get Information about a TTY Device

stty -a -F /dev/pts/0

When -F <device> is not specified, the command interacts with the TTY device associated with the current shell.

The command returns the size of the terminal attached to this TTY device, the line discipline, job control configuration, etc.

Configuring a TTY Device

stty -F <device-name> <attribute> <value>
stty -F /dev/pts/0 rows 30

Disable Echo

stty -echo

Restore to Sane Defaults

stty sane