2015-08-16 09:47:23 +00:00
|
|
|
package pty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func open() (pty, tty *os.File, err error) {
|
2018-12-28 01:28:06 +00:00
|
|
|
pFD, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_CLOEXEC, 0)
|
2015-08-16 09:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-12-28 01:28:06 +00:00
|
|
|
p := os.NewFile(uintptr(pFD), "/dev/ptmx")
|
|
|
|
// In case of error after this point, make sure we close the ptmx fd.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = p.Close() // Best effort.
|
|
|
|
}
|
|
|
|
}()
|
2015-08-16 09:47:23 +00:00
|
|
|
|
|
|
|
sname, err := ptsname(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:28:06 +00:00
|
|
|
if err := grantpt(p); err != nil {
|
2015-08-16 09:47:23 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:28:06 +00:00
|
|
|
if err := unlockpt(p); err != nil {
|
2015-08-16 09:47:23 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := os.OpenFile(sname, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return p, t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptsname(f *os.File) (string, error) {
|
|
|
|
n := make([]byte, _IOC_PARM_LEN(syscall.TIOCPTYGNAME))
|
|
|
|
|
|
|
|
err := ioctl(f.Fd(), syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0])))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range n {
|
|
|
|
if c == 0 {
|
|
|
|
return string(n[:i]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("TIOCPTYGNAME string not NUL-terminated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func grantpt(f *os.File) error {
|
|
|
|
return ioctl(f.Fd(), syscall.TIOCPTYGRANT, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unlockpt(f *os.File) error {
|
|
|
|
return ioctl(f.Fd(), syscall.TIOCPTYUNLK, 0)
|
|
|
|
}
|