summaryrefslogtreecommitdiff
path: root/pkg/systemd/dbus.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-03-18 18:27:33 +0100
committerGitHub <noreply@github.com>2020-03-18 18:27:33 +0100
commit45e7cbfef65d0379af19264c5fa90e1ae9ccb74a (patch)
tree213fbf640875883d598cff217b933b3c413c5707 /pkg/systemd/dbus.go
parentd9eb078e2a1cff73461f285924ab1ab8699e9bca (diff)
parentf4e873c4e10502dd0a7fb14cc2fd87b12760a318 (diff)
downloadpodman-45e7cbfef65d0379af19264c5fa90e1ae9ccb74a.tar.gz
podman-45e7cbfef65d0379af19264c5fa90e1ae9ccb74a.tar.bz2
podman-45e7cbfef65d0379af19264c5fa90e1ae9ccb74a.zip
Merge pull request #5480 from vrothberg/auto-updates
auto update containers in systemd units
Diffstat (limited to 'pkg/systemd/dbus.go')
-rw-r--r--pkg/systemd/dbus.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/pkg/systemd/dbus.go b/pkg/systemd/dbus.go
new file mode 100644
index 000000000..df24667a1
--- /dev/null
+++ b/pkg/systemd/dbus.go
@@ -0,0 +1,47 @@
+package systemd
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strconv"
+
+ "github.com/containers/libpod/pkg/rootless"
+ "github.com/coreos/go-systemd/v22/dbus"
+ godbus "github.com/godbus/dbus/v5"
+)
+
+func dbusAuthRootlessConnection(createBus func(opts ...godbus.ConnOption) (*godbus.Conn, error)) (*godbus.Conn, error) {
+ conn, err := createBus()
+ if err != nil {
+ return nil, err
+ }
+
+ methods := []godbus.Auth{godbus.AuthExternal(strconv.Itoa(rootless.GetRootlessUID()))}
+
+ err = conn.Auth(methods)
+ if err != nil {
+ conn.Close()
+ return nil, err
+ }
+
+ return conn, nil
+}
+
+func newRootlessConnection() (*dbus.Conn, error) {
+ return dbus.NewConnection(func() (*godbus.Conn, error) {
+ return dbusAuthRootlessConnection(func(opts ...godbus.ConnOption) (*godbus.Conn, error) {
+ path := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "systemd/private")
+ return godbus.Dial(fmt.Sprintf("unix:path=%s", path))
+ })
+ })
+}
+
+// ConnectToDBUS returns a DBUS connection. It works both as root and non-root
+// users.
+func ConnectToDBUS() (*dbus.Conn, error) {
+ if rootless.IsRootless() {
+ return newRootlessConnection()
+ }
+ return dbus.NewSystemdConnection()
+}