@@ -261,21 +261,28 @@ func parseKV(raw string) (string, uint64, error) {
261
261
// "pids": "/user.slice/user-1000.slice"
262
262
// etc.
263
263
//
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.
267
266
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 ) {
268
274
f , err := os .Open (path )
269
275
if err != nil {
270
- return nil , err
276
+ return nil , "" , err
271
277
}
272
278
defer f .Close ()
273
- return parseCgroupFromReader (f )
279
+ return parseCgroupFromReaderUnified (f )
274
280
}
275
281
276
- func parseCgroupFromReader (r io.Reader ) (map [string ]string , error ) {
282
+ func parseCgroupFromReaderUnified (r io.Reader ) (map [string ]string , string , error ) {
277
283
var (
278
284
cgroups = make (map [string ]string )
285
+ unified = ""
279
286
s = bufio .NewScanner (r )
280
287
)
281
288
for s .Scan () {
@@ -284,18 +291,20 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
284
291
parts = strings .SplitN (text , ":" , 3 )
285
292
)
286
293
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 )
288
295
}
289
296
for _ , subs := range strings .Split (parts [1 ], "," ) {
290
- if subs != "" {
297
+ if subs == "" {
298
+ unified = parts [2 ]
299
+ } else {
291
300
cgroups [subs ] = parts [2 ]
292
301
}
293
302
}
294
303
}
295
304
if err := s .Err (); err != nil {
296
- return nil , err
305
+ return nil , unified , err
297
306
}
298
- return cgroups , nil
307
+ return cgroups , unified , nil
299
308
}
300
309
301
310
func getCgroupDestination (subsystem string ) (string , error ) {
0 commit comments