Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use kernel-compliant types for {U,G}IDs #620

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 23 additions & 6 deletions proc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package procfs

import (
"bytes"
"math/bits"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -76,9 +77,9 @@ type ProcStatus struct {
NonVoluntaryCtxtSwitches uint64

// UIDs of the process (Real, effective, saved set, and filesystem UIDs)
UIDs [4]string
UIDs [4]uint64
// GIDs of the process (Real, effective, saved set, and filesystem GIDs)
GIDs [4]string
GIDs [4]uint64

// CpusAllowedList: List of cpu cores processes are allowed to run on.
CpusAllowedList []uint64
Expand Down Expand Up @@ -113,22 +114,37 @@ func (p Proc) NewStatus() (ProcStatus, error) {
// convert kB to B
vBytes := vKBytes * 1024

s.fillStatus(k, v, vKBytes, vBytes)
err = s.fillStatus(k, v, vKBytes, vBytes)
if err != nil {
return ProcStatus{}, err
}
}

return s, nil
}

func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) {
func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) error {
switch k {
case "Tgid":
s.TGID = int(vUint)
case "Name":
s.Name = vString
case "Uid":
copy(s.UIDs[:], strings.Split(vString, "\t"))
var err error
for i, v := range strings.Split(vString, "\t") {
s.UIDs[i], err = strconv.ParseUint(v, 10, bits.UintSize)
if err != nil {
return err
}
}
case "Gid":
copy(s.GIDs[:], strings.Split(vString, "\t"))
var err error
for i, v := range strings.Split(vString, "\t") {
s.GIDs[i], err = strconv.ParseUint(v, 10, bits.UintSize)
if err != nil {
return err
}
}
case "NSpid":
s.NSpids = calcNSPidsList(vString)
case "VmPeak":
Expand Down Expand Up @@ -173,6 +189,7 @@ func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintByt
s.CpusAllowedList = calcCpusAllowedList(vString)
}

return nil
}

// TotalCtxtSwitches returns the total context switch.
Expand Down
8 changes: 4 additions & 4 deletions proc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func TestProcStatusUIDs(t *testing.T) {
t.Fatal(err)
}

if want, have := [4]string{"1000", "1000", "1000", "0"}, s.UIDs; want != have {
t.Errorf("want uids %s, have %s", want, have)
if want, have := [4]uint64{1000, 1000, 1000, 0}, s.UIDs; want != have {
t.Errorf("want uids %v, have %v", want, have)
}
}

Expand All @@ -119,8 +119,8 @@ func TestProcStatusGIDs(t *testing.T) {
t.Fatal(err)
}

if want, have := [4]string{"1001", "1001", "1001", "0"}, s.GIDs; want != have {
t.Errorf("want uids %s, have %s", want, have)
if want, have := [4]uint64{1001, 1001, 1001, 0}, s.GIDs; want != have {
t.Errorf("want gids %v, have %v", want, have)
}
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,8 @@ Ngid: 12345
Pid: 26235
PPid: 1234
TracerPid: 0
Uid: 0 0 0 0
Gid: 0 0 0 0
Uid: 0 0 0 0
Gid: 0 0 0 0
Comment on lines +817 to +818
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/spaces/tabs

Uid: 1000 1000 1000 0
Gid: 1001 1001 1001 0

FDSize: 64
Groups:
NStgid: 26235 1
Expand Down