Skip to content

Commit

Permalink
Simplify solaris implementation. (#120)
Browse files Browse the repository at this point in the history
Remove golang.org/x/sys/unix dependency for solaris
Add new go build tag directives
  • Loading branch information
creack authored May 29, 2021
1 parent a76ea1c commit f73a158
Show file tree
Hide file tree
Showing 41 changed files with 318 additions and 252 deletions.
18 changes: 18 additions & 0 deletions asm_solaris_amd64.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build gc
//+build gc

#include "textflag.h"

//
// System calls for amd64, Solaris are implemented in runtime/syscall_solaris.go
//

TEXT ·sysvicall6(SB),NOSPLIT,$0-88
JMP syscall·sysvicall6(SB)

TEXT ·rawSysvicall6(SB),NOSPLIT,$0-88
JMP syscall·rawSysvicall6(SB)
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module github.com/creack/pty

go 1.13

8 changes: 7 additions & 1 deletion ioctl.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// +build !windows,!solaris
//go:build !windows && !solaris
//+build !windows,!solaris

package pty

import "syscall"

const (
TIOCGWINSZ = syscall.TIOCGWINSZ
TIOCSWINSZ = syscall.TIOCSWINSZ
)

func ioctl(fd, cmd, ptr uintptr) error {
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr)
if e != 0 {
Expand Down
3 changes: 2 additions & 1 deletion ioctl_bsd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build darwin dragonfly freebsd netbsd openbsd
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd)
//+build darwin dragonfly freebsd netbsd openbsd

package pty

Expand Down
31 changes: 24 additions & 7 deletions ioctl_solaris.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
//go:build solaris
//+build solaris

package pty

import (
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

//go:cgo_import_dynamic libc_ioctl ioctl "libc.so"
//go:linkname procioctl libc_ioctl
var procioctl uintptr

const (
// see /usr/include/sys/stropts.h
I_PUSH = uintptr((int32('S')<<8 | 002))
I_STR = uintptr((int32('S')<<8 | 010))
I_FIND = uintptr((int32('S')<<8 | 013))

// see /usr/include/sys/ptms.h
ISPTM = (int32('P') << 8) | 1
UNLKPT = (int32('P') << 8) | 2
PTSSTTY = (int32('P') << 8) | 3
ZONEPT = (int32('P') << 8) | 4
OWNERPT = (int32('P') << 8) | 5

// see /usr/include/sys/termios.h
TIOCSWINSZ = (uint32('T') << 8) | 103
TIOCGWINSZ = (uint32('T') << 8) | 104
)

type strioctl struct {
ic_cmd int32
ic_timout int32
ic_len int32
ic_dp unsafe.Pointer
icCmd int32
icTimeout int32
icLen int32
icDP unsafe.Pointer
}

// Defined in asm_solaris_amd64.s.
func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)

func ioctl(fd, cmd, ptr uintptr) error {
return unix.IoctlSetInt(int(fd), uint(cmd), int(ptr))
if _, _, errno := sysvicall6(uintptr(unsafe.Pointer(&procioctl)), 3, fd, cmd, ptr, 0, 0, 0); errno != 0 {
return errno
}
return nil
}
3 changes: 3 additions & 0 deletions pty_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build darwin
//+build darwin

package pty

import (
Expand Down
3 changes: 3 additions & 0 deletions pty_dragonfly.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build dragonfly
//+build dragonfly

package pty

import (
Expand Down
3 changes: 3 additions & 0 deletions pty_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build freebsd
//+build freebsd

package pty

import (
Expand Down
9 changes: 6 additions & 3 deletions pty_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build linux
//+build linux

package pty

import (
Expand Down Expand Up @@ -28,7 +31,7 @@ func open() (pty, tty *os.File, err error) {
return nil, nil, err
}

t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0)
t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0) //nolint:gosec // Expected Open from a variable.
if err != nil {
return nil, nil, err
}
Expand All @@ -37,7 +40,7 @@ func open() (pty, tty *os.File, err error) {

func ptsname(f *os.File) (string, error) {
var n _C_uint
err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))) //nolint:gosec // Expected unsafe pointer for Syscall call.
if err != nil {
return "", err
}
Expand All @@ -47,5 +50,5 @@ func ptsname(f *os.File) (string, error) {
func unlockpt(f *os.File) error {
var u _C_int
// use TIOCSPTLCK with a pointer to zero to clear the lock
return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) //nolint:gosec // Expected unsafe pointer for Syscall call.
}
3 changes: 3 additions & 0 deletions pty_netbsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build netbsd
//+build netbsd

package pty

import (
Expand Down
3 changes: 3 additions & 0 deletions pty_openbsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build openbsd
//+build openbsd

package pty

import (
Expand Down
148 changes: 80 additions & 68 deletions pty_solaris.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build solaris
//+build solaris

package pty

/* based on:
Expand All @@ -10,119 +13,130 @@ import (
"strconv"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

const NODEV = ^uint64(0)

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 {
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)
if err != nil {
return nil, nil, err
}

err = grantpt(p)
if err != nil {
if err := grantpt(p); err != nil {
return nil, nil, err
}

err = unlockpt(p)
if err != nil {
if err := unlockpt(p); err != nil {
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 {
return nil, nil, err
}
t := os.NewFile(uintptr(slavefd), sname)
t := os.NewFile(uintptr(ptsfd), sname)

// In case of error after this point, make sure we close the pts fd.
defer func() {
if err != nil {
_ = t.Close() // Best effort.
}
}()

// pushing terminal driver STREAMS modules as per pts(7)
for _, mod := range []string{"ptem", "ldterm", "ttcompat"} {
err = streams_push(t, mod)
if err != nil {
if err := streamsPush(t, mod); err != nil {
return nil, nil, err
}
}

return p, t, nil
}

func minor(x uint64) uint64 {
return x & 0377
}

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 {
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 {
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 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)

// XXX I_FIND is not returning an error when the module
// is already pushed even though truss reports a return
// value of 1. A bug in the Go Solaris syscall interface?
Expand All @@ -131,10 +145,8 @@ func streams_push(f *os.File, mod string) error {
// but since we are not using libc or XPG4.2, we should not be
// 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
}
err = ioctl(f.Fd(), I_PUSH, uintptr(unsafe.Pointer(&buf[0])))
return err
return ioctl(f.Fd(), I_PUSH, uintptr(unsafe.Pointer(&buf[0])))
}
3 changes: 2 additions & 1 deletion pty_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build !linux,!darwin,!freebsd,!dragonfly,!netbsd,!openbsd,!solaris
//go:build !linux && !darwin && !freebsd && !dragonfly && !netbsd && !openbsd && !solaris
//+build !linux,!darwin,!freebsd,!dragonfly,!netbsd,!openbsd,!solaris

package pty

Expand Down
Loading

0 comments on commit f73a158

Please sign in to comment.