Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4dacf2b

Browse files
committed
Check for non-active/supported cgroups
Signed-off-by: Michael Crosby <[email protected]>
1 parent 39b18af commit 4dacf2b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

cgroup.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources) (Cgrou
3535
if err != nil {
3636
return nil, err
3737
}
38+
var active []Subsystem
3839
for _, s := range subsystems {
40+
// check if subsystem exists
3941
if err := initializeSubsystem(s, path, resources); err != nil {
42+
if err == ErrControllerNotActive {
43+
continue
44+
}
4045
return nil, err
4146
}
47+
active = append(active, s)
4248
}
4349
return &cgroup{
4450
path: path,
45-
subsystems: subsystems,
51+
subsystems: active,
4652
}, nil
4753
}
4854

@@ -60,6 +66,9 @@ func Load(hierarchy Hierarchy, path Path) (Cgroup, error) {
6066
if os.IsNotExist(errors.Cause(err)) {
6167
return nil, ErrCgroupDeleted
6268
}
69+
if err == ErrControllerNotActive {
70+
continue
71+
}
6372
return nil, err
6473
}
6574
if _, err := os.Lstat(s.Path(p)); err != nil {

paths.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func PidPath(pid int) Path {
5757
return existingPath(paths, "")
5858
}
5959

60+
// ErrControllerNotActive is returned when a controller is not supported or enalbed
61+
var ErrControllerNotActive = errors.New("controller is not supported")
62+
6063
func existingPath(paths map[string]string, suffix string) Path {
6164
// localize the paths based on the root mount dest for nested cgroups
6265
for n, p := range paths {
@@ -77,7 +80,7 @@ func existingPath(paths map[string]string, suffix string) Path {
7780
root, ok := paths[string(name)]
7881
if !ok {
7982
if root, ok = paths[fmt.Sprintf("name=%s", name)]; !ok {
80-
return "", fmt.Errorf("unable to find %q in controller set", name)
83+
return "", ErrControllerNotActive
8184
}
8285
}
8386
if suffix != "" {

paths_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,29 @@ func TestEmptySubsystem(t *testing.T) {
112112
}
113113
}
114114
}
115+
116+
func TestSystemd240(t *testing.T) {
117+
const data = `8:net_cls:/
118+
7:memory:/system.slice/docker.service
119+
6:freezer:/
120+
5:blkio:/system.slice/docker.service
121+
4:devices:/system.slice/docker.service
122+
3:cpuset:/
123+
2:cpu,cpuacct:/system.slice/docker.service
124+
1:name=systemd:/system.slice/docker.service
125+
0::/system.slice/docker.service`
126+
r := strings.NewReader(data)
127+
paths, err := parseCgroupFromReader(r)
128+
if err != nil {
129+
t.Fatal(err)
130+
}
131+
132+
path := existingPath(paths, "")
133+
_, err = path("net_prio")
134+
if err == nil {
135+
t.Fatal("error for net_prio shoulld not be nil")
136+
}
137+
if err != ErrControllerNotActive {
138+
t.Fatalf("expected error %q but received %q", ErrControllerNotActive, err)
139+
}
140+
}

0 commit comments

Comments
 (0)