From 162d3aaa553a3cd9005ec73c539d2169ccf93645 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 27 Jan 2022 18:16:34 -0800 Subject: [PATCH] libct/cg/sd: simplify DetectUserDbusSessionBusAddress Apparently, "systemctl --user --no-pager show-environment" is useless without DBUS_SESSION_BUS_ADDRESS or XDG_RUNTIME_DIR set: $ echo $DBUS_SESSION_BUS_ADDRESS, $XDG_RUNTIME_DIR unix:path=/run/user/1000/bus, /run/user/1000 $ systemctl --user --no-pager show-environment | grep DBUS_SESS DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus $ unset DBUS_SESSION_BUS_ADDRESS $ systemctl --user --no-pager show-environment | grep DBUS_SESS DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus $ unset XDG_RUNTIME_DIR $ systemctl --user --no-pager show-environment | grep DBUS_SESS Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=@.host --user to connect to bus of other user) So, it does not make sense to try it to get the address. Also, it does not make sense to try "systemctl --user start dbus" either, for the same reason, so remove that suggestion from the error message text. Since DBUS_SESSION_BUS_ADDRESS environment variable is set by dbus-run-session (or dbus-launch, or something similar that is supposed to be run during the login process), add a suggestion to re-login. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/systemd/user.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/libcontainer/cgroups/systemd/user.go b/libcontainer/cgroups/systemd/user.go index 0f50f76eee4..4b810bc096c 100644 --- a/libcontainer/cgroups/systemd/user.go +++ b/libcontainer/cgroups/systemd/user.go @@ -77,9 +77,8 @@ func DetectUID() (int, error) { return -1, errors.New("could not detect the OwnerUID") } -// DetectUserDbusSessionBusAddress returns $DBUS_SESSION_BUS_ADDRESS if set. -// Otherwise returns "unix:path=$XDG_RUNTIME_DIR/bus" if $XDG_RUNTIME_DIR/bus exists. -// Otherwise parses the value from `systemctl --user show-environment` . +// DetectUserDbusSessionBusAddress returns $DBUS_SESSION_BUS_ADDRESS, if set. +// Otherwise it returns "unix:path=$XDG_RUNTIME_DIR/bus", if $XDG_RUNTIME_DIR/bus exists. func DetectUserDbusSessionBusAddress() (string, error) { if env := os.Getenv("DBUS_SESSION_BUS_ADDRESS"); env != "" { return env, nil @@ -91,16 +90,5 @@ func DetectUserDbusSessionBusAddress() (string, error) { return busAddress, nil } } - b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput() - if err != nil { - return "", fmt.Errorf("could not execute `systemctl --user --no-pager show-environment` (output=%q): %w", string(b), err) - } - scanner := bufio.NewScanner(bytes.NewReader(b)) - for scanner.Scan() { - s := strings.TrimSpace(scanner.Text()) - if strings.HasPrefix(s, "DBUS_SESSION_BUS_ADDRESS=") { - return strings.TrimPrefix(s, "DBUS_SESSION_BUS_ADDRESS="), nil - } - } - 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`") + 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.") }