Skip to content

Commit

Permalink
Check for non-active/supported cgroups
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael committed Feb 14, 2019
1 parent 39b18af commit 4dacf2b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cgroup.go
Expand Up @@ -35,14 +35,20 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources) (Cgrou
if err != nil {
return nil, err
}
var active []Subsystem
for _, s := range subsystems {
// check if subsystem exists
if err := initializeSubsystem(s, path, resources); err != nil {
if err == ErrControllerNotActive {
continue
}
return nil, err
}
active = append(active, s)
}
return &cgroup{
path: path,
subsystems: subsystems,
subsystems: active,
}, nil
}

Expand All @@ -60,6 +66,9 @@ func Load(hierarchy Hierarchy, path Path) (Cgroup, error) {
if os.IsNotExist(errors.Cause(err)) {
return nil, ErrCgroupDeleted
}
if err == ErrControllerNotActive {
continue
}
return nil, err
}
if _, err := os.Lstat(s.Path(p)); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion paths.go
Expand Up @@ -57,6 +57,9 @@ func PidPath(pid int) Path {
return existingPath(paths, "")
}

// ErrControllerNotActive is returned when a controller is not supported or enalbed
var ErrControllerNotActive = errors.New("controller is not supported")

func existingPath(paths map[string]string, suffix string) Path {
// localize the paths based on the root mount dest for nested cgroups
for n, p := range paths {
Expand All @@ -77,7 +80,7 @@ func existingPath(paths map[string]string, suffix string) Path {
root, ok := paths[string(name)]
if !ok {
if root, ok = paths[fmt.Sprintf("name=%s", name)]; !ok {
return "", fmt.Errorf("unable to find %q in controller set", name)
return "", ErrControllerNotActive
}
}
if suffix != "" {
Expand Down
26 changes: 26 additions & 0 deletions paths_test.go
Expand Up @@ -112,3 +112,29 @@ func TestEmptySubsystem(t *testing.T) {
}
}
}

func TestSystemd240(t *testing.T) {
const data = `8:net_cls:/
7:memory:/system.slice/docker.service
6:freezer:/
5:blkio:/system.slice/docker.service
4:devices:/system.slice/docker.service
3:cpuset:/
2:cpu,cpuacct:/system.slice/docker.service
1:name=systemd:/system.slice/docker.service
0::/system.slice/docker.service`
r := strings.NewReader(data)
paths, err := parseCgroupFromReader(r)
if err != nil {
t.Fatal(err)
}

path := existingPath(paths, "")
_, err = path("net_prio")
if err == nil {
t.Fatal("error for net_prio shoulld not be nil")
}
if err != ErrControllerNotActive {
t.Fatalf("expected error %q but received %q", ErrControllerNotActive, err)
}
}

0 comments on commit 4dacf2b

Please sign in to comment.