|
@@ -1,3 +1,6 @@
|
|
|
|
+//go:build solaris
|
|
|
|
+// +build solaris
|
|
|
|
+
|
|
package pty
|
|
package pty
|
|
|
|
|
|
/* based on:
|
|
/* based on:
|
|
@@ -6,122 +9,134 @@ http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libc/port/gen/pt.c
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
- "golang.org/x/sys/unix"
|
|
|
|
"os"
|
|
"os"
|
|
"strconv"
|
|
"strconv"
|
|
"syscall"
|
|
"syscall"
|
|
"unsafe"
|
|
"unsafe"
|
|
)
|
|
)
|
|
|
|
|
|
-const NODEV = ^uint64(0)
|
|
|
|
-
|
|
|
|
func open() (pty, tty *os.File, err error) {
|
|
func open() (pty, tty *os.File, err error) {
|
|
- masterfd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|unix.O_NOCTTY, 0)
|
|
|
|
- //masterfd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_CLOEXEC|unix.O_NOCTTY, 0)
|
|
|
|
|
|
+ ptmxfd, err := syscall.Open("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY, 0)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
- p := os.NewFile(uintptr(masterfd), "/dev/ptmx")
|
|
|
|
|
|
+ p := os.NewFile(uintptr(ptmxfd), "/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.
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
|
|
sname, err := ptsname(p)
|
|
sname, err := ptsname(p)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- err = grantpt(p)
|
|
|
|
- if err != nil {
|
|
|
|
|
|
+ if err := grantpt(p); err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- err = unlockpt(p)
|
|
|
|
- if err != nil {
|
|
|
|
|
|
+ if err := unlockpt(p); err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- slavefd, err := syscall.Open(sname, os.O_RDWR|unix.O_NOCTTY, 0)
|
|
|
|
|
|
+ ptsfd, err := syscall.Open(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
- t := os.NewFile(uintptr(slavefd), sname)
|
|
|
|
|
|
+ t := os.NewFile(uintptr(ptsfd), sname)
|
|
|
|
|
|
- // pushing terminal driver STREAMS modules as per pts(7)
|
|
|
|
- for _, mod := range([]string{"ptem", "ldterm", "ttcompat"}) {
|
|
|
|
- err = streams_push(t, mod)
|
|
|
|
|
|
+ // In case of error after this point, make sure we close the pts fd.
|
|
|
|
+ defer func() {
|
|
if err != nil {
|
|
if err != nil {
|
|
|
|
+ _ = t.Close() // Best effort.
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ // pushing terminal driver STREAMS modules as per pts(7)
|
|
|
|
+ for _, mod := range []string{"ptem", "ldterm", "ttcompat"} {
|
|
|
|
+ if err := streamsPush(t, mod); err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- return p, t, nil
|
|
|
|
-}
|
|
|
|
|
|
|
|
-func minor(x uint64) uint64 {
|
|
|
|
- return x & 0377
|
|
|
|
|
|
+ return p, t, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func ptsdev(fd uintptr) uint64 {
|
|
|
|
- istr := strioctl{ISPTM, 0, 0, nil}
|
|
|
|
- err := ioctl(fd, I_STR, uintptr(unsafe.Pointer(&istr)))
|
|
|
|
|
|
+func ptsname(f *os.File) (string, error) {
|
|
|
|
+ dev, err := ptsdev(f.Fd())
|
|
if err != nil {
|
|
if err != nil {
|
|
- return NODEV
|
|
|
|
|
|
+ return "", err
|
|
}
|
|
}
|
|
- var status unix.Stat_t
|
|
|
|
- err = unix.Fstat(int(fd), &status)
|
|
|
|
- if err != nil {
|
|
|
|
- return NODEV
|
|
|
|
|
|
+ fn := "/dev/pts/" + strconv.FormatInt(int64(dev), 10)
|
|
|
|
+
|
|
|
|
+ if err := syscall.Access(fn, 0); err != nil {
|
|
|
|
+ return "", err
|
|
}
|
|
}
|
|
- return uint64(minor(status.Rdev))
|
|
|
|
|
|
+ return fn, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func ptsname(f *os.File) (string, error) {
|
|
|
|
- dev := ptsdev(f.Fd())
|
|
|
|
- if dev == NODEV {
|
|
|
|
- return "", errors.New("not a master pty")
|
|
|
|
|
|
+func unlockpt(f *os.File) error {
|
|
|
|
+ istr := strioctl{
|
|
|
|
+ icCmd: UNLKPT,
|
|
|
|
+ icTimeout: 0,
|
|
|
|
+ icLen: 0,
|
|
|
|
+ icDP: nil,
|
|
}
|
|
}
|
|
- fn := "/dev/pts/" + strconv.FormatInt(int64(dev), 10)
|
|
|
|
- // access(2) creates the slave device (if the pty exists)
|
|
|
|
- // F_OK == 0 (unistd.h)
|
|
|
|
- err := unix.Access(fn, 0)
|
|
|
|
- if err != nil {
|
|
|
|
- return "", err
|
|
|
|
|
|
+ return ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr)))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func minor(x uint64) uint64 { return x & 0377 }
|
|
|
|
+
|
|
|
|
+func ptsdev(fd uintptr) (uint64, error) {
|
|
|
|
+ istr := strioctl{
|
|
|
|
+ icCmd: ISPTM,
|
|
|
|
+ icTimeout: 0,
|
|
|
|
+ icLen: 0,
|
|
|
|
+ icDP: nil,
|
|
}
|
|
}
|
|
- return fn, nil
|
|
|
|
|
|
+
|
|
|
|
+ if err := ioctl(fd, I_STR, uintptr(unsafe.Pointer(&istr))); err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ var status syscall.Stat_t
|
|
|
|
+ if err := syscall.Fstat(int(fd), &status); err != nil {
|
|
|
|
+ return 0, err
|
|
|
|
+ }
|
|
|
|
+ return uint64(minor(status.Rdev)), nil
|
|
}
|
|
}
|
|
|
|
|
|
-type pt_own struct {
|
|
|
|
- pto_ruid int32
|
|
|
|
- pto_rgid int32
|
|
|
|
|
|
+type ptOwn struct {
|
|
|
|
+ rUID int32
|
|
|
|
+ rGID int32
|
|
}
|
|
}
|
|
|
|
|
|
func grantpt(f *os.File) error {
|
|
func grantpt(f *os.File) error {
|
|
- if ptsdev(f.Fd()) == NODEV {
|
|
|
|
- return errors.New("not a master pty")
|
|
|
|
- }
|
|
|
|
- var pto pt_own
|
|
|
|
- pto.pto_ruid = int32(os.Getuid())
|
|
|
|
- // XXX should first attempt to get gid of DEFAULT_TTY_GROUP="tty"
|
|
|
|
- pto.pto_rgid = int32(os.Getgid())
|
|
|
|
- var istr strioctl
|
|
|
|
- istr.ic_cmd = OWNERPT
|
|
|
|
- istr.ic_timout = 0
|
|
|
|
- istr.ic_len = int32(unsafe.Sizeof(istr))
|
|
|
|
- istr.ic_dp = unsafe.Pointer(&pto)
|
|
|
|
- err := ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr)))
|
|
|
|
- if err != nil {
|
|
|
|
|
|
+ if _, err := ptsdev(f.Fd()); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ pto := ptOwn{
|
|
|
|
+ rUID: int32(os.Getuid()),
|
|
|
|
+ // XXX should first attempt to get gid of DEFAULT_TTY_GROUP="tty"
|
|
|
|
+ rGID: int32(os.Getgid()),
|
|
|
|
+ }
|
|
|
|
+ istr := strioctl{
|
|
|
|
+ icCmd: OWNERPT,
|
|
|
|
+ icTimeout: 0,
|
|
|
|
+ icLen: int32(unsafe.Sizeof(strioctl{})),
|
|
|
|
+ icDP: unsafe.Pointer(&pto),
|
|
|
|
+ }
|
|
|
|
+ if err := ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr))); err != nil {
|
|
return errors.New("access denied")
|
|
return errors.New("access denied")
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func unlockpt(f *os.File) error {
|
|
|
|
- istr := strioctl{UNLKPT, 0, 0, nil}
|
|
|
|
- return ioctl(f.Fd(), I_STR, uintptr(unsafe.Pointer(&istr)))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// push STREAMS modules if not already done so
|
|
|
|
-func streams_push(f *os.File, mod string) error {
|
|
|
|
- var err error
|
|
|
|
|
|
+// streamsPush pushes STREAMS modules if not already done so.
|
|
|
|
+func streamsPush(f *os.File, mod string) error {
|
|
buf := []byte(mod)
|
|
buf := []byte(mod)
|
|
|
|
+
|
|
// XXX I_FIND is not returning an error when the module
|
|
// XXX I_FIND is not returning an error when the module
|
|
// is already pushed even though truss reports a return
|
|
// is already pushed even though truss reports a return
|
|
// value of 1. A bug in the Go Solaris syscall interface?
|
|
// value of 1. A bug in the Go Solaris syscall interface?
|
|
@@ -129,11 +144,9 @@ func streams_push(f *os.File, mod string) error {
|
|
// https://www.illumos.org/issues/9042
|
|
// https://www.illumos.org/issues/9042
|
|
// but since we are not using libc or XPG4.2, we should not be
|
|
// but since we are not using libc or XPG4.2, we should not be
|
|
// double-pushing modules
|
|
// double-pushing modules
|
|
-
|
|
|
|
- err = ioctl(f.Fd(), I_FIND, uintptr(unsafe.Pointer(&buf[0])))
|
|
|
|
- if err != nil {
|
|
|
|
|
|
+
|
|
|
|
+ if err := ioctl(f.Fd(), I_FIND, uintptr(unsafe.Pointer(&buf[0]))); err != nil {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
- err = ioctl(f.Fd(), I_PUSH, uintptr(unsafe.Pointer(&buf[0])))
|
|
|
|
- return err
|
|
|
|
|
|
+ return ioctl(f.Fd(), I_PUSH, uintptr(unsafe.Pointer(&buf[0])))
|
|
}
|
|
}
|