Virtual Console (tty)
Linux console
Linux console aims for kernel or other processes
- to output text-based messages to the user (display)
- to receive text-based input from the user (keyboard)
Therefore, it is a combination of display and keyboard for user interface.
System console
System console is the device which receives all kernel messages and warnings and which allows logins in single user mode. (Reference: CONFIG_VT_CONSOLE)
Linux console devices
- Virtual terminals
- Usually have 6 virtual terminals and switch by Alt+F1, …, and Alt+F6 in text mode (Number 7 for X)
- Or Ctrl+Alt+F1, …, and Ctrl+Alt+F6 in X window mode
- /dev/tty0 or /dev/console: current console or system console
- /dev/tty1, /dev/tty2, …
- virtual consoles
- Serial port (ttyS0, ttyS1…)
- Embedded Linux
- USB serial port (ttyUSB0, ttyUSB1, …)
- such as USB Dongle 3G module
- VGA in text-mode
- Framebuffer (fb)
- Embedded Linux (connect to LCD display)
Linux console_codes or escape code
console_codes(4) - Linux console escape and control sequences
Terminal Emulator
Terminal emulator is a program that emulates a video terminal within some other display architecture, such as
- Konsole
- gnome-terminal
- tilda
- xterm (X Window System)
- iTerm (Mac OS X)
Pseudo devices
Pseudo devices are device nodes on Unix-like systems do not necessarily have to correspond to physical devices.
Examples
/dev/null
/dev/zero
/dev/full
/dev/random
/dev/pts/
Pseudo-terminals
Pseudo-terminals, or PTY, s a pair of pseudo-devices, one of which, the slave, emulates a real text terminal device, the other of which, the master, provides the means by which a terminal emulator process controls the slave.
Master and Slave
- PTM (Pseudo-Terminal Master) & PTS (Pseudo-Terminal Slave)
- Master is not a terminal and just a normal device for read/write.
- Slave is a terminal.write → [Master] — [Slave] → read (stdin for process connected)
read ← [Master] — [Slave] ← write (stdin for master)
- Write to master is treated as input to the foreground process that is connected to slave (like input from keyboard terminal)
- Ctrl+C in terminal emulator will send a SIGINT signal to the foreground process that is connected to slave
- Write to slave is treated as output to master
Other references
Device file
/dev/ptmx
character file (major 5 & minor 2): used to create a pseudoterminal master and slave pair/dev/pts/n
pseudoterminal slave device
Common framework to use pseudo terminal
- Open
/dev/ptmx
filefd_ptm = posix_openpt(O_RDWR);
- or
fd_ptm = open("/dev/ptmx", O_RDWR);
- By opening ptmx device, a fd is returned. Besides, a slave device, let’s say 4, is created under
/dev/pts/4
.
- By opening ptmx device, a fd is returned. Besides, a slave device, let’s say 4, is created under
- Grant access to the slave pseudoterminal
ret = grantpt(fd_ptm);
- Unlock a pseudoterminal master/slave pair
ret = unlockpt(fd_ptm);
- Open slave device
fd_pts = open(ptsname(fd_ptm), O_RDWR);
ptsname
get the corresponding slave device name, such as/dev/pts/4
.
- Fork a child process
- In parent process,
- close fd_pts since it never uses it
- use select to listen message from stdin or from master device (fd_ptm)
- if it is from stdin, then write to master device (fd_ptm). Finally child process will get this message from its stdin.
- if it is from fd_ptm, so it is also from child process (by write to fd_pts), so display it by writing to stdout
- In child process,
- close fd_ptm
- close opened stdin, stdout and stderr because we need to redirect them to fd_pts
- set terminal parameter of slave to RAW mode by tcsetattr
- use
dup(fd_pts);
three times to duplicate 0, 1, and 2 fro fd_pts - then use
execl
-like functions to execute a command such as echo, bc. - so for command, its stdin, stdout and stderr are all redirected to fd_pts.
Examples
- terminal emulator
- script(1)
- expect(1)
- ssh(1), telnet(1), rlogin(1) etc
No comments :
Post a Comment