Skip to content

Commit

Permalink
cgroups: accurate procfs parsing (#6462)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdandrutu authored Nov 2, 2022
2 parents d9a64c1 + cc61f0d commit 2898493
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .chloggen/cgroup-subsys-line-parse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
change_type: bug_fix

component: cgroups

note: split line into exactly 3 parts while parsing /proc/{pid}/cgroup

issues: [6389]
2 changes: 1 addition & 1 deletion internal/cgroups/subsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type CGroupSubsys struct {
// NewCGroupSubsysFromLine returns a new *CGroupSubsys by parsing a string in
// the format of `/proc/$PID/cgroup`
func NewCGroupSubsysFromLine(line string) (*CGroupSubsys, error) {
fields := strings.Split(line, _cgroupSep)
fields := strings.SplitN(line, _cgroupSep, _csFieldCount)

if len(fields) != _csFieldCount {
return nil, cgroupSubsysFormatInvalidError{line}
Expand Down
17 changes: 10 additions & 7 deletions internal/cgroups/subsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func TestNewCGroupSubsysFromLine(t *testing.T) {
Name: "/docker/1234567890abcdef",
},
},
{
name: "sophisticated-path",
line: "4:pids:/example.slice:extra-path-designator",
expectedSubsys: &CGroupSubsys{
ID: 4,
Subsystems: []string{"pids"},
Name: "/example.slice:extra-path-designator",
},
},
}

for _, tt := range testTable {
Expand All @@ -82,7 +91,6 @@ func TestNewCGroupSubsysFromLine(t *testing.T) {
func TestNewCGroupSubsysFromLineErr(t *testing.T) {
lines := []string{
"1:cpu",
"1:cpu,cpuacct:/:/necessary-field",
"not-a-number:cpu:/",
}
_, parseError := strconv.Atoi("not-a-number")
Expand All @@ -97,14 +105,9 @@ func TestNewCGroupSubsysFromLineErr(t *testing.T) {
line: lines[0],
expectedError: cgroupSubsysFormatInvalidError{lines[0]},
},
{
name: "more-fields",
line: lines[1],
expectedError: cgroupSubsysFormatInvalidError{lines[1]},
},
{
name: "illegal-id",
line: lines[2],
line: lines[1],
expectedError: parseError,
},
}
Expand Down

0 comments on commit 2898493

Please sign in to comment.