如何使 chrome 使用会话总线而不是系统总线

How to make chrome use session bus instead of system bus

我试图让 chrome 在 docker 容器上使用会话 dbus 而不是系统总线,但到目前为止失败了。我尝试了以下方法:

Xvfb $DISPLAY -screen 0 1920x1080x24 -pn -noreset >> /tmp/xvfb.log 2>&1 &
export $(dbus-launch)
google-chrome --no-sandbox --no-first-run
Xvfb $DISPLAY -screen 0 1920x1080x24 -pn -noreset >> /tmp/xvfb.log 2>&1 &
dbus-run-session -- google-chrome --no-sandbox --no-first-run

在这两种情况下 DBUS_SESSION_BUS_ADDRESS 都已设置,但看起来 chrome 会忽略它并尝试连接到 /var/run/dbus/system_bus_socket:

[32:135:0820/204612.634920:ERROR:bus.cc(393)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
[32:135:0820/204612.635302:ERROR:bus.cc(393)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
[32:135:0820/204612.635467:ERROR:bus.cc(393)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory

看起来 /var/run/dbus/system_bus_socket 硬编码在 chrome 的源代码中,所以我需要执行以下操作:

#!/bin/sh

export DISPLAY=":1"

start_xvfb() {
    [ -e "/tmp/.X1-lock" ] && rm -f /tmp/.X1-lock
    nohup Xvfb :1 -screen 0 1920x1080x24 -pn -noreset >> /tmp/xvfb.log 2>&1 &
    echo $!
}

XVFB_PID="$(start_xvfb)"
echo -e "[startup] xvfb started with PID $XVFB_PID"
sleep 1

# make dbus visible to other processes
mkdir -p /var/run/dbus/
export DBUS_STARTER_BUS_TYPE="session"
export DBUS_STARTER_ADDRESS="unix:path=/var/run/dbus/system_bus_socket"
export DBUS_SESSION_BUS_ADDRESS="unix:path=/var/run/dbus/system_bus_socket"
unset DBUS_SESSION_BUS_PID
unset DBUS_SESSION_BUS_WINDOWID
# open unix socket for dbus, on common setups it's done by systemd
python -c "import socket; s = socket.socket(socket.AF_UNIX); s.bind('/var/run/dbus/system_bus_socket')"
# start it
dbus-daemon --session --nofork --nosyslog --nopidfile --address=$DBUS_STARTER_ADDRESS >> /tmp/dbus.log 2>&1 &
returncode=$?
DBUS_PID=$!
[ $returncode -ne 0 ] && exit $returncode
echo -e "[startup] dbus started with PID $DBUS_PID"
sleep 1

google-chrome --no-sandbox --no-first-run

returncode=$?
kill $XVFB_PID
kill $DBUS_PID
sleep 1
exit $returncode

chrome 记录:

[34:144:0820/231701.403705:ERROR:object_proxy.cc(622)] Failed to call method: org.freedesktop.DBus.Properties.Get: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
[34:144:0820/231701.404157:ERROR:object_proxy.cc(622)] Failed to call method: org.freedesktop.UPower.GetDisplayDevice: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files
[34:144:0820/231701.404358:ERROR:object_proxy.cc(622)] Failed to call method: org.freedesktop.UPower.EnumerateDevices: object_path= /org/freedesktop/UPower: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files

但没有发生连接错误。