| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package proc |
| 16 |
|
| 17 |
import ( |
| 18 |
"bufio" |
| 19 |
"fmt" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"github.com/containers/psgo/internal/cgroups" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
func GetPIDs() ([]string, error) { |
| 30 |
procDir, err := os.Open("/proc/") |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
defer procDir.Close() |
| 35 |
|
| 36 |
|
| 37 |
pidDirs, err := procDir.Readdirnames(0) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
pids := []string{} |
| 43 |
for _, pidDir := range pidDirs { |
| 44 |
_, err := strconv.Atoi(pidDir) |
| 45 |
if err != nil { |
| 46 |
|
| 47 |
continue |
| 48 |
} |
| 49 |
pids = append(pids, pidDir) |
| 50 |
} |
| 51 |
|
| 52 |
return pids, nil |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func GetPIDsFromCgroup(pid string) ([]string, error) { |
| 58 |
unified, err := cgroups.IsCgroup2UnifiedMode() |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
if unified { |
| 63 |
return getPIDsFromCgroupV2(pid) |
| 64 |
} |
| 65 |
return getPIDsFromCgroupV1(pid) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func getPIDsFromCgroupV1(pid string) ([]string, error) { |
| 71 |
|
| 72 |
pidPath := fmt.Sprintf("/proc/%s/cgroup", pid) |
| 73 |
f, err := os.Open(pidPath) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
defer f.Close() |
| 78 |
|
| 79 |
scanner := bufio.NewScanner(f) |
| 80 |
cgroupPath := "" |
| 81 |
for scanner.Scan() { |
| 82 |
fields := strings.Split(scanner.Text(), ":") |
| 83 |
if len(fields) != 3 { |
| 84 |
continue |
| 85 |
} |
| 86 |
if fields[1] == "pids" { |
| 87 |
cgroupPath = filepath.Join(cgroups.CgroupRoot, "pids", fields[2], "cgroup.procs") |
| 88 |
break |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if cgroupPath == "" { |
| 93 |
return nil, fmt.Errorf("couldn't find v1 pids group for PID %s", pid) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
f, err = os.Open(cgroupPath) |
| 98 |
if err != nil { |
| 99 |
if os.IsNotExist(err) { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if _, errStat := os.Stat(pidPath); errStat == nil { |
| 104 |
cgroupPath = filepath.Join(cgroups.CgroupRoot, "pids", "cgroup.procs") |
| 105 |
f, err = os.Open(cgroupPath) |
| 106 |
} |
| 107 |
} |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
} |
| 112 |
defer f.Close() |
| 113 |
|
| 114 |
pids := []string{} |
| 115 |
scanner = bufio.NewScanner(f) |
| 116 |
for scanner.Scan() { |
| 117 |
pids = append(pids, scanner.Text()) |
| 118 |
} |
| 119 |
|
| 120 |
return pids, nil |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func getPIDsFromCgroupV2(pid string) ([]string, error) { |
| 126 |
|
| 127 |
f, err := os.Open(fmt.Sprintf("/proc/%s/cgroup", pid)) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
defer f.Close() |
| 132 |
|
| 133 |
scanner := bufio.NewScanner(f) |
| 134 |
cgroupSlice := "" |
| 135 |
for scanner.Scan() { |
| 136 |
fields := strings.Split(scanner.Text(), ":") |
| 137 |
if len(fields) != 3 { |
| 138 |
continue |
| 139 |
} |
| 140 |
if fields[1] == "" { |
| 141 |
cgroupSlice = fields[2] |
| 142 |
break |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if cgroupSlice == "" { |
| 147 |
return nil, fmt.Errorf("couldn't find v2 pids group for PID %s", pid) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
f, err = os.Open(filepath.Join(cgroups.CgroupRoot, cgroupSlice, "cgroup.procs")) |
| 152 |
if err != nil { |
| 153 |
return nil, err |
| 154 |
} |
| 155 |
defer f.Close() |
| 156 |
|
| 157 |
pids := []string{} |
| 158 |
scanner = bufio.NewScanner(f) |
| 159 |
for scanner.Scan() { |
| 160 |
pids = append(pids, scanner.Text()) |
| 161 |
} |
| 162 |
|
| 163 |
return pids, nil |
| 164 |
} |