1616package dbus
1717
1818import (
19+ "context"
1920 "encoding/hex"
2021 "fmt"
2122 "os"
@@ -112,39 +113,63 @@ type Conn struct {
112113
113114// New establishes a connection to any available bus and authenticates.
114115// Callers should call Close() when done with the connection.
116+ // Deprecated: use NewWithContext instead
115117func New () (* Conn , error ) {
116- conn , err := NewSystemConnection ()
118+ return NewWithContext (context .Background ())
119+ }
120+
121+ // NewWithContext same as New with context
122+ func NewWithContext (ctx context.Context ) (* Conn , error ) {
123+ conn , err := NewSystemConnectionContext (ctx )
117124 if err != nil && os .Geteuid () == 0 {
118- return NewSystemdConnection ( )
125+ return NewSystemdConnectionContext ( ctx )
119126 }
120127 return conn , err
121128}
122129
123130// NewSystemConnection establishes a connection to the system bus and authenticates.
124131// Callers should call Close() when done with the connection
132+ // Deprecated: use NewSystemConnectionContext instead
125133func NewSystemConnection () (* Conn , error ) {
134+ return NewSystemConnectionContext (context .Background ())
135+ }
136+
137+ // NewSystemConnectionContext same as NewSystemConnection with context
138+ func NewSystemConnectionContext (ctx context.Context ) (* Conn , error ) {
126139 return NewConnection (func () (* dbus.Conn , error ) {
127- return dbusAuthHelloConnection (dbus .SystemBusPrivate )
140+ return dbusAuthHelloConnection (ctx , dbus .SystemBusPrivate )
128141 })
129142}
130143
131144// NewUserConnection establishes a connection to the session bus and
132145// authenticates. This can be used to connect to systemd user instances.
133146// Callers should call Close() when done with the connection.
147+ // Deprecated: use NewUserConnectionContext instead
134148func NewUserConnection () (* Conn , error ) {
149+ return NewUserConnectionContext (context .Background ())
150+ }
151+
152+ // NewUserConnectionContext same as NewUserConnection with context
153+ func NewUserConnectionContext (ctx context.Context ) (* Conn , error ) {
135154 return NewConnection (func () (* dbus.Conn , error ) {
136- return dbusAuthHelloConnection (dbus .SessionBusPrivate )
155+ return dbusAuthHelloConnection (ctx , dbus .SessionBusPrivate )
137156 })
138157}
139158
140159// NewSystemdConnection establishes a private, direct connection to systemd.
141160// This can be used for communicating with systemd without a dbus daemon.
142161// Callers should call Close() when done with the connection.
162+ // Deprecated: use NewSystemdConnectionContext instead
143163func NewSystemdConnection () (* Conn , error ) {
164+ return NewSystemdConnectionContext (context .Background ())
165+ }
166+
167+ // NewSystemdConnectionContext same as NewSystemdConnection with context
168+ func NewSystemdConnectionContext (ctx context.Context ) (* Conn , error ) {
144169 return NewConnection (func () (* dbus.Conn , error ) {
145170 // We skip Hello when talking directly to systemd.
146- return dbusAuthConnection (func (opts ... dbus.ConnOption ) (* dbus.Conn , error ) {
147- return dbus .Dial ("unix:path=/run/systemd/private" )
171+ return dbusAuthConnection (ctx , func (opts ... dbus.ConnOption ) (* dbus.Conn , error ) {
172+ return dbus .Dial ("unix:path=/run/systemd/private" , opts ... )
148173 })
149174 })
150175}
@@ -201,8 +226,8 @@ func (c *Conn) GetManagerProperty(prop string) (string, error) {
201226 return variant .String (), nil
202227}
203228
204- func dbusAuthConnection (createBus func (opts ... dbus.ConnOption ) (* dbus.Conn , error )) (* dbus.Conn , error ) {
205- conn , err := createBus ()
229+ func dbusAuthConnection (ctx context. Context , createBus func (opts ... dbus.ConnOption ) (* dbus.Conn , error )) (* dbus.Conn , error ) {
230+ conn , err := createBus (dbus . WithContext ( ctx ) )
206231 if err != nil {
207232 return nil , err
208233 }
@@ -221,8 +246,8 @@ func dbusAuthConnection(createBus func(opts ...dbus.ConnOption) (*dbus.Conn, err
221246 return conn , nil
222247}
223248
224- func dbusAuthHelloConnection (createBus func (opts ... dbus.ConnOption ) (* dbus.Conn , error )) (* dbus.Conn , error ) {
225- conn , err := dbusAuthConnection (createBus )
249+ func dbusAuthHelloConnection (ctx context. Context , createBus func (opts ... dbus.ConnOption ) (* dbus.Conn , error )) (* dbus.Conn , error ) {
250+ conn , err := dbusAuthConnection (ctx , createBus )
226251 if err != nil {
227252 return nil , err
228253 }
0 commit comments