From 66aee74b1edcce3824899baafcc6a5cb3c96816b Mon Sep 17 00:00:00 2001 From: Vishnu Kannan Date: Tue, 29 Jul 2014 01:41:52 +0000 Subject: [PATCH] Libcontainer fs.GetStats() will not look for cgroups relative to the cgroup of init process if the parent in cgroups.Cgroup is absolute. This is required to get stats of other containers while running inside a docker container. Docker-DCO-1.1-Signed-off-by: Vishnu Kannan (github: vishh) --- cgroups/fs/apply_raw.go | 4 ++++ cgroups/fs/utils_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index e9c06e1e2..2d1a15239 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -150,6 +150,10 @@ func (raw *data) parent(subsystem string) (string, error) { } func (raw *data) path(subsystem string) (string, error) { + // If the cgroup name/path is absolute do not look relative to the cgroup of the init process. + if filepath.IsAbs(raw.cgroup) { + return filepath.Join(raw.root, subsystem, raw.cgroup), nil + } parent, err := raw.parent(subsystem) if err != nil { return "", err diff --git a/cgroups/fs/utils_test.go b/cgroups/fs/utils_test.go index 63d743f06..6ea59bc50 100644 --- a/cgroups/fs/utils_test.go +++ b/cgroups/fs/utils_test.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/docker/libcontainer/cgroups" ) const ( @@ -66,3 +68,20 @@ func TestGetCgroupParamsInt(t *testing.T) { t.Fatal("Expecting error, got none") } } + +func TestAbsolutePathHandling(t *testing.T) { + testCgroup := cgroups.Cgroup{ + Name: "bar", + Parent: "/foo", + } + cgroupData := data{ + root: "/sys/fs/cgroup", + cgroup: "/foo/bar", + c: &testCgroup, + pid: 1, + } + expectedPath := filepath.Join(cgroupData.root, "cpu", testCgroup.Parent, testCgroup.Name) + if path, err := cgroupData.path("cpu"); path != expectedPath || err != nil { + t.Fatalf("expected path %s but got %s %s", expectedPath, path, err) + } +}