Linux terminal, network virtual terminal, pseudo-terminal turn from: http://www.xuebuyuan.com/877887.htmlMany of the network Service applications on Linux, such as L2TP, PPTP, Telnet, all use pseudo terminal. There are friends who are asking the concept of this, and write down what I know, for discussion.
First, the terminal
To understand the pseudo-terminal (Pseudo Terminal), first look at what is "terminal" (Terminal).
A terminal is a type of character device that has many types, usually using a TTY for short-range end devices.
1. Serial port terminal (/DEV/TTYSX)
The serial port terminal (Serial Port Terminal) is a terminal device connected using a computer serial port. The computer sees each serial port as a character device.
These serial ports corresponding to the device name is/DEV/TTYS0,/dev/ttys1, respectively, corresponding to the DOS system COM1, COM2, and so on.
- [Email protected] ~]# ls-l/dev/ttys*
- CRW-RW----1 root UUCP 4, Jan 8 13:39/DEV/TTYS0
- CRW-RW----1 root UUCP 4, 8 13:39/dev/ttys1
- CRW-RW----1 root UUCP 4, 8 13:39/dev/ttys2
- CRW-RW----1 root UUCP 4, 8 13:39/DEV/TTYS3
Copy Code
2. Console Terminal (/dev/ttyn,/dev/console)
In Linux systems, computer monitors are often referred to as console terminals. It simulates a type of terminal (Term=linux) of Linux,
The device files associated with it are: Tty0, Tty1, Tty2 .... When the user logs on from the console, Tty1 is used. When using the ALT+[F1-F6] key combination, we can switch
To Tty2, tty3 ... Go above. Tty1–tty6 is called a virtual terminal, and Tty0 is an alias of the virtual terminal that is currently used, and the information generated by the system is sent to the terminal.
Therefore, regardless of which virtual terminal is currently in use, the system information is sent to the console terminal. Users can log on to different virtual terminals so that the system can have several
A different session period exists. Only the system or Superuser root can write to/dev/tty0.
As a test example, under Console terminal, run the command:
- #echo "Write to TtyS0" >/dev/ttys0
Copy Code
The output can be seen in the serial port terminal:
- # Write to TtyS0
Copy Code
3. Control Terminal (/dev/tty)
The control terminal does not face the device, but the face of the process, about this concept, "advanced programming of the UNIX environment," The 9th chapter is discussed in detail.
Second, from the terminal login brief
When the Linux system boots, it runs the Init process, which executes the/etc/inittab (which is related to the specific init type, I use the BusyBox init, but the essence is the same):
- [Email protected] ~]# Cat/etc/inittab
- :: Sysinit:/etc/init.d/rcs
- :: Respawn:/sbin/getty 9600 ttyS0
Copy Code
It will call Getty on the specified baud rate on the ttyS0, i.e., serial port terminal. After successful opening, Stdout,stdin,stderr is set to the standby and then Getty output: "Login:" and other prompts, waiting for user input.
When the user types the user name, Getty executes The login program, similar to the following: Execle ("Login"). Login can call Getpass () to display password: and read the user's password. and call Getpwnam for password authentication. If successful, the call is similar to execle ("shell"). In this way, the logged-on user has a shell.
Third, pseudo-terminal
The above logon process, for network users, but not completely practical. Obviously, the network user does not need a serial port, and does not need a monitor, he needs to be on his local display device, running the Linux shell. This network user is called the network virtual terminal. In the case of telnetd, it should at least be like this:
Figure one: Telnet login imaginary diagram
Here, this "some end device", naturally cannot be an actual physical device, because there is no such equipment. In this way, the concept of pseudo-terminal is introduced. Pseudo terminal equipment is a special terminal drive device, which does not drive a physical device, but is used to direct the output of the terminal to the application for processing. Pseudo-terminal equipment exists to provide a method of simulating serial terminal behavior under program control.
Pseudo-terminal and the terminal in the form of expression, the biggest difference is that it always appears in pairs, rather than a single one. It is divided into "pseudo-terminal master devices (/dev/ptymn)" and "pseudo-terminal slave devices". (/dev/ttymn). where M and N are named in the following way:
- M:p Q R S t u v w x y z a b c d e a total of 16
- N:0 1 2 3 4 5 6 7 8 9 a B c D E f a total of 16
Copy Code
This way, the default support is 256 maximum.
Any input written to the pseudo-terminal master device will be used as a pseudo-terminal input from the device, and vice versa. Similar to pipelines, such as:
A typical pseudo-terminal process structure such as:
The key of this picture is: if the pseudo-terminal from the device as a traditional terminal equipment, the main device as a process read and write data of an "interface", then it works as the traditional terminal.
The above is just a local process, the introduction of the network, corresponding to the telnetd above, should be the following look:
The same way of logging in, it becomes this:
1. If someone uses the Telnet program to connect to the local server on the Internet, the TELNETD program may start connecting to the device Ptyp2 (m2) on a pseudo-terminal master device.
2, TELNETD produces a sub-process, the Getty program, which opens a corresponding from the device corresponding to the TTYP2 (S2), and set stdin\stdout\stderr;
3, telnetd through the kernel TCP/IP protocol stack from the far end to obtain a character, the character will pass through M2, S2 to Getty program, and Getty program through S2, M2 and telnetd program to return to the network "login:" string information;
4, in this way, the login program and the TELNETD program through the "pseudo-terminal" to communicate;
Iv. number of pseudo-terminals
For Linux applications, knowing the number of pseudo-endpoints is a key thing, or it directly determines the maximum number of supported users, such as PPTP VPN applications. (There is no extra to open the
Pseudo-terminal equipment).
For 2.6.X, the
- Device Drivers--->
- Character Devices--->
- [*] Legacy (BSD) PTY support
- (Maximum) Number of legacy PTY in use
Copy Code
can be set. It should be resized to be large enough to support the application. At the same time, the/dev directory should have the appropriate device files:
- #ls-L/dev/ptyp*
- crw-r--r--1 root root 2, 0 Dec 05:36/dev/ptyp0
- crw-r--r--1 root root 2, 1 Dec 05:36/dev/ptyp1
- crw-r--r--1 root root 2, 2 Dec 05:36/DEV/PTYP2
- crw-r--r--1 root root 2, 3 Dec 05:36/dev/ptyp3
Copy Code
- #ls-L/dev/ttyp*
- CRW-------1 root root 3, 0 Dec 05:36/dev/ttyp0
- CRW-------1 root root 3, 1 Dec 05:36/dev/ttyp1
- crw-r--r--1 root root 3, 2 Dec 05:36/DEV/TTYP2
- CRW-------1 root root 3, 3 Dec 05:36/dev/ttyp3
Copy Code
This way of ordering, and to specify the number of ways is really frustrating. Because it has a number of upper limit problems, the maximum is 256.
To solve this problem, Linux introduces a new naming method: Unix98_ptys. The kernel explains this stuff:
- Linux have traditionally used the bsd-like names/dev/ptyxx for
- Masters And/dev/ttyxx for slaves of pseudo terminals. This scheme
- Has a number of problems. The GNU C Library glibc 2.1 and later,
- However, supports the Unix98 naming standard:in order to acquire a
- Pseudo terminal, a process opens/dev/ptmx; The number of the pseudo
- Terminal is then made available to the process and the pseudo
- Terminal slave can be accessed as/dev/pts/<number>. What is
- Traditionally/dev/ttyp2 'll then BE/DEV/PTS/2, for example.
- All modern Linux systems use the Unix98 ptys. Say Y unless
- You ' re on an embedded system and want to conserve memory.
Copy Code
This way, you can access the pseudo-terminal equipment by accessing the/DEV/PTMX device file, such as my PPTP server, when there are five users dialing in:
- # ls-l/dev/pts/
- CRW-------1 root root 136, 0 Jan 8 12:18 0
- CRW-------1 root root 136, 1 Jan 8 08:08 1
- CRW-------1 root root 136, 2 Jan 8 14:10 2
- CRW-------1 root root 136, 3 Jan 8 14:27 3
- CRW-------1 root root 136, 4 Jan 8 08:29 4
Copy Code
Linux terminal, network Virtual terminal, pseudo terminal