From 23b51209bf7bd11941d5613119309b11029e8373 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 5 Jun 2021 16:37:00 +0200 Subject: [PATCH] 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: https://github.com/opencontainers/runc/commit/d244b4058ee81ca43182bffe536d63ec70326904 Co-authored-by: Kir Kolyshkin Signed-off-by: Sebastiaan van Stijn --- paths.go | 4 ++-- paths_test.go | 4 ++-- utils.go | 11 ++++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/paths.go b/paths.go index 27197eca..ff50c95d 100644 --- a/paths.go +++ b/paths.go @@ -39,7 +39,7 @@ func StaticPath(path string) Path { // NestedPath will nest the cgroups based on the calling processes cgroup // placing its child processes inside its own path func NestedPath(suffix string) Path { - paths, err := parseCgroupFile("/proc/self/cgroup") + paths, err := ParseCgroupFile("/proc/self/cgroup") if err != nil { return errorPath(err) } @@ -50,7 +50,7 @@ func NestedPath(suffix string) Path { // This is commonly used for the Load function to restore an existing container func PidPath(pid int) Path { p := fmt.Sprintf("/proc/%d/cgroup", pid) - paths, err := parseCgroupFile(p) + paths, err := ParseCgroupFile(p) if err != nil { return errorPath(errors.Wrapf(err, "parse cgroup file %s", p)) } diff --git a/paths_test.go b/paths_test.go index 6860dee0..9dd2898d 100644 --- a/paths_test.go +++ b/paths_test.go @@ -41,7 +41,7 @@ func TestSelfPath(t *testing.T) { } else if err != nil { t.Fatal(err) } - paths, err := parseCgroupFile("/proc/self/cgroup") + paths, err := ParseCgroupFile("/proc/self/cgroup") if err != nil { t.Fatal(err) } @@ -63,7 +63,7 @@ func TestPidPath(t *testing.T) { } else if err != nil { t.Fatal(err) } - paths, err := parseCgroupFile("/proc/self/cgroup") + paths, err := ParseCgroupFile("/proc/self/cgroup") if err != nil { t.Fatal(err) } diff --git a/utils.go b/utils.go index ed894b3e..ac7a0d49 100644 --- a/utils.go +++ b/utils.go @@ -285,7 +285,16 @@ func parseKV(raw string) (string, uint64, error) { } } -func parseCgroupFile(path string) (map[string]string, error) { +// ParseCgroupFile parses the given cgroup file, typically /proc/self/cgroup +// or /proc//cgroup, into a map of subsystems to cgroup paths, e.g. +// "cpu": "/user.slice/user-1000.slice" +// "pids": "/user.slice/user-1000.slice" +// etc. +// +// Note that for cgroup v2 unified hierarchy, there are no per-controller +// cgroup paths, so the resulting map will have a single element where the key +// is empty string ("") and the value is the cgroup path the is in. +func ParseCgroupFile(path string) (map[string]string, error) { f, err := os.Open(path) if err != nil { return nil, err