Skip to content

Commit

Permalink
conn: add new method SessionBusPrivateNoAutoStartup
Browse files Browse the repository at this point in the history
SessionBusPrivate automatically launches dbus-launch when the other
detection mechanisms fail.  SessionBusPrivateNoAutoStartup returns an
error without executing a new process, so that it is not leaked.

Closes: #268

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe authored and jsouthworth committed Oct 26, 2021
1 parent 42f42fa commit 2ece603
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,34 @@ func SessionBus() (conn *Conn, err error) {
return
}

func getSessionBusAddress() (string, error) {
func getSessionBusAddress(autolaunch bool) (string, error) {
if address := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); address != "" && address != "autolaunch:" {
return address, nil

} else if address := tryDiscoverDbusSessionBusAddress(); address != "" {
os.Setenv("DBUS_SESSION_BUS_ADDRESS", address)
return address, nil
}
if !autolaunch {
return "", errors.New("dbus: couldn't determine address of session bus")
}
return getSessionBusPlatformAddress()
}

// SessionBusPrivate returns a new private connection to the session bus.
func SessionBusPrivate(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress()
address, err := getSessionBusAddress(true)
if err != nil {
return nil, err
}

return Dial(address, opts...)
}

// SessionBusPrivate returns a new private connection to the session bus. If
// the session bus is not already open, do not attempt to launch it.
func SessionBusPrivateNoAutoStartup(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress(false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +135,7 @@ func SystemBus() (conn *Conn, err error) {

// ConnectSessionBus connects to the session bus.
func ConnectSessionBus(opts ...ConnOption) (*Conn, error) {
address, err := getSessionBusAddress()
address, err := getSessionBusAddress(true)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2ece603

Please sign in to comment.