Skip to content

Commit

Permalink
Move back to using a scanner in FindCgroupMountpointAndRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
gcapizzi committed Sep 25, 2018
1 parent 622d8e8 commit f817b96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ func FindCgroupMountpointAndRoot(cgroupPath, subsystem string) (string, string,
return "", "", NewNotFoundError(subsystem)
}

mountinfo, err := ioutil.ReadFile("/proc/self/mountinfo")
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return "", "", err
}
defer f.Close()

return findCgroupMountpointAndRoot(cgroupPath, subsystem, string(mountinfo))
return findCgroupMountpointAndRootFromReader(f, cgroupPath, subsystem)
}

func findCgroupMountpointAndRoot(cgroupPath, subsystem, mountinfo string) (string, string, error) {
for _, mountInfoEntry := range strings.Split(mountinfo, "\n") {
fields := strings.Fields(mountInfoEntry)
func findCgroupMountpointAndRootFromReader(reader io.Reader, cgroupPath, subsystem string) (string, string, error) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
txt := scanner.Text()
fields := strings.Fields(txt)
if len(fields) < 5 {
continue
}
Expand All @@ -57,6 +60,9 @@ func findCgroupMountpointAndRoot(cgroupPath, subsystem, mountinfo string) (strin
}
}
}
if err := scanner.Err(); err != nil {
return "", "", err
}

return "", "", NewNotFoundError(subsystem)
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestFindCgroupMountpointAndRoot(t *testing.T) {
}

for _, c := range testCases {
mountpoint, _, _ := findCgroupMountpointAndRoot(c.cgroupPath, "devices", fakeMountInfo)
mountpoint, _, _ := findCgroupMountpointAndRootFromReader(strings.NewReader(fakeMountInfo), c.cgroupPath, "devices")
if mountpoint != c.output {
t.Errorf("expected %s, got %s", c.output, mountpoint)
}
Expand Down

0 comments on commit f817b96

Please # to comment.