Skip to content

Commit

Permalink
Only launch one D-Bus instance
Browse files Browse the repository at this point in the history
This ensures that only one D-Bus instance is started and shared by
all connections when a D-Bus instance doesn't exist in the environment
under which the program was started.

Fixes #61
  • Loading branch information
jsouthworth committed Apr 1, 2016
1 parent 6fd6e8a commit b1d7fa2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
systemBusLck sync.Mutex
sessionBus *Conn
sessionBusLck sync.Mutex
sessionEnvLck sync.Mutex
)

// ErrClosed is the error returned by calls on a closed connection.
Expand Down Expand Up @@ -91,6 +92,8 @@ func SessionBus() (conn *Conn, err error) {

// SessionBusPrivate returns a new private connection to the session bus.
func SessionBusPrivate() (*Conn, error) {
sessionEnvLck.Lock()
defer sessionEnvLck.Unlock()
address := os.Getenv("DBUS_SESSION_BUS_ADDRESS")
if address != "" && address != "autolaunch:" {
return Dial(address)
Expand Down
6 changes: 5 additions & 1 deletion conn_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package dbus
import (
"bytes"
"errors"
"os"
"os/exec"
)

Expand All @@ -23,5 +24,8 @@ func sessionBusPlatform() (*Conn, error) {
return nil, errors.New("dbus: couldn't determine address of session bus")
}

return Dial(string(b[i+1 : j]))
env, addr := string(b[0:i]), string(b[i+1:j])
os.Setenv(env, addr)

return Dial(addr)
}

0 comments on commit b1d7fa2

Please sign in to comment.