-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathenv.sh
executable file
·47 lines (40 loc) · 1.65 KB
/
env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Tries to find the D-Bus session bus address, listening on a Unix domain socket
# as needed.
dbus_addr() {
local user home x_display dbus_file abstract_path
user=${SUDO_USER:-$(id -u)}
# Only start socat if not running as root.
if [[ $user == root ]]; then
return
fi
# Find dbus session bus path
home=$(getent passwd "$user" | cut -d: -f6)
x_display=${DISPLAY:-:0}
x_display=${x_display#*:}
x_display=${x_display%.*}
dbus_file="$home/.dbus/session-bus/$(cat /etc/machine-id)-${x_display}"
env_re="^DBUS_SESSION_BUS_ADDRESS=['\"]?unix:abstract=\\K/tmp/dbus-[a-zA-Z0-9]+"
abstract_path=
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && [ -e "$dbus_file" ]; then
abstract_path=$(grep -Po -m1 "$env_re" "$dbus_file") || :
fi
if [ -z "${abstract_path}" ]; then
abstract_path=$(env | grep -Po -m1 "$env_re") || :
fi
# Hurrah! Found a Unix domain socket!
if [ -n "${abstract_path}" ]; then
# If the dbus session bus address is found, try to listen on a named
# Unix domain socket (if not already). This ensures that it is visible
# in the net namespace.
if ! [ -e "${abstract_path}" ]; then
sudo -u "$user" \
socat UNIX-LISTEN:$abstract_path,fork \
ABSTRACT-CONNECT:$abstract_path >&2 &
fi
export DBUS_SESSION_BUS_ADDRESS=unix:path=$abstract_path
fi
}
# Only try to find the dbus session address when running outside the netns.
# If inside the netns, the parent could be determined, and then a swap using
# /proc/xxx/ns/net could be done, but that is ugly.
[ -n "$(ip netns identify)" ] || dbus_addr