mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-12 09:46:25 +08:00
libct/cg: stop using pkg/errors
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -2,11 +2,12 @@ package cgroups
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
@@ -15,7 +16,7 @@ import (
|
|||||||
// It is supposed to be used for cgroup files only.
|
// It is supposed to be used for cgroup files only.
|
||||||
func OpenFile(dir, file string, flags int) (*os.File, error) {
|
func OpenFile(dir, file string, flags int) (*os.File, error) {
|
||||||
if dir == "" {
|
if dir == "" {
|
||||||
return nil, errors.Errorf("no directory specified for %s", file)
|
return nil, fmt.Errorf("no directory specified for %s", file)
|
||||||
}
|
}
|
||||||
return openFile(dir, file, flags)
|
return openFile(dir, file, flags)
|
||||||
}
|
}
|
||||||
@@ -43,7 +44,8 @@ func WriteFile(dir, file, data string) error {
|
|||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
if err := retryingWriteFile(fd, data); err != nil {
|
if err := retryingWriteFile(fd, data); err != nil {
|
||||||
return errors.Wrapf(err, "failed to write %q", data)
|
// Having data in the error message helps in debugging.
|
||||||
|
return fmt.Errorf("failed to write %q: %w", data, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,17 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CpusetGroup struct{}
|
type CpusetGroup struct{}
|
||||||
|
|||||||
@@ -3,17 +3,18 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -171,8 +172,6 @@ func isIgnorableError(rootless bool, err error) bool {
|
|||||||
if !rootless {
|
if !rootless {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// TODO: rm errors.Cause once we switch to %w everywhere
|
|
||||||
err = errors.Cause(err)
|
|
||||||
// Is it an ordinary EPERM?
|
// Is it an ordinary EPERM?
|
||||||
if errors.Is(err, os.ErrPermission) {
|
if errors.Is(err, os.ErrPermission) {
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package fs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
@@ -11,11 +12,11 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -56,7 +57,7 @@ func setMemory(path string, val int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.Errorf("unable to set memory limit to %d (current usage: %d, peak usage: %d)", val, usage, max)
|
return fmt.Errorf("unable to set memory limit to %d (current usage: %d, peak usage: %d)", val, usage, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setSwap(path string, val int64) error {
|
func setSwap(path string, val int64) error {
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package fs2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -25,18 +27,17 @@ import (
|
|||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const UnifiedMountpoint = "/sys/fs/cgroup"
|
const UnifiedMountpoint = "/sys/fs/cgroup"
|
||||||
|
|
||||||
func defaultDirPath(c *configs.Cgroup) (string, error) {
|
func defaultDirPath(c *configs.Cgroup) (string, error) {
|
||||||
if (c.Name != "" || c.Parent != "") && c.Path != "" {
|
if (c.Name != "" || c.Parent != "") && c.Path != "" {
|
||||||
return "", errors.Errorf("cgroup: either Path or Name and Parent should be used, got %+v", c)
|
return "", fmt.Errorf("cgroup: either Path or Name and Parent should be used, got %+v", c)
|
||||||
}
|
}
|
||||||
if len(c.Paths) != 0 {
|
if len(c.Paths) != 0 {
|
||||||
// never set by specconv
|
// never set by specconv
|
||||||
return "", errors.Errorf("cgroup: Paths is unsupported, use Path, got %+v", c)
|
return "", fmt.Errorf("cgroup: Paths is unsupported, use Path, got %+v", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: Do not remove this code. Path safety is important! -- cyphar
|
// XXX: Do not remove this code. Path safety is important! -- cyphar
|
||||||
@@ -89,7 +90,7 @@ func parseCgroupFromReader(r io.Reader) (string, error) {
|
|||||||
parts = strings.SplitN(text, ":", 3)
|
parts = strings.SplitN(text, ":", 3)
|
||||||
)
|
)
|
||||||
if len(parts) < 3 {
|
if len(parts) < 3 {
|
||||||
return "", errors.Errorf("invalid cgroup entry: %q", text)
|
return "", fmt.Errorf("invalid cgroup entry: %q", text)
|
||||||
}
|
}
|
||||||
// text is like "0::/user.slice/user-1001.slice/session-1.scope"
|
// text is like "0::/user.slice/user-1001.slice/session-1.scope"
|
||||||
if parts[0] == "0" && parts[1] == "" {
|
if parts[0] == "0" && parts[1] == "" {
|
||||||
|
|||||||
@@ -3,14 +3,15 @@
|
|||||||
package fs2
|
package fs2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/ebpf"
|
"github.com/opencontainers/runc/libcontainer/cgroups/ebpf"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/ebpf/devicefilter"
|
"github.com/opencontainers/runc/libcontainer/cgroups/ebpf/devicefilter"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/opencontainers/runc/libcontainer/devices"
|
"github.com/opencontainers/runc/libcontainer/devices"
|
||||||
"github.com/opencontainers/runc/libcontainer/userns"
|
"github.com/opencontainers/runc/libcontainer/userns"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isRWM(perms devices.Permissions) bool {
|
func isRWM(perms devices.Permissions) bool {
|
||||||
@@ -64,7 +65,7 @@ func setDevices(dirPath string, r *configs.Resources) error {
|
|||||||
}
|
}
|
||||||
dirFD, err := unix.Open(dirPath, unix.O_DIRECTORY|unix.O_RDONLY, 0o600)
|
dirFD, err := unix.Open(dirPath, unix.O_DIRECTORY|unix.O_RDONLY, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Errorf("cannot get dir FD for %s", dirPath)
|
return fmt.Errorf("cannot get dir FD for %s", dirPath)
|
||||||
}
|
}
|
||||||
defer unix.Close(dirFD)
|
defer unix.Close(dirFD)
|
||||||
if _, err := ebpf.LoadAttachCgroupDeviceFilter(insts, license, dirFD); err != nil {
|
if _, err := ebpf.LoadAttachCgroupDeviceFilter(insts, license, dirFD); err != nil {
|
||||||
|
|||||||
@@ -4,16 +4,16 @@ package fs2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
stdErrors "errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setFreezer(dirPath string, state configs.FreezerState) error {
|
func setFreezer(dirPath string, state configs.FreezerState) error {
|
||||||
@@ -26,7 +26,7 @@ func setFreezer(dirPath string, state configs.FreezerState) error {
|
|||||||
case configs.Thawed:
|
case configs.Thawed:
|
||||||
stateStr = "0"
|
stateStr = "0"
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("invalid freezer state %q requested", state)
|
return fmt.Errorf("invalid freezer state %q requested", state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := cgroups.OpenFile(dirPath, "cgroup.freeze", unix.O_RDWR)
|
fd, err := cgroups.OpenFile(dirPath, "cgroup.freeze", unix.O_RDWR)
|
||||||
@@ -37,7 +37,7 @@ func setFreezer(dirPath string, state configs.FreezerState) error {
|
|||||||
if state != configs.Frozen {
|
if state != configs.Frozen {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.Wrap(err, "freezer not supported")
|
return fmt.Errorf("freezer not supported: %w", err)
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ func setFreezer(dirPath string, state configs.FreezerState) error {
|
|||||||
if actualState, err := readFreezer(dirPath, fd); err != nil {
|
if actualState, err := readFreezer(dirPath, fd); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if actualState != state {
|
} else if actualState != state {
|
||||||
return errors.Errorf(`expected "cgroup.freeze" to be in state %q but was in %q`, state, actualState)
|
return fmt.Errorf(`expected "cgroup.freeze" to be in state %q but was in %q`, state, actualState)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ func getFreezer(dirPath string) (configs.FreezerState, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// If the kernel is too old, then we just treat the freezer as being in
|
// If the kernel is too old, then we just treat the freezer as being in
|
||||||
// an "undefined" state.
|
// an "undefined" state.
|
||||||
if os.IsNotExist(err) || stdErrors.Is(err, unix.ENODEV) {
|
if os.IsNotExist(err) || errors.Is(err, unix.ENODEV) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
return configs.Undefined, err
|
return configs.Undefined, err
|
||||||
@@ -82,7 +82,7 @@ func readFreezer(dirPath string, fd *os.File) (configs.FreezerState, error) {
|
|||||||
case "1\n":
|
case "1\n":
|
||||||
return waitFrozen(dirPath)
|
return waitFrozen(dirPath)
|
||||||
default:
|
default:
|
||||||
return configs.Undefined, errors.Errorf(`unknown "cgroup.freeze" state: %q`, state)
|
return configs.Undefined, fmt.Errorf(`unknown "cgroup.freeze" state: %q`, state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
package fs2
|
package fs2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -10,7 +11,6 @@ import (
|
|||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type parseError = fscommon.ParseError
|
type parseError = fscommon.ParseError
|
||||||
@@ -80,7 +80,7 @@ func (m *manager) Apply(pid int) error {
|
|||||||
if blNeed, nErr := needAnyControllers(m.config.Resources); nErr == nil && !blNeed {
|
if blNeed, nErr := needAnyControllers(m.config.Resources); nErr == nil && !blNeed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.Wrap(err, "rootless needs no limits + no cgrouppath when no permission is granted for cgroups")
|
return fmt.Errorf("rootless needs no limits + no cgrouppath when no permission is granted for cgroups: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -126,7 +126,7 @@ func (m *manager) GetStats() (*cgroups.Stats, error) {
|
|||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
if len(errs) > 0 && !m.rootless {
|
if len(errs) > 0 && !m.rootless {
|
||||||
return st, errors.Errorf("error while statting cgroup v2: %+v", errs)
|
return st, fmt.Errorf("error while statting cgroup v2: %+v", errs)
|
||||||
}
|
}
|
||||||
return st, nil
|
return st, nil
|
||||||
}
|
}
|
||||||
@@ -200,9 +200,8 @@ func (m *manager) setUnified(res map[string]string) error {
|
|||||||
return fmt.Errorf("unified resource %q must be a file name (no slashes)", k)
|
return fmt.Errorf("unified resource %q must be a file name (no slashes)", k)
|
||||||
}
|
}
|
||||||
if err := cgroups.WriteFile(m.dirPath, k, v); err != nil {
|
if err := cgroups.WriteFile(m.dirPath, k, v); err != nil {
|
||||||
errC := errors.Cause(err)
|
|
||||||
// Check for both EPERM and ENOENT since O_CREAT is used by WriteFile.
|
// Check for both EPERM and ENOENT since O_CREAT is used by WriteFile.
|
||||||
if errors.Is(errC, os.ErrPermission) || errors.Is(errC, os.ErrNotExist) {
|
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) {
|
||||||
// Check if a controller is available,
|
// Check if a controller is available,
|
||||||
// to give more specific error if not.
|
// to give more specific error if not.
|
||||||
sk := strings.SplitN(k, ".", 2)
|
sk := strings.SplitN(k, ".", 2)
|
||||||
@@ -214,7 +213,7 @@ func (m *manager) setUnified(res map[string]string) error {
|
|||||||
return fmt.Errorf("unified resource %q can't be set: controller %q not available", k, c)
|
return fmt.Errorf("unified resource %q can't be set: controller %q not available", k, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors.Wrapf(err, "can't set unified resource %q", k)
|
return fmt.Errorf("unable to set unified resource %q: %w", k, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,9 @@
|
|||||||
package fs2
|
package fs2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
@@ -32,7 +31,7 @@ func setHugeTlb(dirPath string, r *configs.Resources) error {
|
|||||||
func statHugeTlb(dirPath string, stats *cgroups.Stats) error {
|
func statHugeTlb(dirPath string, stats *cgroups.Stats) error {
|
||||||
hugePageSizes, err := cgroups.GetHugePageSize()
|
hugePageSizes, err := cgroups.GetHugePageSize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to fetch hugetlb info")
|
return fmt.Errorf("failed to fetch hugetlb info: %w", err)
|
||||||
}
|
}
|
||||||
hugetlbStats := cgroups.HugetlbStats{}
|
hugetlbStats := cgroups.HugetlbStats{}
|
||||||
|
|
||||||
|
|||||||
@@ -4,16 +4,17 @@ package fs2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// numToStr converts an int64 value to a string for writing to a
|
// numToStr converts an int64 value to a string for writing to a
|
||||||
|
|||||||
@@ -3,15 +3,16 @@
|
|||||||
package fs2
|
package fs2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isPidsSet(r *configs.Resources) bool {
|
func isPidsSet(r *configs.Resources) bool {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package systemd
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
@@ -14,11 +15,11 @@ import (
|
|||||||
|
|
||||||
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
||||||
dbus "github.com/godbus/dbus/v5"
|
dbus "github.com/godbus/dbus/v5"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
cgroupdevices "github.com/opencontainers/runc/libcontainer/cgroups/devices"
|
cgroupdevices "github.com/opencontainers/runc/libcontainer/cgroups/devices"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/opencontainers/runc/libcontainer/devices"
|
"github.com/opencontainers/runc/libcontainer/devices"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -92,7 +93,7 @@ func groupPrefix(ruleType devices.Type) (string, error) {
|
|||||||
case devices.CharDevice:
|
case devices.CharDevice:
|
||||||
return "char-", nil
|
return "char-", nil
|
||||||
default:
|
default:
|
||||||
return "", errors.Errorf("device type %v has no group prefix", ruleType)
|
return "", fmt.Errorf("device type %v has no group prefix", ruleType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,9 +143,9 @@ func findDeviceGroup(ruleType devices.Type, ruleMajor int64) (string, error) {
|
|||||||
)
|
)
|
||||||
if n, err := fmt.Sscanf(line, "%d %s", &currMajor, &currName); err != nil || n != 2 {
|
if n, err := fmt.Sscanf(line, "%d %s", &currMajor, &currName); err != nil || n != 2 {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = errors.Errorf("wrong number of fields")
|
err = errors.New("wrong number of fields")
|
||||||
}
|
}
|
||||||
return "", errors.Wrapf(err, "scan /proc/devices line %q", line)
|
return "", fmt.Errorf("scan /proc/devices line %q: %w", line, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if currMajor == ruleMajor {
|
if currMajor == ruleMajor {
|
||||||
@@ -152,7 +153,7 @@ func findDeviceGroup(ruleType devices.Type, ruleMajor int64) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return "", errors.Wrap(err, "reading /proc/devices")
|
return "", fmt.Errorf("reading /proc/devices: %w", err)
|
||||||
}
|
}
|
||||||
// Couldn't find the device group.
|
// Couldn't find the device group.
|
||||||
return "", nil
|
return "", nil
|
||||||
@@ -192,7 +193,7 @@ func generateDeviceProperties(r *configs.Resources) ([]systemdDbus.Property, err
|
|||||||
configEmu := &cgroupdevices.Emulator{}
|
configEmu := &cgroupdevices.Emulator{}
|
||||||
for _, rule := range r.Devices {
|
for _, rule := range r.Devices {
|
||||||
if err := configEmu.Apply(*rule); err != nil {
|
if err := configEmu.Apply(*rule); err != nil {
|
||||||
return nil, errors.Wrap(err, "apply rule for systemd")
|
return nil, fmt.Errorf("unable to apply rule for systemd: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// systemd doesn't support blacklists. So we log a warning, and tell
|
// systemd doesn't support blacklists. So we log a warning, and tell
|
||||||
@@ -213,19 +214,19 @@ func generateDeviceProperties(r *configs.Resources) ([]systemdDbus.Property, err
|
|||||||
// whitelist which is the default for devices.Emulator.
|
// whitelist which is the default for devices.Emulator.
|
||||||
finalRules, err := configEmu.Rules()
|
finalRules, err := configEmu.Rules()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "get simplified rules for systemd")
|
return nil, fmt.Errorf("unable to get simplified rules for systemd: %w", err)
|
||||||
}
|
}
|
||||||
var deviceAllowList []deviceAllowEntry
|
var deviceAllowList []deviceAllowEntry
|
||||||
for _, rule := range finalRules {
|
for _, rule := range finalRules {
|
||||||
if !rule.Allow {
|
if !rule.Allow {
|
||||||
// Should never happen.
|
// Should never happen.
|
||||||
return nil, errors.Errorf("[internal error] cannot add deny rule to systemd DeviceAllow list: %v", *rule)
|
return nil, fmt.Errorf("[internal error] cannot add deny rule to systemd DeviceAllow list: %v", *rule)
|
||||||
}
|
}
|
||||||
switch rule.Type {
|
switch rule.Type {
|
||||||
case devices.BlockDevice, devices.CharDevice:
|
case devices.BlockDevice, devices.CharDevice:
|
||||||
default:
|
default:
|
||||||
// Should never happen.
|
// Should never happen.
|
||||||
return nil, errors.Errorf("invalid device type for DeviceAllow: %v", rule.Type)
|
return nil, fmt.Errorf("invalid device type for DeviceAllow: %v", rule.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := deviceAllowEntry{
|
entry := deviceAllowEntry{
|
||||||
@@ -271,7 +272,7 @@ func generateDeviceProperties(r *configs.Resources) ([]systemdDbus.Property, err
|
|||||||
// "_ n:* _" rules require a device group from /proc/devices.
|
// "_ n:* _" rules require a device group from /proc/devices.
|
||||||
group, err := findDeviceGroup(rule.Type, rule.Major)
|
group, err := findDeviceGroup(rule.Type, rule.Major)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "find device '%v/%d'", rule.Type, rule.Major)
|
return nil, fmt.Errorf("unable to find device '%v/%d': %w", rule.Type, rule.Major, err)
|
||||||
}
|
}
|
||||||
if group == "" {
|
if group == "" {
|
||||||
// Couldn't find a group.
|
// Couldn't find a group.
|
||||||
@@ -342,7 +343,7 @@ func startUnit(cm *dbusConnManager, unitName string, properties []systemdDbus.Pr
|
|||||||
// Please refer to https://pkg.go.dev/github.com/coreos/go-systemd/v22/dbus#Conn.StartUnit
|
// Please refer to https://pkg.go.dev/github.com/coreos/go-systemd/v22/dbus#Conn.StartUnit
|
||||||
if s != "done" {
|
if s != "done" {
|
||||||
resetFailedUnit(cm, unitName)
|
resetFailedUnit(cm, unitName)
|
||||||
return errors.Errorf("error creating systemd unit `%s`: got `%s`", unitName, s)
|
return fmt.Errorf("error creating systemd unit `%s`: got `%s`", unitName, s)
|
||||||
}
|
}
|
||||||
case <-timeout.C:
|
case <-timeout.C:
|
||||||
resetFailedUnit(cm, unitName)
|
resetFailedUnit(cm, unitName)
|
||||||
@@ -432,10 +433,10 @@ func systemdVersionAtoi(verStr string) (int, error) {
|
|||||||
re := regexp.MustCompile(`v?([0-9]+)`)
|
re := regexp.MustCompile(`v?([0-9]+)`)
|
||||||
matches := re.FindStringSubmatch(verStr)
|
matches := re.FindStringSubmatch(verStr)
|
||||||
if len(matches) < 2 {
|
if len(matches) < 2 {
|
||||||
return 0, errors.Errorf("can't parse version %s: incorrect number of matches %v", verStr, matches)
|
return 0, fmt.Errorf("can't parse version %s: incorrect number of matches %v", verStr, matches)
|
||||||
}
|
}
|
||||||
ver, err := strconv.Atoi(matches[1])
|
ver, err := strconv.Atoi(matches[1])
|
||||||
return ver, errors.Wrapf(err, "can't parse version %s", verStr)
|
return ver, fmt.Errorf("can't parse version: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addCpuQuota(cm *dbusConnManager, properties *[]systemdDbus.Property, quota int64, period uint64) {
|
func addCpuQuota(cm *dbusConnManager, properties *[]systemdDbus.Property, quota int64, period uint64) {
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package systemd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bits-and-blooms/bitset"
|
"github.com/bits-and-blooms/bitset"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RangeToBits converts a text representation of a CPU mask (as written to
|
// RangeToBits converts a text representation of a CPU mask (as written to
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ package systemd
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -13,8 +15,8 @@ import (
|
|||||||
|
|
||||||
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
||||||
dbus "github.com/godbus/dbus/v5"
|
dbus "github.com/godbus/dbus/v5"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/userns"
|
"github.com/opencontainers/runc/libcontainer/userns"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// newUserSystemdDbus creates a connection for systemd user-instance.
|
// newUserSystemdDbus creates a connection for systemd user-instance.
|
||||||
@@ -31,17 +33,17 @@ func newUserSystemdDbus() (*systemdDbus.Conn, error) {
|
|||||||
return systemdDbus.NewConnection(func() (*dbus.Conn, error) {
|
return systemdDbus.NewConnection(func() (*dbus.Conn, error) {
|
||||||
conn, err := dbus.Dial(addr)
|
conn, err := dbus.Dial(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while dialing %q", addr)
|
return nil, fmt.Errorf("error while dialing %q: %w", addr, err)
|
||||||
}
|
}
|
||||||
methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(uid))}
|
methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(uid))}
|
||||||
err = conn.Auth(methods)
|
err = conn.Auth(methods)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, errors.Wrapf(err, "error while authenticating connection, address=%q, UID=%d", addr, uid)
|
return nil, fmt.Errorf("error while authenticating connection (address=%q, UID=%d): %w", addr, uid, err)
|
||||||
}
|
}
|
||||||
if err = conn.Hello(); err != nil {
|
if err = conn.Hello(); err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return nil, errors.Wrapf(err, "error while sending Hello message, address=%q, UID=%d", addr, uid)
|
return nil, fmt.Errorf("error while sending Hello message (address=%q, UID=%d): %w", addr, uid, err)
|
||||||
}
|
}
|
||||||
return conn, nil
|
return conn, nil
|
||||||
})
|
})
|
||||||
@@ -57,7 +59,7 @@ func DetectUID() (int, error) {
|
|||||||
}
|
}
|
||||||
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
|
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, errors.Wrapf(err, "could not execute `busctl --user --no-pager status`: %q", string(b))
|
return -1, fmt.Errorf("could not execute `busctl --user --no-pager status` (output: %q): %w", string(b), err)
|
||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(b))
|
scanner := bufio.NewScanner(bytes.NewReader(b))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@@ -66,7 +68,7 @@ func DetectUID() (int, error) {
|
|||||||
uidStr := strings.TrimPrefix(s, "OwnerUID=")
|
uidStr := strings.TrimPrefix(s, "OwnerUID=")
|
||||||
i, err := strconv.Atoi(uidStr)
|
i, err := strconv.Atoi(uidStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, errors.Wrapf(err, "could not detect the OwnerUID: %s", s)
|
return -1, fmt.Errorf("could not detect the OwnerUID: %w", err)
|
||||||
}
|
}
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@@ -93,7 +95,7 @@ func DetectUserDbusSessionBusAddress() (string, error) {
|
|||||||
}
|
}
|
||||||
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
|
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrapf(err, "could not execute `systemctl --user --no-pager show-environment`, output=%q", string(b))
|
return "", fmt.Errorf("could not execute `systemctl --user --no-pager show-environment` (output=%q): %w", string(b), err)
|
||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(b))
|
scanner := bufio.NewScanner(bytes.NewReader(b))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type legacyManager struct {
|
type legacyManager struct {
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ import (
|
|||||||
|
|
||||||
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
||||||
securejoin "github.com/cyphar/filepath-securejoin"
|
securejoin "github.com/cyphar/filepath-securejoin"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
|
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type unifiedManager struct {
|
type unifiedManager struct {
|
||||||
@@ -283,7 +283,7 @@ func (m *unifiedManager) Apply(pid int) error {
|
|||||||
properties = append(properties, c.SystemdProps...)
|
properties = append(properties, c.SystemdProps...)
|
||||||
|
|
||||||
if err := startUnit(m.dbus, unitName, properties); err != nil {
|
if err := startUnit(m.dbus, unitName, properties); err != nil {
|
||||||
return errors.Wrapf(err, "error while starting unit %q with properties %+v", unitName, properties)
|
return fmt.Errorf("unable to start unit %q (properties %+v): %w", unitName, properties, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.initPath(); err != nil {
|
if err := m.initPath(); err != nil {
|
||||||
@@ -440,7 +440,7 @@ func (m *unifiedManager) Set(r *configs.Resources) error {
|
|||||||
|
|
||||||
if err := setUnitProperties(m.dbus, getUnitName(m.cgroups), properties...); err != nil {
|
if err := setUnitProperties(m.dbus, getUnitName(m.cgroups), properties...); err != nil {
|
||||||
_ = m.Freeze(targetFreezerState)
|
_ = m.Freeze(targetFreezerState)
|
||||||
return errors.Wrap(err, "error while setting unit properties")
|
return fmt.Errorf("unable to set unit properties: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset freezer state before we apply the configuration, to avoid clashing
|
// Reset freezer state before we apply the configuration, to avoid clashing
|
||||||
|
|||||||
Reference in New Issue
Block a user