Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: export ParseCgroupFile() #197

Merged
merged 1 commit into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
11 changes: 10 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<pid>/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 <pid> is in.
func ParseCgroupFile(path string) (map[string]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
Expand Down