From 66b7b1311ac80bbafcd2daeef9a5e6e2cd1e2399 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 7 Nov 2018 12:45:52 +0000 Subject: [PATCH] unix: add IoctlGetPtmget on netbsd This ioctl is used to implement ptsname on netbsd. Change-Id: Ic87f1bf7d15c6fbef0c2226a06a4983a504c3f30 Reviewed-on: https://go-review.googlesource.com/c/148097 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- unix/mkpost.go | 4 ++++ unix/syscall_netbsd.go | 8 ++++++++ unix/syscall_netbsd_test.go | 16 ++++++++++++++++ unix/types_netbsd.go | 2 ++ unix/ztypes_netbsd_386.go | 7 +++++++ unix/ztypes_netbsd_amd64.go | 7 +++++++ unix/ztypes_netbsd_arm.go | 7 +++++++ 7 files changed, 51 insertions(+) diff --git a/unix/mkpost.go b/unix/mkpost.go index 7e5c22c4..4dc06923 100644 --- a/unix/mkpost.go +++ b/unix/mkpost.go @@ -65,6 +65,10 @@ func main() { convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`) b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) + // Convert [1024]int8 to [1024]byte in Ptmget members + convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`) + b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte")) + // Remove spare fields (e.g. in Statx_t) spareFieldsRegex := regexp.MustCompile(`X__spare\S*`) b = spareFieldsRegex.ReplaceAll(b, []byte("_")) diff --git a/unix/syscall_netbsd.go b/unix/syscall_netbsd.go index b711aca8..059327a3 100644 --- a/unix/syscall_netbsd.go +++ b/unix/syscall_netbsd.go @@ -13,6 +13,7 @@ package unix import ( + "runtime" "syscall" "unsafe" ) @@ -190,6 +191,13 @@ func IoctlGetTermios(fd int, req uint) (*Termios, error) { return &value, err } +func IoctlGetPtmget(fd int, req uint) (*Ptmget, error) { + var value Ptmget + err := ioctl(fd, req, uintptr(unsafe.Pointer(&value))) + runtime.KeepAlive(value) + return &value, err +} + func Uname(uname *Utsname) error { mib := []_C_int{CTL_KERN, KERN_OSTYPE} n := unsafe.Sizeof(uname.Sysname) diff --git a/unix/syscall_netbsd_test.go b/unix/syscall_netbsd_test.go index 3573a133..41141f96 100644 --- a/unix/syscall_netbsd_test.go +++ b/unix/syscall_netbsd_test.go @@ -5,6 +5,7 @@ package unix_test import ( + "bytes" "testing" "golang.org/x/sys/unix" @@ -33,3 +34,18 @@ func TestSysctlClockinfo(t *testing.T) { t.Logf("tick = %v, tickadj = %v, hz = %v, profhz = %v, stathz = %v", ci.Tick, ci.Tickadj, ci.Hz, ci.Profhz, ci.Stathz) } + +func TestIoctlPtmget(t *testing.T) { + fd, err := unix.Open("/dev/ptmx", unix.O_NOCTTY|unix.O_RDWR, 0666) + if err != nil { + t.Skip("failed to open /dev/ptmx, skipping test") + } + defer unix.Close(fd) + + ptm, err := unix.IoctlGetPtmget(fd, unix.TIOCPTSNAME) + if err != nil { + t.Fatalf("IoctlGetPtmget: %v\n", err) + } + + t.Logf("sfd = %v, ptsname = %v", ptm.Sfd, string(ptm.Sn[:bytes.IndexByte(ptm.Sn[:], 0)])) +} diff --git a/unix/types_netbsd.go b/unix/types_netbsd.go index 1edbf1ba..2dd4f954 100644 --- a/unix/types_netbsd.go +++ b/unix/types_netbsd.go @@ -248,6 +248,8 @@ type Termios C.struct_termios type Winsize C.struct_winsize +type Ptmget C.struct_ptmget + // fchmodat-like syscalls. const ( diff --git a/unix/ztypes_netbsd_386.go b/unix/ztypes_netbsd_386.go index 1fdc5fd2..2dae0c17 100644 --- a/unix/ztypes_netbsd_386.go +++ b/unix/ztypes_netbsd_386.go @@ -402,6 +402,13 @@ type Winsize struct { Ypixel uint16 } +type Ptmget struct { + Cfd int32 + Sfd int32 + Cn [1024]byte + Sn [1024]byte +} + const ( AT_FDCWD = -0x64 AT_SYMLINK_NOFOLLOW = 0x200 diff --git a/unix/ztypes_netbsd_amd64.go b/unix/ztypes_netbsd_amd64.go index 711f7806..1f0e76c0 100644 --- a/unix/ztypes_netbsd_amd64.go +++ b/unix/ztypes_netbsd_amd64.go @@ -409,6 +409,13 @@ type Winsize struct { Ypixel uint16 } +type Ptmget struct { + Cfd int32 + Sfd int32 + Cn [1024]byte + Sn [1024]byte +} + const ( AT_FDCWD = -0x64 AT_SYMLINK_NOFOLLOW = 0x200 diff --git a/unix/ztypes_netbsd_arm.go b/unix/ztypes_netbsd_arm.go index fa1a16ba..53f2159c 100644 --- a/unix/ztypes_netbsd_arm.go +++ b/unix/ztypes_netbsd_arm.go @@ -407,6 +407,13 @@ type Winsize struct { Ypixel uint16 } +type Ptmget struct { + Cfd int32 + Sfd int32 + Cn [1024]byte + Sn [1024]byte +} + const ( AT_FDCWD = -0x64 AT_SYMLINK_NOFOLLOW = 0x200