From 91c7024e529a7dfcc3f80362011c1f0e93d60b60 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Fri, 8 Jan 2016 19:37:18 +0000 Subject: [PATCH] Revert to non-recursive GetPids, add recursive GetAllPids Signed-off-by: Jimmi Dyson --- libcontainer/cgroups/cgroups.go | 3 +++ libcontainer/cgroups/fs/apply_raw.go | 17 ++++++++++++++--- libcontainer/cgroups/systemd/apply_nosystemd.go | 4 ++++ libcontainer/cgroups/systemd/apply_systemd.go | 8 ++++++++ libcontainer/cgroups/utils.go | 9 +++++++-- libcontainer/container_linux.go | 2 +- libcontainer/container_linux_test.go | 13 +++++++++---- libcontainer/init_linux.go | 2 +- 8 files changed, 47 insertions(+), 11 deletions(-) diff --git a/libcontainer/cgroups/cgroups.go b/libcontainer/cgroups/cgroups.go index a08e905caaa..c8f7796567b 100644 --- a/libcontainer/cgroups/cgroups.go +++ b/libcontainer/cgroups/cgroups.go @@ -15,6 +15,9 @@ type Manager interface { // Returns the PIDs inside the cgroup set GetPids() ([]int, error) + // Returns the PIDs inside the cgroup set & all sub-cgroups + GetAllPids() ([]int, error) + // Returns statistics for the cgroup set GetStats() (*Stats, error) diff --git a/libcontainer/cgroups/fs/apply_raw.go b/libcontainer/cgroups/fs/apply_raw.go index 0c4d207ee74..1bc8ef1b055 100644 --- a/libcontainer/cgroups/fs/apply_raw.go +++ b/libcontainer/cgroups/fs/apply_raw.go @@ -217,17 +217,28 @@ func (m *Manager) Freeze(state configs.FreezerState) error { } func (m *Manager) GetPids() ([]int, error) { - d, err := getCgroupData(m.Cgroups, 0) + dir, err := getCgroupPath(m.Cgroups) if err != nil { return nil, err } + return cgroups.GetPids(dir) +} - dir, err := d.path("devices") +func (m *Manager) GetAllPids() ([]int, error) { + dir, err := getCgroupPath(m.Cgroups) if err != nil { return nil, err } + return cgroups.GetAllPids(dir) +} - return cgroups.GetPids(dir) +func getCgroupPath(c *configs.Cgroup) (string, error) { + d, err := getCgroupData(c, 0) + if err != nil { + return "", err + } + + return d.path("devices") } func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) { diff --git a/libcontainer/cgroups/systemd/apply_nosystemd.go b/libcontainer/cgroups/systemd/apply_nosystemd.go index fa3485f1c0c..7de9ae6050b 100644 --- a/libcontainer/cgroups/systemd/apply_nosystemd.go +++ b/libcontainer/cgroups/systemd/apply_nosystemd.go @@ -26,6 +26,10 @@ func (m *Manager) GetPids() ([]int, error) { return nil, fmt.Errorf("Systemd not supported") } +func (m *Manager) GetAllPids() ([]int, error) { + return nil, fmt.Errorf("Systemd not supported") +} + func (m *Manager) Destroy() error { return fmt.Errorf("Systemd not supported") } diff --git a/libcontainer/cgroups/systemd/apply_systemd.go b/libcontainer/cgroups/systemd/apply_systemd.go index 93300cda385..9a3987d4441 100644 --- a/libcontainer/cgroups/systemd/apply_systemd.go +++ b/libcontainer/cgroups/systemd/apply_systemd.go @@ -440,6 +440,14 @@ func (m *Manager) GetPids() ([]int, error) { return cgroups.GetPids(path) } +func (m *Manager) GetAllPids() ([]int, error) { + path, err := getSubsystemPath(m.Cgroups, "devices") + if err != nil { + return nil, err + } + return cgroups.GetAllPids(path) +} + func (m *Manager) GetStats() (*cgroups.Stats, error) { m.mu.Lock() defer m.mu.Unlock() diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index fbdb0cbdab9..422848282a0 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -323,9 +323,14 @@ func GetHugePageSize() ([]string, error) { return pageSizes, nil } -// GetPids returns all pids, that were added to cgroup at path and to all its -// subcgroups. +// GetPids returns all pids, that were added to cgroup at path. func GetPids(path string) ([]int, error) { + return readProcsFile(path) +} + +// GetAllPids returns all pids, that were added to cgroup at path and to all its +// subcgroups. +func GetAllPids(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 { diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 62b228a1244..4886ae611c2 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -129,7 +129,7 @@ func (c *linuxContainer) State() (*State, error) { } func (c *linuxContainer) Processes() ([]int, error) { - pids, err := c.cgroupManager.GetPids() + pids, err := c.cgroupManager.GetAllPids() if err != nil { return nil, newSystemError(err) } diff --git a/libcontainer/container_linux_test.go b/libcontainer/container_linux_test.go index 2e9d1322d64..a66ea067ca7 100644 --- a/libcontainer/container_linux_test.go +++ b/libcontainer/container_linux_test.go @@ -12,15 +12,20 @@ import ( ) type mockCgroupManager struct { - pids []int - stats *cgroups.Stats - paths map[string]string + pids []int + allPids []int + stats *cgroups.Stats + paths map[string]string } func (m *mockCgroupManager) GetPids() ([]int, error) { return m.pids, nil } +func (m *mockCgroupManager) GetAllPids() ([]int, error) { + return m.allPids, nil +} + func (m *mockCgroupManager) GetStats() (*cgroups.Stats, error) { return m.stats, nil } @@ -85,7 +90,7 @@ func TestGetContainerPids(t *testing.T) { container := &linuxContainer{ id: "myid", config: &configs.Config{}, - cgroupManager: &mockCgroupManager{pids: []int{1, 2, 3}}, + cgroupManager: &mockCgroupManager{allPids: []int{1, 2, 3}}, } pids, err := container.Processes() if err != nil { diff --git a/libcontainer/init_linux.go b/libcontainer/init_linux.go index ddb11865958..5637e40c731 100644 --- a/libcontainer/init_linux.go +++ b/libcontainer/init_linux.go @@ -309,7 +309,7 @@ func killCgroupProcesses(m cgroups.Manager) error { if err := m.Freeze(configs.Frozen); err != nil { logrus.Warn(err) } - pids, err := m.GetPids() + pids, err := m.GetAllPids() if err != nil { m.Freeze(configs.Thawed) return err