]>
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 for path
in perm_list
:
100 name
= dbus
.service
.BusName("id.waydro.Container", dbus
.SystemBus(), do_not_queue
=True)
101 except dbus
.exceptions
.NameExistsException
:
102 logging
.error("Container service is already running")
105 status
= helpers
.lxc
.status(args
)
106 if status
== "STOPPED":
107 # Load binder and ashmem drivers
108 cfg
= tools
.config
.load(args
)
109 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
110 if helpers
.drivers
.probeBinderDriver(args
) != 0:
111 logging
.error("Failed to load Binder driver")
112 helpers
.drivers
.probeAshmemDriver(args
)
113 helpers
.drivers
.loadBinderNodes(args
)
114 set_permissions(args
, [
115 "/dev/" + args
.BINDER_DRIVER
,
116 "/dev/" + args
.VNDBINDER_DRIVER
,
117 "/dev/" + args
.HWBINDER_DRIVER
120 mainloop
= GLib
.MainLoop()
122 def sigint_handler(data
):
126 GLib
.unix_signal_add(GLib
.PRIORITY_HIGH
, signal
.SIGINT
, sigint_handler
, None)
127 GLib
.unix_signal_add(GLib
.PRIORITY_HIGH
, signal
.SIGTERM
, sigint_handler
, None)
128 service(args
, mainloop
)
130 logging
.error("WayDroid container is {}".format(status
))
132 def do_start(args
, session
):
133 if "session" in args
:
134 raise RuntimeError("Already tracking a session")
137 command
= [tools
.config
.tools_src
+
138 "/data/scripts/waydroid-net.sh", "start"]
139 tools
.helpers
.run
.user(args
, command
)
142 if which("waydroid-sensord"):
143 tools
.helpers
.run
.user(
144 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
148 command
= ["start", "cgroup-lite"]
149 tools
.helpers
.run
.user(args
, command
, check
=False)
151 # Keep schedtune around in case nesting is supported
152 if os
.path
.ismount("/sys/fs/cgroup/schedtune"):
154 os
.mkdir("/sys/fs/cgroup/schedtune/probe0")
155 os
.mkdir("/sys/fs/cgroup/schedtune/probe0/probe1")
157 command
= ["umount", "-l", "/sys/fs/cgroup/schedtune"]
158 tools
.helpers
.run
.user(args
, command
, check
=False)
160 if os
.path
.exists("/sys/fs/cgroup/schedtune/probe0/probe1"):
161 os
.rmdir("/sys/fs/cgroup/schedtune/probe0/probe1")
162 if os
.path
.exists("/sys/fs/cgroup/schedtune/probe0"):
163 os
.rmdir("/sys/fs/cgroup/schedtune/probe0")
165 #TODO: remove NFC hacks
167 command
= ["stop", "nfcd"]
168 tools
.helpers
.run
.user(args
, command
, check
=False)
169 elif which("systemctl") and (tools
.helpers
.run
.user(args
, ["systemctl", "is-active", "-q", "nfcd"], check
=False) == 0):
170 command
= ["systemctl", "stop", "nfcd"]
171 tools
.helpers
.run
.user(args
, command
, check
=False)
174 set_permissions(args
)
176 # Create session-specific LXC config file
177 helpers
.lxc
.generate_session_lxc_config(args
, session
)
178 # Backwards compatibility
179 with open(tools
.config
.defaults
["lxc"] + "/waydroid/config") as f
:
180 if "config_session" not in f
.read():
181 helpers
.mount
.bind(args
, session
["waydroid_data"],
182 tools
.config
.defaults
["data"])
185 cfg
= tools
.config
.load(args
)
186 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"], session
)
188 helpers
.protocol
.set_aidl_version(args
)
190 helpers
.lxc
.start(args
)
191 services
.hardware_manager
.start(args
)
193 args
.session
= session
195 def stop(args
, quit_session
=True):
197 services
.hardware_manager
.stop(args
)
198 status
= helpers
.lxc
.status(args
)
199 if status
!= "STOPPED":
200 helpers
.lxc
.stop(args
)
201 while helpers
.lxc
.status(args
) != "STOPPED":
205 command
= [tools
.config
.tools_src
+
206 "/data/scripts/waydroid-net.sh", "stop"]
207 tools
.helpers
.run
.user(args
, command
, check
=False)
209 #TODO: remove NFC hacks
211 command
= ["start", "nfcd"]
212 tools
.helpers
.run
.user(args
, command
, check
=False)
213 elif which("systemctl") and (tools
.helpers
.run
.user(args
, ["systemctl", "is-enabled", "-q", "nfcd"], check
=False) == 0):
214 command
= ["systemctl", "start", "nfcd"]
215 tools
.helpers
.run
.user(args
, command
, check
=False)
218 if which("waydroid-sensord"):
219 command
= ["pidof", "waydroid-sensord"]
220 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True).strip()
222 command
= ["kill", "-9", pid
]
223 tools
.helpers
.run
.user(args
, command
, check
=False)
226 helpers
.images
.umount_rootfs(args
)
228 # Backwards compatibility
230 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["data"])
234 if "session" in args
:
237 os
.kill(int(args
.session
["pid"]), signal
.SIGUSR1
)
245 status
= helpers
.lxc
.status(args
)
246 if status
== "RUNNING":
247 helpers
.lxc
.stop(args
)
248 helpers
.lxc
.start(args
)
250 logging
.error("WayDroid container is {}".format(status
))
253 status
= helpers
.lxc
.status(args
)
254 if status
== "RUNNING":
255 helpers
.lxc
.freeze(args
)
256 while helpers
.lxc
.status(args
) == "RUNNING":
259 logging
.error("WayDroid container is {}".format(status
))
262 status
= helpers
.lxc
.status(args
)
263 if status
== "FROZEN":
264 helpers
.lxc
.unfreeze(args
)
265 while helpers
.lxc
.status(args
) == "FROZEN":