cgroup.go 911 Bytes
Newer Older
1
package runtime
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

const HostCgroupPath = "/sys/fs/cgroup"

// ResolveCgroupRootFromHostPID reads the unified cgroup v2 path for a PID via /host/proc.
func ResolveCgroupRootFromHostPID(pid int) (string, error) {
	cgroupFile := filepath.Join(HostProcPath, strconv.Itoa(pid), "cgroup")
	data, err := os.ReadFile(cgroupFile)
	if err != nil {
		return "", fmt.Errorf("failed reading %s: %w", cgroupFile, err)
	}

	for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
		line = strings.TrimSpace(line)
		if line == "" || !strings.HasPrefix(line, "0::") {
			continue
		}
		path := strings.TrimPrefix(line, "0::")
		if path == "" {
			return "/", nil
		}
		if !strings.HasPrefix(path, "/") {
			path = "/" + path
		}
		return filepath.Clean(path), nil
	}

	return "", fmt.Errorf("unified cgroup entry not found in %s", cgroupFile)
}