From 6dad176d0103d8e1b8a51dc3d8c8fe1a7c5582d7 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Mon, 12 Oct 2015 14:28:31 -0700 Subject: [PATCH] Get PIDs from cgroups recursively Also lookup cgroup for systemd is changed to "device" to be consistent with fs implementation. Signed-off-by: Alexander Morozov --- libcontainer/cgroups/fs/apply_raw.go | 2 +- libcontainer/cgroups/systemd/apply_systemd.go | 5 ++-- libcontainer/cgroups/utils.go | 28 ++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) 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 +}