Skip to content

Commit

Permalink
add ParseCgroupFileUnified to get the unified path
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed May 26, 2022
1 parent dae6735 commit dd81920
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
12 changes: 10 additions & 2 deletions paths_test.go
Original file line number Diff line number Diff line change
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)
}
}
25 changes: 18 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,27 @@ func parseKV(raw string) (string, uint64, error) {
// etc.
//
// 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 @@ -282,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

0 comments on commit dd81920

Please sign in to comment.