Skip to content

Commit

Permalink
Fix parsing NSpids field in /proc/{PID}/status
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
timuralp authored and discordianfish committed Jul 14, 2024
1 parent bd11127 commit e416d6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
18 changes: 11 additions & 7 deletions proc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintByt
}
}
case "NSpid":
s.NSpids = calcNSPidsList(vString)
nspids, err := calcNSPidsList(vString)
if err != nil {
return err
}
s.NSpids = nspids
case "VmPeak":
s.VmPeak = vUintBytes
case "VmSize":
Expand Down Expand Up @@ -222,17 +226,17 @@ func calcCpusAllowedList(cpuString string) []uint64 {
return g
}

func calcNSPidsList(nspidsString string) []uint64 {
s := strings.Split(nspidsString, " ")
func calcNSPidsList(nspidsString string) ([]uint64, error) {
s := strings.Split(nspidsString, "\t")
var nspids []uint64

for _, nspid := range s {
nspid, _ := strconv.ParseUint(nspid, 10, 64)
if nspid == 0 {
continue
nspid, err := strconv.ParseUint(nspid, 10, 64)
if err != nil {
return nil, err
}
nspids = append(nspids, nspid)
}

return nspids
return nspids, nil
}
16 changes: 16 additions & 0 deletions proc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,19 @@ func TestCpusAllowedList(t *testing.T) {
t.Errorf("want CpusAllowedList %v, have %v", want, have)
}
}

func TestNsPids(t *testing.T) {
p, err := getProcFixtures(t).Proc(26235)
if err != nil {
t.Fatal(err)
}

s, err := p.NewStatus()
if err != nil {
t.Fatal(err)
}

if want, have := []uint64{26235, 1}, s.NSpids; !reflect.DeepEqual(want, have) {
t.Errorf("want NsPids %v, have %v", want, have)
}
}
8 changes: 4 additions & 4 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,10 @@ Uid: 0 0 0 0
Gid: 0 0 0 0
FDSize: 64
Groups:
NStgid: 26235 1
NSpid: 26235 1
NSpgid: 26235 1
NSsid: 26235 1
NStgid: 26235 1
NSpid: 26235 1
NSpgid: 26235 1
NSsid: 26235 1
VmPeak: 758200 kB
VmSize: 758200 kB
VmLck: 0 kB
Expand Down

0 comments on commit e416d6d

Please sign in to comment.