Saturday, May 23, 2020

Linux terminal subsystem



The terminal subsystem consists of three layers

  • The upper layer implements the character device interface ( open, read, write, close …)
  • The line discipline
  • The lower layer which communicates with the hardware or the pseudoterminal.

The line discipline glues the upper and the lower layer together.
The tty subsystem is passive: it only reacts on either a key being pressed or the user requesting something from it.

How terminal communicate with process(file descriptor file)
Terminal read and write operation
A Unix based system processes communicate with the terminal through terminal character device file, all the text we write in the terminal are store in the input buffer of this file and then the process may read from this to get the data. 

In this mechanism file descriptor 0 SDTIN used for read data from device file and file descriptor 1 STDOUT used for writing data to the file this type of terminal are called hardware-based terminal used in the past.
Now we emulate the terminal using software, a personal computer can run terminal emulator software that replicates the function of a terminal, sometimes allowing concurrent use of local programs and access to a distant terminal host system. The terminal emulators on most Unix-like systems such as gnome-terminal, qterminal, xterm, do emulate physical terminal including support for escaping sequences. 



how process communication with the master and slave terminal system
master and slave terminal
X windows system the program that controls the display, drawing the boxes and buttons.  Suppose we open a terminal emulator in X windows and start writing text in the terminal, the text we write goes from the X window server to terminal emulator which writes the text in the input buffer of "master" pseudo-terminal character device file. The operating system copies data from master to its associated "slave" pseudo-terminal character device file to be read by the process. Each terminal emulator has its own master and slave pseudo-terminal pair.
Usually, the master is connected to a terminal emulator (such as xterm) and the slave is connected to a program being run, most commonly a shell (such as bash). Thus, the slave behaves exactly like a classical terminal.
When the master side is opened, the corresponding slave device can be used in the same manner as any TTY device. The master and the slave device are connected by the Kernel.
Pseudo terminals are used, among others, by network login services (ssh, rlogin, telnet) and to implement terminal emulators (such as xterm, script, screen, tmux, unbuffer, expect). They are also be used to send data to su or passwd (they refuse to read from pipes).

There are two APIs: BSD style and Unix 98 (System V) style.
BSD style pseudo terminals are deprecated on Linux since kernel version 2.6.4 

Unix 98 style

Master pseudo terminals: /dev/ptmx
Slave pseudo terminals: /dev/pts

BSD style

Master: /dev/ptyXY
Slave: /dev/ttyXY

/dev/pts :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudoterminal master (PTM), and a pseudoterminal slave (PTS) device is created in the /dev/pts directory.  Each file descriptor obtained by opening /dev/ptmx is an independent PTM with its own associated PTS, whose path can be found bypassing the file descriptor to its name.
Before opening the pseudoterminal slave, you must pass the master's file descriptor to grantpt() and unlockpt()Once both the pseudoterminal master and slave are open, the slave provides processes with an interface that is identical to that of a real terminal. Data written to the slave is presented on the master file descriptor as input.  Data written to the master is presented to the slave as input.

/dev/tty : 

TTY is the abbreviation of Tele-Typewriter (or Tele-Type)
Originally, a TTY was physically connected to a Unix machine as an input/output device, usually on an RS-232 cable. 

The input was fed on a keyboard.In the beginning, the computer's output was going to a printer and later also to screens It is a special kind of file, it doesn't represent the character device. When a program opens /dev/tty file they get the file descriptor for the so-called controlling terminal that is terminal associated with their process. In simple words, it used for controlling the terminal file.
A TTY (or terminal) device is a special class of a character device.

A Terminal device might act as a controlling terminal for a session, such as
  • virtual consoles
  • serial ports
  • Pseudo-terminal (PTYs)
All registered TTY devices that are present in the kernel are found under sys/class/tty. The most important data structure to implement a TTY driver is the struct tty_driver.

/dev/ttyN: 

These are character device file for representing the virtual console and these virtual consoles has specific features where N represent the number. So let's say in any case if you open /dev/tty1, what you are doing is writing to the terminal of the first virtual console.

We can open six virtual terminals in Linux using CTRL+ALT+F(1 to 6) for each terminal we have one character device file like for the first terminal you have tty1.


No comments:

Post a Comment

Knowing Kali Linux for OSCP

Kali Linux is developed and maintained by the offensive security professional. It is a Debian-based Linux distribution focus at advanced Pen...