in metric/system/cgroup/util.go [306:356]
func guessContainerCgroupPath(v2Loc string, OurPid int) (string, error) {
// check the cache first
if cachePath := cgroupContainerPath.get(); cachePath != "" {
// check the validity of the cache
rawFile, err := os.ReadFile(filepath.Join(v2Loc, cachePath, "cgroup.procs"))
// if we get a read error, assume the cache is invalid, move on
if err == nil {
if foundMatchingPidInProcsFile(OurPid, string(rawFile)) {
return cachePath, nil
}
}
}
// pattern:
// if in a private cgroup namespace,
// traverse over the root cgroup path, look for *.procs files
// go through all of the *.procs files until we have one that contains our pid
// that path is our cgroup
foundCgroupPath := ""
err := filepath.WalkDir(v2Loc, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if strings.Contains(d.Name(), "procs") {
pidfile, err := os.ReadFile(path) //nolint: nilerr // we can get lots of weird permissions errors here, so don't fail on an error
if err != nil {
return nil //nolint: nilerr // we can get lots of weird permissions errors here, so don't fail on an error
}
if foundMatchingPidInProcsFile(OurPid, string(pidfile)) {
foundCgroupPath = path
return nil
}
}
return nil
})
if err != nil {
return "", fmt.Errorf("error traversing paths to find cgroup: %w", err)
}
if foundCgroupPath == "" {
return "", nil
}
// strip to cgroup path
cgroupDir := filepath.Dir(foundCgroupPath)
relativePath := strings.TrimPrefix(cgroupDir, v2Loc)
cgroupContainerPath.set(relativePath)
return relativePath, nil
}