summaryrefslogtreecommitdiffhomepage
path: root/pkg/fd
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-03-03 10:23:55 -0800
committergVisor bot <gvisor-bot@google.com>2021-03-03 10:25:58 -0800
commita9441aea2780da8c93da1c73da860219f98438de (patch)
tree8b12915756f5bfb926218214cd7bc0b3281605fd /pkg/fd
parentb8a5420f49a2afd622ec08b5019e1bf537f7da82 (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/fd')
-rw-r--r--pkg/fd/BUILD2
-rw-r--r--pkg/fd/fd.go31
-rw-r--r--pkg/fd/fd_test.go17
3 files changed, 27 insertions, 23 deletions
diff --git a/pkg/fd/BUILD b/pkg/fd/BUILD
index 872361546..c7abd9655 100644
--- a/pkg/fd/BUILD
+++ b/pkg/fd/BUILD
@@ -6,6 +6,7 @@ go_library(
name = "fd",
srcs = ["fd.go"],
visibility = ["//visibility:public"],
+ deps = ["@org_golang_x_sys//unix:go_default_library"],
)
go_test(
@@ -13,4 +14,5 @@ go_test(
size = "small",
srcs = ["fd_test.go"],
library = ":fd",
+ deps = ["@org_golang_x_sys//unix:go_default_library"],
)
diff --git a/pkg/fd/fd.go b/pkg/fd/fd.go
index cc6b0cdf1..290673a7e 100644
--- a/pkg/fd/fd.go
+++ b/pkg/fd/fd.go
@@ -21,7 +21,8 @@ import (
"os"
"runtime"
"sync/atomic"
- "syscall"
+
+ "golang.org/x/sys/unix"
)
// ReadWriter implements io.ReadWriter, io.ReaderAt, and io.WriterAt for fd. It
@@ -49,7 +50,7 @@ func fixCount(n int, err error) (int, error) {
// Read implements io.Reader.
func (r *ReadWriter) Read(b []byte) (int, error) {
- c, err := fixCount(syscall.Read(r.FD(), b))
+ c, err := fixCount(unix.Read(r.FD(), b))
if c == 0 && len(b) > 0 && err == nil {
return 0, io.EOF
}
@@ -62,7 +63,7 @@ func (r *ReadWriter) Read(b []byte) (int, error) {
func (r *ReadWriter) ReadAt(b []byte, off int64) (c int, err error) {
for len(b) > 0 {
var m int
- m, err = fixCount(syscall.Pread(r.FD(), b, off))
+ m, err = fixCount(unix.Pread(r.FD(), b, off))
if m == 0 && err == nil {
return c, io.EOF
}
@@ -82,21 +83,21 @@ func (r *ReadWriter) Write(b []byte) (int, error) {
var n, remaining int
for remaining = len(b); remaining > 0; {
woff := len(b) - remaining
- n, err = syscall.Write(r.FD(), b[woff:])
+ n, err = unix.Write(r.FD(), b[woff:])
if n > 0 {
- // syscall.Write wrote some bytes. This is the common case.
+ // unix.Write wrote some bytes. This is the common case.
remaining -= n
} else {
if err == nil {
- // syscall.Write did not write anything nor did it return an error.
+ // unix.Write did not write anything nor did it return an error.
//
- // There is no way to guarantee that a subsequent syscall.Write will
+ // There is no way to guarantee that a subsequent unix.Write will
// make forward progress so just panic.
- panic(fmt.Sprintf("syscall.Write returned %d with no error", n))
+ panic(fmt.Sprintf("unix.Write returned %d with no error", n))
}
- if err != syscall.EINTR {
+ if err != unix.EINTR {
// If the write failed for anything other than a signal, bail out.
break
}
@@ -110,7 +111,7 @@ func (r *ReadWriter) Write(b []byte) (int, error) {
func (r *ReadWriter) WriteAt(b []byte, off int64) (c int, err error) {
for len(b) > 0 {
var m int
- m, err = fixCount(syscall.Pwrite(r.FD(), b, off))
+ m, err = fixCount(unix.Pwrite(r.FD(), b, off))
if err != nil {
break
}
@@ -167,7 +168,7 @@ func New(fd int) *FD {
//
// The returned FD is always blocking (Go 1.9+).
func NewFromFile(file *os.File) (*FD, error) {
- fd, err := syscall.Dup(int(file.Fd()))
+ fd, err := unix.Dup(int(file.Fd()))
// Technically, the runtime may call the finalizer on file as soon as
// Fd() returns.
runtime.KeepAlive(file)
@@ -196,7 +197,7 @@ func NewFromFiles(files []*os.File) ([]*FD, error) {
// Open is equivalent to open(2).
func Open(path string, openmode int, perm uint32) (*FD, error) {
- f, err := syscall.Open(path, openmode|syscall.O_LARGEFILE, perm)
+ f, err := unix.Open(path, openmode|unix.O_LARGEFILE, perm)
if err != nil {
return nil, err
}
@@ -205,7 +206,7 @@ func Open(path string, openmode int, perm uint32) (*FD, error) {
// OpenAt is equivalent to openat(2).
func OpenAt(dir *FD, path string, flags int, mode uint32) (*FD, error) {
- f, err := syscall.Openat(dir.FD(), path, flags, mode)
+ f, err := unix.Openat(dir.FD(), path, flags, mode)
if err != nil {
return nil, err
}
@@ -220,7 +221,7 @@ func OpenAt(dir *FD, path string, flags int, mode uint32) (*FD, error) {
// Concurrently calling Close and any other method is undefined.
func (f *FD) Close() error {
runtime.SetFinalizer(f, nil)
- return syscall.Close(int(atomic.SwapInt64(&f.fd, -1)))
+ return unix.Close(int(atomic.SwapInt64(&f.fd, -1)))
}
// Release relinquishes ownership of the contained file descriptor.
@@ -241,7 +242,7 @@ func (f *FD) Release() int {
// This operation is somewhat expensive, so care should be taken to minimize
// its use.
func (f *FD) File() (*os.File, error) {
- fd, err := syscall.Dup(f.FD())
+ fd, err := unix.Dup(f.FD())
if err != nil {
return nil, err
}
diff --git a/pkg/fd/fd_test.go b/pkg/fd/fd_test.go
index 5fb0ad47d..da71364d4 100644
--- a/pkg/fd/fd_test.go
+++ b/pkg/fd/fd_test.go
@@ -17,8 +17,9 @@ package fd
import (
"math"
"os"
- "syscall"
"testing"
+
+ "golang.org/x/sys/unix"
)
func TestSetNegOne(t *testing.T) {
@@ -29,22 +30,22 @@ func TestSetNegOne(t *testing.T) {
}
var tests []entry
- fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
+ fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
if err != nil {
- t.Fatal("syscall.Socket:", err)
+ t.Fatal("unix.Socket:", err)
}
f1 := New(fd)
tests = append(tests, entry{
"Release",
f1,
func() error {
- return syscall.Close(f1.Release())
+ return unix.Close(f1.Release())
},
})
- fd, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
+ fd, err = unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
if err != nil {
- t.Fatal("syscall.Socket:", err)
+ t.Fatal("unix.Socket:", err)
}
f2 := New(fd)
tests = append(tests, entry{
@@ -85,9 +86,9 @@ func TestStartsNegOne(t *testing.T) {
}
func TestFileDotFile(t *testing.T) {
- fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
+ fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
if err != nil {
- t.Fatal("syscall.Socket:", err)
+ t.Fatal("unix.Socket:", err)
}
f := New(fd)