Skip to content

Commit 51e607f

Browse files
authored
Merge pull request #3356 from kolyshkin/user-dbus
libct/cg/sd: simplify DetectUserDbusSessionBusAddress
2 parents 7fd8b57 + 1a93520 commit 51e607f

File tree

1 file changed

+4
-16
lines changed
  • libcontainer/cgroups/systemd

1 file changed

+4
-16
lines changed

libcontainer/cgroups/systemd/user.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,18 @@ func DetectUID() (int, error) {
7777
return -1, errors.New("could not detect the OwnerUID")
7878
}
7979

80-
// DetectUserDbusSessionBusAddress returns $DBUS_SESSION_BUS_ADDRESS if set.
81-
// Otherwise returns "unix:path=$XDG_RUNTIME_DIR/bus" if $XDG_RUNTIME_DIR/bus exists.
82-
// Otherwise parses the value from `systemctl --user show-environment` .
80+
// DetectUserDbusSessionBusAddress returns $DBUS_SESSION_BUS_ADDRESS, if set.
81+
// Otherwise it returns "unix:path=$XDG_RUNTIME_DIR/bus", if $XDG_RUNTIME_DIR/bus exists.
8382
func DetectUserDbusSessionBusAddress() (string, error) {
8483
if env := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); env != "" {
8584
return env, nil
8685
}
8786
if xdr := os.Getenv("XDG_RUNTIME_DIR"); xdr != "" {
8887
busPath := filepath.Join(xdr, "bus")
8988
if _, err := os.Stat(busPath); err == nil {
90-
busAddress := "unix:path=" + busPath
89+
busAddress := "unix:path=" + dbus.EscapeBusAddressValue(busPath)
9190
return busAddress, nil
9291
}
9392
}
94-
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
95-
if err != nil {
96-
return "", fmt.Errorf("could not execute `systemctl --user --no-pager show-environment` (output=%q): %w", string(b), err)
97-
}
98-
scanner := bufio.NewScanner(bytes.NewReader(b))
99-
for scanner.Scan() {
100-
s := strings.TrimSpace(scanner.Text())
101-
if strings.HasPrefix(s, "DBUS_SESSION_BUS_ADDRESS=") {
102-
return strings.TrimPrefix(s, "DBUS_SESSION_BUS_ADDRESS="), nil
103-
}
104-
}
105-
return "", errors.New("could not detect DBUS_SESSION_BUS_ADDRESS from `systemctl --user --no-pager show-environment`. Make sure you have installed the dbus-user-session or dbus-daemon package and then run: `systemctl --user start dbus`")
93+
return "", errors.New("could not detect DBUS_SESSION_BUS_ADDRESS from the environment; make sure you have installed the dbus-user-session or dbus-daemon package; note you may need to re-login")
10694
}

0 commit comments

Comments
 (0)