]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/actions/container_manager.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 from shutil
import which
12 from tools
import helpers
13 from tools
import services
16 import dbus
.exceptions
17 from gi
.repository
import GLib
19 class DbusContainerManager(dbus
.service
.Object
):
20 def __init__(self
, looper
, bus
, object_path
, args
):
23 dbus
.service
.Object
.__init
__(self
, bus
, object_path
)
25 @dbus.service.method("id.waydro.ContainerManager", in_signature
='a{ss}', out_signature
='', sender_keyword
="sender", connection_keyword
="conn")
26 def Start(self
, session
, sender
, conn
):
27 dbus_info
= dbus
.Interface(conn
.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus/Bus", False), "org.freedesktop.DBus")
28 uid
= dbus_info
.GetConnectionUnixUser(sender
)
29 if str(uid
) not in ["0", session
["user_id"]]:
30 raise RuntimeError("Cannot start a session on behalf of another user")
31 pid
= dbus_info
.GetConnectionUnixProcessID(sender
)
32 if str(uid
) != "0" and str(pid
) != session
["pid"]:
33 raise RuntimeError("Invalid session pid")
34 do_start(self
.args
, session
)
36 @dbus.service.method("id.waydro.ContainerManager", in_signature
='b', out_signature
='')
37 def Stop(self
, quit_session
):
38 stop(self
.args
, quit_session
)
40 @dbus.service.method("id.waydro.ContainerManager", in_signature
='', out_signature
='')
44 @dbus.service.method("id.waydro.ContainerManager", in_signature
='', out_signature
='')
48 @dbus.service.method("id.waydro.ContainerManager", in_signature
='', out_signature
='a{ss}')
51 session
= self
.args
.session
52 session
["state"] = helpers
.lxc
.status(self
.args
)
54 except AttributeError:
57 def service(args
, looper
):
58 dbus_obj
= DbusContainerManager(looper
, dbus
.SystemBus(), '/ContainerManager', args
)
61 def set_permissions(args
, perm_list
=None, mode
="777"):
62 def chmod(path
, mode
):
63 if os
.path
.exists(path
):
64 command
= ["chmod", mode
, "-R", path
]
65 tools
.helpers
.run
.user(args
, command
, check
=False)
74 "/sys/kernel/debug/sync/sw_sync",
89 perm_list
.extend(glob
.glob("/dev/dri/renderD*"))
91 perm_list
.extend(glob
.glob("/dev/fb*"))
93 perm_list
.extend(glob
.glob("/dev/video*"))
95 perm_list
.extend(glob
.glob("/dev/dma_heap/*"))
97 for path
in perm_list
:
102 name
= dbus
.service
.BusName("id.waydro.Container", dbus
.SystemBus(), do_not_queue
=True)
103 except dbus
.exceptions
.NameExistsException
:
104 logging
.error("Container service is already running")
107 status
= helpers
.lxc
.status(args
)
108 if status
== "STOPPED":
109 # Load binder and ashmem drivers
110 cfg
= tools
.config
.load(args
)
111 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
112 if helpers
.drivers
.probeBinderDriver(args
) != 0:
113 logging
.error("Failed to load Binder driver")
114 helpers
.drivers
.probeAshmemDriver(args
)
115 helpers
.drivers
.loadBinderNodes(args
)
116 set_permissions(args
, [
117 "/dev/" + args
.BINDER_DRIVER
,
118 "/dev/" + args
.VNDBINDER_DRIVER
,
119 "/dev/" + args
.HWBINDER_DRIVER
122 mainloop
= GLib
.MainLoop()
124 def sigint_handler(data
):
128 GLib
.unix_signal_add(GLib
.PRIORITY_HIGH
, signal
.SIGINT
, sigint_handler
, None)
129 GLib
.unix_signal_add(GLib
.PRIORITY_HIGH
, signal
.SIGTERM
, sigint_handler
, None)
130 service(args
, mainloop
)
132 logging
.error("WayDroid container is {}".format(status
))
134 def do_start(args
, session
):
135 if "session" in args
:
136 raise RuntimeError("Already tracking a session")
139 command
= [tools
.config
.tools_src
+
140 "/data/scripts/waydroid-net.sh", "start"]
141 tools
.helpers
.run
.user(args
, command
)
144 if which("waydroid-sensord"):
145 tools
.helpers
.run
.user(
146 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
150 command
= ["start", "cgroup-lite"]
151 tools
.helpers
.run
.user(args
, command
, check
=False)
153 # Keep schedtune around in case nesting is supported
154 if os
.path
.ismount("/sys/fs/cgroup/schedtune"):
156 os
.mkdir("/sys/fs/cgroup/schedtune/probe0")
157 os
.mkdir("/sys/fs/cgroup/schedtune/probe0/probe1")
159 command
= ["umount", "-l", "/sys/fs/cgroup/schedtune"]
160 tools
.helpers
.run
.user(args
, command
, check
=False)
162 if os
.path
.exists("/sys/fs/cgroup/schedtune/probe0/probe1"):
163 os
.rmdir("/sys/fs/cgroup/schedtune/probe0/probe1")
164 if os
.path
.exists("/sys/fs/cgroup/schedtune/probe0"):
165 os
.rmdir("/sys/fs/cgroup/schedtune/probe0")
167 #TODO: remove NFC hacks
169 command
= ["stop", "nfcd"]
170 tools
.helpers
.run
.user(args
, command
, check
=False)
171 elif which("systemctl") and (tools
.helpers
.run
.user(args
, ["systemctl", "is-active", "-q", "nfcd"], check
=False) == 0):
172 command
= ["systemctl", "stop", "nfcd"]
173 tools
.helpers
.run
.user(args
, command
, check
=False)
176 set_permissions(args
)
178 # Create session-specific LXC config file
179 helpers
.lxc
.generate_session_lxc_config(args
, session
)
180 # Backwards compatibility
181 with open(tools
.config
.defaults
["lxc"] + "/waydroid/config") as f
:
182 if "config_session" not in f
.read():
183 helpers
.mount
.bind(args
, session
["waydroid_data"],
184 tools
.config
.defaults
["data"])
187 cfg
= tools
.config
.load(args
)
188 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"], session
)
190 helpers
.protocol
.set_aidl_version(args
)
192 helpers
.lxc
.start(args
)
193 services
.hardware_manager
.start(args
)
195 args
.session
= session
197 def stop(args
, quit_session
=True):
199 services
.hardware_manager
.stop(args
)
200 status
= helpers
.lxc
.status(args
)
201 if status
!= "STOPPED":
202 helpers
.lxc
.stop(args
)
203 while helpers
.lxc
.status(args
) != "STOPPED":
207 command
= [tools
.config
.tools_src
+
208 "/data/scripts/waydroid-net.sh", "stop"]
209 tools
.helpers
.run
.user(args
, command
, check
=False)
211 #TODO: remove NFC hacks
213 command
= ["start", "nfcd"]
214 tools
.helpers
.run
.user(args
, command
, check
=False)
215 elif which("systemctl") and (tools
.helpers
.run
.user(args
, ["systemctl", "is-enabled", "-q", "nfcd"], check
=False) == 0):
216 command
= ["systemctl", "start", "nfcd"]
217 tools
.helpers
.run
.user(args
, command
, check
=False)
220 if which("waydroid-sensord"):
221 command
= ["pidof", "waydroid-sensord"]
222 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True).strip()
224 command
= ["kill", "-9", pid
]
225 tools
.helpers
.run
.user(args
, command
, check
=False)
228 helpers
.images
.umount_rootfs(args
)
230 # Backwards compatibility
232 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["data"])
236 if "session" in args
:
239 os
.kill(int(args
.session
["pid"]), signal
.SIGUSR1
)
247 status
= helpers
.lxc
.status(args
)
248 if status
== "RUNNING":
249 helpers
.lxc
.stop(args
)
250 helpers
.lxc
.start(args
)
252 logging
.error("WayDroid container is {}".format(status
))
255 status
= helpers
.lxc
.status(args
)
256 if status
== "RUNNING":
257 helpers
.lxc
.freeze(args
)
258 while helpers
.lxc
.status(args
) == "RUNNING":
261 logging
.error("WayDroid container is {}".format(status
))
264 status
= helpers
.lxc
.status(args
)
265 if status
== "FROZEN":
266 helpers
.lxc
.unfreeze(args
)
267 while helpers
.lxc
.status(args
) == "FROZEN":