2023-04-26 20:20:43 -04:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
|
|
|
|
2019-04-19 07:03:37 -04:00
|
|
|
package pty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func open() (pty, tty *os.File, err error) {
|
|
|
|
p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// In case of error after this point, make sure we close the ptmx fd.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = p.Close() // Best effort.
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sname, err := ptsname(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unlockpt(p); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:20:43 -04:00
|
|
|
t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0) //nolint:gosec // Expected Open from a variable.
|
2019-04-19 07:03:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return p, t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptsname(f *os.File) (string, error) {
|
|
|
|
var n _C_uint
|
2024-01-08 04:22:09 -05:00
|
|
|
err := ioctl(f, syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))) //nolint:gosec // Expected unsafe pointer for Syscall call.
|
2019-04-19 07:03:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "/dev/pts/" + strconv.Itoa(int(n)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unlockpt(f *os.File) error {
|
|
|
|
var u _C_int
|
2024-01-08 04:22:09 -05:00
|
|
|
// use TIOCSPTLCK with a pointer to zero to clear the lock.
|
|
|
|
return ioctl(f, syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) //nolint:gosec // Expected unsafe pointer for Syscall call.
|
2019-04-19 07:03:37 -04:00
|
|
|
}
|