2024-09-09 03:11:50 +09:00
|
|
|
package dbus
|
|
|
|
|
|
|
|
import (
|
2024-09-24 19:37:50 +09:00
|
|
|
"fmt"
|
2024-10-09 20:41:42 +09:00
|
|
|
"os"
|
2024-09-09 03:11:50 +09:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2024-10-09 20:41:42 +09:00
|
|
|
const (
|
|
|
|
SessionBusAddress = "DBUS_SESSION_BUS_ADDRESS"
|
|
|
|
SystemBusAddress = "DBUS_SYSTEM_BUS_ADDRESS"
|
|
|
|
)
|
2024-09-09 03:11:50 +09:00
|
|
|
|
2024-09-27 23:52:38 +09:00
|
|
|
var (
|
2024-10-09 20:41:42 +09:00
|
|
|
addresses [2]string
|
|
|
|
addressOnce sync.Once
|
2024-09-27 23:52:38 +09:00
|
|
|
)
|
|
|
|
|
2024-10-09 20:41:42 +09:00
|
|
|
func Address() (session, system string) {
|
|
|
|
addressOnce.Do(func() {
|
|
|
|
// resolve upstream session bus address
|
|
|
|
if addr, ok := os.LookupEnv(SessionBusAddress); !ok {
|
|
|
|
// fall back to default format
|
|
|
|
addresses[0] = fmt.Sprintf("unix:path=/run/user/%d/bus", os.Getuid())
|
|
|
|
} else {
|
|
|
|
addresses[0] = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve upstream system bus address
|
|
|
|
if addr, ok := os.LookupEnv(SystemBusAddress); !ok {
|
|
|
|
// fall back to default hardcoded value
|
|
|
|
addresses[1] = "unix:path=/run/dbus/system_bus_socket"
|
|
|
|
} else {
|
|
|
|
addresses[1] = addr
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return addresses[0], addresses[1]
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|