Skip to content

Commit

Permalink
Merge pull request #90 from giuseppe/fix-reading-pids-in-containerize…
Browse files Browse the repository at this point in the history
…d-cgroup-v1

pids: fix reading pids when running in a cgroup v1 container
  • Loading branch information
vrothberg authored Sep 22, 2021
2 parents b2c8a53 + efcb7f0 commit e7871f4
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/proc/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func GetPIDsFromCgroup(pid string) ([]string, error) {
// cgroup.
func getPIDsFromCgroupV1(pid string) ([]string, error) {
// First, find the corresponding path to the PID cgroup.
f, err := os.Open(fmt.Sprintf("/proc/%s/cgroup", pid))
pidPath := fmt.Sprintf("/proc/%s/cgroup", pid)
f, err := os.Open(pidPath)
if err != nil {
return nil, err
}
Expand All @@ -95,7 +96,18 @@ func getPIDsFromCgroupV1(pid string) ([]string, error) {
// Second, extract the PIDs inside the cgroup.
f, err = os.Open(cgroupPath)
if err != nil {
return nil, err
if os.IsNotExist(err) {
// OCI runtimes might mount the container cgroup at the root, breaking what it showed
// in /proc/$PID/cgroup and the path.
// Check if the PID still exists to make sure the process is still alive.
if _, errStat := os.Stat(pidPath); errStat == nil {
cgroupPath = filepath.Join(cgroups.CgroupRoot, "pids", "cgroup.procs")
f, err = os.Open(cgroupPath)
}
}
if err != nil {
return nil, err
}
}
defer f.Close()

Expand Down

0 comments on commit e7871f4

Please sign in to comment.