Skip to content

Commit

Permalink
Merge pull request #2291 from kolyshkin/errors-unwrap-v2
Browse files Browse the repository at this point in the history
Use errors.As() and errors.Is() to unwrap errors
  • Loading branch information
crosbymichael authored Apr 3, 2020
2 parents ec8c695 + b2272b2 commit e4363b0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
11 changes: 8 additions & 3 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sync"
"syscall"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/kmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs2/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion libcontainer/cgroups/fscommon/fscommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e4363b0

Please sign in to comment.