Skip to content

Commit 19d9bfd

Browse files
csweichelroboquat
authored andcommitted
[agent-smith] Fix proc stat
1 parent f173d2a commit 19d9bfd

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

components/ee/agent-smith/pkg/detector/proc.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func statProc(pid int) (*stat, error) {
109109
func parseStat(r io.Reader) (res *stat, err error) {
110110
var (
111111
ppid uint64
112+
foundPPID bool
112113
starttime uint64
113114
i = -1
114115
)
@@ -126,6 +127,7 @@ func parseStat(r io.Reader) (res *stat, err error) {
126127

127128
if i == 2 {
128129
ppid, err = strconv.ParseUint(string(text), 10, 64)
130+
foundPPID = true
129131
}
130132
if i == 20 {
131133
starttime, err = strconv.ParseUint(string(text), 10, 64)
@@ -138,11 +140,14 @@ func parseStat(r io.Reader) (res *stat, err error) {
138140
i++
139141
}
140142
}
143+
if err != nil {
144+
return nil, err
145+
}
141146
if err := scan.Err(); err != nil {
142147
return nil, err
143148
}
144149

145-
if ppid == 0 || starttime == 0 {
150+
if !foundPPID || starttime == 0 {
146151
return nil, fmt.Errorf("cannot parse stat")
147152
}
148153

components/ee/agent-smith/pkg/detector/proc_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ func TestParseStat(t *testing.T) {
375375
Content: "80275 (cat) R 717 80275 717 34817 80275 4194304 85 0 0 0 0 0 0 0 26 6 1 0 4733826 5771264 135 18446744073709551615 94070799228928 94070799254577 140722983793472 0 0 0 0 0 0 0 0 0 17 14 0 0 0 0 0 94070799272592 94070799274176 94070803738624 140722983801930 140722983801950 140722983801950 140722983821291 0",
376376
Expectation: Expectation{S: &stat{PPID: 717, Starttime: 4733826}},
377377
},
378+
{
379+
Name: "pid 1",
380+
Content: "1 (systemd) S 0 1 1 0 -1 4194560 62769 924461 98 1590 388 255 2488 1097 20 0 1 0 63 175169536 3435 18446744073709551615 94093530578944 94093531561125 140726309452800 0 0 0 671173123 4096 1260 1 0 0 17 3 0 0 32 0 0 94093531915152 94093532201000 94093562523648 140726309453736 140726309453747 140726309453747 140726309453805 0",
381+
Expectation: Expectation{S: &stat{Starttime: 63}},
382+
},
383+
{
384+
Name: "kthreadd",
385+
Content: "2 (kthreadd) S 0 0 0 0 -1 2129984 0 0 0 0 3 0 0 0 20 0 1 0 63 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0",
386+
Expectation: Expectation{S: &stat{Starttime: 63}},
387+
},
378388
}
379389
for _, test := range tests {
380390
t.Run(test.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)