]>
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 signal_handler(sig
, frame
):
86 services
.hardware_manager
.stop(args
)
90 status
= helpers
.lxc
.status(args
)
91 if status
== "STOPPED":
92 # Load binder and ashmem drivers
93 cfg
= tools
.config
.load(args
)
94 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
95 if helpers
.drivers
.probeBinderDriver(args
) != 0:
96 logging
.error("Failed to load Binder driver")
97 if helpers
.drivers
.probeAshmemDriver(args
) != 0:
98 logging
.error("Failed to load Ashmem driver")
99 helpers
.drivers
.loadBinderNodes(args
)
101 "/dev/" + args
.BINDER_DRIVER
,
102 "/dev/" + args
.VNDBINDER_DRIVER
,
103 "/dev/" + args
.HWBINDER_DRIVER
106 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
107 session_cfg
= tools
.config
.load_session()
108 if session_cfg
["session"]["state"] != "STOPPED":
109 logging
.warning("Found session config on state: {}, restart session".format(
110 session_cfg
["session"]["state"]))
111 os
.remove(tools
.config
.session_defaults
["config_path"])
112 logging
.debug("Container manager is waiting for session to load")
113 while not os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
116 # Load session configs
117 session_cfg
= tools
.config
.load_session()
120 make_prop(args
.work
+ "/waydroid.prop")
123 command
= [tools
.config
.tools_src
+
124 "/data/scripts/waydroid-net.sh", "start"]
125 tools
.helpers
.run
.user(args
, command
, check
=False)
128 if which("waydroid-sensord"):
129 tools
.helpers
.run
.user(
130 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
133 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"])
135 helpers
.protocol
.set_aidl_version(args
)
138 helpers
.mount
.bind(args
, session_cfg
["session"]["waydroid_data"],
139 tools
.config
.defaults
["data"])
143 command
= ["start", "cgroup-lite"]
144 tools
.helpers
.run
.user(args
, command
, check
=False)
145 if os
.path
.ismount("/sys/fs/cgroup/schedtune"):
146 command
= ["umount", "-l", "/sys/fs/cgroup/schedtune"]
147 tools
.helpers
.run
.user(args
, command
, check
=False)
149 #TODO: remove NFC hacks
151 command
= ["stop", "nfcd"]
152 tools
.helpers
.run
.user(args
, command
, check
=False)
157 helpers
.lxc
.start(args
)
158 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
160 while session_cfg
["session"]["state"] != "RUNNING" and timeout
> 0:
161 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
163 "waiting {} seconds for container to start...".format(timeout
))
164 timeout
= timeout
- 1
166 if session_cfg
["session"]["state"] != "RUNNING":
167 raise OSError("container failed to start")
168 tools
.config
.save_session(session_cfg
)
170 services
.hardware_manager
.start(args
)
172 signal
.signal(signal
.SIGINT
, signal_handler
)
173 while os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
174 session_cfg
= tools
.config
.load_session()
175 if session_cfg
["session"]["state"] == "STOPPED":
176 services
.hardware_manager
.stop(args
)
178 elif session_cfg
["session"]["state"] == "UNFREEZE":
179 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
180 tools
.config
.save_session(session_cfg
)
184 logging
.warning("session manager stopped, stopping container and waiting...")
186 services
.hardware_manager
.stop(args
)
189 logging
.error("WayDroid container is {}".format(status
))
192 status
= helpers
.lxc
.status(args
)
193 if status
!= "STOPPED":
194 helpers
.lxc
.stop(args
)
195 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
196 session_cfg
= tools
.config
.load_session()
197 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
198 tools
.config
.save_session(session_cfg
)
201 command
= [tools
.config
.tools_src
+
202 "/data/scripts/waydroid-net.sh", "stop"]
203 tools
.helpers
.run
.user(args
, command
, check
=False)
205 #TODO: remove NFC hacks
207 command
= ["start", "nfcd"]
208 tools
.helpers
.run
.user(args
, command
, check
=False)
211 if which("waydroid-sensord"):
212 command
= ["pidof", "waydroid-sensord"]
213 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True).strip()
215 command
= ["kill", "-9", pid
]
216 tools
.helpers
.run
.user(args
, command
, check
=False)
219 helpers
.images
.umount_rootfs(args
)
222 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["data"])
225 logging
.error("WayDroid container is {}".format(status
))
228 status
= helpers
.lxc
.status(args
)
229 if status
== "RUNNING":
230 helpers
.lxc
.stop(args
)
231 helpers
.lxc
.start(args
)
233 logging
.error("WayDroid container is {}".format(status
))
236 status
= helpers
.lxc
.status(args
)
237 if status
== "RUNNING":
238 helpers
.lxc
.freeze(args
)
239 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
240 session_cfg
= tools
.config
.load_session()
241 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
242 tools
.config
.save_session(session_cfg
)
244 logging
.error("WayDroid container is {}".format(status
))
247 status
= helpers
.lxc
.status(args
)
248 if status
== "FROZEN":
249 helpers
.lxc
.unfreeze(args
)
250 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
251 session_cfg
= tools
.config
.load_session()
252 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
253 tools
.config
.save_session(session_cfg
)
255 logging
.error("WayDroid container is {}".format(status
))