Merge pull request #2291 from kolyshkin/errors-unwrap-v2

Use errors.As() and errors.Is() to unwrap errors
This commit is contained in:
Michael Crosby 2020-04-03 11:46:11 -04:00 committed by GitHub
commit e4363b0387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sync"
"syscall"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
@ -110,14 +111,18 @@ func isIgnorableError(rootless bool, err error) bool {
if !rootless {
return false
}
// TODO: rm errors.Cause once we switch to %w everywhere
err = errors.Cause(err)
// Is it an ordinary EPERM?
if os.IsPermission(err) {
if errors.Is(err, os.ErrPermission) {
return true
}
// Handle some specific syscall errors.
errno := errors.Unwrap(err)
return errno == unix.EROFS || errno == unix.EPERM || errno == unix.EACCES
var errno syscall.Errno
if errors.As(err, &errno) {
return errno == unix.EROFS || errno == unix.EPERM || errno == unix.EACCES
}
return false
}
func (m *Manager) getSubsystems() subsystemSet {

View File

@ -47,7 +47,7 @@ func setKernelMemory(path string, kernelMemoryLimit int64) error {
// The EBUSY signal is returned on attempts to write to the
// memory.kmem.limit_in_bytes file if the cgroup has children or
// once tasks have been attached to the cgroup
if errors.Unwrap(err) == unix.EBUSY {
if errors.Is(err, unix.EBUSY) {
return fmt.Errorf("failed to set %s, because either tasks have already joined this cgroup or it has children", cgroupKernelMemoryLimit)
}
return fmt.Errorf("failed to write %v to %v: %v", kernelMemoryLimit, cgroupKernelMemoryLimit, err)

View File

@ -28,7 +28,7 @@ func statPidsWithoutController(dirPath string, stats *cgroups.Stats) error {
// if the controller is not enabled, let's read PIDS from cgroups.procs
// (or threads if cgroup.threads is enabled)
contents, err := ioutil.ReadFile(filepath.Join(dirPath, "cgroup.procs"))
if err != nil && errors.Unwrap(err) == unix.ENOTSUP {
if errors.Is(err, unix.ENOTSUP) {
contents, err = ioutil.ReadFile(filepath.Join(dirPath, "cgroup.threads"))
}
if err != nil {

View File

@ -41,10 +41,20 @@ func ReadFile(dir, file string) (string, error) {
func retryingWriteFile(filename string, data []byte, perm os.FileMode) error {
for {
err := ioutil.WriteFile(filename, data, perm)
if errors.Unwrap(err) == syscall.EINTR {
if isInterruptedWriteFile(err) {
logrus.Infof("interrupted while writing %s to %s", string(data), filename)
continue
}
return err
}
}
func isInterruptedWriteFile(err error) bool {
if patherr, ok := err.(*os.PathError); ok {
errno, ok2 := patherr.Err.(syscall.Errno)
if ok2 && errno == syscall.EINTR {
return true
}
}
return false
}

View File

@ -576,7 +576,7 @@ func WriteCgroupProc(dir string, pid int) error {
// EINVAL might mean that the task being added to cgroup.procs is in state
// TASK_NEW. We should attempt to do so again.
if errors.Unwrap(err) == unix.EINVAL {
if errors.Is(err, unix.EINVAL) {
time.Sleep(30 * time.Millisecond)
continue
}

View File

@ -1855,7 +1855,7 @@ func (c *linuxContainer) isPaused() (bool, error) {
data, err := ioutil.ReadFile(filepath.Join(fcg, filename))
if err != nil {
// If freezer cgroup is not mounted, the container would just be not paused.
if os.IsNotExist(err) || errors.Unwrap(err) == syscall.ENODEV {
if os.IsNotExist(err) || errors.Is(err, syscall.ENODEV) {
return false, nil
}
return false, newSystemErrorWithCause(err, "checking if container is paused")