-
Notifications
You must be signed in to change notification settings - Fork 166
/
procfs.go
184 lines (149 loc) · 3.58 KB
/
procfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package procfs
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strings"
"github.com/fntlnz/mountinfo"
"github.com/spf13/afero"
)
var ProcFs = afero.NewOsFs()
func FindPidByPodContainer(podUID, containerID string) (string, error) {
d, err := ProcFs.Open("/proc")
if err != nil {
return "", err
}
defer d.Close()
for {
dirs, err := d.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
return "", err
}
for _, di := range dirs {
if !di.IsDir() {
continue
}
dname := di.Name()
if dname[0] < '0' || dname[0] > '9' {
continue
}
mi, err := getMountInfo(path.Join("/proc", dname, "mountinfo"))
if err != nil {
continue
}
for _, m := range mi {
root := m.Root
// See https://github.com/kubernetes/kubernetes/blob/2f3a4ec9cb96e8e2414834991d63c59988c3c866/pkg/kubelet/cm/cgroup_manager_linux.go#L81-L85
// Note that these identifiers are currently specific to systemd, however, this mounting approach is what allows us to find the containerized
// process.
//
// EG: /kubepods/burstable/pod31dd0274-bb43-4975-bdbc-7e10047a23f8/851c75dad6ad8ce6a5d9b9129a4eb1645f7c6e5ba8406b12d50377b665737072
// /kubepods/burstable/pod{POD_ID}/{CONTAINER_ID}
//
// This "needle" that we look for in the mountinfo haystack should match one and only one container.
needle := path.Join(podUID, containerID)
if strings.Contains(root, needle) {
return dname, nil
}
}
}
}
return "", fmt.Errorf("no process found for specified pod and container")
}
func FindPidsForContainer(pid string) ([]string, error) {
d, err := ProcFs.Open("/proc")
if err != nil {
return nil, err
}
defer d.Close()
pids := []string{}
ns, err := readlink(path.Join("/proc", pid, "ns", "pid"))
if err != nil {
return nil, err
}
for {
dirs, err := d.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for _, di := range dirs {
if !di.IsDir() {
continue
}
dname := di.Name()
if dname[0] < '0' || dname[0] > '9' {
continue
}
cns, err := readlink(path.Join("/proc", dname, "ns", "pid"))
if err != nil {
return nil, err
}
if cns == ns {
pids = append(pids, dname)
}
}
}
return pids, nil
}
func GetFinalNamespacePid(pid string) (string, error) {
status, err := ProcFs.Open(path.Join("/proc", pid, "status"))
if err != nil {
return "", err
}
scanner := bufio.NewScanner(status)
var line string
for scanner.Scan() {
line = scanner.Text()
if strings.HasPrefix(line, "NSpid") {
break
}
}
if err := scanner.Err(); err != nil {
return "", err
}
fields := strings.Fields(line)
return fields[len(fields)-1], nil
}
func GetProcExe(pid string) (string, error) {
exe, err := readlink(path.Join("/proc", pid, "exe"))
if err != nil {
return "", err
}
return exe, nil
}
func GetProcComm(pid string) (string, error) {
comm, err := afero.ReadFile(ProcFs, path.Join("/proc", pid, "comm"))
if err != nil {
return "", err
}
return string(comm), nil
}
func GetProcCmdline(pid string) (string, error) {
cmdline, err := afero.ReadFile(ProcFs, path.Join("/proc", pid, "cmdline"))
if err != nil {
return "", err
}
return string(cmdline), nil
}
func getMountInfo(fd string) ([]mountinfo.Mountinfo, error) {
file, err := ProcFs.Open(fd)
if err != nil {
return nil, err
}
defer file.Close()
return mountinfo.ParseMountInfo(file)
}
func readlink(name string) (string, error) {
if r, ok := ProcFs.(afero.LinkReader); ok {
return r.ReadlinkIfPossible(name)
}
return "", &os.PathError{Op: "readlink", Path: name, Err: afero.ErrNoReadlink}
}