Skip to content

Commit

Permalink
chore: Use kernel-compliant types for {U,G}IDs
Browse files Browse the repository at this point in the history
As defined in the `torvalds/linux` git tree, `uidgid_types.h`:
https://github.com/torvalds/linux/blob/8e938e39866920ddc266898e6ae1fffc5c8f51aa/include/linux/uidgid_types.h#L8

Fixes: prometheus#372
Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
  • Loading branch information
rexagod committed Mar 22, 2024
1 parent f7c2619 commit 938405f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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

0 comments on commit 938405f

Please sign in to comment.