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 <vmarmol@google.com>
This commit is contained in:
Victor Marmol 2015-02-06 09:54:52 -08:00
parent 4bd39999a0
commit e0de51f53c
1 changed files with 22 additions and 10 deletions

View File

@ -1,11 +1,11 @@
package fs package fs
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync"
"github.com/docker/libcontainer/cgroups" "github.com/docker/libcontainer/cgroups"
) )
@ -25,20 +25,31 @@ var (
) )
// The absolute path to the root of the cgroup hierarchies. // The absolute path to the root of the cgroup hierarchies.
var cgroupRootLock sync.Mutex
var cgroupRoot string var cgroupRoot string
// TODO(vmarmol): Report error here, we'll probably need to wait for the new API. // Gets the cgroupRoot.
func init() { func getCgroupRoot() (string, error) {
cgroupRootLock.Lock()
defer cgroupRootLock.Unlock()
if cgroupRoot != "" {
return cgroupRoot, nil
}
// we can pick any subsystem to find the root // we can pick any subsystem to find the root
cpuRoot, err := cgroups.FindCgroupMountpoint("cpu") cpuRoot, err := cgroups.FindCgroupMountpoint("cpu")
if err != nil { if err != nil {
return return "", err
} }
cgroupRoot = filepath.Dir(cpuRoot) root := filepath.Dir(cpuRoot)
if _, err := os.Stat(cgroupRoot); err != nil { if _, err := os.Stat(root); err != nil {
return return "", err
} }
cgroupRoot = root
return cgroupRoot, nil
} }
type subsystem interface { type subsystem interface {
@ -152,8 +163,9 @@ func GetPids(c *cgroups.Cgroup) ([]int, error) {
} }
func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) { func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) {
if cgroupRoot == "" { root, err := getCgroupRoot()
return nil, fmt.Errorf("failed to find the cgroup root") if err != nil {
return nil, err
} }
cgroup := c.Name cgroup := c.Name
@ -162,7 +174,7 @@ func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) {
} }
return &data{ return &data{
root: cgroupRoot, root: root,
cgroup: cgroup, cgroup: cgroup,
c: c, c: c,
pid: pid, pid: pid,