Skip to content

Commit 23b5120

Browse files
thaJeztahkolyshkin
andcommitted
utils: export ParseCgroupFile()
this exports the ParseCgroupFile() function, so that external consumers (such as moby) can use this function, instead of the implementation in runc's libcontainer (which is not intended for external consumers). The GoDoc documentation was borrowed from runc: opencontainers/runc@d244b40 Co-authored-by: Kir Kolyshkin <[email protected]> Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ff350a8 commit 23b5120

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

paths.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func StaticPath(path string) Path {
3939
// NestedPath will nest the cgroups based on the calling processes cgroup
4040
// placing its child processes inside its own path
4141
func NestedPath(suffix string) Path {
42-
paths, err := parseCgroupFile("/proc/self/cgroup")
42+
paths, err := ParseCgroupFile("/proc/self/cgroup")
4343
if err != nil {
4444
return errorPath(err)
4545
}
@@ -50,7 +50,7 @@ func NestedPath(suffix string) Path {
5050
// This is commonly used for the Load function to restore an existing container
5151
func PidPath(pid int) Path {
5252
p := fmt.Sprintf("/proc/%d/cgroup", pid)
53-
paths, err := parseCgroupFile(p)
53+
paths, err := ParseCgroupFile(p)
5454
if err != nil {
5555
return errorPath(errors.Wrapf(err, "parse cgroup file %s", p))
5656
}

paths_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestSelfPath(t *testing.T) {
4141
} else if err != nil {
4242
t.Fatal(err)
4343
}
44-
paths, err := parseCgroupFile("/proc/self/cgroup")
44+
paths, err := ParseCgroupFile("/proc/self/cgroup")
4545
if err != nil {
4646
t.Fatal(err)
4747
}
@@ -63,7 +63,7 @@ func TestPidPath(t *testing.T) {
6363
} else if err != nil {
6464
t.Fatal(err)
6565
}
66-
paths, err := parseCgroupFile("/proc/self/cgroup")
66+
paths, err := ParseCgroupFile("/proc/self/cgroup")
6767
if err != nil {
6868
t.Fatal(err)
6969
}

utils.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,16 @@ func parseKV(raw string) (string, uint64, error) {
285285
}
286286
}
287287

288-
func parseCgroupFile(path string) (map[string]string, error) {
288+
// ParseCgroupFile parses the given cgroup file, typically /proc/self/cgroup
289+
// or /proc/<pid>/cgroup, into a map of subsystems to cgroup paths, e.g.
290+
// "cpu": "/user.slice/user-1000.slice"
291+
// "pids": "/user.slice/user-1000.slice"
292+
// etc.
293+
//
294+
// Note that for cgroup v2 unified hierarchy, there are no per-controller
295+
// cgroup paths, so the resulting map will have a single element where the key
296+
// is empty string ("") and the value is the cgroup path the <pid> is in.
297+
func ParseCgroupFile(path string) (map[string]string, error) {
289298
f, err := os.Open(path)
290299
if err != nil {
291300
return nil, err

0 commit comments

Comments
 (0)