]>
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 props
.append(key
+ "=" + session_cfg
["session"][cfg_key
])
22 if not os
.path
.isfile(args
.work
+ "/waydroid_base.prop"):
23 raise RuntimeError("waydroid_base.prop Not found")
24 with open(args
.work
+ "/waydroid_base.prop") as f
:
25 props
= f
.read().splitlines()
27 raise RuntimeError("waydroid_base.prop is broken!!?")
29 add_prop("waydroid.host.user", "user_name")
30 add_prop("waydroid.host.uid", "user_id")
31 add_prop("waydroid.host.gid", "group_id")
32 add_prop("waydroid.xdg_runtime_dir", "xdg_runtime_dir")
33 add_prop("waydroid.pulse_runtime_path", "pulse_runtime_path")
34 add_prop("waydroid.wayland_display", "wayland_display")
35 if which("waydroid-sensord") is None:
36 props
.append("waydroid.stub_sensors_hal=1")
37 dpi
= session_cfg
["session"]["lcd_density"]
39 props
.append("ro.sf.lcd_density=" + dpi
)
41 final_props
= open(full_props_path
, "w")
43 final_props
.write(prop
+ "\n")
45 os
.chmod(full_props_path
, 0o644)
47 def set_permissions(perm_list
=None, mode
="777"):
48 def chmod(path
, mode
):
49 if os
.path
.exists(path
):
50 command
= ["chmod", mode
, "-R", path
]
51 tools
.helpers
.run
.root(args
, command
, check
=False)
60 "/sys/kernel/debug/sync/sw_sync",
74 # Wayland and pulse socket permissions
75 session_cfg
["session"]["pulse_runtime_path"],
76 session_cfg
["session"]["xdg_runtime_dir"]
80 perm_list
.extend(glob
.glob("/dev/fb*"))
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
.root(args
, command
, check
=False)
128 tools
.helpers
.run
.root(
129 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
132 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"])
135 helpers
.mount
.bind(args
, session_cfg
["session"]["waydroid_data"],
136 tools
.config
.defaults
["data"])
140 command
= ["start", "cgroup-lite"]
141 tools
.helpers
.run
.root(args
, command
, check
=False)
142 helpers
.mount
.umount_all(args
, "/sys/fs/cgroup/schedtune")
144 #TODO: remove NFC hacks
146 command
= ["stop", "nfcd"]
147 tools
.helpers
.run
.root(args
, command
, check
=False)
152 helpers
.lxc
.start(args
)
153 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
155 while session_cfg
["session"]["state"] != "RUNNING" and timeout
> 0:
156 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
158 "waiting {} seconds for container to start...".format(timeout
))
159 timeout
= timeout
- 1
161 if session_cfg
["session"]["state"] != "RUNNING":
162 raise OSError("container failed to start")
163 tools
.config
.save_session(session_cfg
)
165 if not hasattr(args
, 'hardwareLoop'):
166 services
.hardware_manager
.start(args
)
168 signal
.signal(signal
.SIGINT
, signal_handler
)
169 while os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
170 session_cfg
= tools
.config
.load_session()
171 if session_cfg
["session"]["state"] == "STOPPED":
172 services
.hardware_manager
.stop(args
)
174 elif session_cfg
["session"]["state"] == "UNFREEZE":
175 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
176 tools
.config
.save_session(session_cfg
)
180 logging
.warning("session manager stopped, stopping container and waiting...")
184 logging
.error("WayDroid container is {}".format(status
))
187 status
= helpers
.lxc
.status(args
)
188 if status
!= "STOPPED":
189 helpers
.lxc
.stop(args
)
190 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
191 session_cfg
= tools
.config
.load_session()
192 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
193 tools
.config
.save_session(session_cfg
)
196 command
= [tools
.config
.tools_src
+
197 "/data/scripts/waydroid-net.sh", "stop"]
198 tools
.helpers
.run
.root(args
, command
, check
=False)
200 #TODO: remove NFC hacks
202 command
= ["start", "nfcd"]
203 tools
.helpers
.run
.root(args
, command
, check
=False)
206 if which("waydroid-sensord"):
207 command
= ["pidof", "waydroid-sensord"]
208 pid
= tools
.helpers
.run
.root(args
, command
, check
=False, output_return
=True)
210 command
= ["kill", "-9", pid
]
211 tools
.helpers
.run
.root(args
, command
, check
=False)
214 logging
.error("WayDroid container is {}".format(status
))
217 status
= helpers
.lxc
.status(args
)
218 if status
== "RUNNING":
219 helpers
.lxc
.stop(args
)
220 helpers
.lxc
.start(args
)
222 logging
.error("WayDroid container is {}".format(status
))
225 status
= helpers
.lxc
.status(args
)
226 if status
== "RUNNING":
227 helpers
.lxc
.freeze(args
)
228 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
229 session_cfg
= tools
.config
.load_session()
230 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
231 tools
.config
.save_session(session_cfg
)
233 logging
.error("WayDroid container is {}".format(status
))
236 status
= helpers
.lxc
.status(args
)
237 if status
== "FROZEN":
238 helpers
.lxc
.unfreeze(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
))