From 6c198ae2d065c37f44316e0de3df7f3b88950923 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 13 Oct 2015 13:57:30 -0700 Subject: [PATCH] Reorder checks in Walk to avoid panics Also added test for host PID namespace Signed-off-by: Alexander Morozov --- libcontainer/cgroups/utils.go | 21 +++++++++------------ libcontainer/integration/exec_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 32142edb..d00e0e2e 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -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 diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 267c0731..00306f17 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -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) + } +}