Skip to content

Commit

Permalink
check that StartTransientUnit succeeds
Browse files Browse the repository at this point in the history
Signed-off-by: lifubang <[email protected]>
  • Loading branch information
lifubang committed Apr 22, 2020
1 parent 46be7b6 commit a5d5d89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 20 additions & 0 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ import (
"github.com/opencontainers/runc/libcontainer/configs"
)

// Refer to https://github.com/coreos/go-systemd/blob/5a0db84d3dc459ccdc6ffcc44b1c452bf9f171cb/dbus/methods.go#L78-L101
// or https://godoc.org/github.com/coreos/go-systemd/dbus#Conn.StartUnit
// job completion: one of done, canceled, timeout, failed, dependency, skipped.
// done indicates successful execution of a job. canceled indicates that a job
// has been canceled before it finished execution. timeout indicates that the
// job timeout was reached. failed indicates that the job failed. dependency
// indicates that a job this job has been depending on failed and the job hence
// has been removed too. skipped indicates that a job was skipped because it
// didn't apply to the units current state.
type DbusJobCompletionResultType string

const (
DbusJobDone DbusJobCompletionResultType = "done"
DbusJobCanceled DbusJobCompletionResultType = "canceled"
DbusJobTimeout DbusJobCompletionResultType = "timeout"
DbusJobFailed DbusJobCompletionResultType = "failed"
DbusJobDependency DbusJobCompletionResultType = "dependency"
DbusJobSkipped DbusJobCompletionResultType = "skipped"
)

var (
connOnce sync.Once
connDbus *systemdDbus.Conn
Expand Down
20 changes: 18 additions & 2 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -139,7 +140,11 @@ func (m *unifiedManager) Apply(pid int) error {
statusChan := make(chan string, 1)
if _, err := dbusConnection.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
select {
case <-statusChan:
case s := <-statusChan:
if DbusJobCompletionResultType(s) != DbusJobDone {
dbusConnection.ResetFailedUnit(unitName)
return errors.Errorf("error creating systemd unit `%s`: got `%s`", unitName, s)
}
case <-time.After(time.Second):
logrus.Warnf("Timed out while waiting for StartTransientUnit(%s) completion signal from dbus. Continuing...", unitName)
}
Expand Down Expand Up @@ -168,7 +173,18 @@ func (m *unifiedManager) Destroy() error {
if err != nil {
return err
}
dbusConnection.StopUnit(getUnitName(m.cgroups), "replace", nil)
unitName := getUnitName(m.cgroups)
statusChan := make(chan string, 1)
if _, err := dbusConnection.StopUnit(unitName, "replace", statusChan); err == nil {
select {
case s := <-statusChan:
if DbusJobCompletionResultType(s) != DbusJobDone {
logrus.Warnf("error stopping unit `%s`: got `%s`. Continuing...", unitName, s)
}
case <-time.After(time.Second):
logrus.Warnf("Timed out while waiting for StopUnit(%s) completion signal from dbus. Continuing...", unitName)
}
}

// XXX this is probably not needed, systemd should handle it
err = os.Remove(m.path)
Expand Down

0 comments on commit a5d5d89

Please sign in to comment.