diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-03-03 10:23:55 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-03-03 10:25:58 -0800 |
commit | a9441aea2780da8c93da1c73da860219f98438de (patch) | |
tree | 8b12915756f5bfb926218214cd7bc0b3281605fd /pkg/fdchannel | |
parent | b8a5420f49a2afd622ec08b5019e1bf537f7da82 (diff) |
[op] Replace syscall package usage with golang.org/x/sys/unix in pkg/.
The syscall package has been deprecated in favor of golang.org/x/sys.
Note that syscall is still used in the following places:
- pkg/sentry/socket/hostinet/stack.go: some netlink related functionalities
are not yet available in golang.org/x/sys.
- syscall.Stat_t is still used in some places because os.FileInfo.Sys() still
returns it and not unix.Stat_t.
Updates #214
PiperOrigin-RevId: 360701387
Diffstat (limited to 'pkg/fdchannel')
-rw-r--r-- | pkg/fdchannel/BUILD | 2 | ||||
-rw-r--r-- | pkg/fdchannel/fdchannel_test.go | 3 | ||||
-rw-r--r-- | pkg/fdchannel/fdchannel_unsafe.go | 40 |
3 files changed, 24 insertions, 21 deletions
diff --git a/pkg/fdchannel/BUILD b/pkg/fdchannel/BUILD index 0b34cef03..d240132e5 100644 --- a/pkg/fdchannel/BUILD +++ b/pkg/fdchannel/BUILD @@ -8,6 +8,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/gohacks", + "@org_golang_x_sys//unix:go_default_library", ], ) @@ -18,5 +19,6 @@ go_test( library = ":fdchannel", deps = [ "//pkg/sync", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/fdchannel/fdchannel_test.go b/pkg/fdchannel/fdchannel_test.go index 7a8a63a59..9616e30c5 100644 --- a/pkg/fdchannel/fdchannel_test.go +++ b/pkg/fdchannel/fdchannel_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/sync" ) @@ -41,7 +42,7 @@ func TestSendRecvFD(t *testing.T) { defer recvEP.Destroy() recvFD, err := recvEP.RecvFDNonblock() - if err != syscall.EAGAIN && err != syscall.EWOULDBLOCK { + if err != unix.EAGAIN && err != unix.EWOULDBLOCK { t.Errorf("RecvFDNonblock before SendFD: got (%d, %v), wanted (<unspecified>, EAGAIN or EWOULDBLOCK", recvFD, err) } diff --git a/pkg/fdchannel/fdchannel_unsafe.go b/pkg/fdchannel/fdchannel_unsafe.go index 0ebdedf26..1f24a448d 100644 --- a/pkg/fdchannel/fdchannel_unsafe.go +++ b/pkg/fdchannel/fdchannel_unsafe.go @@ -20,9 +20,9 @@ package fdchannel import ( "fmt" - "syscall" "unsafe" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/gohacks" ) @@ -33,7 +33,7 @@ const sizeofInt32 = int(unsafe.Sizeof(int32(0))) // representing connected sockets that may be passed to separate calls to // NewEndpoint to create connected Endpoints. func NewConnectedSockets() ([2]int, error) { - return syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC, 0) + return unix.Socketpair(unix.AF_UNIX, unix.SOCK_SEQPACKET|unix.SOCK_CLOEXEC, 0) } // Endpoint sends file descriptors to, and receives them from, another @@ -42,8 +42,8 @@ func NewConnectedSockets() ([2]int, error) { // Endpoint is not copyable or movable by value. type Endpoint struct { sockfd int32 - msghdr syscall.Msghdr - cmsg *syscall.Cmsghdr // followed by sizeofInt32 bytes of data + msghdr unix.Msghdr + cmsg *unix.Cmsghdr // followed by sizeofInt32 bytes of data } // Init must be called on zero-value Endpoints before first use. sockfd must be @@ -53,11 +53,11 @@ func (ep *Endpoint) Init(sockfd int) { // domains) permit zero-length datagrams." - recv(2). Experimentally, // sendmsg+recvmsg for a zero-length datagram is slightly faster than // sendmsg+recvmsg for a single byte over a stream socket. - cmsgSlice := make([]byte, syscall.CmsgSpace(sizeofInt32)) + cmsgSlice := make([]byte, unix.CmsgSpace(sizeofInt32)) cmsgSliceHdr := (*gohacks.SliceHeader)(unsafe.Pointer(&cmsgSlice)) ep.sockfd = int32(sockfd) ep.msghdr.Control = (*byte)(cmsgSliceHdr.Data) - ep.cmsg = (*syscall.Cmsghdr)(cmsgSliceHdr.Data) + ep.cmsg = (*unix.Cmsghdr)(cmsgSliceHdr.Data) // ep.msghdr.Controllen and ep.cmsg.* are mutated by recvmsg(2), so they're // set before calling sendmsg/recvmsg. } @@ -73,7 +73,7 @@ func NewEndpoint(sockfd int) *Endpoint { // Destroy releases resources owned by ep. No other Endpoint methods may be // called after Destroy. func (ep *Endpoint) Destroy() { - syscall.Close(int(ep.sockfd)) + unix.Close(int(ep.sockfd)) ep.sockfd = -1 } @@ -84,19 +84,19 @@ func (ep *Endpoint) Destroy() { // Shutdown is the only Endpoint method that may be called concurrently with // other methods. func (ep *Endpoint) Shutdown() { - syscall.Shutdown(int(ep.sockfd), syscall.SHUT_RDWR) + unix.Shutdown(int(ep.sockfd), unix.SHUT_RDWR) } // SendFD sends the open file description represented by the given file // descriptor to the connected Endpoint. func (ep *Endpoint) SendFD(fd int) error { - cmsgLen := syscall.CmsgLen(sizeofInt32) - ep.cmsg.Level = syscall.SOL_SOCKET - ep.cmsg.Type = syscall.SCM_RIGHTS + cmsgLen := unix.CmsgLen(sizeofInt32) + ep.cmsg.Level = unix.SOL_SOCKET + ep.cmsg.Type = unix.SCM_RIGHTS ep.cmsg.SetLen(cmsgLen) *ep.cmsgData() = int32(fd) ep.msghdr.SetControllen(cmsgLen) - _, _, e := syscall.Syscall(syscall.SYS_SENDMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), 0) + _, _, e := unix.Syscall(unix.SYS_SENDMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), 0) if e != 0 { return e } @@ -118,13 +118,13 @@ func (ep *Endpoint) RecvFDNonblock() (int, error) { } func (ep *Endpoint) recvFD(nonblock bool) (int, error) { - cmsgLen := syscall.CmsgLen(sizeofInt32) + cmsgLen := unix.CmsgLen(sizeofInt32) ep.msghdr.SetControllen(cmsgLen) - var e syscall.Errno + var e unix.Errno if nonblock { - _, _, e = syscall.RawSyscall(syscall.SYS_RECVMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), syscall.MSG_TRUNC|syscall.MSG_DONTWAIT) + _, _, e = unix.RawSyscall(unix.SYS_RECVMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), unix.MSG_TRUNC|unix.MSG_DONTWAIT) } else { - _, _, e = syscall.Syscall(syscall.SYS_RECVMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), syscall.MSG_TRUNC) + _, _, e = unix.Syscall(unix.SYS_RECVMSG, uintptr(ep.sockfd), uintptr(unsafe.Pointer(&ep.msghdr)), unix.MSG_TRUNC) } if e != 0 { return -1, e @@ -132,13 +132,13 @@ func (ep *Endpoint) recvFD(nonblock bool) (int, error) { if int(ep.msghdr.Controllen) != cmsgLen { return -1, fmt.Errorf("received control message has incorrect length: got %d, wanted %d", ep.msghdr.Controllen, cmsgLen) } - if ep.cmsg.Level != syscall.SOL_SOCKET || ep.cmsg.Type != syscall.SCM_RIGHTS { - return -1, fmt.Errorf("received control message has incorrect (level, type): got (%v, %v), wanted (%v, %v)", ep.cmsg.Level, ep.cmsg.Type, syscall.SOL_SOCKET, syscall.SCM_RIGHTS) + if ep.cmsg.Level != unix.SOL_SOCKET || ep.cmsg.Type != unix.SCM_RIGHTS { + return -1, fmt.Errorf("received control message has incorrect (level, type): got (%v, %v), wanted (%v, %v)", ep.cmsg.Level, ep.cmsg.Type, unix.SOL_SOCKET, unix.SCM_RIGHTS) } return int(*ep.cmsgData()), nil } func (ep *Endpoint) cmsgData() *int32 { - // syscall.CmsgLen(0) == syscall.cmsgAlignOf(syscall.SizeofCmsghdr) - return (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(ep.cmsg)) + uintptr(syscall.CmsgLen(0)))) + // unix.CmsgLen(0) == unix.cmsgAlignOf(unix.SizeofCmsghdr) + return (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(ep.cmsg)) + uintptr(unix.CmsgLen(0)))) } |