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

ParseCgroupFile: fix wrong comment about unified hierarchy ; add ParseCgroupFileUnified to get the unified path #232

Merged
merged 2 commits into from May 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions paths_test.go
Expand Up @@ -102,7 +102,7 @@ func TestEmptySubsystem(t *testing.T) {
1:name=systemd:/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service
0::/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service`
r := strings.NewReader(data)
paths, err := parseCgroupFromReader(r)
paths, unified, err := parseCgroupFromReaderUnified(r)
if err != nil {
t.Fatal(err)
}
Expand All @@ -111,6 +111,10 @@ func TestEmptySubsystem(t *testing.T) {
t.Fatalf("empty subsystem for %q", path)
}
}
unifiedExpected := "/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service"
if unified != unifiedExpected {
t.Fatalf("expected %q, got %q", unifiedExpected, unified)
}
}

func TestSystemd240(t *testing.T) {
Expand All @@ -127,7 +131,7 @@ func TestSystemd240(t *testing.T) {
1:name=systemd:/system.slice/docker.service
0::/system.slice/docker.service`
r := strings.NewReader(data)
paths, err := parseCgroupFromReader(r)
paths, unified, err := parseCgroupFromReaderUnified(r)
if err != nil {
t.Fatal(err)
}
Expand All @@ -140,4 +144,8 @@ func TestSystemd240(t *testing.T) {
if err != ErrControllerNotActive {
t.Fatalf("expected error %q but received %q", ErrControllerNotActive, err)
}
unifiedExpected := "/system.slice/docker.service"
if unified != unifiedExpected {
t.Fatalf("expected %q, got %q", unifiedExpected, unified)
}
}
29 changes: 19 additions & 10 deletions utils.go
Expand Up @@ -261,21 +261,28 @@ func parseKV(raw string) (string, uint64, error) {
// "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.
// The resulting map does not have an element for cgroup v2 unified hierarchy.
// Use ParseCgroupFileUnified to get the unified path.
func ParseCgroupFile(path string) (map[string]string, error) {
x, _, err := ParseCgroupFileUnified(path)
return x, err
}

// ParseCgroupFileUnified returns legacy subsystem paths as the first value,
// and returns the unified path as the second value.
func ParseCgroupFileUnified(path string) (map[string]string, string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
return nil, "", err
}
defer f.Close()
return parseCgroupFromReader(f)
return parseCgroupFromReaderUnified(f)
}

func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
func parseCgroupFromReaderUnified(r io.Reader) (map[string]string, string, error) {
var (
cgroups = make(map[string]string)
unified = ""
s = bufio.NewScanner(r)
)
for s.Scan() {
Expand All @@ -284,18 +291,20 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
parts = strings.SplitN(text, ":", 3)
)
if len(parts) < 3 {
return nil, fmt.Errorf("invalid cgroup entry: %q", text)
return nil, unified, fmt.Errorf("invalid cgroup entry: %q", text)
}
for _, subs := range strings.Split(parts[1], ",") {
if subs != "" {
if subs == "" {
unified = parts[2]
} else {
cgroups[subs] = parts[2]
}
}
}
if err := s.Err(); err != nil {
return nil, err
return nil, unified, err
}
return cgroups, nil
return cgroups, unified, nil
}

func getCgroupDestination(subsystem string) (string, error) {
Expand Down