2013-09-12 23:42:26 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
sname, err := ptsname(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = unlockpt(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:47:38 -04:00
|
|
|
t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
|
2013-09-12 23:42:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return p, t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptsname(f *os.File) (string, error) {
|
2014-05-20 17:18:52 -04:00
|
|
|
var n _C_uint
|
2014-12-17 16:23:24 -05:00
|
|
|
err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
|
2013-09-12 23:42:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-05-20 17:18:52 -04:00
|
|
|
return "/dev/pts/" + strconv.Itoa(int(n)), nil
|
2013-09-12 23:42:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func unlockpt(f *os.File) error {
|
2014-05-20 17:18:52 -04:00
|
|
|
var u _C_int
|
|
|
|
// use TIOCSPTLCK with a zero valued arg to clear the slave pty lock
|
2014-12-17 16:23:24 -05:00
|
|
|
return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
2013-09-12 23:42:26 -04:00
|
|
|
}
|