Skip to content

Commit 4104367

Browse files
odinugekolyshkin
authored andcommitted
libct/cg/sd/v1: Fix unnecessary freeze/thaw
This fixes the behavior intended to avoid freezing containers/control groups without it being necessary. This is important for end users of libcontainer who rely on the behavior of no freeze. The previous implementation would always get error trying to get DevicePolicy from the Unit via dbus, since the Unit interface doesn't contain DevicePolicy. Signed-off-by: Odin Ugedal <[email protected]>
1 parent 4d26c4a commit 4104367

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

libcontainer/cgroups/systemd/common.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ func getUnitName(c *configs.Cgroup) string {
311311
return c.Name
312312
}
313313

314+
// This code should be in sync with getUnitName.
315+
func getUnitType(unitName string) string {
316+
if strings.HasSuffix(unitName, ".slice") {
317+
return "Slice"
318+
}
319+
return "Scope"
320+
}
321+
314322
// isDbusError returns true if the error is a specific dbus error.
315323
func isDbusError(err error, name string) bool {
316324
if err != nil {
@@ -389,10 +397,10 @@ func resetFailedUnit(cm *dbusConnManager, name string) {
389397
}
390398
}
391399

392-
func getUnitProperty(cm *dbusConnManager, unitName string, propertyName string) (*systemdDbus.Property, error) {
400+
func getUnitTypeProperty(cm *dbusConnManager, unitName string, unitType string, propertyName string) (*systemdDbus.Property, error) {
393401
var prop *systemdDbus.Property
394402
err := cm.retryOnDisconnect(func(c *systemdDbus.Conn) (Err error) {
395-
prop, Err = c.GetUnitPropertyContext(context.TODO(), unitName, propertyName)
403+
prop, Err = c.GetUnitTypePropertyContext(context.TODO(), unitName, unitType, propertyName)
396404
return Err
397405
})
398406
return prop, err

libcontainer/cgroups/systemd/systemd_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ func TestSystemdVersion(t *testing.T) {
4040
}
4141
}
4242

43+
func TestValidUnitTypes(t *testing.T) {
44+
testCases := []struct {
45+
unitName string
46+
expectedUnitType string
47+
}{
48+
{"system.slice", "Slice"},
49+
{"kubepods.slice", "Slice"},
50+
{"testing-container:ab.scope", "Scope"},
51+
}
52+
for _, sdTest := range testCases {
53+
unitType := getUnitType(sdTest.unitName)
54+
if unitType != sdTest.expectedUnitType {
55+
t.Errorf("getUnitType(%s); want %q; got %q", sdTest.unitName, sdTest.expectedUnitType, unitType)
56+
}
57+
}
58+
}
59+
4360
func newManager(config *configs.Cgroup) cgroups.Manager {
4461
if cgroups.IsCgroup2UnifiedMode() {
4562
return NewUnifiedManager(config, "", false)

libcontainer/cgroups/systemd/v1.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"os"
88
"path/filepath"
9+
"reflect"
910
"strings"
1011
"sync"
1112

@@ -353,15 +354,20 @@ func (m *legacyManager) freezeBeforeSet(unitName string, r *configs.Resources) (
353354
// a non-existent unit returns default properties,
354355
// and settings in (2) are the defaults.
355356
//
356-
// Do not return errors from getUnitProperty, as they alone
357+
// Do not return errors from getUnitTypeProperty, as they alone
357358
// should not prevent Set from working.
358-
devPolicy, e := getUnitProperty(m.dbus, unitName, "DevicePolicy")
359+
360+
unitType := getUnitType(unitName)
361+
362+
devPolicy, e := getUnitTypeProperty(m.dbus, unitName, unitType, "DevicePolicy")
359363
if e == nil && devPolicy.Value == dbus.MakeVariant("auto") {
360-
devAllow, e := getUnitProperty(m.dbus, unitName, "DeviceAllow")
361-
if e == nil && devAllow.Value == dbus.MakeVariant([]deviceAllowEntry{}) {
362-
needsFreeze = false
363-
needsThaw = false
364-
return
364+
devAllow, e := getUnitTypeProperty(m.dbus, unitName, unitType, "DeviceAllow")
365+
if e == nil {
366+
if rv := reflect.ValueOf(devAllow.Value.Value()); rv.Kind() == reflect.Slice && rv.Len() == 0 {
367+
needsFreeze = false
368+
needsThaw = false
369+
return
370+
}
365371
}
366372
}
367373
}

0 commit comments

Comments
 (0)