|
75 | 75 | hasStartTransientUnit bool |
76 | 76 | hasStartTransientSliceUnit bool |
77 | 77 | hasTransientDefaultDependencies bool |
78 | | - hasDelegate bool |
| 78 | + hasDelegateScope bool |
| 79 | + hasDelegateSlice bool |
79 | 80 | ) |
80 | 81 |
|
81 | 82 | func newProp(name string, units interface{}) systemdDbus.Property { |
@@ -150,12 +151,12 @@ func UseSystemd() bool { |
150 | 151 | theConn.StopUnit(scope, "replace", nil) |
151 | 152 |
|
152 | 153 | // 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 { |
156 | 157 | if dbusError, ok := err.(dbus.Error); ok { |
157 | 158 | if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") { |
158 | | - hasDelegate = false |
| 159 | + hasDelegateScope = false |
159 | 160 | } |
160 | 161 | } |
161 | 162 | } |
@@ -187,6 +188,22 @@ func UseSystemd() bool { |
187 | 188 | time.Sleep(time.Millisecond) |
188 | 189 | } |
189 | 190 |
|
| 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 | + |
190 | 207 | // Not critical because of the stop unit logic above. |
191 | 208 | theConn.StopUnit(scope, "replace", nil) |
192 | 209 | theConn.StopUnit(slice, "replace", nil) |
@@ -242,9 +259,16 @@ func (m *Manager) Apply(pid int) error { |
242 | 259 | properties = append(properties, newProp("PIDs", []uint32{uint32(pid)})) |
243 | 260 | } |
244 | 261 |
|
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 | + } |
248 | 272 | } |
249 | 273 |
|
250 | 274 | // Always enable accounting, this gets us the same behaviour as the fs implementation, |
|
0 commit comments