Skip to content

Commit aa8003c

Browse files
committed
Fix systemd full path
Signed-off-by: Cameron Sparr <[email protected]>
1 parent cf1b326 commit aa8003c

2 files changed

Lines changed: 95 additions & 3 deletions

File tree

v2/manager.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,39 @@ func setDevices(path string, devices []specs.LinuxDeviceCgroup) error {
701701
return nil
702702
}
703703

704+
// getSystemdFullPath returns the full systemd path when creating a systemd slice group.
705+
// the reason this is necessary is because the "-" character has a special meaning in
706+
// systemd slice. For example, when creating a slice called "my-group-112233.slice",
707+
// systemd will create a hierarchy like this:
708+
// /sys/fs/cgroup/my.slice/my-group.slice/my-group-112233.slice
709+
func getSystemdFullPath(slice, group string) string {
710+
return filepath.Join(defaultCgroup2Path, dashesToPath(slice), dashesToPath(group))
711+
}
712+
713+
// dashesToPath converts a slice name with dashes to it's corresponding systemd filesystem path.
714+
func dashesToPath(in string) string {
715+
path := ""
716+
if strings.HasSuffix(in, ".slice") && strings.Contains(in, "-") {
717+
parts := strings.Split(in, "-")
718+
for i := range parts {
719+
s := strings.Join(parts[0:i+1], "-")
720+
if !strings.HasSuffix(s, ".slice") {
721+
s += ".slice"
722+
}
723+
path = filepath.Join(path, s)
724+
}
725+
} else {
726+
path = filepath.Join(path, in)
727+
}
728+
return path
729+
}
730+
704731
func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, error) {
705732
if slice == "" {
706733
slice = defaultSlice
707734
}
708735
ctx := context.TODO()
709-
path := filepath.Join(defaultCgroup2Path, slice, group)
736+
path := getSystemdFullPath(slice, group)
710737
conn, err := systemdDbus.NewWithContext(ctx)
711738
if err != nil {
712739
return &Manager{}, err
@@ -796,9 +823,9 @@ func LoadSystemd(slice, group string) (*Manager, error) {
796823
if slice == "" {
797824
slice = defaultSlice
798825
}
799-
group = filepath.Join(defaultCgroup2Path, slice, group)
826+
path := getSystemdFullPath(slice, group)
800827
return &Manager{
801-
path: group,
828+
path: path,
802829
}, nil
803830
}
804831

v2/manager_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/stretchr/testify/assert"
2526
"go.uber.org/goleak"
2627
)
2728

@@ -74,3 +75,67 @@ func TestEventChanCleanupOnCgroupRemoval(t *testing.T) {
7475
}
7576
goleak.VerifyNone(t)
7677
}
78+
79+
func TestSystemdFullPath(t *testing.T) {
80+
tests := []struct {
81+
inputSlice string
82+
inputGroup string
83+
expectedOut string
84+
}{
85+
{
86+
inputSlice: "user.slice",
87+
inputGroup: "myGroup.slice",
88+
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.slice",
89+
},
90+
{
91+
inputSlice: "/",
92+
inputGroup: "myGroup.slice",
93+
expectedOut: "/sys/fs/cgroup/myGroup.slice",
94+
},
95+
{
96+
inputSlice: "system.slice",
97+
inputGroup: "myGroup.slice",
98+
expectedOut: "/sys/fs/cgroup/system.slice/myGroup.slice",
99+
},
100+
{
101+
inputSlice: "user.slice",
102+
inputGroup: "my-group.slice",
103+
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice",
104+
},
105+
{
106+
inputSlice: "user.slice",
107+
inputGroup: "my-group-more-dashes.slice",
108+
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-more.slice/my-group-more-dashes.slice",
109+
},
110+
{
111+
inputSlice: "user.slice",
112+
inputGroup: "my-group-dashes.slice",
113+
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-dashes.slice",
114+
},
115+
{
116+
inputSlice: "user.slice",
117+
inputGroup: "myGroup.scope",
118+
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.scope",
119+
},
120+
{
121+
inputSlice: "user.slice",
122+
inputGroup: "my-group-dashes.scope",
123+
expectedOut: "/sys/fs/cgroup/user.slice/my-group-dashes.scope",
124+
},
125+
{
126+
inputSlice: "test-waldo.slice",
127+
inputGroup: "my-group.slice",
128+
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.slice/my-group.slice",
129+
},
130+
{
131+
inputSlice: "test-waldo.slice",
132+
inputGroup: "my.service",
133+
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.service",
134+
},
135+
}
136+
137+
for _, test := range tests {
138+
actual := getSystemdFullPath(test.inputSlice, test.inputGroup)
139+
assert.Equal(t, test.expectedOut, actual)
140+
}
141+
}

0 commit comments

Comments
 (0)