在Linux中中断“没有回显的控制台输入”
DOS 有int 21h / AH=08H: Console input without echo。
Linux 有类似的东西吗?如果我需要在输入的值显示在终端之前对其进行处理。
回答
在 Linux 下,是 tty 在“发送”到请求程序之前缓冲输入的字符。
这是通过终端模式控制的:raw(无缓冲)或cooked(也分别称为非规范和规范模式)。
这些模式实际上是tty的属性,可以通过tcgetattr和tcsetattr来控制。
例如,可以在此处找到将终端设置为非规范模式而没有回显的代码(有关VTIME和VMIN控制字符的更多信息可以在此处找到)。
那是C,所以我们需要把它翻译成汇编。从源代码tcgetattr我们可以看到,tty 属性是通过 IOCTL 检索到带有命令的 stdin 的TCGETS(值 0x5401),同样,它们使用带有命令TCSETS(值 0x5402)的 IOCTL 进行设置。
读取的结构不是struct termios 但是struct __kernel_termios,它基本上是前者的缩短版本。IOCTL 必须发送到stdin文件(STDIN_FILENO值为 0 的文件描述符)。
知道如何实现tcgetattr,tcsetattr我们只需要获取常量的值(likeICANON和 similar)。
我建议使用编译器(例如这里) 以查找公共常量的值并检查结构的偏移量。
对于非公共常量(在它们的翻译单元之外不可见),我们必须求助于阅读源(这不是特别困难,但必须小心找到正确的源)。
下面是调用 IOCTL 以获取-修改-设置 TTY 属性以启用原始模式的 64 位程序。
然后程序等待单个字符并显示它递增(例如a -> b)。
请注意,该程序已在 Linux (5.x) 下进行了测试,并且由于各种常量在不同的 Unix 克隆中会改变值,因此它不可移植。
我使用了 NASM 并为 定义了一个结构struct __kernel_termios,我还使用了很多符号常量来使代码更具可读性。我真的不喜欢在汇编中使用结构,但 NASM 只是一个薄的宏层(如果你还没有习惯它们,最好习惯它们)。
最后,我假设熟悉 64 位 Linux 汇编编程。
BITS 64
GLOBAL _start
;
; System calls numbers
;
%define SYS_READ 0
%define SYS_WRITE 1
%define SYS_IOCTL 16
%define SYS_EXIT 60
;
; __kernel_termios structure
;
%define KERNEL_NCC 19
struc termios
.c_iflag: resd 1 ;input mode flags
.c_oflag: resd 1 ;output mode flags
.c_cflag: resd 1 ;control mode flags
.c_lflag: resd 1 ;local mode flags
.c_line: resb 1 ;line discipline
.c_cc: resb KERNEL_NCC ;control characters
endstruc
;
; IOCTL commands
;
%define TCGETS 0x5401
%define TCSETS 0x5402
;
; TTY local flags
;
%define ECHO 8
%define ICANON 2
;
; TTY control chars
;
%define VMIN 6
%define VTIME 5
;
; Standard file descriptors
;
%define STDIN_FILENO 0
%define STDOUT_FILENO 1
SECTION .bss
;The char read (reserve a DWORD to make termios_data be aligned on DWORDs boundary)
data resd 1
;The TTY attributes
termios_data resb termios_size
SECTION .text
_start:
;
;Get the terminal settings by sending the TCGETS IOCTL to stdin
;
mov edi, STDIN_FILENO ;Send IOCTL to stdin (Less efficient but more readable)
mov esi, TCGETS ;The TCGETS command
lea rdx, [REL termios_data] ;The arg, the buffer where to store the TTY attribs
mov eax, SYS_IOCTL ;Do the syscall
syscall
;
;Set the raw mode by clearing ECHO and ICANON and setting VMIN = 1, VTIME = 0
;
and DWORD [REL termios_data + termios.c_lflag], ~(ICANON | ECHO) ;Clear ECHO and ICANON
mov BYTE [REL termios_data + termios.c_cc + VMIN], 1
mov BYTE [REL termios_data + termios.c_cc + VTIME], 0
;
;Set the terminal settings
;
mov edi, STDIN_FILENO ;Send to stdin (Less efficient but more readable)
mov esi, TCSETS ;Use TCSETS as the command
lea rdx, [REL termios_data] ;Use the same data read (and altered) before
mov eax, SYS_IOCTL ;Do the syscall
syscall
;
;Read a char
;
mov edi, STDIN_FILENO ;Read from stdin (Less efficient but more readable)
lea rsi, [REL data] ;Read into data
mov edx, 1 ;Read only 1 char
mov eax, SYS_READ ;Do the syscall (Less efficient but more readable)
syscall
;
;Increment the char (as an example)
;
inc BYTE [REL data]
;
;Print the char
;
mov edi, STDOUT_FILENO ;Write to stdout
lea rsi, [REL data] ;Write the altered char
mov edx, 1 ;Only 1 char to write
mov eax, SYS_WRITE ;Do the syscall
syscall
;
;Restore the terminal settins (similar to the code above)
;
mov edi, STDIN_FILENO
mov esi, TCGETS
lea rdx, [REL termios_data]
mov eax, SYS_IOCTL
syscall
;Set ECHO and ICANON
or DWORD [REL termios_data + termios.c_lflag], ICANON | ECHO
mov edi, STDIN_FILENO
mov esi, TCSETS
lea rdx, [REL termios_data]
mov eax, SYS_IOCTL
syscall
;
;Exit
;
xor edi, edi
mov eax, SYS_EXIT
syscall