diff options
Diffstat (limited to 'pkg/syserror')
-rw-r--r-- | pkg/syserror/BUILD | 2 | ||||
-rw-r--r-- | pkg/syserror/syserror.go | 131 | ||||
-rw-r--r-- | pkg/syserror/syserror_test.go | 38 |
3 files changed, 87 insertions, 84 deletions
diff --git a/pkg/syserror/BUILD b/pkg/syserror/BUILD index b13c15d9b..7d2f5adf6 100644 --- a/pkg/syserror/BUILD +++ b/pkg/syserror/BUILD @@ -6,6 +6,7 @@ go_library( name = "syserror", srcs = ["syserror.go"], visibility = ["//visibility:public"], + deps = ["@org_golang_x_sys//unix:go_default_library"], ) go_test( @@ -13,5 +14,6 @@ go_test( srcs = ["syserror_test.go"], deps = [ ":syserror", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/syserror/syserror.go b/pkg/syserror/syserror.go index f516c8e46..97de17afe 100644 --- a/pkg/syserror/syserror.go +++ b/pkg/syserror/syserror.go @@ -20,68 +20,69 @@ package syserror import ( "errors" - "syscall" + + "golang.org/x/sys/unix" ) // The following variables have the same meaning as their syscall equivalent. var ( - E2BIG = error(syscall.E2BIG) - EACCES = error(syscall.EACCES) - EADDRINUSE = error(syscall.EADDRINUSE) - EAGAIN = error(syscall.EAGAIN) - EBADF = error(syscall.EBADF) - EBADFD = error(syscall.EBADFD) - EBUSY = error(syscall.EBUSY) - ECHILD = error(syscall.ECHILD) - ECONNABORTED = error(syscall.ECONNABORTED) - ECONNREFUSED = error(syscall.ECONNREFUSED) - ECONNRESET = error(syscall.ECONNRESET) - EDEADLK = error(syscall.EDEADLK) - EEXIST = error(syscall.EEXIST) - EFAULT = error(syscall.EFAULT) - EFBIG = error(syscall.EFBIG) - EIDRM = error(syscall.EIDRM) - EINTR = error(syscall.EINTR) - EINVAL = error(syscall.EINVAL) - EIO = error(syscall.EIO) - EISDIR = error(syscall.EISDIR) - ELIBBAD = error(syscall.ELIBBAD) - ELOOP = error(syscall.ELOOP) - EMFILE = error(syscall.EMFILE) - EMLINK = error(syscall.EMLINK) - EMSGSIZE = error(syscall.EMSGSIZE) - ENAMETOOLONG = error(syscall.ENAMETOOLONG) + E2BIG = error(unix.E2BIG) + EACCES = error(unix.EACCES) + EADDRINUSE = error(unix.EADDRINUSE) + EAGAIN = error(unix.EAGAIN) + EBADF = error(unix.EBADF) + EBADFD = error(unix.EBADFD) + EBUSY = error(unix.EBUSY) + ECHILD = error(unix.ECHILD) + ECONNABORTED = error(unix.ECONNABORTED) + ECONNREFUSED = error(unix.ECONNREFUSED) + ECONNRESET = error(unix.ECONNRESET) + EDEADLK = error(unix.EDEADLK) + EEXIST = error(unix.EEXIST) + EFAULT = error(unix.EFAULT) + EFBIG = error(unix.EFBIG) + EIDRM = error(unix.EIDRM) + EINTR = error(unix.EINTR) + EINVAL = error(unix.EINVAL) + EIO = error(unix.EIO) + EISDIR = error(unix.EISDIR) + ELIBBAD = error(unix.ELIBBAD) + ELOOP = error(unix.ELOOP) + EMFILE = error(unix.EMFILE) + EMLINK = error(unix.EMLINK) + EMSGSIZE = error(unix.EMSGSIZE) + ENAMETOOLONG = error(unix.ENAMETOOLONG) ENOATTR = ENODATA - ENOBUFS = error(syscall.ENOBUFS) - ENODATA = error(syscall.ENODATA) - ENODEV = error(syscall.ENODEV) - ENOENT = error(syscall.ENOENT) - ENOEXEC = error(syscall.ENOEXEC) - ENOLCK = error(syscall.ENOLCK) - ENOLINK = error(syscall.ENOLINK) - ENOMEM = error(syscall.ENOMEM) - ENOSPC = error(syscall.ENOSPC) - ENOSYS = error(syscall.ENOSYS) - ENOTCONN = error(syscall.ENOTCONN) - ENOTDIR = error(syscall.ENOTDIR) - ENOTEMPTY = error(syscall.ENOTEMPTY) - ENOTSOCK = error(syscall.ENOTSOCK) - ENOTSUP = error(syscall.ENOTSUP) - ENOTTY = error(syscall.ENOTTY) - ENXIO = error(syscall.ENXIO) - EOPNOTSUPP = error(syscall.EOPNOTSUPP) - EOVERFLOW = error(syscall.EOVERFLOW) - EPERM = error(syscall.EPERM) - EPIPE = error(syscall.EPIPE) - ERANGE = error(syscall.ERANGE) - EREMOTE = error(syscall.EREMOTE) - EROFS = error(syscall.EROFS) - ESPIPE = error(syscall.ESPIPE) - ESRCH = error(syscall.ESRCH) - ETIMEDOUT = error(syscall.ETIMEDOUT) - EUSERS = error(syscall.EUSERS) - EWOULDBLOCK = error(syscall.EWOULDBLOCK) - EXDEV = error(syscall.EXDEV) + ENOBUFS = error(unix.ENOBUFS) + ENODATA = error(unix.ENODATA) + ENODEV = error(unix.ENODEV) + ENOENT = error(unix.ENOENT) + ENOEXEC = error(unix.ENOEXEC) + ENOLCK = error(unix.ENOLCK) + ENOLINK = error(unix.ENOLINK) + ENOMEM = error(unix.ENOMEM) + ENOSPC = error(unix.ENOSPC) + ENOSYS = error(unix.ENOSYS) + ENOTCONN = error(unix.ENOTCONN) + ENOTDIR = error(unix.ENOTDIR) + ENOTEMPTY = error(unix.ENOTEMPTY) + ENOTSOCK = error(unix.ENOTSOCK) + ENOTSUP = error(unix.ENOTSUP) + ENOTTY = error(unix.ENOTTY) + ENXIO = error(unix.ENXIO) + EOPNOTSUPP = error(unix.EOPNOTSUPP) + EOVERFLOW = error(unix.EOVERFLOW) + EPERM = error(unix.EPERM) + EPIPE = error(unix.EPIPE) + ERANGE = error(unix.ERANGE) + EREMOTE = error(unix.EREMOTE) + EROFS = error(unix.EROFS) + ESPIPE = error(unix.ESPIPE) + ESRCH = error(unix.ESRCH) + ETIMEDOUT = error(unix.ETIMEDOUT) + EUSERS = error(unix.EUSERS) + EWOULDBLOCK = error(unix.EWOULDBLOCK) + EXDEV = error(unix.EXDEV) ) var ( @@ -102,16 +103,16 @@ var ( ) // errorMap is the map used to convert generic errors into errnos. -var errorMap = map[error]syscall.Errno{} +var errorMap = map[error]unix.Errno{} // errorUnwrappers is an array of unwrap functions to extract typed errors. -var errorUnwrappers = []func(error) (syscall.Errno, bool){} +var errorUnwrappers = []func(error) (unix.Errno, bool){} // AddErrorTranslation allows modules to populate the error map by adding their // own translations during initialization. Returns if the error translation is // accepted or not. A pre-existing translation will not be overwritten by the // new translation. -func AddErrorTranslation(from error, to syscall.Errno) bool { +func AddErrorTranslation(from error, to unix.Errno) bool { if _, ok := errorMap[from]; ok { return false } @@ -122,13 +123,13 @@ func AddErrorTranslation(from error, to syscall.Errno) bool { // AddErrorUnwrapper registers an unwrap method that can extract a concrete error // from a typed, but not initialized, error. -func AddErrorUnwrapper(unwrap func(e error) (syscall.Errno, bool)) { +func AddErrorUnwrapper(unwrap func(e error) (unix.Errno, bool)) { errorUnwrappers = append(errorUnwrappers, unwrap) } // TranslateError translates errors to errnos, it will return false if // the error was not registered. -func TranslateError(from error) (syscall.Errno, bool) { +func TranslateError(from error) (unix.Errno, bool) { err, ok := errorMap[from] if ok { return err, ok @@ -222,7 +223,7 @@ func SyscallRestartErrnoFromReturn(rv uintptr) (SyscallRestartErrno, bool) { } func init() { - AddErrorTranslation(ErrWouldBlock, syscall.EWOULDBLOCK) - AddErrorTranslation(ErrInterrupted, syscall.EINTR) - AddErrorTranslation(ErrExceedsFileSizeLimit, syscall.EFBIG) + AddErrorTranslation(ErrWouldBlock, unix.EWOULDBLOCK) + AddErrorTranslation(ErrInterrupted, unix.EINTR) + AddErrorTranslation(ErrExceedsFileSizeLimit, unix.EFBIG) } diff --git a/pkg/syserror/syserror_test.go b/pkg/syserror/syserror_test.go index 7036467c4..c141e5f6e 100644 --- a/pkg/syserror/syserror_test.go +++ b/pkg/syserror/syserror_test.go @@ -16,9 +16,9 @@ package syserror_test import ( "errors" - "syscall" "testing" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/syserror" ) @@ -26,7 +26,7 @@ var globalError error func BenchmarkAssignErrno(b *testing.B) { for i := b.N; i > 0; i-- { - globalError = syscall.EINVAL + globalError = unix.EINVAL } } @@ -37,10 +37,10 @@ func BenchmarkAssignError(b *testing.B) { } func BenchmarkCompareErrno(b *testing.B) { - globalError = syscall.EAGAIN + globalError = unix.EAGAIN j := 0 for i := b.N; i > 0; i-- { - if globalError == syscall.EINVAL { + if globalError == unix.EINVAL { j++ } } @@ -57,15 +57,15 @@ func BenchmarkCompareError(b *testing.B) { } func BenchmarkSwitchErrno(b *testing.B) { - globalError = syscall.EPERM + globalError = unix.EPERM j := 0 for i := b.N; i > 0; i-- { switch globalError { - case syscall.EINVAL: + case unix.EINVAL: j += 1 - case syscall.EINTR: + case unix.EINTR: j += 2 - case syscall.EAGAIN: + case unix.EAGAIN: j += 3 } } @@ -89,9 +89,9 @@ func BenchmarkSwitchError(b *testing.B) { type translationTestTable struct { fn string errIn error - syscallErrorIn syscall.Errno + syscallErrorIn unix.Errno expectedBool bool - expectedTranslation syscall.Errno + expectedTranslation unix.Errno } func TestErrorTranslation(t *testing.T) { @@ -100,16 +100,16 @@ func TestErrorTranslation(t *testing.T) { testTable := []translationTestTable{ {"TranslateError", myError, 0, false, 0}, {"TranslateError", myError2, 0, false, 0}, - {"AddErrorTranslation", myError, syscall.EAGAIN, true, 0}, - {"AddErrorTranslation", myError, syscall.EAGAIN, false, 0}, - {"AddErrorTranslation", myError, syscall.EPERM, false, 0}, - {"TranslateError", myError, 0, true, syscall.EAGAIN}, + {"AddErrorTranslation", myError, unix.EAGAIN, true, 0}, + {"AddErrorTranslation", myError, unix.EAGAIN, false, 0}, + {"AddErrorTranslation", myError, unix.EPERM, false, 0}, + {"TranslateError", myError, 0, true, unix.EAGAIN}, {"TranslateError", myError2, 0, false, 0}, - {"AddErrorTranslation", myError2, syscall.EPERM, true, 0}, - {"AddErrorTranslation", myError2, syscall.EPERM, false, 0}, - {"AddErrorTranslation", myError2, syscall.EAGAIN, false, 0}, - {"TranslateError", myError, 0, true, syscall.EAGAIN}, - {"TranslateError", myError2, 0, true, syscall.EPERM}, + {"AddErrorTranslation", myError2, unix.EPERM, true, 0}, + {"AddErrorTranslation", myError2, unix.EPERM, false, 0}, + {"AddErrorTranslation", myError2, unix.EAGAIN, false, 0}, + {"TranslateError", myError, 0, true, unix.EAGAIN}, + {"TranslateError", myError2, 0, true, unix.EPERM}, } for _, tt := range testTable { switch tt.fn { |