From 68d869b94342316dbc5f6649b42d2a65918c69b2 Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Sun, 2 Oct 2022 15:02:33 +0000 Subject: [PATCH] unix: migrate some illumos definitions to solaris Fixes golang/go#56000 Change-Id: Ica6549385ddb466c4697cc45851e2b59fd6e0f24 Reviewed-on: https://go-review.googlesource.com/c/sys/+/437357 Run-TryBot: Ian Lance Taylor Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Tobias Klauser Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills --- unix/syscall_illumos.go | 106 --------------------------------- unix/syscall_illumos_test.go | 69 --------------------- unix/syscall_solaris.go | 104 ++++++++++++++++++++++++++++++++ unix/syscall_solaris_test.go | 53 +++++++++++++++++ unix/types_illumos.go | 49 --------------- unix/types_solaris.go | 33 ++++++++++ unix/zsyscall_illumos_amd64.go | 28 +-------- unix/zsyscall_solaris_amd64.go | 28 ++++++++- unix/ztypes_illumos_amd64.go | 42 ------------- unix/ztypes_solaris_amd64.go | 35 +++++++++++ 10 files changed, 253 insertions(+), 294 deletions(-) delete mode 100644 unix/syscall_illumos_test.go delete mode 100644 unix/types_illumos.go delete mode 100644 unix/ztypes_illumos_amd64.go diff --git a/unix/syscall_illumos.go b/unix/syscall_illumos.go index e48244a9..87db5a6a 100644 --- a/unix/syscall_illumos.go +++ b/unix/syscall_illumos.go @@ -10,8 +10,6 @@ package unix import ( - "fmt" - "runtime" "unsafe" ) @@ -79,107 +77,3 @@ func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) { } return } - -//sys putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error) - -func Putmsg(fd int, cl []byte, data []byte, flags int) (err error) { - var clp, datap *strbuf - if len(cl) > 0 { - clp = &strbuf{ - Len: int32(len(cl)), - Buf: (*int8)(unsafe.Pointer(&cl[0])), - } - } - if len(data) > 0 { - datap = &strbuf{ - Len: int32(len(data)), - Buf: (*int8)(unsafe.Pointer(&data[0])), - } - } - return putmsg(fd, clp, datap, flags) -} - -//sys getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error) - -func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags int, err error) { - var clp, datap *strbuf - if len(cl) > 0 { - clp = &strbuf{ - Maxlen: int32(len(cl)), - Buf: (*int8)(unsafe.Pointer(&cl[0])), - } - } - if len(data) > 0 { - datap = &strbuf{ - Maxlen: int32(len(data)), - Buf: (*int8)(unsafe.Pointer(&data[0])), - } - } - - if err = getmsg(fd, clp, datap, &flags); err != nil { - return nil, nil, 0, err - } - - if len(cl) > 0 { - retCl = cl[:clp.Len] - } - if len(data) > 0 { - retData = data[:datap.Len] - } - return retCl, retData, flags, nil -} - -func IoctlSetIntRetInt(fd int, req uint, arg int) (int, error) { - return ioctlRet(fd, req, uintptr(arg)) -} - -func IoctlSetString(fd int, req uint, val string) error { - bs := make([]byte, len(val)+1) - copy(bs[:len(bs)-1], val) - err := ioctl(fd, req, uintptr(unsafe.Pointer(&bs[0]))) - runtime.KeepAlive(&bs[0]) - return err -} - -// Lifreq Helpers - -func (l *Lifreq) SetName(name string) error { - if len(name) >= len(l.Name) { - return fmt.Errorf("name cannot be more than %d characters", len(l.Name)-1) - } - for i := range name { - l.Name[i] = int8(name[i]) - } - return nil -} - -func (l *Lifreq) SetLifruInt(d int) { - *(*int)(unsafe.Pointer(&l.Lifru[0])) = d -} - -func (l *Lifreq) GetLifruInt() int { - return *(*int)(unsafe.Pointer(&l.Lifru[0])) -} - -func (l *Lifreq) SetLifruUint(d uint) { - *(*uint)(unsafe.Pointer(&l.Lifru[0])) = d -} - -func (l *Lifreq) GetLifruUint() uint { - return *(*uint)(unsafe.Pointer(&l.Lifru[0])) -} - -func IoctlLifreq(fd int, req uint, l *Lifreq) error { - return ioctl(fd, req, uintptr(unsafe.Pointer(l))) -} - -// Strioctl Helpers - -func (s *Strioctl) SetInt(i int) { - s.Len = int32(unsafe.Sizeof(i)) - s.Dp = (*int8)(unsafe.Pointer(&i)) -} - -func IoctlSetStrioctlRetInt(fd int, req uint, s *Strioctl) (int, error) { - return ioctlRet(fd, req, uintptr(unsafe.Pointer(s))) -} diff --git a/unix/syscall_illumos_test.go b/unix/syscall_illumos_test.go deleted file mode 100644 index dbfe5665..00000000 --- a/unix/syscall_illumos_test.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2021 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 illumos -// +build illumos - -package unix_test - -import ( - "fmt" - "os/exec" - "strings" - "testing" - - "golang.org/x/sys/unix" -) - -func TestLifreqSetName(t *testing.T) { - var l unix.Lifreq - err := l.SetName("12345678901234356789012345678901234567890") - if err == nil { - t.Fatal(`Lifreq.SetName should reject names that are too long`) - } - err = l.SetName("tun0") - if err != nil { - t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err) - } -} - -func TestLifreqGetMTU(t *testing.T) { - // Find links and their MTU using CLI tooling - // $ dladm show-link -p -o link,mtu - // net0:1500 - out, err := exec.Command("dladm", "show-link", "-p", "-o", "link,mtu").Output() - if err != nil { - t.Fatalf("unable to use dladm to find data links: %v", err) - } - lines := strings.Split(string(out), "\n") - tc := make(map[string]string) - for _, line := range lines { - v := strings.Split(line, ":") - if len(v) == 2 { - tc[v[0]] = v[1] - } - } - ip_fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0) - if err != nil { - t.Fatalf("could not open udp socket: %v", err) - } - // SIOCGLIFMTU is negative which confuses the compiler if used inline: - // Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in - // "constant -1065850502 overflows uint" - reqnum := int(unix.SIOCGLIFMTU) - var l unix.Lifreq - for link, mtu := range tc { - err = l.SetName(link) - if err != nil { - t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err) - } - if err = unix.IoctlLifreq(ip_fd, uint(reqnum), &l); err != nil { - t.Fatalf("unable to SIOCGLIFMTU: %v", err) - } - m := l.GetLifruUint() - if fmt.Sprintf("%d", m) != mtu { - t.Errorf("unable to read MTU correctly: expected %s, got %d", mtu, m) - } - } -} diff --git a/unix/syscall_solaris.go b/unix/syscall_solaris.go index 8c6f4092..2109e569 100644 --- a/unix/syscall_solaris.go +++ b/unix/syscall_solaris.go @@ -1026,3 +1026,107 @@ func (e *EventPort) Get(s []PortEvent, min int, timeout *Timespec) (int, error) } return valid, err } + +//sys putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error) + +func Putmsg(fd int, cl []byte, data []byte, flags int) (err error) { + var clp, datap *strbuf + if len(cl) > 0 { + clp = &strbuf{ + Len: int32(len(cl)), + Buf: (*int8)(unsafe.Pointer(&cl[0])), + } + } + if len(data) > 0 { + datap = &strbuf{ + Len: int32(len(data)), + Buf: (*int8)(unsafe.Pointer(&data[0])), + } + } + return putmsg(fd, clp, datap, flags) +} + +//sys getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error) + +func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags int, err error) { + var clp, datap *strbuf + if len(cl) > 0 { + clp = &strbuf{ + Maxlen: int32(len(cl)), + Buf: (*int8)(unsafe.Pointer(&cl[0])), + } + } + if len(data) > 0 { + datap = &strbuf{ + Maxlen: int32(len(data)), + Buf: (*int8)(unsafe.Pointer(&data[0])), + } + } + + if err = getmsg(fd, clp, datap, &flags); err != nil { + return nil, nil, 0, err + } + + if len(cl) > 0 { + retCl = cl[:clp.Len] + } + if len(data) > 0 { + retData = data[:datap.Len] + } + return retCl, retData, flags, nil +} + +func IoctlSetIntRetInt(fd int, req uint, arg int) (int, error) { + return ioctlRet(fd, req, uintptr(arg)) +} + +func IoctlSetString(fd int, req uint, val string) error { + bs := make([]byte, len(val)+1) + copy(bs[:len(bs)-1], val) + err := ioctl(fd, req, uintptr(unsafe.Pointer(&bs[0]))) + runtime.KeepAlive(&bs[0]) + return err +} + +// Lifreq Helpers + +func (l *Lifreq) SetName(name string) error { + if len(name) >= len(l.Name) { + return fmt.Errorf("name cannot be more than %d characters", len(l.Name)-1) + } + for i := range name { + l.Name[i] = int8(name[i]) + } + return nil +} + +func (l *Lifreq) SetLifruInt(d int) { + *(*int)(unsafe.Pointer(&l.Lifru[0])) = d +} + +func (l *Lifreq) GetLifruInt() int { + return *(*int)(unsafe.Pointer(&l.Lifru[0])) +} + +func (l *Lifreq) SetLifruUint(d uint) { + *(*uint)(unsafe.Pointer(&l.Lifru[0])) = d +} + +func (l *Lifreq) GetLifruUint() uint { + return *(*uint)(unsafe.Pointer(&l.Lifru[0])) +} + +func IoctlLifreq(fd int, req uint, l *Lifreq) error { + return ioctl(fd, req, uintptr(unsafe.Pointer(l))) +} + +// Strioctl Helpers + +func (s *Strioctl) SetInt(i int) { + s.Len = int32(unsafe.Sizeof(i)) + s.Dp = (*int8)(unsafe.Pointer(&i)) +} + +func IoctlSetStrioctlRetInt(fd int, req uint, s *Strioctl) (int, error) { + return ioctlRet(fd, req, uintptr(unsafe.Pointer(s))) +} diff --git a/unix/syscall_solaris_test.go b/unix/syscall_solaris_test.go index 93f2732e..fc150409 100644 --- a/unix/syscall_solaris_test.go +++ b/unix/syscall_solaris_test.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "runtime" + "strings" "testing" "golang.org/x/sys/unix" @@ -331,3 +332,55 @@ func TestPortEventSlices(t *testing.T) { t.Errorf("port.Close() failed: %v", err) } } + +func TestLifreqSetName(t *testing.T) { + var l unix.Lifreq + err := l.SetName("12345678901234356789012345678901234567890") + if err == nil { + t.Fatal(`Lifreq.SetName should reject names that are too long`) + } + err = l.SetName("tun0") + if err != nil { + t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err) + } +} + +func TestLifreqGetMTU(t *testing.T) { + // Find links and their MTU using CLI tooling + // $ dladm show-link -p -o link,mtu + // net0:1500 + out, err := exec.Command("dladm", "show-link", "-p", "-o", "link,mtu").Output() + if err != nil { + t.Fatalf("unable to use dladm to find data links: %v", err) + } + lines := strings.Split(string(out), "\n") + tc := make(map[string]string) + for _, line := range lines { + v := strings.Split(line, ":") + if len(v) == 2 { + tc[v[0]] = v[1] + } + } + ip_fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0) + if err != nil { + t.Fatalf("could not open udp socket: %v", err) + } + // SIOCGLIFMTU is negative which confuses the compiler if used inline: + // Using "unix.IoctlLifreq(ip_fd, unix.SIOCGLIFMTU, &l)" results in + // "constant -1065850502 overflows uint" + reqnum := int(unix.SIOCGLIFMTU) + var l unix.Lifreq + for link, mtu := range tc { + err = l.SetName(link) + if err != nil { + t.Fatalf("Lifreq.SetName(%q) failed: %v", link, err) + } + if err = unix.IoctlLifreq(ip_fd, uint(reqnum), &l); err != nil { + t.Fatalf("unable to SIOCGLIFMTU: %v", err) + } + m := l.GetLifruUint() + if fmt.Sprintf("%d", m) != mtu { + t.Errorf("unable to read MTU correctly: expected %s, got %d", mtu, m) + } + } +} diff --git a/unix/types_illumos.go b/unix/types_illumos.go deleted file mode 100644 index d47ab635..00000000 --- a/unix/types_illumos.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2021 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 ignore -// +build ignore - -/* -Input to cgo -godefs. See README.md -*/ - -package unix - -/* -#include -#include -#include - -// Many illumos distributions ship a 3rd party tun/tap driver -// from https://github.com/kaizawa/tuntap -// It supports a pair of IOCTLs defined at -// https://github.com/kaizawa/tuntap/blob/master/if_tun.h#L91-L93 -#define TUNNEWPPA (('T'<<16) | 0x0001) -#define TUNSETPPA (('T'<<16) | 0x0002) -*/ -import "C" - -const ( - TUNNEWPPA = C.TUNNEWPPA - TUNSETPPA = C.TUNSETPPA - - // sys/stropts.h: - I_STR = C.I_STR - I_POP = C.I_POP - I_PUSH = C.I_PUSH - I_LINK = C.I_LINK - I_UNLINK = C.I_UNLINK - I_PLINK = C.I_PLINK - I_PUNLINK = C.I_PUNLINK - - // sys/sockio.h: - IF_UNITSEL = C.IF_UNITSEL -) - -type strbuf C.struct_strbuf - -type Strioctl C.struct_strioctl - -type Lifreq C.struct_lifreq diff --git a/unix/types_solaris.go b/unix/types_solaris.go index 7d79700d..98a4d859 100644 --- a/unix/types_solaris.go +++ b/unix/types_solaris.go @@ -39,8 +39,10 @@ package unix #include #include #include +#include #include #include +#include #include #include #include @@ -80,6 +82,12 @@ struct goIovec { size_t iov_len; }; +// Solaris and the major illumos distributions ship a 3rd party tun/tap driver +// from https://github.com/kaizawa/tuntap +// It supports a pair of IOCTLs defined at +// https://github.com/kaizawa/tuntap/blob/master/if_tun.h#L91-L93 +#define TUNNEWPPA (('T'<<16) | 0x0001) +#define TUNSETPPA (('T'<<16) | 0x0002) */ import "C" @@ -306,3 +314,28 @@ const ( MOUNTEDOVER = C.MOUNTEDOVER FILE_EXCEPTION = C.FILE_EXCEPTION ) + +// STREAMS and Tun + +const ( + TUNNEWPPA = C.TUNNEWPPA + TUNSETPPA = C.TUNSETPPA + + // sys/stropts.h: + I_STR = C.I_STR + I_POP = C.I_POP + I_PUSH = C.I_PUSH + I_LINK = C.I_LINK + I_UNLINK = C.I_UNLINK + I_PLINK = C.I_PLINK + I_PUNLINK = C.I_PUNLINK + + // sys/sockio.h: + IF_UNITSEL = C.IF_UNITSEL +) + +type strbuf C.struct_strbuf + +type Strioctl C.struct_strioctl + +type Lifreq C.struct_lifreq diff --git a/unix/zsyscall_illumos_amd64.go b/unix/zsyscall_illumos_amd64.go index af5cb064..b57c7050 100644 --- a/unix/zsyscall_illumos_amd64.go +++ b/unix/zsyscall_illumos_amd64.go @@ -15,25 +15,19 @@ import ( //go:cgo_import_dynamic libc_writev writev "libc.so" //go:cgo_import_dynamic libc_pwritev pwritev "libc.so" //go:cgo_import_dynamic libc_accept4 accept4 "libsocket.so" -//go:cgo_import_dynamic libc_putmsg putmsg "libc.so" -//go:cgo_import_dynamic libc_getmsg getmsg "libc.so" //go:linkname procreadv libc_readv //go:linkname procpreadv libc_preadv //go:linkname procwritev libc_writev //go:linkname procpwritev libc_pwritev //go:linkname procaccept4 libc_accept4 -//go:linkname procputmsg libc_putmsg -//go:linkname procgetmsg libc_getmsg var ( procreadv, procpreadv, procwritev, procpwritev, - procaccept4, - procputmsg, - procgetmsg syscallFunc + procaccept4 syscallFunc ) // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT @@ -106,23 +100,3 @@ func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, } return } - -// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT - -func putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error) { - _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procputmsg)), 4, uintptr(fd), uintptr(unsafe.Pointer(clptr)), uintptr(unsafe.Pointer(dataptr)), uintptr(flags), 0, 0) - if e1 != 0 { - err = e1 - } - return -} - -// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT - -func getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error) { - _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procgetmsg)), 4, uintptr(fd), uintptr(unsafe.Pointer(clptr)), uintptr(unsafe.Pointer(dataptr)), uintptr(unsafe.Pointer(flags)), 0, 0) - if e1 != 0 { - err = e1 - } - return -} diff --git a/unix/zsyscall_solaris_amd64.go b/unix/zsyscall_solaris_amd64.go index fdf53f8d..91f5a2bd 100644 --- a/unix/zsyscall_solaris_amd64.go +++ b/unix/zsyscall_solaris_amd64.go @@ -147,6 +147,8 @@ import ( //go:cgo_import_dynamic libc_port_dissociate port_dissociate "libc.so" //go:cgo_import_dynamic libc_port_get port_get "libc.so" //go:cgo_import_dynamic libc_port_getn port_getn "libc.so" +//go:cgo_import_dynamic libc_putmsg putmsg "libc.so" +//go:cgo_import_dynamic libc_getmsg getmsg "libc.so" //go:linkname procpipe libc_pipe //go:linkname procpipe2 libc_pipe2 @@ -284,6 +286,8 @@ import ( //go:linkname procport_dissociate libc_port_dissociate //go:linkname procport_get libc_port_get //go:linkname procport_getn libc_port_getn +//go:linkname procputmsg libc_putmsg +//go:linkname procgetmsg libc_getmsg var ( procpipe, @@ -421,7 +425,9 @@ var ( procport_associate, procport_dissociate, procport_get, - procport_getn syscallFunc + procport_getn, + procputmsg, + procgetmsg syscallFunc ) // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT @@ -2065,3 +2071,23 @@ func port_getn(port int, pe *portEvent, max uint32, nget *uint32, timeout *Times } return } + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error) { + _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procputmsg)), 4, uintptr(fd), uintptr(unsafe.Pointer(clptr)), uintptr(unsafe.Pointer(dataptr)), uintptr(flags), 0, 0) + if e1 != 0 { + err = e1 + } + return +} + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error) { + _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procgetmsg)), 4, uintptr(fd), uintptr(unsafe.Pointer(clptr)), uintptr(unsafe.Pointer(dataptr)), uintptr(unsafe.Pointer(flags)), 0, 0) + if e1 != 0 { + err = e1 + } + return +} diff --git a/unix/ztypes_illumos_amd64.go b/unix/ztypes_illumos_amd64.go deleted file mode 100644 index 4c485261..00000000 --- a/unix/ztypes_illumos_amd64.go +++ /dev/null @@ -1,42 +0,0 @@ -// cgo -godefs types_illumos.go | go run mkpost.go -// Code generated by the command above; see README.md. DO NOT EDIT. - -//go:build amd64 && illumos -// +build amd64,illumos - -package unix - -const ( - TUNNEWPPA = 0x540001 - TUNSETPPA = 0x540002 - - I_STR = 0x5308 - I_POP = 0x5303 - I_PUSH = 0x5302 - I_LINK = 0x530c - I_UNLINK = 0x530d - I_PLINK = 0x5316 - I_PUNLINK = 0x5317 - - IF_UNITSEL = -0x7ffb8cca -) - -type strbuf struct { - Maxlen int32 - Len int32 - Buf *int8 -} - -type Strioctl struct { - Cmd int32 - Timout int32 - Len int32 - Dp *int8 -} - -type Lifreq struct { - Name [32]int8 - Lifru1 [4]byte - Type uint32 - Lifru [336]byte -} diff --git a/unix/ztypes_solaris_amd64.go b/unix/ztypes_solaris_amd64.go index c1a9b83a..0400747c 100644 --- a/unix/ztypes_solaris_amd64.go +++ b/unix/ztypes_solaris_amd64.go @@ -480,3 +480,38 @@ const ( MOUNTEDOVER = 0x40000000 FILE_EXCEPTION = 0x60000070 ) + +const ( + TUNNEWPPA = 0x540001 + TUNSETPPA = 0x540002 + + I_STR = 0x5308 + I_POP = 0x5303 + I_PUSH = 0x5302 + I_LINK = 0x530c + I_UNLINK = 0x530d + I_PLINK = 0x5316 + I_PUNLINK = 0x5317 + + IF_UNITSEL = -0x7ffb8cca +) + +type strbuf struct { + Maxlen int32 + Len int32 + Buf *int8 +} + +type Strioctl struct { + Cmd int32 + Timout int32 + Len int32 + Dp *int8 +} + +type Lifreq struct { + Name [32]int8 + Lifru1 [4]byte + Type uint32 + Lifru [336]byte +}