Home > Backend Development > Golang > How to use pseudo terminal (pty) in golang language

How to use pseudo terminal (pty) in golang language

PHPz
Release: 2023-04-14 09:35:24
Original
1842 people have browsed it

In modern operating systems, the terminal window is one of the most basic ways of human-computer interaction. However, our understanding of the terminal window should not be limited to input and output. In fact, the terminal window should have multiple functions such as "respond to signals", "modify window size", etc., so as to meet people's diverse needs. In the Linux system, we can use pseudo terminal (PTY) to implement these functions, and the Golang language also provides many convenient APIs for operating PTY, allowing us to easily control and interact with the terminal.

1. What is a pseudo terminal

First of all, in order to understand the pseudo terminal in depth, we must first understand the two special files under the Linux system-tty and pty. The tty file is usually located in /dev/ttyx and is the interface for users to interact with the Linux kernel. For example, when you enter commands in the terminal, you interact through the tty interface. PTY is a logical concept that represents a pseudo terminal. It is usually located in /dev/ptmx in Linux systems and is the interface for users to interact with terminal devices. The purpose of the pseudo terminal is to create a virtual terminal window through it, read the file handle created by the lock when the user enters characters, and at the same time forward the byte stream from the application to the child process, and return the output stream from the child process to users.

In fact, pseudo-terminal is a concept of simulation (simulated terminal). It is a set of terminal interfaces provided by the kernel. Through them, users and terminal programs can perform tasks using standard input and output streams and terminal devices. Interaction. The pseudo terminal will create two file descriptors, one for reading and one for writing. One file descriptor can be used to write user input, and the other can be used to read data returned by the terminal program. Under this mechanism, we can control the terminal window to achieve more flexible operations.

2. How to use pseudo-terminal in golang language

In golang language, the steps to use pseudo-terminal are divided into three steps:

  1. Create pty

In golang language, we can create a pty through the StartProcess function in the os library. This function will return a value of type *os.Process, and this value is the pty we can control. Handle, which contains input and output streams and other information related to the process. The code is as follows:

import (
    "os"
    "os/exec"
    "syscall"
)

cmd := "sh"
exe := exec.Command(cmd)
// 设置ptmx为console
exe.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}

// 打开ptmx
ptmx, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)

// 解锁ptmx
tmp := make([]byte, 0)
if _, err := ptmx.Read(tmp); err != nil {
    panic(err)
}

// 给pty配置大小
ws := &Winsize{Width: 80, Height: 40, Xpixel: 0, Ypixel: 0}
if err := IoctlSetWinsize(int(ptmx.Fd()), uintptr(unsafe.Pointer(ws))); err != nil {
    panic(err)
}

// 创建子进程pty
exe.Stdout = ptmx  //配置pty的输入输出流
exe.Stdin = ptmx
exe.Stderr = ptmx
if err := exe.Start(); err != nil {
    panic(err)
}
pid := exe.Process.Pid
Copy after login
  1. Read and write pty

After pty is created, we can write and read to pty through the Read and Write methods of the os library The data is there, the code is as follows:

//读取输出
buf := make([]byte, 1024)
n, err := ptmx.Read(buf)
if err != nil {
    panic(err)
}
fmt.Println(string(buf[:n]))

//写入输入
if _, err := ptmx.Write([]byte("ls -al\n")); err != nil {
    panic(err)
}
Copy after login
  1. Waiting for the pty process to end

Finally, we need to wait for the pty process to exit through the exe.Wait() method.

Summary

Through the above steps, we can implement pty in golang language. Overall, the pseudo terminal is a very important Linux system mechanism. Using it, we can achieve very powerful terminal control and interaction functions. Although it brings some complexity, if you understand it, you will have a better chance of building excellent terminal window interactive applications.

The above is the detailed content of How to use pseudo terminal (pty) in golang language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template