mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
f2614f2107
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
60 lines
1 KiB
Go
60 lines
1 KiB
Go
package pty
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"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
|
|
}
|
|
|
|
sname, err := ptsname(p)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
err = grantpt(p)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
err = unlockpt(p)
|
|
if err != nil {
|
|
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)
|
|
}
|