From c0e14b8b5a879c3b95bce0645c0f5e508f774ca4 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 23 Sep 2020 14:25:04 -0700 Subject: [PATCH] libct/cg/fs.getCgroupRoot: reuse (cached) cgroup mountinfo Drop the custom mountinfo parser, reuse the cgroups.GetCgroupMounts() to get the cgroup root. Faster, and much less code. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs/fs.go | 43 ++++++----------------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/libcontainer/cgroups/fs/fs.go b/libcontainer/cgroups/fs/fs.go index d4c0e7c20..a42ce4535 100644 --- a/libcontainer/cgroups/fs/fs.go +++ b/libcontainer/cgroups/fs/fs.go @@ -3,11 +3,9 @@ package fs import ( - "bufio" "fmt" "os" "path/filepath" - "strings" "sync" "github.com/opencontainers/runc/libcontainer/cgroups" @@ -133,46 +131,19 @@ func getCgroupRoot() (string, error) { return cgroupRoot, nil } - // slow path: parse mountinfo, find the first mount where fs=cgroup - // (e.g. "/sys/fs/cgroup/memory"), use its parent. - f, err := os.Open("/proc/self/mountinfo") + // slow path: parse mountinfo + mi, err := cgroups.GetCgroupMounts(false) if err != nil { return "", err } - defer f.Close() - - var root string - scanner := bufio.NewScanner(f) - for scanner.Scan() { - text := scanner.Text() - fields := strings.Split(text, " ") - // Safe as mountinfo encodes mountpoints with spaces as \040. - index := strings.Index(text, " - ") - postSeparatorFields := strings.Fields(text[index+3:]) - numPostFields := len(postSeparatorFields) - - // This is an error as we can't detect if the mount is for "cgroup" - if numPostFields == 0 { - return "", fmt.Errorf("mountinfo: found no fields post '-' in %q", text) - } - - if postSeparatorFields[0] == "cgroup" { - // Check that the mount is properly formatted. - if numPostFields < 3 { - return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text) - } - - root = filepath.Dir(fields[4]) - break - } - } - if err := scanner.Err(); err != nil { - return "", err - } - if root == "" { + if len(mi) < 1 { return "", errors.New("no cgroup mount found in mountinfo") } + // Get the first cgroup mount (e.g. "/sys/fs/cgroup/memory"), + // use its parent directory. + root := filepath.Dir(mi[0].Mountpoint) + if _, err := os.Stat(root); err != nil { return "", err }