Skip to content

Commit e416d6d

Browse files
timuralpdiscordianfish
authored andcommittedJul 14, 2024·
Fix parsing NSpids field in /proc/{PID}/status
The NSpids field uses a tab (\t) to separate the list of PIDs. The procfs parser should split on \t, rather than space. This was not caught because the test fixtures themselves used the space character. The patch updates the parser, the fixtures, and adds an explicit test for NSpids. Signed-off-by: Timur Alperovich <timur.alperovich@databricks.com>
1 parent bd11127 commit e416d6d

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed
 

‎proc_status.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintByt
146146
}
147147
}
148148
case "NSpid":
149-
s.NSpids = calcNSPidsList(vString)
149+
nspids, err := calcNSPidsList(vString)
150+
if err != nil {
151+
return err
152+
}
153+
s.NSpids = nspids
150154
case "VmPeak":
151155
s.VmPeak = vUintBytes
152156
case "VmSize":
@@ -222,17 +226,17 @@ func calcCpusAllowedList(cpuString string) []uint64 {
222226
return g
223227
}
224228

225-
func calcNSPidsList(nspidsString string) []uint64 {
226-
s := strings.Split(nspidsString, " ")
229+
func calcNSPidsList(nspidsString string) ([]uint64, error) {
230+
s := strings.Split(nspidsString, "\t")
227231
var nspids []uint64
228232

229233
for _, nspid := range s {
230-
nspid, _ := strconv.ParseUint(nspid, 10, 64)
231-
if nspid == 0 {
232-
continue
234+
nspid, err := strconv.ParseUint(nspid, 10, 64)
235+
if err != nil {
236+
return nil, err
233237
}
234238
nspids = append(nspids, nspid)
235239
}
236240

237-
return nspids
241+
return nspids, nil
238242
}

‎proc_status_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,19 @@ func TestCpusAllowedList(t *testing.T) {
139139
t.Errorf("want CpusAllowedList %v, have %v", want, have)
140140
}
141141
}
142+
143+
func TestNsPids(t *testing.T) {
144+
p, err := getProcFixtures(t).Proc(26235)
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
149+
s, err := p.NewStatus()
150+
if err != nil {
151+
t.Fatal(err)
152+
}
153+
154+
if want, have := []uint64{26235, 1}, s.NSpids; !reflect.DeepEqual(want, have) {
155+
t.Errorf("want NsPids %v, have %v", want, have)
156+
}
157+
}

‎testdata/fixtures.ttar

+4-4
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,10 @@ Uid: 0 0 0 0
818818
Gid: 0 0 0 0
819819
FDSize: 64
820820
Groups:
821-
NStgid: 26235 1
822-
NSpid: 26235 1
823-
NSpgid: 26235 1
824-
NSsid: 26235 1
821+
NStgid: 26235 1
822+
NSpid: 26235 1
823+
NSpgid: 26235 1
824+
NSsid: 26235 1
825825
VmPeak: 758200 kB
826826
VmSize: 758200 kB
827827
VmLck: 0 kB

0 commit comments

Comments
 (0)
Please sign in to comment.