Skip to content

Commit

Permalink
libct/cg/sd: add renew dbus connection
Browse files Browse the repository at this point in the history
[@kolyshkin: doc nits, use dbus.ErrClosed and isDbusError]

Signed-off-by: Shiming Zhang <wzshiming@foxmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
wzshiming authored and kolyshkin committed Apr 27, 2021
1 parent bacfc2c commit 15fee98
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions libcontainer/cgroups/systemd/dbus.go
Expand Up @@ -7,6 +7,8 @@ import (
"sync"

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
dbus "github.com/godbus/dbus/v5"
"github.com/sirupsen/logrus"
)

type dbusConnManager struct {
Expand Down Expand Up @@ -57,3 +59,31 @@ func (d *dbusConnManager) newConnection() (*systemdDbus.Conn, error) {
}
return systemdDbus.NewWithContext(context.TODO())
}

// resetConnection resets the connection to its initial state
// (so it can be reconnected if necessary).
func (d *dbusConnManager) resetConnection(conn *systemdDbus.Conn) {
d.Lock()
defer d.Unlock()
if d.conn != nil && d.conn == conn {
d.conn.Close()
d.conn = nil
}
}

var errDbusConnClosed = dbus.ErrClosed.Error()

// checkAndReconnect checks if the connection is disconnected,
// and tries reconnect if it is.
func (d *dbusConnManager) checkAndReconnect(conn *systemdDbus.Conn, err error) {
if !isDbusError(err, errDbusConnClosed) {
return
}
d.resetConnection(conn)

// Try to reconnect
_, err = d.getConnection()
if err != nil {
logrus.Warnf("Dbus disconnected and failed to reconnect: %s", err)
}
}
2 changes: 2 additions & 0 deletions libcontainer/cgroups/systemd/v1.go
Expand Up @@ -173,6 +173,7 @@ func (m *legacyManager) Apply(pid int) error {
properties = append(properties, c.SystemdProps...)

if err := startUnit(dbusConnection, unitName, properties); err != nil {
m.dbus.checkAndReconnect(dbusConnection, err)
return err
}

Expand Down Expand Up @@ -376,6 +377,7 @@ func (m *legacyManager) Set(container *configs.Config) error {
}

if err := dbusConnection.SetUnitProperties(getUnitName(container.Cgroups), true, properties...); err != nil {
m.dbus.checkAndReconnect(dbusConnection, err)
_ = m.Freeze(targetFreezerState)
return err
}
Expand Down
2 changes: 2 additions & 0 deletions libcontainer/cgroups/systemd/v2.go
Expand Up @@ -288,6 +288,7 @@ func (m *unifiedManager) Apply(pid int) error {
properties = append(properties, c.SystemdProps...)

if err := startUnit(dbusConnection, unitName, properties); err != nil {
m.dbus.checkAndReconnect(dbusConnection, err)
return errors.Wrapf(err, "error while starting unit %q with properties %+v", unitName, properties)
}

Expand Down Expand Up @@ -462,6 +463,7 @@ func (m *unifiedManager) Set(container *configs.Config) error {
}

if err := dbusConnection.SetUnitProperties(getUnitName(m.cgroups), true, properties...); err != nil {
m.dbus.checkAndReconnect(dbusConnection, err)
_ = m.Freeze(targetFreezerState)
return errors.Wrap(err, "error while setting unit properties")
}
Expand Down

0 comments on commit 15fee98

Please sign in to comment.