forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the dbus-connection code from libpod's healthcheck to pkg/systemd to allow for sharing the logic. Needed for the auto-updates work. Signed-off-by: Valentin Rothberg <[email protected]>
- Loading branch information
Showing
2 changed files
with
51 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |