libct: refactor addIntoCgroupV2, fix wrt rootless

1. Refactor addIntoCgroupV2 in an attempt to simplify it.

2. Fix the bug of not trying the init cgroup fallback if
   rootlessCgroup is set. This is a bug because rootlessCgroup
   tells to ignore cgroup join errors, not to never try the fallback.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2026-02-09 17:01:51 -08:00
parent 94133fab97
commit 1d030fab7d
+26 -14
View File
@@ -319,23 +319,35 @@ func (p *setnsProcess) initProcessCgroupPath() string {
func (p *setnsProcess) addIntoCgroupV2() error {
sub := p.process.SubCgroupPaths[""]
err := p.manager.AddPid(sub, p.pid())
if err != nil && !p.rootlessCgroups {
// Failed to join the configured cgroup, fall back to container init's cgroup
// unless sub-cgroup is explicitly requested.
if sub == "" {
if path := p.initProcessCgroupPath(); path != "" {
logrus.Debugf("adding pid %d to configured cgroup failed (%v), will join container init cgroup %q",
p.pid(), err, path)
// NOTE: path is not guaranteed to exist because we didn't pause the container.
err = cgroups.WriteCgroupProc(path, p.pid())
}
}
if err != nil {
return fmt.Errorf("error adding pid %d to cgroups: %w", p.pid(), err)
}
if err == nil {
return nil
}
// Failed to join the configured cgroup. Fall back to container init's cgroup
// unless sub-cgroup is explicitly requested.
var path string
if sub != "" {
goto fail
}
path = p.initProcessCgroupPath()
if path == "" {
goto fail
}
logrus.Debugf("adding pid %d to configured cgroup failed (%v), will join container init cgroup %q", p.pid(), err, path)
// NOTE: path is not guaranteed to exist because we didn't pause the container.
err = cgroups.WriteCgroupProc(path, p.pid())
if err != nil {
goto fail
}
return nil
fail:
if p.rootlessCgroups {
// Ignore cgroup join errors when rootless.
return nil
}
return fmt.Errorf("error adding pid %d to cgroups: %w", p.pid(), err)
}
func (p *setnsProcess) addIntoCgroup() error {