Skip to content

Commit

Permalink
pids: fix reading pids when running in a cgroup v1 container
Browse files Browse the repository at this point in the history
OCI runtimes might mount the container cgroup at the cgroup root
itself, breaking the correspondence between what is displayed in
/proc/$PID/cgroup and the path under the cgroup root.

When the cgroup path doesn't exist, check if the process is still
alive and attempt to read the pids from the root cgroup.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 21, 2021
1 parent b2c8a53 commit efcb7f0
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 efcb7f0

Please sign in to comment.