Debian Codegraph @

internal/proc/pids.go

raw symbols SCIP
1 // Copyright 2018-2019 psgo authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // GetPIDs extracts and returns all PIDs from /proc.
29 func GetPIDs() ([]string, error) {
30 procDir, err := os.Open("/proc/")
31 if err != nil {
32 return nil, err
33 }
34 defer procDir.Close() //nolint:errcheck
35
36 // extract string slice of all directories in procDir
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 // skip non-numerical entries (e.g., `/proc/softirqs`)
47 continue
48 }
49 pids = append(pids, pidDir)
50 }
51
52 return pids, nil
53 }
54
55 // GetPIDsFromCgroup returns a strings slice of all pids listed in pid's pids
56 // cgroup. It automatically detects if we're running in unified mode or not.
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 // getPIDsFromCgroupV1 returns a strings slice of all pids listed in pid's pids
69 // cgroup.
70 func getPIDsFromCgroupV1(pid string) ([]string, error) {
71 // First, find the corresponding path to the PID cgroup.
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() //nolint:errcheck
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 // Second, extract the PIDs inside the cgroup.
97 f, err = os.Open(cgroupPath)
98 if err != nil {
99 if os.IsNotExist(err) {
100 // OCI runtimes might mount the container cgroup at the root, breaking what it showed
101 // in /proc/$PID/cgroup and the path.
102 // Check if the PID still exists to make sure the process is still alive.
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() //nolint:errcheck
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 // getPIDsFromCgroupV2 returns a strings slice of all pids listed in pid's pids
124 // cgroup.
125 func getPIDsFromCgroupV2(pid string) ([]string, error) {
126 // First, find the corresponding path to the PID cgroup.
127 f, err := os.Open(fmt.Sprintf("/proc/%s/cgroup", pid))
128 if err != nil {
129 return nil, err
130 }
131 defer f.Close() //nolint:errcheck
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 // Second, extract the PIDs inside the cgroup.
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() //nolint:errcheck
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 }