Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 6c5441e

Browse files
committed
libct/cg/fs: move paths init to NewManager
1. Separate path initialization logic from Apply to initPaths, and call initPaths from NewManager, so: - we can error out early (in NewManager rather than Apply); - always have m.paths available (e.g. in Destroy or Exists). - do not unnecessarily call subsysPath from Apply in case the paths were already provided. 2. Add a check for non-nil cgroups.Resources to NewManager, since initPaths, as well as some controller's Apply methods, need it. 3. Move cgroups.Resources.Unified check from Apply to NewManager, so we can error out early (same check exists in Set). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 097c6d7 commit 6c5441e

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

libcontainer/cgroups/fs/fs.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ type manager struct {
5555
}
5656

5757
func NewManager(cg *configs.Cgroup, paths map[string]string) (cgroups.Manager, error) {
58+
// Some v1 controllers (cpu, cpuset, and devices) expect
59+
// cgroups.Resources to not be nil in Apply.
60+
if cg.Resources == nil {
61+
return nil, errors.New("cgroup v1 manager needs configs.Resources to be set during manager creation")
62+
}
63+
if cg.Resources.Unified != nil {
64+
return nil, cgroups.ErrV1NoUnified
65+
}
66+
67+
if paths == nil {
68+
var err error
69+
paths, err = initPaths(cg)
70+
if err != nil {
71+
return nil, err
72+
}
73+
}
74+
5875
return &manager{
5976
cgroups: cg,
6077
paths: paths,
@@ -90,37 +107,21 @@ func (m *manager) Apply(pid int) (err error) {
90107
defer m.mu.Unlock()
91108

92109
c := m.cgroups
93-
if c.Resources.Unified != nil {
94-
return cgroups.ErrV1NoUnified
95-
}
96110

97-
root, err := rootPath()
98-
if err != nil {
99-
return err
100-
}
101-
102-
inner, err := innerPath(c)
103-
104-
m.paths = make(map[string]string)
105111
for _, sys := range subsystems {
106-
p, err := subsysPath(root, inner, sys.Name())
107-
if err != nil {
108-
// The non-presence of the devices subsystem is
109-
// considered fatal for security reasons.
110-
if cgroups.IsNotFound(err) && (c.SkipDevices || sys.Name() != "devices") {
111-
continue
112-
}
113-
return err
112+
name := sys.Name()
113+
p, ok := m.paths[name]
114+
if !ok {
115+
continue
114116
}
115-
m.paths[sys.Name()] = p
116117

117118
if err := sys.Apply(p, c.Resources, pid); err != nil {
118119
// In the case of rootless (including euid=0 in userns), where an
119120
// explicit cgroup path hasn't been set, we don't bail on error in
120121
// case of permission problems. Cases where limits have been set
121122
// (and we couldn't create our own cgroup) are handled by Set.
122-
if isIgnorableError(c.Rootless, err) && m.cgroups.Path == "" {
123-
delete(m.paths, sys.Name())
123+
if isIgnorableError(c.Rootless, err) && c.Path == "" {
124+
delete(m.paths, name)
124125
continue
125126
}
126127
return err

libcontainer/cgroups/fs/paths.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ var (
2121

2222
const defaultCgroupRoot = "/sys/fs/cgroup"
2323

24+
func initPaths(cg *configs.Cgroup) (map[string]string, error) {
25+
root, err := rootPath()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
inner, err := innerPath(cg)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
paths := make(map[string]string)
36+
for _, sys := range subsystems {
37+
name := sys.Name()
38+
path, err := subsysPath(root, inner, name)
39+
if err != nil {
40+
// The non-presence of the devices subsystem
41+
// is considered fatal for security reasons.
42+
if cgroups.IsNotFound(err) && (cg.SkipDevices || name != "devices") {
43+
continue
44+
}
45+
46+
return nil, err
47+
}
48+
paths[name] = path
49+
}
50+
51+
return paths, nil
52+
}
53+
2454
func tryDefaultCgroupRoot() string {
2555
var st, pst unix.Stat_t
2656

libcontainer/factory_linux_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ func TestFactoryLoadContainer(t *testing.T) {
138138
},
139139
}
140140
expectedConfig = &configs.Config{
141-
Rootfs: "/mycontainer/root",
142-
Hooks: expectedHooks,
143-
Cgroups: &configs.Cgroup{},
141+
Rootfs: "/mycontainer/root",
142+
Hooks: expectedHooks,
143+
Cgroups: &configs.Cgroup{
144+
Resources: &configs.Resources{},
145+
},
144146
}
145147
expectedState = &State{
146148
BaseState: BaseState{

0 commit comments

Comments
 (0)