]>
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
11 from tools
import helpers
12 from tools
import services
16 def make_prop(full_props_path
):
17 def add_prop(key
, cfg_key
):
18 value
= session_cfg
["session"][cfg_key
]
20 value
= value
.replace("/mnt/", "/mnt_extra/")
21 props
.append(key
+ "=" + value
)
23 if not os
.path
.isfile(args
.work
+ "/waydroid_base.prop"):
24 raise RuntimeError("waydroid_base.prop Not found")
25 with open(args
.work
+ "/waydroid_base.prop") as f
:
26 props
= f
.read().splitlines()
28 raise RuntimeError("waydroid_base.prop is broken!!?")
30 add_prop("waydroid.host.user", "user_name")
31 add_prop("waydroid.host.uid", "user_id")
32 add_prop("waydroid.host.gid", "group_id")
33 add_prop("waydroid.xdg_runtime_dir", "xdg_runtime_dir")
34 add_prop("waydroid.pulse_runtime_path", "pulse_runtime_path")
35 add_prop("waydroid.wayland_display", "wayland_display")
36 if which("waydroid-sensord") is None:
37 props
.append("waydroid.stub_sensors_hal=1")
38 dpi
= session_cfg
["session"]["lcd_density"]
40 props
.append("ro.sf.lcd_density=" + dpi
)
42 final_props
= open(full_props_path
, "w")
44 final_props
.write(prop
+ "\n")
46 os
.chmod(full_props_path
, 0o644)
48 def set_permissions(perm_list
=None, mode
="777"):
49 def chmod(path
, mode
):
50 if os
.path
.exists(path
):
51 command
= ["chmod", mode
, "-R", path
]
52 tools
.helpers
.run
.user(args
, command
, check
=False)
61 "/sys/kernel/debug/sync/sw_sync",
77 perm_list
.extend(glob
.glob("/dev/fb*"))
79 for path
in perm_list
:
82 def signal_handler(sig
, frame
):
83 services
.hardware_manager
.stop(args
)
87 status
= helpers
.lxc
.status(args
)
88 if status
== "STOPPED":
89 # Load binder and ashmem drivers
90 cfg
= tools
.config
.load(args
)
91 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
92 if helpers
.drivers
.probeBinderDriver(args
) != 0:
93 logging
.error("Failed to load Binder driver")
94 if helpers
.drivers
.probeAshmemDriver(args
) != 0:
95 logging
.error("Failed to load Ashmem driver")
96 helpers
.drivers
.loadBinderNodes(args
)
98 "/dev/" + args
.BINDER_DRIVER
,
99 "/dev/" + args
.VNDBINDER_DRIVER
,
100 "/dev/" + args
.HWBINDER_DRIVER
103 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
104 session_cfg
= tools
.config
.load_session()
105 if session_cfg
["session"]["state"] != "STOPPED":
106 logging
.warning("Found session config on state: {}, restart session".format(
107 session_cfg
["session"]["state"]))
108 os
.remove(tools
.config
.session_defaults
["config_path"])
109 logging
.debug("Container manager is waiting for session to load")
110 while not os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
113 # Load session configs
114 session_cfg
= tools
.config
.load_session()
117 make_prop(args
.work
+ "/waydroid.prop")
120 command
= [tools
.config
.tools_src
+
121 "/data/scripts/waydroid-net.sh", "start"]
122 tools
.helpers
.run
.user(args
, command
, check
=False)
125 tools
.helpers
.run
.user(
126 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
129 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"])
132 helpers
.mount
.bind(args
, session_cfg
["session"]["waydroid_data"],
133 tools
.config
.defaults
["data"])
137 command
= ["start", "cgroup-lite"]
138 tools
.helpers
.run
.user(args
, command
, check
=False)
139 helpers
.mount
.umount_all(args
, "/sys/fs/cgroup/schedtune")
141 #TODO: remove NFC hacks
143 command
= ["stop", "nfcd"]
144 tools
.helpers
.run
.user(args
, command
, check
=False)
149 helpers
.lxc
.start(args
)
150 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
152 while session_cfg
["session"]["state"] != "RUNNING" and timeout
> 0:
153 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
155 "waiting {} seconds for container to start...".format(timeout
))
156 timeout
= timeout
- 1
158 if session_cfg
["session"]["state"] != "RUNNING":
159 raise OSError("container failed to start")
160 tools
.config
.save_session(session_cfg
)
162 services
.hardware_manager
.start(args
)
164 signal
.signal(signal
.SIGINT
, signal_handler
)
165 while os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
166 session_cfg
= tools
.config
.load_session()
167 if session_cfg
["session"]["state"] == "STOPPED":
168 services
.hardware_manager
.stop(args
)
170 elif session_cfg
["session"]["state"] == "UNFREEZE":
171 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
172 tools
.config
.save_session(session_cfg
)
176 logging
.warning("session manager stopped, stopping container and waiting...")
178 services
.hardware_manager
.stop(args
)
181 logging
.error("WayDroid container is {}".format(status
))
184 status
= helpers
.lxc
.status(args
)
185 if status
!= "STOPPED":
186 helpers
.lxc
.stop(args
)
187 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
188 session_cfg
= tools
.config
.load_session()
189 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
190 tools
.config
.save_session(session_cfg
)
193 command
= [tools
.config
.tools_src
+
194 "/data/scripts/waydroid-net.sh", "stop"]
195 tools
.helpers
.run
.user(args
, command
, check
=False)
197 #TODO: remove NFC hacks
199 command
= ["start", "nfcd"]
200 tools
.helpers
.run
.user(args
, command
, check
=False)
203 if which("waydroid-sensord"):
204 command
= ["pidof", "waydroid-sensord"]
205 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True)
207 command
= ["kill", "-9", pid
]
208 tools
.helpers
.run
.user(args
, command
, check
=False)
211 logging
.error("WayDroid container is {}".format(status
))
214 status
= helpers
.lxc
.status(args
)
215 if status
== "RUNNING":
216 helpers
.lxc
.stop(args
)
217 helpers
.lxc
.start(args
)
219 logging
.error("WayDroid container is {}".format(status
))
222 status
= helpers
.lxc
.status(args
)
223 if status
== "RUNNING":
224 helpers
.lxc
.freeze(args
)
225 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
226 session_cfg
= tools
.config
.load_session()
227 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
228 tools
.config
.save_session(session_cfg
)
230 logging
.error("WayDroid container is {}".format(status
))
233 status
= helpers
.lxc
.status(args
)
234 if status
== "FROZEN":
235 helpers
.lxc
.unfreeze(args
)
236 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
237 session_cfg
= tools
.config
.load_session()
238 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
239 tools
.config
.save_session(session_cfg
)
241 logging
.error("WayDroid container is {}".format(status
))