/
processes_linux.go
277 lines (201 loc) · 5.57 KB
/
processes_linux.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//go:build linux
// +build linux
// Package process provides methods for gathering information about active processes
package process
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/system"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// ProcessInfo contains basic info about process
type ProcessInfo struct {
Command string // Full command
User string // Username
PID int // PID
Parent int // Parent process PID
Children []*ProcessInfo // Slice with child processes
IsThread bool // True if process is thread
}
// ////////////////////////////////////////////////////////////////////////////////// //
// procFS is path to procfs
var procFS = "/proc"
// ////////////////////////////////////////////////////////////////////////////////// //
// GetTree returns root process with all subprocesses on the system
func GetTree(pid ...int) (*ProcessInfo, error) {
root := 1
if len(pid) != 0 {
root = pid[0]
}
if !fsutil.IsExist(procFS + "/" + strconv.Itoa(root)) {
return nil, fmt.Errorf("Process with PID %d doesn't exist", pid)
}
list, err := findInfo(procFS, make(map[int]string))
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, fmt.Errorf("Can't find any processes")
}
return processListToTree(list, root), nil
}
// GetList returns slice with all active processes on the system
func GetList() ([]*ProcessInfo, error) {
return findInfo(procFS, make(map[int]string))
}
// ////////////////////////////////////////////////////////////////////////////////// //
func findInfo(dir string, userMap map[int]string) ([]*ProcessInfo, error) {
var result []*ProcessInfo
dirs := fsutil.List(dir, true, fsutil.ListingFilter{Perms: "DRX"})
for _, pid := range dirs {
if !isPID(pid) {
continue
}
taskDir := dir + "/" + pid + "/task"
if fsutil.IsExist(taskDir) {
threads, err := findInfo(taskDir, userMap)
if err != nil {
return nil, err
}
if len(threads) == 0 {
continue
}
result = append(result, threads...)
continue
}
info, err := readProcessInfo(dir+"/"+pid, pid, userMap)
if err != nil {
return nil, err
}
if info == nil {
continue
}
result = append(result, info)
}
return result, nil
}
func readProcessInfo(dir, pid string, userMap map[int]string) (*ProcessInfo, error) {
pidInt, err := strconv.Atoi(pid)
if err != nil {
return nil, err
}
// The process had died after the moment when we have created a list of processes
if !fsutil.IsExist(dir + "/cmdline") {
return nil, nil
}
cmd, err := os.ReadFile(dir + "/cmdline")
if err != nil {
return nil, err
}
if len(cmd) == 0 {
return nil, nil
}
uid, _, err := fsutil.GetOwner(dir)
if err != nil {
return nil, err
}
username, err := getProcessUser(uid, userMap)
if err != nil {
return nil, err
}
ppid, isThread := getProcessParent(dir, pidInt)
return &ProcessInfo{
Command: formatCommand(string(cmd)),
User: username,
PID: pidInt,
Parent: ppid,
IsThread: isThread,
}, nil
}
func getProcessUser(uid int, userMap map[int]string) (string, error) {
if uid == 0 {
return "root", nil
}
if userMap[uid] != "" {
return userMap[uid], nil
}
user, err := system.LookupUser(strconv.Itoa(uid))
if err != nil {
return "", err
}
userMap[uid] = user.Name
return user.Name, nil
}
func getProcessParent(pidDir string, pid int) (int, bool) {
tgid, ppid := getParentPIDs(pidDir)
if tgid != pid {
return tgid, true
}
return ppid, false
}
func getParentPIDs(pidDir string) (int, int) {
data, err := os.ReadFile(pidDir + "/status")
if err != nil {
return -1, -1
}
var ppid, tgid string
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "Tgid:") {
tgid = strings.TrimSpace(line[5:])
}
if strings.HasPrefix(line, "PPid:") {
ppid = strings.TrimSpace(line[5:])
}
if ppid != "" && tgid != "" {
break
}
}
if tgid == "" || ppid == "" {
return -1, -1
}
tgidInt, tgidErr := strconv.Atoi(tgid)
ppidInt, ppidErr := strconv.Atoi(ppid)
if tgidErr != nil || ppidErr != nil {
return -1, -1
}
return tgidInt, ppidInt
}
func formatCommand(cmd string) string {
// Normalize delimiters
command := strings.Replace(cmd, "\000", " ", -1)
// Remove space on the end of command
command = strings.TrimSpace(command)
return command
}
func processListToTree(processes []*ProcessInfo, root int) *ProcessInfo {
var result = make(map[int]*ProcessInfo)
for _, info := range processes {
result[info.PID] = info
}
for _, process := range result {
if process.Parent < 0 {
continue
}
parentProcess := result[process.Parent]
if parentProcess == nil {
continue
}
parentProcess.Children = append(parentProcess.Children, process)
}
return result[root]
}
func isPID(pid string) bool {
if pid == "" {
return false
}
// Pid must start from number
switch pid[0] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return true
}
return false
}