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

Posted

技术标签:

【中文标题】如何让 Chrome 使用会话总线而不是系统总线【英文标题】:How to make chrome use session bus instead of system bus 【发布时间】:2021-10-22 09:59:01 【问题描述】:

我正在尝试让 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

【问题讨论】:

【参考方案1】:

看起来/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

但没有发生连接错误。

【讨论】:

以上是关于如何让 Chrome 使用会话总线而不是系统总线的主要内容,如果未能解决你的问题,请参考以下文章

面向业务的微服务消息总线

dbus 如何重新连接到新的会话总线守护进程

请问CAN总线的速率为啥用波特率表示,而不用比特率表示?

消息总线Bus

消息总线Bus

MVB总线与RS485是啥关系?