From e0de51f53c6b2711f39f4f29eb58b63a9ebf2c5c Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Fri, 6 Feb 2015 09:54:52 -0800 Subject: [PATCH] Retry getting the cgroup root at apply time. This will allow late-binding of the cgroup hierarchy. Fixes docker/docker#8791 Signed-off-by: Victor Marmol --- cgroups/fs/apply_raw.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 58046b0a..bbc05552 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -1,11 +1,11 @@ package fs import ( - "fmt" "io/ioutil" "os" "path/filepath" "strconv" + "sync" "github.com/docker/libcontainer/cgroups" ) @@ -25,20 +25,31 @@ var ( ) // The absolute path to the root of the cgroup hierarchies. +var cgroupRootLock sync.Mutex var cgroupRoot string -// TODO(vmarmol): Report error here, we'll probably need to wait for the new API. -func init() { +// Gets the cgroupRoot. +func getCgroupRoot() (string, error) { + cgroupRootLock.Lock() + defer cgroupRootLock.Unlock() + + if cgroupRoot != "" { + return cgroupRoot, nil + } + // we can pick any subsystem to find the root cpuRoot, err := cgroups.FindCgroupMountpoint("cpu") if err != nil { - return + return "", err } - cgroupRoot = filepath.Dir(cpuRoot) + root := filepath.Dir(cpuRoot) - if _, err := os.Stat(cgroupRoot); err != nil { - return + if _, err := os.Stat(root); err != nil { + return "", err } + + cgroupRoot = root + return cgroupRoot, nil } type subsystem interface { @@ -152,8 +163,9 @@ func GetPids(c *cgroups.Cgroup) ([]int, error) { } func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) { - if cgroupRoot == "" { - return nil, fmt.Errorf("failed to find the cgroup root") + root, err := getCgroupRoot() + if err != nil { + return nil, err } cgroup := c.Name @@ -162,7 +174,7 @@ func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) { } return &data{ - root: cgroupRoot, + root: root, cgroup: cgroup, c: c, pid: pid,