-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_process.go
More file actions
188 lines (155 loc) · 4.11 KB
/
parser_process.go
File metadata and controls
188 lines (155 loc) · 4.11 KB
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
package uss
import (
"strconv"
"strings"
)
// extractProcessMetadata extracts process-related metadata from the process raw string
// and populates the Entry fields
func extractProcessMetadata(entry *Entry, processRaw string) {
// Extract users:((name,pid=N,fd=M),...)
extractUsers(entry, processRaw)
// Extract simple key:value patterns
extractKeyValue(entry, processRaw, "uid:", func(val string) {
if uid, err := strconv.Atoi(val); err == nil {
entry.UID = &uid
}
})
extractKeyValue(entry, processRaw, "ino:", func(val string) {
if ino, err := strconv.ParseUint(val, 10, 64); err == nil {
entry.Inode = &ino
}
})
extractKeyValue(entry, processRaw, "cgroup:", func(val string) {
entry.CGroup = &val
})
extractKeyValue(entry, processRaw, "v6only:", func(val string) {
if v6, err := strconv.Atoi(val); err == nil {
entry.V6Only = &v6
}
})
extractKeyValue(entry, processRaw, "fwmark:", func(val string) {
entry.FWMark = &val
})
extractKeyValue(entry, processRaw, "sk:", func(val string) {
entry.Sk = &val
})
extractKeyValue(entry, processRaw, "dev:", func(val string) {
entry.Dev = &val
})
// Check for peers: marker
if strings.Contains(processRaw, "peers:") {
peers := "peers:"
entry.Peers = &peers
}
}
// extractUsers parses users:((name,pid=N,fd=M),...) format
func extractUsers(entry *Entry, processRaw string) {
// Find users:(( and then manually extract until matching ))
startMarker := "users:(("
idx := strings.Index(processRaw, startMarker)
if idx == -1 {
return
}
// Start after "users:(("
start := idx + len(startMarker)
// Find the matching )) by counting parentheses
parenCount := 2 // We already have ((
end := start
for end < len(processRaw) && parenCount > 0 {
if processRaw[end] == '(' {
parenCount++
} else if processRaw[end] == ')' {
parenCount--
}
end++
}
if parenCount != 0 {
// Malformed, couldn't find matching parentheses
return
}
// Extract content between (( and ))
// end is now pointing after the last ), so we need to go back 2 positions
usersContent := processRaw[start : end-2]
// Split by ),( to get individual user entries
userEntries := strings.Split(usersContent, "),(")
for _, userEntry := range userEntries {
user := parseUserEntry(userEntry)
if user.Name != "" {
entry.Users = append(entry.Users, user)
}
}
}
// parseUserEntry parses a single user entry like: "name",pid=N,fd=M
func parseUserEntry(s string) User {
user := User{}
// Split by comma
parts := splitUserParts(s)
for _, part := range parts {
part = strings.TrimSpace(part)
// Name is quoted
if strings.HasPrefix(part, `"`) && strings.HasSuffix(part, `"`) {
user.Name = strings.Trim(part, `"`)
continue
}
// pid=N
if strings.HasPrefix(part, "pid=") {
if pid, err := strconv.Atoi(strings.TrimPrefix(part, "pid=")); err == nil {
user.PID = pid
}
continue
}
// fd=M
if strings.HasPrefix(part, "fd=") {
if fd, err := strconv.Atoi(strings.TrimPrefix(part, "fd=")); err == nil {
user.FD = fd
}
continue
}
}
return user
}
// splitUserParts splits by comma but respects quoted strings
func splitUserParts(s string) []string {
var parts []string
var current strings.Builder
inQuote := false
for _, ch := range s {
if ch == '"' {
inQuote = !inQuote
current.WriteRune(ch)
} else if ch == ',' && !inQuote {
parts = append(parts, current.String())
current.Reset()
} else {
current.WriteRune(ch)
}
}
if current.Len() > 0 {
parts = append(parts, current.String())
}
return parts
}
// extractKeyValue finds key:value patterns and calls the handler with the value
func extractKeyValue(entry *Entry, processRaw, key string, handler func(string)) {
idx := strings.Index(processRaw, key)
if idx == -1 {
return
}
// Start after the key
start := idx + len(key)
if start >= len(processRaw) {
return
}
// Extract value until whitespace or end
end := start
for end < len(processRaw) && !isWhitespace(processRaw[end]) {
end++
}
value := processRaw[start:end]
if value != "" {
handler(value)
}
}
func isWhitespace(b byte) bool {
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
}