Skip to content

Commit 0dbfc16

Browse files
edef1cjonboulle
authored andcommitted
dbus: expose NewConnection method (#153)
By providing a callback to NewConnection, users can now initialise a session over a transport of their choosing
1 parent 1d9051f commit 0dbfc16

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

dbus/dbus.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type Conn struct {
8686
// New establishes a connection to the system bus and authenticates.
8787
// Callers should call Close() when done with the connection.
8888
func New() (*Conn, error) {
89-
return newConnection(func() (*dbus.Conn, error) {
89+
return NewConnection(func() (*dbus.Conn, error) {
9090
return dbusAuthHelloConnection(dbus.SystemBusPrivate)
9191
})
9292
}
@@ -95,7 +95,7 @@ func New() (*Conn, error) {
9595
// authenticates. This can be used to connect to systemd user instances.
9696
// Callers should call Close() when done with the connection.
9797
func NewUserConnection() (*Conn, error) {
98-
return newConnection(func() (*dbus.Conn, error) {
98+
return NewConnection(func() (*dbus.Conn, error) {
9999
return dbusAuthHelloConnection(dbus.SessionBusPrivate)
100100
})
101101
}
@@ -104,7 +104,7 @@ func NewUserConnection() (*Conn, error) {
104104
// This can be used for communicating with systemd without a dbus daemon.
105105
// Callers should call Close() when done with the connection.
106106
func NewSystemdConnection() (*Conn, error) {
107-
return newConnection(func() (*dbus.Conn, error) {
107+
return NewConnection(func() (*dbus.Conn, error) {
108108
// We skip Hello when talking directly to systemd.
109109
return dbusAuthConnection(func() (*dbus.Conn, error) {
110110
return dbus.Dial("unix:path=/run/systemd/private")
@@ -118,13 +118,18 @@ func (c *Conn) Close() {
118118
c.sigconn.Close()
119119
}
120120

121-
func newConnection(createBus func() (*dbus.Conn, error)) (*Conn, error) {
122-
sysconn, err := createBus()
121+
// NewConnection establishes a connection to a bus using a caller-supplied function.
122+
// This allows connecting to remote buses through a user-supplied mechanism.
123+
// The supplied function may be called multiple times, and should return independent connections.
124+
// The returned connection must be fully initialised: the org.freedesktop.DBus.Hello call must have succeeded,
125+
// and any authentication should be handled by the function.
126+
func NewConnection(dialBus func() (*dbus.Conn, error)) (*Conn, error) {
127+
sysconn, err := dialBus()
123128
if err != nil {
124129
return nil, err
125130
}
126131

127-
sigconn, err := createBus()
132+
sigconn, err := dialBus()
128133
if err != nil {
129134
sysconn.Close()
130135
return nil, err

0 commit comments

Comments
 (0)