diff options
author | gVisor bot <gvisor-bot@google.com> | 2021-10-29 21:07:05 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-10-29 21:07:05 +0000 |
commit | 806fa5c3235c08334de306eb5a5a40a51ee61c9f (patch) | |
tree | 1a357aa5ffff62553adb164e8c3ec57eea3e1460 /pkg | |
parent | 9c247a0b6254bedf6231a3bda77b7476e13a6ff2 (diff) | |
parent | b822923b706d6d2c5206451040f51a8c2f961353 (diff) |
Merge release-20211019.0-53-gb822923b7 (automated)
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/errors/linuxerr/linuxerr.go | 123 | ||||
-rw-r--r-- | pkg/p9/handlers.go | 11 | ||||
-rw-r--r-- | pkg/p9/server.go | 4 | ||||
-rw-r--r-- | pkg/sentry/kernel/task_syscall.go | 4 |
4 files changed, 77 insertions, 65 deletions
diff --git a/pkg/errors/linuxerr/linuxerr.go b/pkg/errors/linuxerr/linuxerr.go index 5905ef593..e44a55afd 100644 --- a/pkg/errors/linuxerr/linuxerr.go +++ b/pkg/errors/linuxerr/linuxerr.go @@ -34,41 +34,41 @@ const maxErrno uint32 = errno.EHWPOISON + 1 // (e.g. unix.Errno(EPERM.Errno()) == unix.EPERM is true). Converting unix/syscall.Errno // to the errors should be done via the lookup methods provided. var ( - NOERROR = errors.New(errno.NOERRNO, "not an error") - EPERM = errors.New(errno.EPERM, "operation not permitted") - ENOENT = errors.New(errno.ENOENT, "no such file or directory") - ESRCH = errors.New(errno.ESRCH, "no such process") - EINTR = errors.New(errno.EINTR, "interrupted system call") - EIO = errors.New(errno.EIO, "I/O error") - ENXIO = errors.New(errno.ENXIO, "no such device or address") - E2BIG = errors.New(errno.E2BIG, "argument list too long") - ENOEXEC = errors.New(errno.ENOEXEC, "exec format error") - EBADF = errors.New(errno.EBADF, "bad file number") - ECHILD = errors.New(errno.ECHILD, "no child processes") - EAGAIN = errors.New(errno.EAGAIN, "try again") - ENOMEM = errors.New(errno.ENOMEM, "out of memory") - EACCES = errors.New(errno.EACCES, "permission denied") - EFAULT = errors.New(errno.EFAULT, "bad address") - ENOTBLK = errors.New(errno.ENOTBLK, "block device required") - EBUSY = errors.New(errno.EBUSY, "device or resource busy") - EEXIST = errors.New(errno.EEXIST, "file exists") - EXDEV = errors.New(errno.EXDEV, "cross-device link") - ENODEV = errors.New(errno.ENODEV, "no such device") - ENOTDIR = errors.New(errno.ENOTDIR, "not a directory") - EISDIR = errors.New(errno.EISDIR, "is a directory") - EINVAL = errors.New(errno.EINVAL, "invalid argument") - ENFILE = errors.New(errno.ENFILE, "file table overflow") - EMFILE = errors.New(errno.EMFILE, "too many open files") - ENOTTY = errors.New(errno.ENOTTY, "not a typewriter") - ETXTBSY = errors.New(errno.ETXTBSY, "text file busy") - EFBIG = errors.New(errno.EFBIG, "file too large") - ENOSPC = errors.New(errno.ENOSPC, "no space left on device") - ESPIPE = errors.New(errno.ESPIPE, "illegal seek") - EROFS = errors.New(errno.EROFS, "read-only file system") - EMLINK = errors.New(errno.EMLINK, "too many links") - EPIPE = errors.New(errno.EPIPE, "broken pipe") - EDOM = errors.New(errno.EDOM, "math argument out of domain of func") - ERANGE = errors.New(errno.ERANGE, "math result not representable") + noError *errors.Error = nil + EPERM = errors.New(errno.EPERM, "operation not permitted") + ENOENT = errors.New(errno.ENOENT, "no such file or directory") + ESRCH = errors.New(errno.ESRCH, "no such process") + EINTR = errors.New(errno.EINTR, "interrupted system call") + EIO = errors.New(errno.EIO, "I/O error") + ENXIO = errors.New(errno.ENXIO, "no such device or address") + E2BIG = errors.New(errno.E2BIG, "argument list too long") + ENOEXEC = errors.New(errno.ENOEXEC, "exec format error") + EBADF = errors.New(errno.EBADF, "bad file number") + ECHILD = errors.New(errno.ECHILD, "no child processes") + EAGAIN = errors.New(errno.EAGAIN, "try again") + ENOMEM = errors.New(errno.ENOMEM, "out of memory") + EACCES = errors.New(errno.EACCES, "permission denied") + EFAULT = errors.New(errno.EFAULT, "bad address") + ENOTBLK = errors.New(errno.ENOTBLK, "block device required") + EBUSY = errors.New(errno.EBUSY, "device or resource busy") + EEXIST = errors.New(errno.EEXIST, "file exists") + EXDEV = errors.New(errno.EXDEV, "cross-device link") + ENODEV = errors.New(errno.ENODEV, "no such device") + ENOTDIR = errors.New(errno.ENOTDIR, "not a directory") + EISDIR = errors.New(errno.EISDIR, "is a directory") + EINVAL = errors.New(errno.EINVAL, "invalid argument") + ENFILE = errors.New(errno.ENFILE, "file table overflow") + EMFILE = errors.New(errno.EMFILE, "too many open files") + ENOTTY = errors.New(errno.ENOTTY, "not a typewriter") + ETXTBSY = errors.New(errno.ETXTBSY, "text file busy") + EFBIG = errors.New(errno.EFBIG, "file too large") + ENOSPC = errors.New(errno.ENOSPC, "no space left on device") + ESPIPE = errors.New(errno.ESPIPE, "illegal seek") + EROFS = errors.New(errno.EROFS, "read-only file system") + EMLINK = errors.New(errno.EMLINK, "too many links") + EPIPE = errors.New(errno.EPIPE, "broken pipe") + EDOM = errors.New(errno.EDOM, "math argument out of domain of func") + ERANGE = errors.New(errno.ERANGE, "math result not representable") // Errno values from include/uapi/asm-generic/errno.h. EDEADLK = errors.New(errno.EDEADLK, "resource deadlock would occur") @@ -186,7 +186,7 @@ var errNotValidError = errors.New(errno.Errno(maxErrno), "not a valid error") // errnos (especially uint32(sycall.Errno)) and *errors.Error. var errorSlice = []*errors.Error{ // Errno values from include/uapi/asm-generic/errno-base.h. - errno.NOERRNO: NOERROR, + errno.NOERRNO: noError, errno.EPERM: EPERM, errno.ENOENT: ENOENT, errno.ESRCH: ESRCH, @@ -324,32 +324,45 @@ var errorSlice = []*errors.Error{ errno.EHWPOISON: EHWPOISON, } -// ErrorFromErrno gets an error from the list and panics if an invalid entry is requested. -func ErrorFromErrno(e errno.Errno) *errors.Error { - err := errorSlice[e] +// ErrorFromUnix returns a linuxerr from a unix.Errno. +func ErrorFromUnix(err unix.Errno) error { + if err == unix.Errno(0) { + return nil + } + e := errorSlice[errno.Errno(err)] // Done this way because a single comparison in benchmarks is 2-3 faster // than something like ( if err == nil && err > 0 ). - if err != errNotValidError { - return err + if e == errNotValidError { + panic(fmt.Sprintf("invalid error requested with errno: %v", e)) } - panic(fmt.Sprintf("invalid error requested with errno: %d", e)) + return e } -// Equals compars a linuxerr to a given error -// TODO(b/34162363): Remove when syserror is removed. -func Equals(e *errors.Error, err error) bool { - if err == nil { - return e == NOERROR || e == nil +// ToError converts a linuxerr to an error type. +func ToError(err *errors.Error) error { + if err == noError { + return nil } - if e == nil { - return err == NOERROR || err == unix.Errno(0) + return err +} + +// ToUnix converts a linuxerr to a unix.Errno. +func ToUnix(e *errors.Error) unix.Errno { + var unixErr unix.Errno + if e != noError { + unixErr = unix.Errno(e.Errno()) } + return unixErr +} - switch err.(type) { - case *errors.Error: - return e == err - case unix.Errno, error: - return unix.Errno(e.Errno()) == err +// Equals compars a linuxerr to a given error. +func Equals(e *errors.Error, err error) bool { + var unixErr unix.Errno + if e != noError { + unixErr = unix.Errno(e.Errno()) + } + if err == nil { + err = noError } - return false + return e == err || unixErr == err } diff --git a/pkg/p9/handlers.go b/pkg/p9/handlers.go index a8f8a9d03..2657081e3 100644 --- a/pkg/p9/handlers.go +++ b/pkg/p9/handlers.go @@ -23,7 +23,6 @@ import ( "sync/atomic" "golang.org/x/sys/unix" - "gvisor.dev/gvisor/pkg/abi/linux/errno" "gvisor.dev/gvisor/pkg/errors" "gvisor.dev/gvisor/pkg/errors/linuxerr" "gvisor.dev/gvisor/pkg/fd" @@ -46,7 +45,7 @@ func ExtractErrno(err error) unix.Errno { // Attempt to unwrap. switch e := err.(type) { case *errors.Error: - return unix.Errno(e.Errno()) + return linuxerr.ToUnix(e) case unix.Errno: return e case *os.PathError: @@ -69,7 +68,7 @@ func newErr(err error) *Rlerror { // ExtractLinuxerrErrno extracts a *errors.Error from a error, best effort. // TODO(b/34162363): Merge this with ExtractErrno. -func ExtractLinuxerrErrno(err error) *errors.Error { +func ExtractLinuxerrErrno(err error) error { switch err { case os.ErrNotExist: return linuxerr.ENOENT @@ -84,9 +83,9 @@ func ExtractLinuxerrErrno(err error) *errors.Error { // Attempt to unwrap. switch e := err.(type) { case *errors.Error: - return e + return linuxerr.ToError(e) case unix.Errno: - return linuxerr.ErrorFromErrno(errno.Errno(e)) + return linuxerr.ErrorFromUnix(e) case *os.PathError: return ExtractLinuxerrErrno(e.Err) case *os.SyscallError: @@ -103,7 +102,7 @@ func ExtractLinuxerrErrno(err error) *errors.Error { // newErrFromLinuxerr returns an Rlerror from the linuxerr list. // TODO(b/34162363): Merge this with newErr. func newErrFromLinuxerr(err error) *Rlerror { - return &Rlerror{Error: uint32(ExtractLinuxerrErrno(err).Errno())} + return &Rlerror{Error: uint32(ExtractErrno(err))} } // handler is implemented for server-handled messages. diff --git a/pkg/p9/server.go b/pkg/p9/server.go index 241ab44ef..6428ad745 100644 --- a/pkg/p9/server.go +++ b/pkg/p9/server.go @@ -19,7 +19,7 @@ import ( "runtime/debug" "sync/atomic" - "gvisor.dev/gvisor/pkg/abi/linux/errno" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/errors/linuxerr" "gvisor.dev/gvisor/pkg/fd" "gvisor.dev/gvisor/pkg/fdchannel" @@ -510,7 +510,7 @@ func (cs *connState) handle(m message) (r message) { // It will be removed a followup, when all the unix.Errno errors are // replaced with linuxerr. if rlError, ok := r.(*Rlerror); ok { - e := linuxerr.ErrorFromErrno(errno.Errno(rlError.Error)) + e := linuxerr.ErrorFromUnix(unix.Errno(rlError.Error)) r = newErrFromLinuxerr(e) } } else { diff --git a/pkg/sentry/kernel/task_syscall.go b/pkg/sentry/kernel/task_syscall.go index 2b1d7e114..2b85fe1ca 100644 --- a/pkg/sentry/kernel/task_syscall.go +++ b/pkg/sentry/kernel/task_syscall.go @@ -381,7 +381,7 @@ func ExtractErrno(err error, sysno int) int { case unix.Errno: return int(err) case *errors.Error: - return int(err.Errno()) + return int(linuxerr.ToUnix(err)) case *memmap.BusError: // Bus errors may generate SIGBUS, but for syscalls they still // return EFAULT. See case in task_run.go where the fault is @@ -395,7 +395,7 @@ func ExtractErrno(err error, sysno int) int { return ExtractErrno(err.Err, sysno) default: if errno, ok := linuxerr.TranslateError(err); ok { - return int(errno.Errno()) + return int(linuxerr.ToUnix(errno)) } } panic(fmt.Sprintf("Unknown syscall %d error: %v", sysno, err)) |