Skip to content

Commit 4d8d27c

Browse files
committed
Detect whether Delegate is available on both slices and scopes
Starting with systemd 237, in preparation for cgroup v2, delegation is only now available for scopes, not slices. Update libcontainer code to detect whether delegation is available on both and use that information when creating new slices. Signed-off-by: Filipe Brandenburger <[email protected]>
1 parent cc4307a commit 4d8d27c

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ var (
7575
hasStartTransientUnit bool
7676
hasStartTransientSliceUnit bool
7777
hasTransientDefaultDependencies bool
78-
hasDelegate bool
78+
hasDelegateScope bool
79+
hasDelegateSlice bool
7980
)
8081

8182
func newProp(name string, units interface{}) systemdDbus.Property {
@@ -150,12 +151,12 @@ func UseSystemd() bool {
150151
theConn.StopUnit(scope, "replace", nil)
151152

152153
// Assume StartTransientUnit on a scope allows Delegate
153-
hasDelegate = true
154-
dl := newProp("Delegate", true)
155-
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{dl}, nil); err != nil {
154+
hasDelegateScope = true
155+
dlScope := newProp("Delegate", true)
156+
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{dlScope}, nil); err != nil {
156157
if dbusError, ok := err.(dbus.Error); ok {
157158
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
158-
hasDelegate = false
159+
hasDelegateScope = false
159160
}
160161
}
161162
}
@@ -187,6 +188,20 @@ func UseSystemd() bool {
187188
time.Sleep(time.Millisecond)
188189
}
189190

191+
// Not critical because of the stop unit logic above.
192+
theConn.StopUnit(slice, "replace", nil)
193+
194+
// Assume StartTransientUnit on a slice allows Delegate
195+
hasDelegateSlice = true
196+
dlSlice := newProp("Delegate", true)
197+
if _, err := theConn.StartTransientUnit(slice, "replace", []systemdDbus.Property{dlSlice}, nil); err != nil {
198+
if dbusError, ok := err.(dbus.Error); ok {
199+
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
200+
hasDelegateSlice = false
201+
}
202+
}
203+
}
204+
190205
// Not critical because of the stop unit logic above.
191206
theConn.StopUnit(scope, "replace", nil)
192207
theConn.StopUnit(slice, "replace", nil)
@@ -242,9 +257,16 @@ func (m *Manager) Apply(pid int) error {
242257
properties = append(properties, newProp("PIDs", []uint32{uint32(pid)}))
243258
}
244259

245-
if hasDelegate {
246-
// This is only supported on systemd versions 218 and above.
247-
properties = append(properties, newProp("Delegate", true))
260+
// Check if we can delegate. This is only supported on systemd versions 218 and above.
261+
if strings.HasSuffix(unitName, ".slice") {
262+
if hasDelegateSlice {
263+
// systemd 237 and above no longer allows delegation on a slice
264+
properties = append(properties, newProp("Delegate", true))
265+
}
266+
} else {
267+
if hasDelegateScope {
268+
properties = append(properties, newProp("Delegate", true))
269+
}
248270
}
249271

250272
// Always enable accounting, this gets us the same behaviour as the fs implementation,

0 commit comments

Comments
 (0)