]>
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 set_permissions(perm_list
=None, mode
="777"):
18 def chmod(path
, mode
):
19 if os
.path
.exists(path
):
20 command
= ["chmod", mode
, "-R", path
]
21 tools
.helpers
.run
.user(args
, command
, check
=False)
30 "/sys/kernel/debug/sync/sw_sync",
46 perm_list
.extend(glob
.glob("/dev/fb*"))
48 perm_list
.extend(glob
.glob("/dev/video*"))
50 for path
in perm_list
:
53 def signal_handler(sig
, frame
):
54 services
.hardware_manager
.stop(args
)
58 status
= helpers
.lxc
.status(args
)
59 if status
== "STOPPED":
60 # Load binder and ashmem drivers
61 cfg
= tools
.config
.load(args
)
62 if cfg
["waydroid"]["vendor_type"] == "MAINLINE":
63 if helpers
.drivers
.probeBinderDriver(args
) != 0:
64 logging
.error("Failed to load Binder driver")
65 if helpers
.drivers
.probeAshmemDriver(args
) != 0:
66 logging
.error("Failed to load Ashmem driver")
67 helpers
.drivers
.loadBinderNodes(args
)
69 "/dev/" + args
.BINDER_DRIVER
,
70 "/dev/" + args
.VNDBINDER_DRIVER
,
71 "/dev/" + args
.HWBINDER_DRIVER
74 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
75 session_cfg
= tools
.config
.load_session()
76 if session_cfg
["session"]["state"] != "STOPPED":
77 logging
.warning("Found session config on state: {}, restart session".format(
78 session_cfg
["session"]["state"]))
79 os
.remove(tools
.config
.session_defaults
["config_path"])
80 logging
.debug("Container manager is waiting for session to load")
81 while not os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
84 # Load session configs
85 session_cfg
= tools
.config
.load_session()
88 command
= [tools
.config
.tools_src
+
89 "/data/scripts/waydroid-net.sh", "start"]
90 tools
.helpers
.run
.user(args
, command
, check
=False)
93 if which("waydroid-sensord"):
94 tools
.helpers
.run
.user(
95 args
, ["waydroid-sensord", "/dev/" + args
.HWBINDER_DRIVER
], output
="background")
98 helpers
.images
.mount_rootfs(args
, cfg
["waydroid"]["images_path"])
100 helpers
.protocol
.set_aidl_version(args
)
103 helpers
.mount
.bind(args
, session_cfg
["session"]["waydroid_data"],
104 tools
.config
.defaults
["data"])
108 command
= ["start", "cgroup-lite"]
109 tools
.helpers
.run
.user(args
, command
, check
=False)
110 if os
.path
.ismount("/sys/fs/cgroup/schedtune"):
111 command
= ["umount", "-l", "/sys/fs/cgroup/schedtune"]
112 tools
.helpers
.run
.user(args
, command
, check
=False)
114 #TODO: remove NFC hacks
116 command
= ["stop", "nfcd"]
117 tools
.helpers
.run
.user(args
, command
, check
=False)
122 helpers
.lxc
.start(args
)
123 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
125 while session_cfg
["session"]["state"] != "RUNNING" and timeout
> 0:
126 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
128 "waiting {} seconds for container to start...".format(timeout
))
129 timeout
= timeout
- 1
131 if session_cfg
["session"]["state"] != "RUNNING":
132 raise OSError("container failed to start")
133 tools
.config
.save_session(session_cfg
)
135 services
.hardware_manager
.start(args
)
137 signal
.signal(signal
.SIGINT
, signal_handler
)
138 while os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
139 session_cfg
= tools
.config
.load_session()
140 if session_cfg
["session"]["state"] == "STOPPED":
141 services
.hardware_manager
.stop(args
)
143 elif session_cfg
["session"]["state"] == "UNFREEZE":
144 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
145 tools
.config
.save_session(session_cfg
)
149 logging
.warning("session manager stopped, stopping container and waiting...")
151 services
.hardware_manager
.stop(args
)
154 logging
.error("WayDroid container is {}".format(status
))
157 status
= helpers
.lxc
.status(args
)
158 if status
!= "STOPPED":
159 helpers
.lxc
.stop(args
)
160 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
161 session_cfg
= tools
.config
.load_session()
162 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
163 tools
.config
.save_session(session_cfg
)
166 command
= [tools
.config
.tools_src
+
167 "/data/scripts/waydroid-net.sh", "stop"]
168 tools
.helpers
.run
.user(args
, command
, check
=False)
170 #TODO: remove NFC hacks
172 command
= ["start", "nfcd"]
173 tools
.helpers
.run
.user(args
, command
, check
=False)
176 if which("waydroid-sensord"):
177 command
= ["pidof", "waydroid-sensord"]
178 pid
= tools
.helpers
.run
.user(args
, command
, check
=False, output_return
=True).strip()
180 command
= ["kill", "-9", pid
]
181 tools
.helpers
.run
.user(args
, command
, check
=False)
184 helpers
.images
.umount_rootfs(args
)
187 helpers
.mount
.umount_all(args
, tools
.config
.defaults
["data"])
190 logging
.error("WayDroid container is {}".format(status
))
193 status
= helpers
.lxc
.status(args
)
194 if status
== "RUNNING":
195 helpers
.lxc
.stop(args
)
196 helpers
.lxc
.start(args
)
198 logging
.error("WayDroid container is {}".format(status
))
201 status
= helpers
.lxc
.status(args
)
202 if status
== "RUNNING":
203 helpers
.lxc
.freeze(args
)
204 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
205 session_cfg
= tools
.config
.load_session()
206 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
207 tools
.config
.save_session(session_cfg
)
209 logging
.error("WayDroid container is {}".format(status
))
212 status
= helpers
.lxc
.status(args
)
213 if status
== "FROZEN":
214 helpers
.lxc
.unfreeze(args
)
215 if os
.path
.exists(tools
.config
.session_defaults
["config_path"]):
216 session_cfg
= tools
.config
.load_session()
217 session_cfg
["session"]["state"] = helpers
.lxc
.status(args
)
218 tools
.config
.save_session(session_cfg
)
220 logging
.error("WayDroid container is {}".format(status
))