Knowledge about pseudo Terminals

Source: Internet
Author: User
Tags telnet program

I saw a good article about the pseudo-terminal. I posted it and studied it in another day.

Many network service applications such as L2TP, PPTP, and Telnet on Linux use pseudo terminals. Some may ask about this concept and write down what I know for discussion.

I. Terminal
To understand the Pseudo Terminal, let's take a look at what is the terminal ).

A terminal is a type of terminal device. It has multiple types and generally uses tty for short.

1. Serial Port Terminal (/dev/ttysx)
The serial port terminal (serial port terminal) is a terminal device connected by a computer serial port. The computer regards each serial port as a character device.
The device names of these serial ports are/dev/ttys0 and/dev/ttys1, which correspond to COM1 and com2 in the dossystem respectively.

  1. [Root @ Kendo ~] # Ls-L/dev/TTYs *
  2. CrW-RW ---- 1 root uucp 4, 64 Jan 8 13:39/dev/ttys0
  3. CrW-RW ---- 1 root uucp 4, 65 Jan 8 13:39/dev/ttys1
  4. CrW-RW ---- 1 root uucp 4, 66 Jan 8 13:39/dev/ttys2
  5. CrW-RW ---- 1 root uucp 4, 67 Jan 8 13:39/dev/ttys3

Copy code

2. Console terminal (/dev/ttyn,/dev/console)
In Linux, a computer monitor is usually called a console ). It simulates a Linux terminal (term = Linux ),
The associated device files are: tty0, tty1, tty2 ....... When a user logs on from the console, tty1 is used. With Alt + [F1-F6] keys, we can switch
To tty2, tty3 ...... Go above. Tty1-tty6 is called a virtual terminal, while tty0 is an alias of the currently used virtual terminal. The information generated by the system is sent to the terminal.
Therefore, no matter which virtual terminal is being used, the system information will be sent to the console terminal. Users can log on to different virtual terminals, so that the system can have a few
Different sessions exist. Only the system or Super User Root can write data to/dev/tty0.

As a test example, run the following command on the console:

  1. # Echo "write to ttys0">/dev/ttys0

Copy code

In the serial port terminal, you can see the output:

  1. # Write to ttys0

Copy code

3. control terminal (/dev/tty)
The control terminal is not a device, but a process. For this concept, Chapter 9th of UNIX advanced programming is described in detail.

Ii. logon from Terminal

During Linux boot, The INIT process will be run and/etc/inittab will be executed (this is related to the specific init type. I used the init of busybox, but its essence is the same ):

  1. [Root @ Skynet ~] # Cat/etc/inittab
  2. : Sysinit:/etc/init. d/RCS
  3. : Respawn:/sbin/Getty 9600 ttys0

Copy code

It calls Getty to open ttys0 at the specified baud rate, that is, the serial port terminal. After stdout, stdin, and stderr are all set to the backup, and the Getty output is a prompt such as "login:", waiting for user input.

After you enter the user name, Getty executes the login program, similar to execle ("login "). Login can call getpass () to display the password and read the user password. And call getpwnam for password verification. If the call is successful, execle ("shell") is called "). In this way, the login user has a shell.

3. Pseudo Terminal
The above logon process is not fully practical for network users. Obviously, a network user does not need a serial port or a monitor. What he needs is on his local display device,
Run the Linux Shell. Taking telnetd as an example, it should at least be like this:
Upload

Download Attachment
(10.05 KB)

Figure 1: telnet login hypothetical Diagram

Here, this "a final device" is naturally not an actual physical terminal device, because there is no such device at all. In this way, the concept of Pseudo Terminal is introduced. A pseudo-terminal device is a special Terminal Driver. It does not drive a physical device, but is used to direct terminal output to an application for processing. Pseudo-terminal devices exist to provide a method to simulate the behavior of serial terminals under program control.

The biggest difference between a Pseudo Terminal and a terminal is that it always appears in pairs instead of a single one. It can be divided into "pseudo-terminal master device (/dev/ptymn)" and "pseudo-terminal Slave Device ". (/Dev/ttymn ). M and n are named as follows:

  1. M: p q r s t u v w x y z a B c d e a total of 16
  2. N: 0 1 2 3 4 5 6 7 8 9 a B c d e f a total of 16

Copy code

By default, a maximum of 256 instances are supported.

Any input written to the master device of the Pseudo Terminal serves as the input from the device of the Pseudo Terminal, and vice versa. Similar to pipelines, such:
Uploading from

Download Attachment
(8.89 KB)

A typical pseudo-Terminal Process structure is as follows:
Uploading from

Download Attachment
(14.96 KB)

The key to this figure is: if you think of a Pseudo Terminal as a traditional terminal device and the master device as an "interface" for reading and writing data from a process, how does it work, just like traditional terminals.

The above is just a local process that introduces the network and corresponds to telnetd. It should look like this:
Uploading from

Download Attachment
(10.71 KB)

The same login method becomes like this:
1. If someone uses the telnet program to connect to the local server online, the telnetd program may start to connect to the device ptyp2 (m2) (a pseudo-terminal master device ).
2. telnetd generates a sub-process and carries out the Getty program. It opens a corresponding ttyp2 (S2) corresponding to the slave device, and sets stdin \ stdout \ stderr;
3. When telnetd obtains a character from the remote end through the Kernel TCP/IP protocol stack, the character is passed to the Getty program through m2 and S2, the getty program returns "login:" string information to the network through S2, m2, and telnetd;
4. In this way, the login program communicates with the telnetd program through the "Pseudo Terminal;

4. Number of pseudo Terminals
For Linux applications, it is critical to know the number of pseudo terminals, or it directly determines the maximum number of supported users, such as pptp vpn applications. (There are no redundant ones to open.
False terminal device ).
For 2.6.x

  1. Device Drivers --->
  2. Character devices --->
  3. [*] Legacy (BSD) Pty support
  4. (256) Maximum number of legacy Pty in use

Copy code

Can be set. It should be adjusted to be large enough to support applications. At the same time, the/dev directory should contain the corresponding device files:

  1. # Ls-L/dev/ptyp *
  2. CrW-r -- 1 Root 2, 0 Dec 18 05:36/dev/ptyp0
  3. CrW-r -- 1 Root 2, 1 Dec 18 05:36/dev/ptyp1
  4. CrW-r -- 1 Root 2, 2 Dec 18 05:36/dev/ptyp2
  5. CrW-r -- 1 Root 2, 3 Dec 18 05:36/dev/ptyp3

Copy code

  1. # Ls-L/dev/ttyp *
  2. CrW ------- 1 Root 3, 0 Dec 18 05:36/dev/ttyp0
  3. CrW ------- 1 Root 3, 1 Dec 18 05:36/dev/ttyp1
  4. CrW-r -- 1 Root 3, 2 Dec 18 05:36/dev/ttyp2
  5. CrW ------- 1 Root 3, 3 Dec 18 05:36/dev/ttyp3

Copy code

In this way, it is depressing to specify the number of commands. Because it has a quantity limit problem, the maximum value is 256.

To solve this problem, Linux introduces a new naming method: unix98_ptys. The kernel explains this as follows:

  1. Linux has traditionally used the BSD-like names/dev/ptyxx
  2. Masters and/dev/ttyxx for slaves of pseudo terminals. This scheme
  3. Has a number of problems. The gnu c library glibc 2.1 and later,
  4. However, supports the unix98 Naming Standard: in order to acquire
  5. Pseudo Terminal, a process opens/dev/ptmx; the number of the pseudo
  6. Terminal is then made available to the process and the pseudo
  7. Terminal slave can be accessed as/dev/pts/<number>. What was
  8. Traditionally/dev/ttyp2 will then be/dev/pts/2, for example.
  9. All modern Linux systems use the unix98 ptys. Say y unless
  10. You're on an embedded system and want to conserve memory.

Copy code

In this way, you can access the pseudo-terminal device by accessing the/dev/ptmx device file. For example, on my PPTP server, when five users dial in:

  1. # Ls-L/dev/pts/
  2. CrW ------- 1 Root 136, 0 Jan 8 0
  3. CrW ------- 1 Root 136, 1 Jan 8 08:08 1
  4. CrW ------- 1 Root 136, 2 Jan 8 2
  5. CrW ------- 1 Root 136, 3 Jan 8 3
  6. CrW ------- 1 Root 136, 4 Jan 8 4


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.