Skip to content

Commit

Permalink
Merge pull request #2280 from kolyshkin/errors-unwrap
Browse files Browse the repository at this point in the history
Use errors.Unwrap() where possible
  • Loading branch information
crosbymichael authored Apr 2, 2020
2 parents 6ca9d8e + 12e156f commit f8e1388
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 56 deletions.
16 changes: 4 additions & 12 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,13 @@ func isIgnorableError(rootless bool, err error) bool {
if !rootless {
return false
}
err = errors.Cause(err)
// Is it an ordinary EPERM?
if os.IsPermission(errors.Cause(err)) {
if os.IsPermission(err) {
return true
}

// Try to handle other errnos.
var errno error
switch err := errors.Cause(err).(type) {
case *os.PathError:
errno = err.Err
case *os.LinkError:
errno = err.Err
case *os.SyscallError:
errno = err.Err
}
// Handle some specific syscall errors.
errno := errors.Unwrap(err)
return errno == unix.EROFS || errno == unix.EPERM || errno == unix.EACCES
}

Expand Down
10 changes: 2 additions & 8 deletions libcontainer/cgroups/fs/kmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall" // for Errno type only

"github.com/opencontainers/runc/libcontainer/cgroups"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -49,12 +47,8 @@ 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 pathErr, ok := err.(*os.PathError); ok {
if errNo, ok := pathErr.Err.(syscall.Errno); ok {
if errNo == unix.EBUSY {
return fmt.Errorf("failed to set %s, because either tasks have already joined this cgroup or it has children", cgroupKernelMemoryLimit)
}
}
if errors.Unwrap(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
12 changes: 1 addition & 11 deletions libcontainer/cgroups/fs2/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package fs2

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -25,20 +24,11 @@ func setPids(dirPath string, cgroup *configs.Cgroup) error {
return nil
}

func isNOTSUP(err error) bool {
switch err := err.(type) {
case *os.PathError:
return err.Err == unix.ENOTSUP
default:
return false
}
}

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 && isNOTSUP(err) {
if err != nil && errors.Unwrap(err) == unix.ENOTSUP {
contents, err = ioutil.ReadFile(filepath.Join(dirPath, "cgroup.threads"))
}
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions libcontainer/cgroups/fscommon/fscommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,10 @@ 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 isInterruptedWriteFile(err) {
if errors.Unwrap(err) == syscall.EINTR {
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
}
11 changes: 1 addition & 10 deletions 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 isEINVAL(err) {
if errors.Unwrap(err) == unix.EINVAL {
time.Sleep(30 * time.Millisecond)
continue
}
Expand All @@ -586,15 +586,6 @@ func WriteCgroupProc(dir string, pid int) error {
return err
}

func isEINVAL(err error) bool {
switch err := err.(type) {
case *os.PathError:
return err.Err == unix.EINVAL
default:
return false
}
}

// Since the OCI spec is designed for cgroup v1, in some cases
// there is need to convert from the cgroup v1 configuration to cgroup v2
// the formula for BlkIOWeight is y = (1 + (x - 10) * 9999 / 990)
Expand Down
5 changes: 1 addition & 4 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1855,10 +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) {
return false, nil
}
if pathError, isPathError := err.(*os.PathError); isPathError && pathError.Err == syscall.ENODEV {
if os.IsNotExist(err) || errors.Unwrap(err) == syscall.ENODEV {
return false, nil
}
return false, newSystemErrorWithCause(err, "checking if container is paused")
Expand Down

0 comments on commit f8e1388

Please sign in to comment.