2014-01-20 19:15:44 +00:00
|
|
|
package cgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/mount"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
|
|
|
|
func FindCgroupMountpoint(subsystem string) (string, error) {
|
|
|
|
mounts, err := mount.GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if mount.Fstype == "cgroup" {
|
|
|
|
for _, opt := range strings.Split(mount.VfsOpts, ",") {
|
|
|
|
if opt == subsystem {
|
|
|
|
return mount.Mountpoint, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("cgroup mountpoint not found for %s", subsystem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the relative path to the cgroup docker is running in.
|
2014-01-28 15:17:51 +00:00
|
|
|
func GetThisCgroupDir(subsystem string) (string, error) {
|
2014-01-20 19:15:44 +00:00
|
|
|
f, err := os.Open("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return parseCgroupFile(subsystem, f)
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:12:08 +00:00
|
|
|
func GetInitCgroupDir(subsystem string) (string, error) {
|
|
|
|
f, err := os.Open("/proc/1/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return parseCgroupFile(subsystem, f)
|
|
|
|
}
|
|
|
|
|
2014-01-20 19:15:44 +00:00
|
|
|
func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
text := s.Text()
|
|
|
|
parts := strings.Split(text, ":")
|
2014-02-20 22:12:08 +00:00
|
|
|
for _, subs := range strings.Split(parts[1], ",") {
|
|
|
|
if subs == subsystem {
|
|
|
|
return parts[2], nil
|
|
|
|
}
|
2014-01-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("cgroup '%s' not found in /proc/self/cgroup", subsystem)
|
|
|
|
}
|