Skip to content

Commit

Permalink
Refactor systemd connection initialization
Browse files Browse the repository at this point in the history
1. Use the simpler IsRunningSystemd for checking
if systemd is running.
2. Lazy initialization the systemd dbus connection
in the systemd package.

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
  • Loading branch information
mrunalp committed Jan 14, 2020
1 parent b66df41 commit 610df46
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
41 changes: 21 additions & 20 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ const (
)

var (
connLock sync.Mutex
theConn *systemdDbus.Conn
connOnce sync.Once
connDbus *systemdDbus.Conn
connErr error
)

func newProp(name string, units interface{}) systemdDbus.Property {
Expand All @@ -97,22 +98,13 @@ func IsRunningSystemd() bool {
return fi.IsDir()
}

func UseSystemd() bool {
if !IsRunningSystemd() {
return false
}

connLock.Lock()
defer connLock.Unlock()

if theConn == nil {
var err error
theConn, err = systemdDbus.New()
if err != nil {
return false
}
}
return true
// getDbusConnection lazy initializes systemd dbus connection
// and returns it
func getDbusConnection() (*systemdDbus.Conn, error) {
connOnce.Do(func() {
connDbus, connErr = systemdDbus.New()
})
return connDbus, connErr
}

func NewSystemdCgroupsManager() (func(config *configs.Cgroup, paths map[string]string) cgroups.Manager, error) {
Expand Down Expand Up @@ -245,8 +237,13 @@ func (m *LegacyManager) Apply(pid int) error {
}
}

dbusConnection, err := getDbusConnection()
if err != nil {
return err
}

statusChan := make(chan string, 1)
if _, err := theConn.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
if _, err := dbusConnection.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
select {
case <-statusChan:
case <-time.After(time.Second):
Expand Down Expand Up @@ -280,9 +277,13 @@ func (m *LegacyManager) Destroy() error {
if m.Cgroups.Paths != nil {
return nil
}
dbusConnection, err := getDbusConnection()
if err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
theConn.StopUnit(getUnitName(m.Cgroups), "replace", nil)
dbusConnection.StopUnit(getUnitName(m.Cgroups), "replace", nil)
if err := cgroups.RemovePaths(m.Paths); err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions libcontainer/cgroups/systemd/unified_hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,13 @@ func (m *UnifiedManager) Apply(pid int) error {
}
}

dbusConnection, err := getDbusConnection()
if err != nil {
return err
}

statusChan := make(chan string, 1)
if _, err := theConn.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
if _, err := dbusConnection.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
select {
case <-statusChan:
case <-time.After(time.Second):
Expand Down Expand Up @@ -171,9 +176,13 @@ func (m *UnifiedManager) Destroy() error {
if m.Cgroups.Paths != nil {
return nil
}
dbusConnection, err := getDbusConnection()
if err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
theConn.StopUnit(getUnitName(m.Cgroups), "replace", nil)
dbusConnection.StopUnit(getUnitName(m.Cgroups), "replace", nil)
if err := cgroups.RemovePaths(m.Paths); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func TestFreeze(t *testing.T) {
}

func TestSystemdFreeze(t *testing.T) {
if !systemd.UseSystemd() {
if !systemd.IsRunningSystemd() {
t.Skip("Systemd is unsupported")
}
testFreeze(t, true)
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestCpuShares(t *testing.T) {
}

func TestCpuSharesSystemd(t *testing.T) {
if !systemd.UseSystemd() {
if !systemd.IsRunningSystemd() {
t.Skip("Systemd is unsupported")
}
testCpuShares(t, true)
Expand Down Expand Up @@ -598,7 +598,7 @@ func TestPids(t *testing.T) {
}

func TestPidsSystemd(t *testing.T) {
if !systemd.UseSystemd() {
if !systemd.IsRunningSystemd() {
t.Skip("Systemd is unsupported")
}
testPids(t, true)
Expand Down Expand Up @@ -684,7 +684,7 @@ func TestRunWithKernelMemory(t *testing.T) {
}

func TestRunWithKernelMemorySystemd(t *testing.T) {
if !systemd.UseSystemd() {
if !systemd.IsRunningSystemd() {
t.Skip("Systemd is unsupported")
}
testRunWithKernelMemory(t, true)
Expand Down
2 changes: 1 addition & 1 deletion utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
cgroupManager = libcontainer.RootlessCgroupfs
}
if context.GlobalBool("systemd-cgroup") {
if systemd.UseSystemd() {
if systemd.IsRunningSystemd() {
cgroupManager = libcontainer.SystemdCgroups
} else {
return nil, fmt.Errorf("systemd cgroup flag passed, but systemd support for managing cgroups is not available")
Expand Down

0 comments on commit 610df46

Please sign in to comment.