-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpty_linux.go
More file actions
51 lines (45 loc) · 1.15 KB
/
pty_linux.go
File metadata and controls
51 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//go:build linux
package execx
import (
"fmt"
"os"
"syscall"
"unsafe"
)
func ptyCheck() error {
return nil
}
func openPTY() (*os.File, *os.File, error) {
return openPTYWith(os.OpenFile, ptyIoctl)
}
func openPTYWith(openFile func(string, int, os.FileMode) (*os.File, error), ioctl func(uintptr, uintptr, uintptr) error) (*os.File, *os.File, error) {
master, err := openFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
return nil, nil, err
}
fd := master.Fd()
unlock := int32(0)
if err := ioctl(fd, syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&unlock))); err != nil {
_ = master.Close()
return nil, nil, err
}
var ptyNum uint32
if err := ioctl(fd, syscall.TIOCGPTN, uintptr(unsafe.Pointer(&ptyNum))); err != nil {
_ = master.Close()
return nil, nil, err
}
name := fmt.Sprintf("/dev/pts/%d", ptyNum)
slave, err := openFile(name, os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
_ = master.Close()
return nil, nil, err
}
return master, slave, nil
}
func ptyIoctl(fd uintptr, req uintptr, arg uintptr) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, req, arg)
if errno != 0 {
return errno
}
return nil
}