diff --git a/libcontainer/cgroups/fs/apply_raw.go b/libcontainer/cgroups/fs/apply_raw.go index 815f0825..a0a93a4a 100644 --- a/libcontainer/cgroups/fs/apply_raw.go +++ b/libcontainer/cgroups/fs/apply_raw.go @@ -213,7 +213,7 @@ func (m *Manager) GetPids() ([]int, error) { return nil, err } - return cgroups.ReadProcsFile(dir) + return cgroups.GetPids(dir) } func getCgroupData(c *configs.Cgroup, pid int) (*data, error) { diff --git a/libcontainer/cgroups/systemd/apply_systemd.go b/libcontainer/cgroups/systemd/apply_systemd.go index f7f7ca2b..d878be9c 100644 --- a/libcontainer/cgroups/systemd/apply_systemd.go +++ b/libcontainer/cgroups/systemd/apply_systemd.go @@ -411,12 +411,11 @@ func (m *Manager) Freeze(state configs.FreezerState) error { } func (m *Manager) GetPids() ([]int, error) { - path, err := getSubsystemPath(m.Cgroups, "cpu") + path, err := getSubsystemPath(m.Cgroups, "devices") if err != nil { return nil, err } - - return cgroups.ReadProcsFile(path) + return cgroups.GetPids(path) } func (m *Manager) GetStats() (*cgroups.Stats, error) { diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 8ff7e3b0..32142edb 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -193,7 +193,7 @@ func GetInitCgroupDir(subsystem string) (string, error) { return getControllerPath(subsystem, cgroups) } -func ReadProcsFile(dir string) ([]int, error) { +func readProcsFile(dir string) ([]int, error) { f, err := os.Open(filepath.Join(dir, "cgroup.procs")) if err != nil { return nil, err @@ -322,3 +322,29 @@ func GetHugePageSize() ([]string, error) { return pageSizes, nil } + +// GetPids returns all pids, that were added to cgroup at path and to all its +// subcgroups. +func GetPids(path string) ([]int, error) { + var pids []int + // collect pids from all sub-cgroups + err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error { + if info.IsDir() { + return nil + } + + dir, file := filepath.Split(p) + if file == "cgroup.procs" { + if iErr != nil { + return iErr + } + cPids, err := readProcsFile(dir) + if err != nil { + return err + } + pids = append(pids, cPids...) + } + return nil + }) + return pids, err +}