Skip to content

Commit

Permalink
Fix systemd full path
Browse files Browse the repository at this point in the history
Signed-off-by: Cameron Sparr <sparrc@users.noreply.github.com>
  • Loading branch information
sparrc committed Feb 18, 2022
1 parent cf1b326 commit aa8003c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
33 changes: 30 additions & 3 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,39 @@ func setDevices(path string, devices []specs.LinuxDeviceCgroup) error {
return nil
}

// getSystemdFullPath returns the full systemd path when creating a systemd slice group.
// the reason this is necessary is because the "-" character has a special meaning in
// systemd slice. For example, when creating a slice called "my-group-112233.slice",
// systemd will create a hierarchy like this:
// /sys/fs/cgroup/my.slice/my-group.slice/my-group-112233.slice
func getSystemdFullPath(slice, group string) string {
return filepath.Join(defaultCgroup2Path, dashesToPath(slice), dashesToPath(group))
}

// dashesToPath converts a slice name with dashes to it's corresponding systemd filesystem path.
func dashesToPath(in string) string {
path := ""
if strings.HasSuffix(in, ".slice") && strings.Contains(in, "-") {
parts := strings.Split(in, "-")
for i := range parts {
s := strings.Join(parts[0:i+1], "-")
if !strings.HasSuffix(s, ".slice") {
s += ".slice"
}
path = filepath.Join(path, s)
}
} else {
path = filepath.Join(path, in)
}
return path
}

func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, error) {
if slice == "" {
slice = defaultSlice
}
ctx := context.TODO()
path := filepath.Join(defaultCgroup2Path, slice, group)
path := getSystemdFullPath(slice, group)
conn, err := systemdDbus.NewWithContext(ctx)
if err != nil {
return &Manager{}, err
Expand Down Expand Up @@ -796,9 +823,9 @@ func LoadSystemd(slice, group string) (*Manager, error) {
if slice == "" {
slice = defaultSlice
}
group = filepath.Join(defaultCgroup2Path, slice, group)
path := getSystemdFullPath(slice, group)
return &Manager{
path: group,
path: path,
}, nil
}

Expand Down
65 changes: 65 additions & 0 deletions v2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)

Expand Down Expand Up @@ -74,3 +75,67 @@ func TestEventChanCleanupOnCgroupRemoval(t *testing.T) {
}
goleak.VerifyNone(t)
}

func TestSystemdFullPath(t *testing.T) {
tests := []struct {
inputSlice string
inputGroup string
expectedOut string
}{
{
inputSlice: "user.slice",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.slice",
},
{
inputSlice: "/",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/myGroup.slice",
},
{
inputSlice: "system.slice",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/system.slice/myGroup.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-more-dashes.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-more.slice/my-group-more-dashes.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-dashes.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-dashes.slice",
},
{
inputSlice: "user.slice",
inputGroup: "myGroup.scope",
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.scope",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-dashes.scope",
expectedOut: "/sys/fs/cgroup/user.slice/my-group-dashes.scope",
},
{
inputSlice: "test-waldo.slice",
inputGroup: "my-group.slice",
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.slice/my-group.slice",
},
{
inputSlice: "test-waldo.slice",
inputGroup: "my.service",
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.service",
},
}

for _, test := range tests {
actual := getSystemdFullPath(test.inputSlice, test.inputGroup)
assert.Equal(t, test.expectedOut, actual)
}
}

0 comments on commit aa8003c

Please sign in to comment.