Merge pull request #332 from LK4D4/fix_panic_in_getpids

Reorder checks in Walk to avoid panics
This commit is contained in:
Mrunal Patel 2015-10-13 15:33:31 -07:00
commit 872c4ac223
2 changed files with 35 additions and 12 deletions

View File

@ -329,21 +329,18 @@ 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() {
dir, file := filepath.Split(p)
if file != "cgroup.procs" {
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...)
if iErr != nil {
return iErr
}
cPids, err := readProcsFile(dir)
if err != nil {
return err
}
pids = append(pids, cPids...)
return nil
})
return pids, err

View File

@ -1250,3 +1250,29 @@ func TestRootfsPropagationSharedMount(t *testing.T) {
t.Fatalf("Mount in container on %s did not propagate to host on %s. finmnt output=%s", dir2cont, dir2host, outtrim)
}
}
func TestPIDHost(t *testing.T) {
if testing.Short() {
return
}
rootfs, err := newRootfs()
ok(t, err)
defer remove(rootfs)
l, err := os.Readlink("/proc/1/ns/pid")
ok(t, err)
config := newTemplateConfig(rootfs)
config.Namespaces.Remove(configs.NEWPID)
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/pid")
ok(t, err)
if exitCode != 0 {
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
}
if actual := strings.Trim(buffers.Stdout.String(), "\n"); actual != l {
t.Fatalf("ipc link not equal to host link %q %q", actual, l)
}
}