Skip to content

Commit

Permalink
libct/cg/sd: add dbus manager
Browse files Browse the repository at this point in the history
[@kolyshkin: documentation nits]

Signed-off-by: Shiming Zhang <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
wzshiming authored and kolyshkin committed Apr 27, 2021
1 parent 42a18e7 commit cdbed6f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
18 changes: 0 additions & 18 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package systemd

import (
"bufio"
"context"
"fmt"
"math"
"os"
Expand All @@ -29,10 +28,6 @@ const (
)

var (
connOnce sync.Once
connDbus *systemdDbus.Conn
connErr error

versionOnce sync.Once
version int

Expand Down Expand Up @@ -292,19 +287,6 @@ func generateDeviceProperties(rules []*devices.Rule) ([]systemdDbus.Property, er
return properties, nil
}

// getDbusConnection lazy initializes systemd dbus connection
// and returns it
func getDbusConnection(rootless bool) (*systemdDbus.Conn, error) {
connOnce.Do(func() {
if rootless {
connDbus, connErr = NewUserSystemdDbus()
} else {
connDbus, connErr = systemdDbus.NewWithContext(context.TODO())
}
})
return connDbus, connErr
}

func newProp(name string, units interface{}) systemdDbus.Property {
return systemdDbus.Property{
Name: name,
Expand Down
59 changes: 59 additions & 0 deletions libcontainer/cgroups/systemd/dbus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// +build linux

package systemd

import (
"context"
"sync"

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
)

type dbusConnManager struct {
conn *systemdDbus.Conn
rootless bool
sync.RWMutex
}

// newDbusConnManager initializes systemd dbus connection manager.
func newDbusConnManager(rootless bool) *dbusConnManager {
return &dbusConnManager{
rootless: rootless,
}
}

// getConnection lazily initializes and returns systemd dbus connection.
func (d *dbusConnManager) getConnection() (*systemdDbus.Conn, error) {
// In the case where d.conn != nil
// Use the read lock the first time to ensure
// that Conn can be acquired at the same time.
d.RLock()
if conn := d.conn; conn != nil {
d.RUnlock()
return conn, nil
}
d.RUnlock()

// In the case where d.conn == nil
// Use write lock to ensure that only one
// will be created
d.Lock()
defer d.Unlock()
if conn := d.conn; conn != nil {
return conn, nil
}

conn, err := d.newConnection()
if err != nil {
return nil, err
}
d.conn = conn
return conn, nil
}

func (d *dbusConnManager) newConnection() (*systemdDbus.Conn, error) {
if d.rootless {
return NewUserSystemdDbus()
}
return systemdDbus.NewWithContext(context.TODO())
}
8 changes: 5 additions & 3 deletions libcontainer/cgroups/systemd/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ type legacyManager struct {
mu sync.Mutex
cgroups *configs.Cgroup
paths map[string]string
dbus *dbusConnManager
}

func NewLegacyManager(cg *configs.Cgroup, paths map[string]string) cgroups.Manager {
return &legacyManager{
cgroups: cg,
paths: paths,
dbus: newDbusConnManager(false),
}
}

Expand Down Expand Up @@ -164,7 +166,7 @@ func (m *legacyManager) Apply(pid int) error {
properties = append(properties,
newProp("DefaultDependencies", false))

dbusConnection, err := getDbusConnection(false)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand Down Expand Up @@ -208,7 +210,7 @@ func (m *legacyManager) Destroy() error {
m.mu.Lock()
defer m.mu.Unlock()

dbusConnection, err := getDbusConnection(false)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand Down Expand Up @@ -341,7 +343,7 @@ func (m *legacyManager) Set(container *configs.Config) error {
if container.Cgroups.Resources.Unified != nil {
return cgroups.ErrV1NoUnified
}
dbusConnection, err := getDbusConnection(false)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ type unifiedManager struct {
// path is like "/sys/fs/cgroup/user.slice/user-1001.slice/session-1.scope"
path string
rootless bool
dbus *dbusConnManager
}

func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) cgroups.Manager {
return &unifiedManager{
cgroups: config,
path: path,
rootless: rootless,
dbus: newDbusConnManager(rootless),
}
}

Expand Down Expand Up @@ -279,7 +281,7 @@ func (m *unifiedManager) Apply(pid int) error {
properties = append(properties,
newProp("DefaultDependencies", false))

dbusConnection, err := getDbusConnection(m.rootless)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand All @@ -305,7 +307,7 @@ func (m *unifiedManager) Destroy() error {
m.mu.Lock()
defer m.mu.Unlock()

dbusConnection, err := getDbusConnection(m.rootless)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand Down Expand Up @@ -344,7 +346,7 @@ func (m *unifiedManager) getSliceFull() (string, error) {
}

if m.rootless {
dbusConnection, err := getDbusConnection(m.rootless)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -427,7 +429,7 @@ func (m *unifiedManager) GetStats() (*cgroups.Stats, error) {
}

func (m *unifiedManager) Set(container *configs.Config) error {
dbusConnection, err := getDbusConnection(m.rootless)
dbusConnection, err := m.dbus.getConnection()
if err != nil {
return err
}
Expand Down

0 comments on commit cdbed6f

Please sign in to comment.