Skip to content

Commit

Permalink
Revert to non-recursive GetPids, add recursive GetAllPids
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Jan 8, 2016
1 parent f03b7f8 commit 08d32ad
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
3 changes: 3 additions & 0 deletions libcontainer/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions libcontainer/cgroups/systemd/apply_nosystemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
8 changes: 8 additions & 0 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 9 additions & 4 deletions libcontainer/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 08d32ad

Please sign in to comment.