Skip to content

Commit 6a1d0a5

Browse files
committed
libct: factor out addIntoCgroup from setnsProcess.start
Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent a85b5fc commit 6a1d0a5

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

libcontainer/process_linux.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,34 @@ func (p *setnsProcess) setFinalCPUAffinity() error {
198198
return nil
199199
}
200200

201+
func (p *setnsProcess) addIntoCgroup() error {
202+
for _, path := range p.cgroupPaths {
203+
if err := cgroups.WriteCgroupProc(path, p.pid()); err != nil && !p.rootlessCgroups {
204+
// On cgroup v2 + nesting + domain controllers, WriteCgroupProc may fail with EBUSY.
205+
// https://github.com/opencontainers/runc/issues/2356#issuecomment-621277643
206+
// Try to join the cgroup of InitProcessPid.
207+
if cgroups.IsCgroup2UnifiedMode() && p.initProcessPid != 0 {
208+
initProcCgroupFile := fmt.Sprintf("/proc/%d/cgroup", p.initProcessPid)
209+
initCg, initCgErr := cgroups.ParseCgroupFile(initProcCgroupFile)
210+
if initCgErr == nil {
211+
if initCgPath, ok := initCg[""]; ok {
212+
initCgDirpath := filepath.Join(fs2.UnifiedMountpoint, initCgPath)
213+
logrus.Debugf("adding pid %d to cgroups %v failed (%v), attempting to join %q (obtained from %s)",
214+
p.pid(), p.cgroupPaths, err, initCg, initCgDirpath)
215+
// NOTE: initCgDirPath is not guaranteed to exist because we didn't pause the container.
216+
err = cgroups.WriteCgroupProc(initCgDirpath, p.pid())
217+
}
218+
}
219+
}
220+
if err != nil {
221+
return fmt.Errorf("error adding pid %d to cgroups: %w", p.pid(), err)
222+
}
223+
}
224+
}
225+
226+
return nil
227+
}
228+
201229
func (p *setnsProcess) start() (retErr error) {
202230
defer p.comm.closeParent()
203231

@@ -231,28 +259,8 @@ func (p *setnsProcess) start() (retErr error) {
231259
if err := p.execSetns(); err != nil {
232260
return fmt.Errorf("error executing setns process: %w", err)
233261
}
234-
for _, path := range p.cgroupPaths {
235-
if err := cgroups.WriteCgroupProc(path, p.pid()); err != nil && !p.rootlessCgroups {
236-
// On cgroup v2 + nesting + domain controllers, WriteCgroupProc may fail with EBUSY.
237-
// https://github.com/opencontainers/runc/issues/2356#issuecomment-621277643
238-
// Try to join the cgroup of InitProcessPid.
239-
if cgroups.IsCgroup2UnifiedMode() && p.initProcessPid != 0 {
240-
initProcCgroupFile := fmt.Sprintf("/proc/%d/cgroup", p.initProcessPid)
241-
initCg, initCgErr := cgroups.ParseCgroupFile(initProcCgroupFile)
242-
if initCgErr == nil {
243-
if initCgPath, ok := initCg[""]; ok {
244-
initCgDirpath := filepath.Join(fs2.UnifiedMountpoint, initCgPath)
245-
logrus.Debugf("adding pid %d to cgroups %v failed (%v), attempting to join %q (obtained from %s)",
246-
p.pid(), p.cgroupPaths, err, initCg, initCgDirpath)
247-
// NOTE: initCgDirPath is not guaranteed to exist because we didn't pause the container.
248-
err = cgroups.WriteCgroupProc(initCgDirpath, p.pid())
249-
}
250-
}
251-
}
252-
if err != nil {
253-
return fmt.Errorf("error adding pid %d to cgroups: %w", p.pid(), err)
254-
}
255-
}
262+
if err := p.addIntoCgroup(); err != nil {
263+
return err
256264
}
257265
// Set final CPU affinity right after the process is moved into container's cgroup.
258266
if err := p.setFinalCPUAffinity(); err != nil {

0 commit comments

Comments
 (0)