Skip to content

Commit 037668e

Browse files
committed
libct/cg/fs2: simplify parseCgroupFromReader
For cgroup v2, we always expect /proc/$PID/cgroup contents like this: > 0::/user.slice/user-1000.slice/[email protected]/app.slice/vte-spawn-f71c3fb8-519d-4e2d-b13e-9252594b1e05.scope So, it does not make sense to parse it using strings.Split, we can just cut the prefix and return the rest. Code tested by TestParseCgroupFromReader. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 075cea3 commit 037668e

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

libcontainer/cgroups/fs2/defaultpath.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,9 @@ func parseCgroupFile(path string) (string, error) {
8383
func parseCgroupFromReader(r io.Reader) (string, error) {
8484
s := bufio.NewScanner(r)
8585
for s.Scan() {
86-
var (
87-
text = s.Text()
88-
parts = strings.SplitN(text, ":", 3)
89-
)
90-
if len(parts) < 3 {
91-
return "", fmt.Errorf("invalid cgroup entry: %q", text)
92-
}
93-
// text is like "0::/user.slice/user-1001.slice/session-1.scope"
94-
if parts[0] == "0" && parts[1] == "" {
95-
return parts[2], nil
86+
// "0::/user.slice/user-1001.slice/session-1.scope"
87+
if path, ok := strings.CutPrefix(s.Text(), "0::"); ok {
88+
return path, nil
9689
}
9790
}
9891
if err := s.Err(); err != nil {

0 commit comments

Comments
 (0)