Skip to content

Commit 188f73d

Browse files
authored
Merge pull request #232 from AkihiroSuda/fix-unified
ParseCgroupFile: fix wrong comment about unified hierarchy ; add ParseCgroupFileUnified to get the unified path
2 parents a8e04ba + dd81920 commit 188f73d

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

paths_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestEmptySubsystem(t *testing.T) {
102102
1:name=systemd:/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service
103103
0::/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service`
104104
r := strings.NewReader(data)
105-
paths, err := parseCgroupFromReader(r)
105+
paths, unified, err := parseCgroupFromReaderUnified(r)
106106
if err != nil {
107107
t.Fatal(err)
108108
}
@@ -111,6 +111,10 @@ func TestEmptySubsystem(t *testing.T) {
111111
t.Fatalf("empty subsystem for %q", path)
112112
}
113113
}
114+
unifiedExpected := "/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service"
115+
if unified != unifiedExpected {
116+
t.Fatalf("expected %q, got %q", unifiedExpected, unified)
117+
}
114118
}
115119

116120
func TestSystemd240(t *testing.T) {
@@ -127,7 +131,7 @@ func TestSystemd240(t *testing.T) {
127131
1:name=systemd:/system.slice/docker.service
128132
0::/system.slice/docker.service`
129133
r := strings.NewReader(data)
130-
paths, err := parseCgroupFromReader(r)
134+
paths, unified, err := parseCgroupFromReaderUnified(r)
131135
if err != nil {
132136
t.Fatal(err)
133137
}
@@ -140,4 +144,8 @@ func TestSystemd240(t *testing.T) {
140144
if err != ErrControllerNotActive {
141145
t.Fatalf("expected error %q but received %q", ErrControllerNotActive, err)
142146
}
147+
unifiedExpected := "/system.slice/docker.service"
148+
if unified != unifiedExpected {
149+
t.Fatalf("expected %q, got %q", unifiedExpected, unified)
150+
}
143151
}

utils.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,28 @@ func parseKV(raw string) (string, uint64, error) {
261261
// "pids": "/user.slice/user-1000.slice"
262262
// etc.
263263
//
264-
// Note that for cgroup v2 unified hierarchy, there are no per-controller
265-
// cgroup paths, so the resulting map will have a single element where the key
266-
// is empty string ("") and the value is the cgroup path the <pid> is in.
264+
// The resulting map does not have an element for cgroup v2 unified hierarchy.
265+
// Use ParseCgroupFileUnified to get the unified path.
267266
func ParseCgroupFile(path string) (map[string]string, error) {
267+
x, _, err := ParseCgroupFileUnified(path)
268+
return x, err
269+
}
270+
271+
// ParseCgroupFileUnified returns legacy subsystem paths as the first value,
272+
// and returns the unified path as the second value.
273+
func ParseCgroupFileUnified(path string) (map[string]string, string, error) {
268274
f, err := os.Open(path)
269275
if err != nil {
270-
return nil, err
276+
return nil, "", err
271277
}
272278
defer f.Close()
273-
return parseCgroupFromReader(f)
279+
return parseCgroupFromReaderUnified(f)
274280
}
275281

276-
func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
282+
func parseCgroupFromReaderUnified(r io.Reader) (map[string]string, string, error) {
277283
var (
278284
cgroups = make(map[string]string)
285+
unified = ""
279286
s = bufio.NewScanner(r)
280287
)
281288
for s.Scan() {
@@ -284,18 +291,20 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
284291
parts = strings.SplitN(text, ":", 3)
285292
)
286293
if len(parts) < 3 {
287-
return nil, fmt.Errorf("invalid cgroup entry: %q", text)
294+
return nil, unified, fmt.Errorf("invalid cgroup entry: %q", text)
288295
}
289296
for _, subs := range strings.Split(parts[1], ",") {
290-
if subs != "" {
297+
if subs == "" {
298+
unified = parts[2]
299+
} else {
291300
cgroups[subs] = parts[2]
292301
}
293302
}
294303
}
295304
if err := s.Err(); err != nil {
296-
return nil, err
305+
return nil, unified, err
297306
}
298-
return cgroups, nil
307+
return cgroups, unified, nil
299308
}
300309

301310
func getCgroupDestination(subsystem string) (string, error) {

0 commit comments

Comments
 (0)