Skip to content

Commit 910752f

Browse files
committed
Merge pull request #463 from jimmidyson/non-recursive-pids
Revert to non-recursive GetPids, add recursive GetAllPids
2 parents 749928a + 91c7024 commit 910752f

File tree

8 files changed

+47
-11
lines changed

8 files changed

+47
-11
lines changed

libcontainer/cgroups/cgroups.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type Manager interface {
1515
// Returns the PIDs inside the cgroup set
1616
GetPids() ([]int, error)
1717

18+
// Returns the PIDs inside the cgroup set & all sub-cgroups
19+
GetAllPids() ([]int, error)
20+
1821
// Returns statistics for the cgroup set
1922
GetStats() (*Stats, error)
2023

libcontainer/cgroups/fs/apply_raw.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,28 @@ func (m *Manager) Freeze(state configs.FreezerState) error {
217217
}
218218

219219
func (m *Manager) GetPids() ([]int, error) {
220-
d, err := getCgroupData(m.Cgroups, 0)
220+
dir, err := getCgroupPath(m.Cgroups)
221221
if err != nil {
222222
return nil, err
223223
}
224+
return cgroups.GetPids(dir)
225+
}
224226

225-
dir, err := d.path("devices")
227+
func (m *Manager) GetAllPids() ([]int, error) {
228+
dir, err := getCgroupPath(m.Cgroups)
226229
if err != nil {
227230
return nil, err
228231
}
232+
return cgroups.GetAllPids(dir)
233+
}
229234

230-
return cgroups.GetPids(dir)
235+
func getCgroupPath(c *configs.Cgroup) (string, error) {
236+
d, err := getCgroupData(c, 0)
237+
if err != nil {
238+
return "", err
239+
}
240+
241+
return d.path("devices")
231242
}
232243

233244
func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {

libcontainer/cgroups/systemd/apply_nosystemd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func (m *Manager) GetPids() ([]int, error) {
2626
return nil, fmt.Errorf("Systemd not supported")
2727
}
2828

29+
func (m *Manager) GetAllPids() ([]int, error) {
30+
return nil, fmt.Errorf("Systemd not supported")
31+
}
32+
2933
func (m *Manager) Destroy() error {
3034
return fmt.Errorf("Systemd not supported")
3135
}

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ func (m *Manager) GetPids() ([]int, error) {
440440
return cgroups.GetPids(path)
441441
}
442442

443+
func (m *Manager) GetAllPids() ([]int, error) {
444+
path, err := getSubsystemPath(m.Cgroups, "devices")
445+
if err != nil {
446+
return nil, err
447+
}
448+
return cgroups.GetAllPids(path)
449+
}
450+
443451
func (m *Manager) GetStats() (*cgroups.Stats, error) {
444452
m.mu.Lock()
445453
defer m.mu.Unlock()

libcontainer/cgroups/utils.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,14 @@ func GetHugePageSize() ([]string, error) {
323323
return pageSizes, nil
324324
}
325325

326-
// GetPids returns all pids, that were added to cgroup at path and to all its
327-
// subcgroups.
326+
// GetPids returns all pids, that were added to cgroup at path.
328327
func GetPids(path string) ([]int, error) {
328+
return readProcsFile(path)
329+
}
330+
331+
// GetAllPids returns all pids, that were added to cgroup at path and to all its
332+
// subcgroups.
333+
func GetAllPids(path string) ([]int, error) {
329334
var pids []int
330335
// collect pids from all sub-cgroups
331336
err := filepath.Walk(path, func(p string, info os.FileInfo, iErr error) error {

libcontainer/container_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *linuxContainer) State() (*State, error) {
129129
}
130130

131131
func (c *linuxContainer) Processes() ([]int, error) {
132-
pids, err := c.cgroupManager.GetPids()
132+
pids, err := c.cgroupManager.GetAllPids()
133133
if err != nil {
134134
return nil, newSystemError(err)
135135
}

libcontainer/container_linux_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ import (
1212
)
1313

1414
type mockCgroupManager struct {
15-
pids []int
16-
stats *cgroups.Stats
17-
paths map[string]string
15+
pids []int
16+
allPids []int
17+
stats *cgroups.Stats
18+
paths map[string]string
1819
}
1920

2021
func (m *mockCgroupManager) GetPids() ([]int, error) {
2122
return m.pids, nil
2223
}
2324

25+
func (m *mockCgroupManager) GetAllPids() ([]int, error) {
26+
return m.allPids, nil
27+
}
28+
2429
func (m *mockCgroupManager) GetStats() (*cgroups.Stats, error) {
2530
return m.stats, nil
2631
}
@@ -85,7 +90,7 @@ func TestGetContainerPids(t *testing.T) {
8590
container := &linuxContainer{
8691
id: "myid",
8792
config: &configs.Config{},
88-
cgroupManager: &mockCgroupManager{pids: []int{1, 2, 3}},
93+
cgroupManager: &mockCgroupManager{allPids: []int{1, 2, 3}},
8994
}
9095
pids, err := container.Processes()
9196
if err != nil {

libcontainer/init_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func killCgroupProcesses(m cgroups.Manager) error {
309309
if err := m.Freeze(configs.Frozen); err != nil {
310310
logrus.Warn(err)
311311
}
312-
pids, err := m.GetPids()
312+
pids, err := m.GetAllPids()
313313
if err != nil {
314314
m.Freeze(configs.Thawed)
315315
return err

0 commit comments

Comments
 (0)