engine: resources: svc: Use dbus session bus for user session svc

This patch adds a util function, SessionBusUsable, that makes and returns
a new usable dbus session bus. If the svc bool session is true, the resource
will use a bus created with that function.
This commit is contained in:
Jonathan Gold
2018-12-13 18:56:01 -05:00
committed by James Shubin
parent 4297a39d03
commit 28f343ac50
2 changed files with 26 additions and 2 deletions

View File

@@ -86,6 +86,7 @@ func (obj *SvcRes) Watch() error {
}
var conn *systemd.Conn
var bus *dbus.Conn
var err error
if obj.Session {
conn, err = systemd.NewUserConnection() // user session
@@ -99,7 +100,11 @@ func (obj *SvcRes) Watch() error {
defer conn.Close()
// if we share the bus with others, we will get each others messages!!
bus, err := util.SystemBusPrivateUsable() // don't share the bus connection!
if obj.Session {
bus, err = util.SessionBusPrivateUsable()
} else {
bus, err = util.SystemBusPrivateUsable()
}
if err != nil {
return errwrap.Wrapf(err, "failed to connect to bus")
}

View File

@@ -359,7 +359,7 @@ func TimeAfterOrBlock(t int) <-chan time.Time {
return time.After(time.Duration(t) * time.Second)
}
// SystemBusPrivateUsable makes using the private bus usable
// SystemBusPrivateUsable makes using the private bus usable.
// TODO: should be upstream: https://github.com/godbus/dbus/issues/15
func SystemBusPrivateUsable() (conn *dbus.Conn, err error) {
conn, err = dbus.SystemBusPrivate()
@@ -378,6 +378,25 @@ func SystemBusPrivateUsable() (conn *dbus.Conn, err error) {
return conn, nil // success
}
// SessionBusPrivateUsable makes using the private bus usable.
// TODO: should be upstream: https://github.com/godbus/dbus/issues/15
func SessionBusPrivateUsable() (conn *dbus.Conn, err error) {
conn, err = dbus.SessionBusPrivate()
if err != nil {
return nil, err
}
if err = conn.Auth(nil); err != nil {
conn.Close()
conn = nil
return
}
if err = conn.Hello(); err != nil {
conn.Close()
conn = nil
}
return conn, nil // success
}
// SortedStrSliceCompare takes two lists of strings and returns whether or not
// they are equivalent. It will return nil if both sets contain the same
// elements, regardless of order, and an error if they do not.