forked from pekman/netns-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netns-exec-dbus
executable file
·65 lines (59 loc) · 2 KB
/
netns-exec-dbus
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
# find D-Bus session bus addresses
dbus_addr="$DBUS_SESSION_BUS_ADDRESS"
if [ -z "$dbus_addr" ]; then
machine_id="$(cat /etc/machine-id 2>/dev/null)"
display_num="${DISPLAY#*:}"
display_num="${display_num%.*}"
if [ -n "$machine_id" ] && [ -n "$display_num" ]; then
file="$HOME/.dbus/session-bus/$machine_id-$display_num"
dbus_addr="$(grep -m 1 '^DBUS_SESSION_BUS_ADDRESS=' "$file")"
dbus_addr="${dbus_addr#DBUS_SESSION_BUS_ADDRESS=}"
fi
fi
# check if the addresses contain a normal (non-abstract) unix domain
# socket transport
has_normal_unix_transport () {
local IFS
IFS=';'
for addr in $dbus_addr; do
case "$addr" in
unix:path=*|unix:*,path=*)
return 0 ;;
esac
done
return 1
}
# don't start the proxy if there is no d-bus address or if there is a
# normal unix transport, which works fine without a proxy
if [ -n "$dbus_addr" ] && ! has_normal_unix_transport; then
# find abstract unix transport
IFS=';'
for addr in $dbus_addr; do
case "$addr" in
unix:abstract=*|unix:*,abstract=*)
# parse address, get socket name and guid
name=
guid_opt=
IFS=','
for part in ${addr#unix:}; do
case "$part" in
abstract=*)
name="${part#abstract=}" ;;
guid=*)
guid_opt=",$part" ;;
esac
done
if [ -n "$name" ]; then
# start proxy and kill it when shell exits
proxy_path="/tmp/dbus-proxy-$$"
socat UNIX-LISTEN:"$proxy_path",fork ABSTRACT-CONNECT:"$name" &
trap "kill -TERM $! && wait" 0
export DBUS_SESSION_BUS_ADDRESS="unix:path=$proxy_path$guid_opt"
fi
break
;;
esac
done
fi
netns-exec "$@"