-
Notifications
You must be signed in to change notification settings - Fork 77
/
ps_linux.go
121 lines (100 loc) · 2.95 KB
/
ps_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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ps
import (
"fmt"
"os"
"path"
"regexp"
"strconv"
)
// LinuxClient is for finding processes on linux distributions.
type LinuxClient struct{}
const (
// defaultLinuxProcDir is the default location of proc filesystem mount point in
// a linux system.
defaultLinuxProcDir = "/proc/"
)
var (
// linuxProcDir is the location of proc filesystem mout point currently set up
// in the current execution. Unit tests may want to adjust it in order to simulate
// the target system.
linuxProcDir = defaultLinuxProcDir
)
// init creates the Linux process finder.
func init() {
Client = &LinuxClient{}
}
// Find finds all processes with the executable path matching the provided regex.
func (p LinuxClient) Find(exeMatch string) ([]Process, error) {
var result []Process
procExpression, err := regexp.Compile("^[0-9]*$")
if err != nil {
return nil, fmt.Errorf("failed to compile process dir expression: %+v", err)
}
exeExpression, err := regexp.Compile(exeMatch)
if err != nil {
return nil, fmt.Errorf("failed to compile process exec matching expression: %+v", err)
}
files, err := os.ReadDir(linuxProcDir)
if err != nil {
return nil, fmt.Errorf("failed to read linux proc dir: %+v", err)
}
for _, file := range files {
if !file.IsDir() {
continue
}
if !procExpression.MatchString(file.Name()) {
continue
}
processRootDir := path.Join(linuxProcDir, file.Name())
exeLinkPath := path.Join(processRootDir, "exe")
exePath, err := os.Readlink(exeLinkPath)
if err != nil {
continue
}
if !exeExpression.MatchString(exePath) {
continue
}
cmdlinePath := path.Join(processRootDir, "cmdline")
dat, err := os.ReadFile(cmdlinePath)
if err != nil {
return nil, fmt.Errorf("error reading cmdline file: %v", err)
}
var commandLine []string
var token []byte
for _, curr := range dat {
if curr == 0 {
commandLine = append(commandLine, string(token))
token = nil
} else {
token = append(token, curr)
}
}
pid, err := strconv.Atoi(file.Name())
if err != nil {
return nil, fmt.Errorf("error parsing PID: %v", err)
}
result = append(result, Process{
Pid: pid,
Exe: exePath,
CommandLine: commandLine,
})
}
return result, nil
}
// Find finds all processes with the executable matching the provided regex.
func Find(exeMatch string) ([]Process, error) {
return Client.Find(exeMatch)
}