13
def accept_new_connection(host_adder, container_sock):
14
newconn = container_sock.accept()[0]
15
descriptors.append(newconn)
17
host_sock = socket(AF_UNIX, SOCK_STREAM)
18
host_sock.connect(host_adder)
19
descriptors.append(host_sock)
21
socket_pairs.append([newconn, host_sock])
24
def get_socket_pair(socket):
25
for i in range(len(socket_pairs)):
26
if socket in socket_pairs[i]:
27
return socket_pairs[i]
30
def get_socket_partner(socket):
31
socket_pair = get_socket_pair(socket)
33
for i in range(len(socket_pair)):
34
if socket != socket_pair[i]:
38
def close_connections(remove_socket):
39
partner_socket = get_socket_partner(remove_socket)
41
socket_pair = get_socket_pair(remove_socket)
42
socket_pairs.remove(socket_pair)
44
descriptors.remove(remove_socket)
45
remove_socket.shutdown(SHUT_RDWR)
48
descriptors.remove(partner_socket)
49
partner_socket.shutdown(SHUT_RDWR)
50
partner_socket.close()
53
def close_all_connections():
54
for i, j in socket_pairs:
61
def get_host_maliit_socket():
62
address_bus_name = "org.maliit.server"
63
address_object_path = "/org/maliit/server/address"
64
address_interface = "org.maliit.Server.Address"
65
address_property = "address"
67
session_bus = dbus.SessionBus()
68
maliit_object = session_bus.get_object('org.maliit.server', '/org/maliit/server/address')
70
interface = dbus.Interface(maliit_object, dbus.PROPERTIES_IFACE)
71
address = interface.Get('org.maliit.Server.Address', 'address')
73
partition_key = 'unix:abstract='
74
address = address.split(',')[0]
75
address = address.partition(partition_key)[2]
76
address = "\0%s" % address
81
def get_host_dbus_socket():
84
with open(os.path.join(libertine.utils.get_user_runtime_dir(), 'dbus-session'), 'r') as fd:
85
dbus_session_str = fd.read()
89
dbus_session_split = dbus_session_str.rsplit('=', 1)
90
if len(dbus_session_split) > 1:
91
host_dbus_socket = dbus_session_split[1].rstrip('\n')
92
# We need to add a \0 to the start of an abstract socket path to connect to it
93
if dbus_session_str.find('abstract') >= 0:
94
host_dbus_socket = "\0%s" % host_dbus_socket
96
return host_dbus_socket
99
def socket_cleanup(signum, frame):
100
for socket in descriptors:
103
close_all_connections()
105
for socket_path in session_socket_paths:
106
os.remove(socket_path)
110
signal.signal(signal.SIGTERM, socket_cleanup)
111
signal.signal(signal.SIGINT, socket_cleanup)
115
rlist, wlist, elist = select.select(descriptors, [], [])
116
except InterruptedError:
122
if sock.fileno() == -1:
125
if sock in host_session_socket_path_map:
126
accept_new_connection(host_session_socket_path_map[sock], sock)
130
data = sock.recv(4096)
132
close_connections(sock)
136
close_connections(sock)
139
send_sock = get_socket_partner(sock)
141
if send_sock.fileno() < 0:
145
while totalsent < len(data):
146
sent = send_sock.send(data)
149
close_connections(sock)
151
totalsent = totalsent + sent
154
def create_socket(session_socket_path):
156
sock = socket(AF_UNIX, SOCK_STREAM)
161
sock.bind(session_socket_path)
172
def create_container_socket(session_socket_path, get_host_session_path_function):
173
container_session_sock = create_socket(session_socket_path)
175
if container_session_sock is not None:
177
host_session_path = get_host_session_path_function()
179
container_session_sock.close()
180
container_session_sock = None
181
os.remove(session_socket_path)
184
host_session_socket_path_map.update({container_session_sock:host_session_path})
186
session_socket_paths.append(session_socket_path)
187
descriptors.append(container_session_sock)
191
host_session_socket_path_map = {}
192
session_socket_paths = []
195
create_container_socket(sys.argv[1], get_host_dbus_socket)
199
create_container_socket(sys.argv[2], get_host_maliit_socket)