Skip to content

Commit 59c60d1

Browse files
Merge pull request #1776 from filbranden/systemd2
Detect whether Delegate is available on both slices and scopes
2 parents 3cbb2fa + 0e16bd9 commit 59c60d1

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 32 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,22 @@ 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+
// Starting with systemd v237, Delegate is not even a property of slices anymore,
200+
// so the D-Bus call fails with "InvalidArgs" error.
201+
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") || strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.InvalidArgs") {
202+
hasDelegateSlice = false
203+
}
204+
}
205+
}
206+
190207
// Not critical because of the stop unit logic above.
191208
theConn.StopUnit(scope, "replace", nil)
192209
theConn.StopUnit(slice, "replace", nil)
@@ -242,9 +259,16 @@ func (m *Manager) Apply(pid int) error {
242259
properties = append(properties, newProp("PIDs", []uint32{uint32(pid)}))
243260
}
244261

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

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

0 commit comments

Comments
 (0)