Skip to content

Commit 820383e

Browse files
authored
Merge pull request #2 from WeidiDeng/copilot/refactor-ioctl-usage
refactor: ioctl ptr parameter from uintptr to any
2 parents edfbf75 + 6d89a20 commit 820383e

15 files changed

Lines changed: 63 additions & 52 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ go get github.com/creack/pty
1010

1111
## Examples
1212

13-
Note that those examples are for demonstration purpose only, to showcase how to use the library. They are not meant to be used in any kind of production environment. If you want to **set deadlines to work** and `Close()` **interrupting** `Read()` on the returned `*os.File`, you will need to call `syscall.SetNonblock` manually.
13+
Note that those examples are for demonstration purpose only, to showcase how to use the library. They are not meant to be used in any kind of production environment.
1414

1515
### Command
1616

io_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"runtime"
1111
"sync"
12-
"syscall"
1312
"testing"
1413
"time"
1514
)
@@ -30,9 +29,6 @@ var glTestFdLock sync.Mutex
3029
//nolint:paralleltest // Potential in (*os.File).Fd().
3130
func TestReadDeadline(t *testing.T) {
3231
ptmx, success := prepare(t)
33-
if err := syscall.SetNonblock(int(ptmx.Fd()), true); err != nil {
34-
t.Fatalf("Error: set non block: %s", err)
35-
}
3632

3733
if err := ptmx.SetDeadline(time.Now().Add(timeout / 10)); err != nil {
3834
if errors.Is(err, os.ErrNoDeadline) {
@@ -62,9 +58,6 @@ func TestReadDeadline(t *testing.T) {
6258
//nolint:paralleltest // Potential in (*os.File).Fd().
6359
func TestReadClose(t *testing.T) {
6460
ptmx, success := prepare(t)
65-
if err := syscall.SetNonblock(int(ptmx.Fd()), true); err != nil {
66-
t.Fatalf("Error: set non block: %s", err)
67-
}
6861

6962
go func() {
7063
time.Sleep(timeout / 10)

ioctl.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ package pty
55

66
import "os"
77

8-
func ioctl(f *os.File, cmd, ptr uintptr) error {
9-
return ioctlInner(f.Fd(), cmd, ptr) // Fall back to blocking io.
10-
}
11-
12-
// NOTE: Unused. Keeping for reference.
13-
func ioctlNonblock(f *os.File, cmd, ptr uintptr) error {
8+
func ioctl(f *os.File, cmd uintptr, ptr any) error {
149
sc, e := f.SyscallConn()
1510
if e != nil {
1611
return ioctlInner(f.Fd(), cmd, ptr) // Fall back to blocking io (old behavior).

ioctl_inner.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
//go:build !windows && !solaris && !aix
2-
// +build !windows,!solaris,!aix
1+
//go:build !windows && !solaris && !aix && go1.12
2+
// +build !windows,!solaris,!aix,go1.12
33

44
package pty
55

6-
import "syscall"
6+
import (
7+
"reflect"
8+
"syscall"
9+
)
710

811
// Local syscall const values.
912
const (
1013
TIOCGWINSZ = syscall.TIOCGWINSZ
1114
TIOCSWINSZ = syscall.TIOCSWINSZ
1215
)
1316

14-
func ioctlInner(fd, cmd, ptr uintptr) error {
15-
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr)
17+
func ioctlInner(fd, cmd uintptr, ptr any) error {
18+
var e syscall.Errno
19+
if ptr == nil {
20+
_, _, e = syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, uintptr(0))
21+
} else {
22+
_, _, e = syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, uintptr(reflect.ValueOf(ptr).UnsafePointer())) //nolint:gosec // ptr-to-uintptr at syscall site.
23+
}
1624
if e != 0 {
1725
return e
1826
}

ioctl_legacy.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@
33

44
package pty
55

6-
import "os"
6+
import (
7+
"os"
8+
"reflect"
9+
"syscall"
10+
)
711

8-
func ioctl(f *os.File, cmd, ptr uintptr) error {
12+
func ioctl(f *os.File, cmd uintptr, ptr interface{}) error {
913
return ioctlInner(f.Fd(), cmd, ptr) // fall back to blocking io (old behavior)
1014
}
15+
16+
func ioctlInner(fd, cmd uintptr, ptr interface{}) error {
17+
var e syscall.Errno
18+
if ptr == nil {
19+
_, _, e = syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, uintptr(0))
20+
} else {
21+
_, _, e = syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, reflect.ValueOf(ptr).UnsafeAddr()) //nolint:gosec // ptr-to-uintptr at syscall site.
22+
}
23+
if e != 0 {
24+
return e
25+
}
26+
return nil
27+
}

ioctl_solaris.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package pty
55

66
import (
7+
"reflect"
78
"syscall"
89
"unsafe"
910
)
@@ -40,8 +41,14 @@ type strioctl struct {
4041
// Defined in asm_solaris_amd64.s.
4142
func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
4243

43-
func ioctlInner(fd, cmd, ptr uintptr) error {
44-
if _, _, errno := sysvicall6(uintptr(unsafe.Pointer(&procioctl)), 3, fd, cmd, ptr, 0, 0, 0); errno != 0 {
44+
func ioctlInner(fd, cmd uintptr, ptr any) error {
45+
var errno syscall.Errno
46+
if ptr == nil {
47+
_, _, errno = sysvicall6(uintptr(unsafe.Pointer(&procioctl)), 3, fd, cmd, uintptr(0), 0, 0, 0)
48+
} else {
49+
_, _, errno = sysvicall6(uintptr(unsafe.Pointer(&procioctl)), 3, fd, cmd, uintptr(reflect.ValueOf(ptr).UnsafePointer()), 0, 0, 0) //nolint:gosec // ptr-to-uintptr at syscall site.
50+
}
51+
if errno != 0 {
4552
return errno
4653
}
4754
return nil

ioctl_unsupported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ const (
88
TIOCSWINSZ = 0
99
)
1010

11-
func ioctlInner(fd, cmd, ptr uintptr) error {
11+
func ioctlInner(fd, cmd uintptr, ptr any) error {
1212
return ErrUnsupported
1313
}

pty_darwin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"os"
99
"syscall"
10-
"unsafe"
1110
)
1211

1312
func open() (pty, tty *os.File, err error) {
@@ -46,7 +45,7 @@ func open() (pty, tty *os.File, err error) {
4645
func ptsname(f *os.File) (string, error) {
4746
n := make([]byte, _IOC_PARM_LEN(syscall.TIOCPTYGNAME))
4847

49-
err := ioctl(f, syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0])))
48+
err := ioctl(f, syscall.TIOCPTYGNAME, &n[0])
5049
if err != nil {
5150
return "", err
5251
}
@@ -60,9 +59,9 @@ func ptsname(f *os.File) (string, error) {
6059
}
6160

6261
func grantpt(f *os.File) error {
63-
return ioctl(f, syscall.TIOCPTYGRANT, 0)
62+
return ioctl(f, syscall.TIOCPTYGRANT, nil)
6463
}
6564

6665
func unlockpt(f *os.File) error {
67-
return ioctl(f, syscall.TIOCPTYUNLK, 0)
66+
return ioctl(f, syscall.TIOCPTYUNLK, nil)
6867
}

pty_dragonfly.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ func unlockpt(f *os.File) error {
5555
}
5656

5757
func isptmaster(f *os.File) (bool, error) {
58-
err := ioctl(f, syscall.TIOCISPTMASTER, 0)
58+
err := ioctl(f, syscall.TIOCISPTMASTER, nil)
5959
return err == nil, err
6060
}
61-
6261
var (
6362
emptyFiodgnameArg fiodgnameArg
6463
ioctl_FIODNAME = _IOW('f', 120, unsafe.Sizeof(emptyFiodgnameArg))
@@ -68,7 +67,7 @@ func ptsname(f *os.File) (string, error) {
6867
name := make([]byte, _C_SPECNAMELEN)
6968
fa := fiodgnameArg{Name: (*byte)(unsafe.Pointer(&name[0])), Len: _C_SPECNAMELEN, Pad_cgo_0: [4]byte{0, 0, 0, 0}}
7069

71-
err := ioctl(f, ioctl_FIODNAME, uintptr(unsafe.Pointer(&fa)))
70+
err := ioctl(f, ioctl_FIODNAME, &fa)
7271
if err != nil {
7372
return "", err
7473
}

pty_freebsd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func open() (pty, tty *os.File, err error) {
4545
}
4646

4747
func isptmaster(f *os.File) (bool, error) {
48-
err := ioctl(f, syscall.TIOCPTMASTER, 0)
48+
err := ioctl(f, syscall.TIOCPTMASTER, nil)
4949
return err == nil, err
5050
}
51-
5251
var (
5352
emptyFiodgnameArg fiodgnameArg
5453
ioctlFIODGNAME = _IOW('f', 120, unsafe.Sizeof(emptyFiodgnameArg))
@@ -68,7 +67,7 @@ func ptsname(f *os.File) (string, error) {
6867
buf = make([]byte, n)
6968
arg = fiodgnameArg{Len: n, Buf: (*byte)(unsafe.Pointer(&buf[0]))}
7069
)
71-
if err := ioctl(f, ioctlFIODGNAME, uintptr(unsafe.Pointer(&arg))); err != nil {
70+
if err := ioctl(f, ioctlFIODGNAME, &arg); err != nil {
7271
return "", err
7372
}
7473

0 commit comments

Comments
 (0)