]>
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
17 def make_prop(full_props_path
):
18 def add_prop(key
, cfg_key
):
19 value
= session_cfg
["session"][cfg_key
]
21 value
= value
.replace("/mnt/", "/mnt_extra/")
22 props
.append(key
+ "=" + value
)
24 if not os
.path
.isfile(args
.work
+ "/waydroid_base.prop"):
25 raise RuntimeError("waydroid_base.prop Not found")
26 with open(args
.work
+ "/waydroid_base.prop") as f
:
27 props
= f
.read().splitlines()
29 raise RuntimeError("waydroid_base.prop is broken!!?")
31 add_prop("waydroid.host.user", "user_name")
32 add_prop("waydroid.host.uid", "user_id")
33 add_prop("waydroid.host.gid", "group_id")
34 add_prop("waydroid.xdg_runtime_dir", "xdg_runtime_dir")
35 add_prop("waydroid.pulse_runtime_path", "pulse_runtime_path")
36 add_prop("waydroid.wayland_display", "wayland_display")
37 if which("waydroid-sensord") is None:
38 props
.append("waydroid.stub_sensors_hal=1")
39 dpi
= session_cfg
["session"]["lcd_density"]
41 props
.append("ro.sf.lcd_density=" + dpi
)
43 final_props
= open(full_props_path
, "w")
45 final_props
.write(prop
+ "\n")
47 os
.chmod(full_props_path
, 0o644)
49 def set_permissions(perm_list
=None, mode
="777"):
50 def chmod(path
, mode
):
51 if os
.path
.exists(path
):
52 command
= ["chmod", mode
, "-R", path
]
53 tools
.helpers
.run
.user(args
, command
, check
=False)
62 "/sys/kernel/debug/sync/sw_sync",
78 perm_list
.extend(glob
.glob("/dev/fb*"))
80 perm_list
.extend(glob
.glob("/dev/video*"))
82 for path
in perm_list
:
85 def set_aidl_version():
86 cfg
= tools
.config
.load(args
)
89 android_api
= int(helpers
.props
.file_get(args
,
90 tools
.config
.defaults
["rootfs"] + "/system/build.prop",
91 "ro.build.version.sdk"))
93 logging
.error("Failed to parse android version from system.img")
96 binder_protocol
= "aidl"
98 elif android_api
< 30:
99 binder_protocol
= "aidl2"
100 sm_protocol
= "aidl2"
101 elif android_api
< 31:
102 binder_protocol
= "aidl3"
103 sm_protocol
= "aidl3"
105 binder_protocol
= "aidl3"
106 sm_protocol
= "aidl4"
108 cfg
["waydroid"]["binder_protocol"] = binder_protocol
109 cfg
["waydroid"]["service_manager_protocol"] = sm_protocol
110 tools
.config
.save(args
, cfg
)
112 def signal_handler(sig
, frame
):
113 services
.hardware_manager
.stop(args
)
117 status
= helpers
.lxc
.status(args
)
118 if status
== "STOPPED":
119 # Load binder and ashmem drivers
120 cfg
= tools
.config
.load(args
)
121 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
122 if helpers
.drivers
.probeBinderDriver(args
) != 0:
123 logging
.error("Failed to load Binder driver")
124 if helpers
.drivers
.probeAshmemDriver(args
) != 0:
125 logging
.error("Failed to load Ashmem driver")
126 helpers
.drivers
.loadBinderNodes(args
)
128 "/dev/" + args
.BINDER_DRIVER
,
129 "/dev/" + args
.VNDBINDER_DRIVER
,
130 "/dev/" + args
.HWBINDER_DRIVER
133 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
134 session_cfg
= tools
.config
.load_session()
135 if session_cfg
["session"]["state"] != "STOPPED":
136 logging
.warning("Found session config on state: {}, restart session".format(
137 session_cfg
["session"]["state"]))
138 os
.remove(tools
.config
.session_defaults
["config_path"])
139 logging
.debug("Container manager is waiting for session to load")
140 while not os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
143 # Load session configs
144 session_cfg
= tools
.config
.load_session()
147 make_prop(args
.work
+ "/waydroid.prop")
150 command
= [tools
.config
.tools_src
+
151 "/data/scripts/waydroid-net.sh", "start"]
152 tools
.helpers
.run
.user(args
, command
, check
=False)
155 if which("waydroid-sensord"):
156 tools
.helpers
.run
.user(
157 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
160 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"])
165 helpers
.mount
.bind(args
, session_cfg
["session"]["waydroid_data"],
166 tools
.config
.defaults
["data"])
170 command
= ["start", "cgroup-lite"]
171 tools
.helpers
.run
.user(args
, command
, check
=False)
172 command
= ["umount", "-l", "/sys/fs/cgroup/schedtune"]
173 tools
.helpers
.run
.user(args
, command
, check
=False)
175 #TODO: remove NFC hacks
177 command
= ["stop", "nfcd"]
178 tools
.helpers
.run
.user(args
, command
, check
=False)
183 helpers
.lxc
.start(args
)
184 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
186 while session_cfg
["session"]["state"] != "RUNNING" and timeout
> 0:
187 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
189 "waiting {} seconds for container to start...".format(timeout
))
190 timeout
= timeout
- 1
192 if session_cfg
["session"]["state"] != "RUNNING":
193 raise OSError("container failed to start")
194 tools
.config
.save_session(session_cfg
)
196 services
.hardware_manager
.start(args
)
198 signal
.signal(signal
.SIGINT
, signal_handler
)
199 while os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
200 session_cfg
= tools
.config
.load_session()
201 if session_cfg
["session"]["state"] == "STOPPED":
202 services
.hardware_manager
.stop(args
)
204 elif session_cfg
["session"]["state"] == "UNFREEZE":
205 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
206 tools
.config
.save_session(session_cfg
)
210 logging
.warning("session manager stopped, stopping container and waiting...")
212 services
.hardware_manager
.stop(args
)
215 logging
.error("WayDroid container is {}".format(status
))
218 status
= helpers
.lxc
.status(args
)
219 if status
!= "STOPPED":
220 helpers
.lxc
.stop(args
)
221 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
222 session_cfg
= tools
.config
.load_session()
223 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
224 tools
.config
.save_session(session_cfg
)
227 command
= [tools
.config
.tools_src
+
228 "/data/scripts/waydroid-net.sh", "stop"]
229 tools
.helpers
.run
.user(args
, command
, check
=False)
231 #TODO: remove NFC hacks
233 command
= ["start", "nfcd"]
234 tools
.helpers
.run
.user(args
, command
, check
=False)
237 if which("waydroid-sensord"):
238 command
= ["pidof", "waydroid-sensord"]
239 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True).strip()
241 command
= ["kill", "-9", pid
]
242 tools
.helpers
.run
.user(args
, command
, check
=False)
245 helpers
.images
.umount_rootfs(args
)
248 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["data"])
251 logging
.error("WayDroid container is {}".format(status
))
254 status
= helpers
.lxc
.status(args
)
255 if status
== "RUNNING":
256 helpers
.lxc
.stop(args
)
257 helpers
.lxc
.start(args
)
259 logging
.error("WayDroid container is {}".format(status
))
262 status
= helpers
.lxc
.status(args
)
263 if status
== "RUNNING":
264 helpers
.lxc
.freeze(args
)
265 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
266 session_cfg
= tools
.config
.load_session()
267 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
268 tools
.config
.save_session(session_cfg
)
270 logging
.error("WayDroid container is {}".format(status
))
273 status
= helpers
.lxc
.status(args
)
274 if status
== "FROZEN":
275 helpers
.lxc
.unfreeze(args
)
276 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
277 session_cfg
= tools
.config
.load_session()
278 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
279 tools
.config
.save_session(session_cfg
)
281 logging
.error("WayDroid container is {}".format(status
))