|
| 1 | +package cgresolver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// Mount represents a cgroup or cgroup2 mount. |
| 11 | +// Subsystems will be nil if the mount is for a unified hierarchy/cgroup v2 |
| 12 | +// in that case, CGroupV2 will be true. |
| 13 | +type Mount struct { |
| 14 | + Mountpoint string |
| 15 | + Root string |
| 16 | + Subsystems []string |
| 17 | + CGroupV2 bool // true if this is a cgroup2 mount |
| 18 | +} |
| 19 | + |
| 20 | +const ( |
| 21 | + mountinfoPath = "/proc/self/mountinfo" |
| 22 | +) |
| 23 | + |
| 24 | +// CGroupMountInfo parses /proc/self/mountinfo and returns info about all cgroup and cgroup2 mounts |
| 25 | +func CGroupMountInfo() ([]Mount, error) { |
| 26 | + mountinfoContents, mntInfoReadErr := os.ReadFile(mountinfoPath) |
| 27 | + if mntInfoReadErr != nil { |
| 28 | + return nil, fmt.Errorf("failed to read contents of %s: %w", |
| 29 | + mountinfoPath, mntInfoReadErr) |
| 30 | + } |
| 31 | + |
| 32 | + mounts, mntsErr := getCGroupMountsFromMountinfo(string(mountinfoContents)) |
| 33 | + if mntsErr != nil { |
| 34 | + return nil, fmt.Errorf("failed to list cgroupfs mounts: %w", mntsErr) |
| 35 | + } |
| 36 | + |
| 37 | + return mounts, nil |
| 38 | +} |
| 39 | + |
| 40 | +func getCGroupMountsFromMountinfo(mountinfo string) ([]Mount, error) { |
| 41 | + // mountinfo is line-delimited, then space-delimited |
| 42 | + mountinfoLines := strings.Split(mountinfo, "\n") |
| 43 | + if len(mountinfoLines) == 0 { |
| 44 | + return nil, fmt.Errorf("unexpectedly empty mountinfo (one line): %q", mountinfo) |
| 45 | + } |
| 46 | + out := make([]Mount, 0, len(mountinfoLines)) |
| 47 | + for _, line := range mountinfoLines { |
| 48 | + if len(line) == 0 { |
| 49 | + continue |
| 50 | + } |
| 51 | + sections := strings.SplitN(line, " - ", 2) |
| 52 | + if len(sections) < 2 { |
| 53 | + return nil, fmt.Errorf("missing section separator in line %q", line) |
| 54 | + } |
| 55 | + s2Fields := strings.SplitN(sections[1], " ", 3) |
| 56 | + if len(s2Fields) < 3 { |
| 57 | + return nil, fmt.Errorf("line %q contains %d fields in second section, expected 3", |
| 58 | + line, len(s2Fields)) |
| 59 | + |
| 60 | + } |
| 61 | + isCG2 := false |
| 62 | + switch s2Fields[0] { |
| 63 | + case "cgroup": |
| 64 | + isCG2 = false |
| 65 | + case "cgroup2": |
| 66 | + isCG2 = true |
| 67 | + default: |
| 68 | + // skip anything that's not a cgroup |
| 69 | + continue |
| 70 | + } |
| 71 | + s1Fields := strings.Split(sections[0], " ") |
| 72 | + if len(s1Fields) < 5 { |
| 73 | + return nil, fmt.Errorf("too few fields in line %q before optional separator: %d; expected 5", |
| 74 | + line, len(s1Fields)) |
| 75 | + } |
| 76 | + mntpnt, mntPntUnescapeErr := unOctalEscape(s1Fields[4]) |
| 77 | + if mntPntUnescapeErr != nil { |
| 78 | + return nil, fmt.Errorf("failed to unescape mountpoint %q: %w", s1Fields[4], mntPntUnescapeErr) |
| 79 | + } |
| 80 | + rootPath, rootUnescErr := unOctalEscape(s1Fields[3]) |
| 81 | + if rootUnescErr != nil { |
| 82 | + return nil, fmt.Errorf("failed to unescape mount root %q: %w", s1Fields[3], rootUnescErr) |
| 83 | + } |
| 84 | + mnt := Mount{ |
| 85 | + CGroupV2: isCG2, |
| 86 | + Mountpoint: mntpnt, |
| 87 | + Root: rootPath, |
| 88 | + Subsystems: nil, |
| 89 | + } |
| 90 | + // only bother with the mount options to find subsystems if cgroup v1 |
| 91 | + if !isCG2 { |
| 92 | + for _, mntOpt := range strings.Split(s2Fields[2], ",") { |
| 93 | + switch mntOpt { |
| 94 | + case "ro", "rw": |
| 95 | + // These mount options are lies, (or at least |
| 96 | + // only reflect the original mount, without |
| 97 | + // considering the layering of later bind-mounts) |
| 98 | + continue |
| 99 | + case "": |
| 100 | + continue |
| 101 | + default: |
| 102 | + mnt.Subsystems = append(mnt.Subsystems, mntOpt) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + out = append(out, mnt) |
| 108 | + |
| 109 | + } |
| 110 | + return out, nil |
| 111 | +} |
| 112 | + |
| 113 | +func unOctalEscape(str string) (string, error) { |
| 114 | + b := strings.Builder{} |
| 115 | + b.Grow(len(str)) |
| 116 | + for { |
| 117 | + backslashIdx := strings.IndexByte(str, byte('\\')) |
| 118 | + if backslashIdx == -1 { |
| 119 | + b.WriteString(str) |
| 120 | + return b.String(), nil |
| 121 | + } |
| 122 | + b.WriteString(str[:backslashIdx]) |
| 123 | + // if the end of the escape is beyond the end of the string, abort! |
| 124 | + if backslashIdx+3 >= len(str) { |
| 125 | + return "", fmt.Errorf("invalid offset: %d+3 >= len %d", backslashIdx, len(str)) |
| 126 | + } |
| 127 | + // slice out the octal 3-digit component |
| 128 | + esc := str[backslashIdx+1 : backslashIdx+4] |
| 129 | + asciiVal, parseUintErr := strconv.ParseUint(esc, 8, 8) |
| 130 | + if parseUintErr != nil { |
| 131 | + return "", fmt.Errorf("failed to parse escape value %q: %w", esc, parseUintErr) |
| 132 | + } |
| 133 | + b.WriteByte(byte(asciiVal)) |
| 134 | + str = str[backslashIdx+4:] |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments