-
Notifications
You must be signed in to change notification settings - Fork 0
/
restartable.go
400 lines (348 loc) · 9.49 KB
/
restartable.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//go:build linux
package main
import (
"bytes"
"errors"
"fmt"
"golang.org/x/sys/unix"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
)
import flag "github.com/spf13/pflag"
const version string = "2.3.4"
// ProcFS defines an interface for /proc/ filesystem access
type ProcFS interface {
ReadFile(path string) ([]byte, error)
ReadLink(path string) (string, error)
Close() error
}
// ProcPidFS defines an interface for /proc/<pid> filesystem access
type ProcPidFS interface {
ProcFS
PID() int
}
// RealProcPidFS implements ProcPidFS for real /proc/<pid> filesystem
type RealProcPidFS struct {
ProcPidFS
dirFd int
pid int
}
// OpenProc opens a /proc/<pid> directory and returns a ProcPidFS instance
func OpenProcPid(procDir string, pid int) (*RealProcPidFS, error) {
if procDir == "" {
procDir = "/proc"
}
path := filepath.Join(procDir, strconv.Itoa(pid))
dirFd, err := unix.Open(path, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_PATH, 0)
if err != nil {
return nil, &os.PathError{Op: "open", Path: fmt.Sprintf("/proc/%d", pid), Err: err}
}
return &RealProcPidFS{dirFd: dirFd, pid: pid}, nil
}
// Close releases the file descriptor
func (p *RealProcPidFS) Close() error {
err := unix.Close(p.dirFd)
if err != nil {
return &os.PathError{Op: "close", Path: "/proc", Err: err}
}
return nil
}
// ReadFile reads a file inside /proc/<pid>
func (p *RealProcPidFS) ReadFile(path string) ([]byte, error) {
fd, err := unix.Openat(p.dirFd, path, unix.O_RDONLY|unix.O_NOFOLLOW, 0)
if err != nil {
return nil, &os.PathError{Op: "openat", Path: fmt.Sprintf("/proc/%d/%s", p.pid, path), Err: err}
}
defer unix.Close(fd)
data := make([]byte, 0, 8192)
for {
if len(data) >= cap(data) {
d := append(data[:cap(data)], 0)
data = d[:len(data)]
}
if n, err := unix.Read(fd, data[len(data):cap(data)]); n > 0 {
data = data[:len(data)+n]
} else {
if err != nil {
err = &os.PathError{Op: "read", Path: fmt.Sprintf("/proc/%d/%s", p.pid, path), Err: err}
}
return data, err
}
}
}
// ReadLink reads a symbolic link inside /proc/<pid>
func (p *RealProcPidFS) ReadLink(path string) (string, error) {
for size := unix.PathMax; ; size *= 2 {
data := make([]byte, unix.PathMax)
if n, err := unix.Readlinkat(p.dirFd, path, data); err != nil {
return "", &os.PathError{Op: "readlinkat", Path: fmt.Sprintf("/proc/%d/%s", p.pid, path), Err: err}
} else if n != size {
return string(data[:n]), nil
}
}
}
// PID returns the process ID
func (p *RealProcPidFS) PID() int {
return p.pid
}
var (
regexDeleted = regexp.MustCompile(`/.* \(deleted\)$`)
regexIgnored = regexp.MustCompile(`^/(dev|memfd:|run| )`)
regexExecMap = regexp.MustCompile(`^[0-9a-f]+-[0-9a-f]+ r(w|-)x`)
)
// getDeleted retrieves deleted file mappings for a process
func getDeleted(maps string) []string {
var files []string
for _, line := range strings.Split(maps, "\n") {
file := regexDeleted.FindString(line)
if file != "" && regexExecMap.MatchString(line) && !regexIgnored.MatchString(file) {
files = append(files, quoteString(strings.TrimSuffix(file, " (deleted)")))
}
}
sort.Strings(files)
return files
}
// getService retrieves the service name
func getService(cgroup string, userService bool) string {
cgroup = strings.TrimSpace(cgroup)
if strings.HasSuffix(cgroup, ".service") {
// Systemd
if userService && strings.Contains(cgroup, "/user.slice/") || !userService && strings.Contains(cgroup, "/system.slice/") {
return strings.TrimSuffix(cgroup[strings.LastIndex(cgroup, "/")+1:], ".service")
}
} else if strings.Contains(cgroup, ":name=openrc:/") {
// OpenRC
return cgroup[strings.LastIndex(cgroup, "/")+1:]
}
return "-"
}
// getCommand retrieves the command
func getCommand(data []byte, exe string, fullPath bool, statusName string) string {
data = bytes.TrimSuffix(data, []byte("\x00"))
cmdline := strings.Split(string(data), "\x00")
var command string
if fullPath {
// Use full path
// cmdline is empty if zombie, but zombies have void maps
exe = strings.TrimSuffix(exe, " (deleted)")
if exe != "" && !strings.HasPrefix(cmdline[0], "/") && filepath.Base(cmdline[0]) == filepath.Base(exe) {
cmdline[0] = exe
}
command = strings.Join(cmdline, " ")
} else {
command = statusName
// The command may be truncated to 15 chars in /proc/<pid>/status
// Also, kernel usermode helpers use "none"
if (len(command) == 15 || command == "none") && len(cmdline) > 0 && cmdline[0] != "" {
command = cmdline[0]
}
if strings.HasPrefix(command, "/") {
command = filepath.Base(command)
} else {
command = strings.Split(command, " ")[0]
}
}
if command == "" {
command = "-"
}
return command
}
// parseStatusField extracts a field value from the status file given a key
func parseStatusField(data, key string) string {
if key != "Name" {
key = "\n" + key
}
key = key + ":\t"
start := strings.Index(data, key)
if start == -1 {
return ""
}
start += len(key)
end := strings.IndexByte(data[start:], '\n')
if end == -1 {
end = len(data[start:])
}
return data[start : start+end]
}
// ProcessInfo holds process information
type ProcessInfo struct {
Command string
Deleted []string
Pid int
Ppid int
Uid int
Service string
}
// getProcessInfo gets process information
func getProcessInfo(fs ProcPidFS, fullPath bool, userService bool) (*ProcessInfo, error) {
maps, err := fs.ReadFile("maps")
if err != nil {
if errors.Is(err, unix.EACCES) {
err = nil
}
return nil, err
}
deleted := getDeleted(string(maps))
if len(deleted) == 0 {
return nil, nil
}
data, err := fs.ReadFile("status")
if err != nil {
return nil, err
}
status := string(data)
ppid, _ := strconv.Atoi(parseStatusField(status, "PPid"))
uid, _ := strconv.Atoi(strings.Fields(parseStatusField(status, "Uid"))[0])
cmdline, err := fs.ReadFile("cmdline")
if err != nil {
return nil, err
}
exe, err := fs.ReadLink("exe")
if err != nil {
exe = ""
}
command := getCommand(cmdline, exe, fullPath, parseStatusField(status, "Name"))
cgroup, err := fs.ReadFile("cgroup")
if err != nil {
cgroup = []byte("")
}
service := getService(string(cgroup), userService)
return &ProcessInfo{
Command: quoteString(command),
Deleted: deleted,
Pid: fs.PID(),
Ppid: ppid,
Uid: uid,
Service: service,
}, nil
}
// Quote special characters
func quoteString(str string) string {
if len(str) > 0 {
str = strconv.Quote(str)
return str[1 : len(str)-1]
}
return ""
}
// Get username from UID
func getUser(uid int) string {
if info, err := user.LookupId(strconv.Itoa(uid)); err != nil {
return "-"
} else {
return info.Username
}
}
type ProcessLister interface {
ListProcesses() ([]int, error)
}
type DefaultProcessLister struct{}
func (d DefaultProcessLister) ListProcesses() ([]int, error) {
entries, err := os.ReadDir("/proc")
if err != nil {
return nil, err
}
var pids []int
for _, entry := range entries {
if pid, err := strconv.Atoi(entry.Name()); err == nil {
pids = append(pids, pid)
}
}
sort.Ints(pids)
return pids, nil
}
type Opts struct {
short int
user bool
verbose bool
version bool
}
func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (ProcPidFS, error)) {
pids, err := lister.ListProcesses()
if err != nil {
log.Fatal(err)
}
if opts.short < 3 {
fmt.Printf("%s\t%s\t%s\t%-20s\t%20s\t%s\n", "PID", "PPID", "UID", "User", "Service", "Command")
}
channel := make(map[int]chan *ProcessInfo, len(pids))
for _, pid := range pids {
channel[pid] = make(chan *ProcessInfo, 1)
}
for _, pid := range pids {
go func(pid int) {
defer close(channel[pid])
fs, err := openProc(pid)
if err != nil {
if !errors.Is(err, unix.ENOENT) {
log.Print(err)
}
return
}
defer fs.Close()
info, err := getProcessInfo(fs, opts.verbose, opts.user)
if err != nil {
log.Print(err)
}
channel[pid] <- info
}(pid)
}
services := make(map[string]bool)
for _, pid := range pids {
proc := <-channel[pid]
if proc == nil {
continue
}
if opts.short < 2 || proc.Service != "-" {
fmt.Printf("%d\t%d\t%d\t%-20s\t%20s\t%s\n", proc.Pid, proc.Ppid, proc.Uid, getUser(proc.Uid), proc.Service, proc.Command)
} else if proc.Service != "-" {
services[proc.Service] = true
}
if opts.short == 0 {
for _, deleted := range proc.Deleted {
fmt.Printf("\t%s\n", deleted)
}
}
}
if opts.short == 3 && len(services) > 0 {
// Print services in sorted mode
ss := make([]string, 0, len(services))
for s := range services {
ss = append(ss, s)
}
sort.Strings(ss)
for _, service := range ss {
fmt.Println(service)
}
}
}
func main() {
log.SetPrefix("ERROR: ")
log.SetFlags(0)
var opts Opts
flag.CountVarP(&opts.short, "short", "s", "Create a short table not showing the deleted files. Given twice, show only processes which are associated with a system service. Given three times, list the associated system service names only.")
flag.BoolVarP(&opts.user, "user", "u", false, "show user services instead of system services")
flag.BoolVarP(&opts.verbose, "verbose", "v", false, "verbose output")
flag.BoolVarP(&opts.version, "version", "V", false, "show version and exit")
flag.Parse()
if opts.version {
fmt.Printf("v%s %v %s/%s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if flag.NArg() > 0 {
flag.Usage()
os.Exit(1)
}
if os.Geteuid() != 0 {
fmt.Fprintln(os.Stderr, "WARN: Run this program as root")
}
runProcessMonitor(DefaultProcessLister{}, opts, func(pid int) (ProcPidFS, error) {
return OpenProcPid("/proc", pid)
})
}