github.com/dolfly/pty@v1.2.1/pty_linux.go (about) 1 //go:build linux 2 //+build linux 3 4 package pty 5 6 import ( 7 "os" 8 "strconv" 9 "syscall" 10 "unsafe" 11 ) 12 13 func open() (pty, tty *os.File, err error) { 14 p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) 15 if err != nil { 16 return nil, nil, err 17 } 18 // In case of error after this point, make sure we close the ptmx fd. 19 defer func() { 20 if err != nil { 21 _ = p.Close() // Best effort. 22 } 23 }() 24 25 sname, err := ptsname(p) 26 if err != nil { 27 return nil, nil, err 28 } 29 30 if err := unlockpt(p); err != nil { 31 return nil, nil, err 32 } 33 34 t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0) //nolint:gosec // Expected Open from a variable. 35 if err != nil { 36 return nil, nil, err 37 } 38 return p, t, nil 39 } 40 41 func ptsname(f *os.File) (string, error) { 42 var n _C_uint 43 err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))) //nolint:gosec // Expected unsafe pointer for Syscall call. 44 if err != nil { 45 return "", err 46 } 47 return "/dev/pts/" + strconv.Itoa(int(n)), nil 48 } 49 50 func unlockpt(f *os.File) error { 51 var u _C_int 52 // use TIOCSPTLCK with a pointer to zero to clear the lock 53 return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) //nolint:gosec // Expected unsafe pointer for Syscall call. 54 }