diff --git a/cgroups/cgroups.go b/cgroups/cgroups.go index 26fef454..59845486 100644 --- a/cgroups/cgroups.go +++ b/cgroups/cgroups.go @@ -37,5 +37,5 @@ type Cgroup struct { type ActiveCgroup interface { Cleanup() error - Paths() ([]string, error) + Paths() (map[string]string, error) } diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 9a73dcde..e20cdbb9 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -153,14 +153,14 @@ func (raw *data) parent(subsystem string) (string, error) { return filepath.Join(raw.root, subsystem, initPath), nil } -func (raw *data) Paths() ([]string, error) { - var paths []string +func (raw *data) Paths() (map[string]string, error) { + paths := make(map[string]string) for sysname := range subsystems { path, err := raw.path(sysname) if err != nil { return nil, err } - paths = append(paths, path) + paths[sysname] = path } return paths, nil } diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index db06e1b6..8a0e69a2 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -22,6 +22,7 @@ import ( type systemdCgroup struct { cleanupDirs []string + cgroup *cgroups.Cgroup } type subsystem interface { @@ -100,6 +101,7 @@ func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) { res systemdCgroup ) + res.cgroup = c // First set up things not supported by systemd // -1 disables memorySwap @@ -320,8 +322,22 @@ func writeFile(dir, file, data string) error { return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700) } -func (c *systemdCgroup) Paths() ([]string, error) { - return c.cleanupDirs, nil +func (c *systemdCgroup) Paths() (map[string]string, error) { + paths := make(map[string]string) + for sysname := range subsystems { + subsystemPath, err := getSubsystemPath(c.cgroup, sysname) + if err != nil { + // Don't fail if a cgroup hierarchy was not found, just skip this subsystem + if err == cgroups.ErrNotFound { + continue + } + + return nil, err + } + paths[sysname] = subsystemPath + } + + return paths, nil } func (c *systemdCgroup) Cleanup() error { diff --git a/cgroups/utils.go b/cgroups/utils.go index 4220267b..6688ff71 100644 --- a/cgroups/utils.go +++ b/cgroups/utils.go @@ -175,7 +175,7 @@ func pathExists(path string) bool { return true } -func EnterPid(cgroupPaths []string, pid int) error { +func EnterPid(cgroupPaths map[string]string, pid int) error { for _, path := range cgroupPaths { if pathExists(path) { if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), diff --git a/state.go b/state.go index e7d03bc0..208b4c62 100644 --- a/state.go +++ b/state.go @@ -19,8 +19,8 @@ type State struct { // Network runtime state. NetworkState network.NetworkState `json:"network_state,omitempty"` - // Path to all the cgroup dirs. - CgroupPaths []string `json:"cgroup_paths,omitempty"` + // Path to all the cgroups setup for a container. Key is cgroup subsystem name. + CgroupPaths map[string]string `json:"cgroup_paths,omitempty"` } // The running state of the container.