1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
5 from tools
import helpers
10 import multiprocessing
16 from gi
.repository
import GLib
18 def is_initialized(args
):
19 return os
.path
.isfile(args
.config
) and os
.path
.isdir(tools
.config
.defaults
["rootfs"])
21 def get_vendor_type(args
):
22 vndk_str
= helpers
.props
.host_get(args
, "ro.vndk.version")
27 vndk
-= 1 # 12L -> Halium 12
29 ret
= "HALIUM_" + str(vndk
- 19)
33 def setup_config(args
):
34 cfg
= tools
.config
.load(args
)
35 args
.arch
= helpers
.arch
.host()
36 cfg
["waydroid"]["arch"] = args
.arch
38 args
.vendor_type
= get_vendor_type(args
)
39 cfg
["waydroid"]["vendor_type"] = args
.vendor_type
41 helpers
.drivers
.setupBinderNodes(args
)
42 cfg
["waydroid"]["binder"] = args
.BINDER_DRIVER
43 cfg
["waydroid"]["vndbinder"] = args
.VNDBINDER_DRIVER
44 cfg
["waydroid"]["hwbinder"] = args
.HWBINDER_DRIVER
46 has_preinstalled_images
= False
47 preinstalled_images_paths
= tools
.config
.defaults
["preinstalled_images_paths"]
48 for preinstalled_images
in preinstalled_images_paths
:
49 if os
.path
.isdir(preinstalled_images
):
50 if os
.path
.isfile(preinstalled_images
+ "/system.img") and os
.path
.isfile(preinstalled_images
+ "/vendor.img"):
51 has_preinstalled_images
= True
52 args
.images_path
= preinstalled_images
55 logging
.warning("Found directory {} but missing system or vendor image, ignoring...".format(preinstalled_images
))
57 if not args
.images_path
:
58 args
.images_path
= tools
.config
.defaults
["images_path"]
59 cfg
["waydroid"]["images_path"] = args
.images_path
61 if has_preinstalled_images
:
62 cfg
["waydroid"]["system_ota"] = args
.system_ota
= "None"
63 cfg
["waydroid"]["vendor_ota"] = args
.vendor_ota
= "None"
64 cfg
["waydroid"]["system_datetime"] = tools
.config
.defaults
["system_datetime"]
65 cfg
["waydroid"]["vendor_datetime"] = tools
.config
.defaults
["vendor_datetime"]
66 tools
.config
.save(args
, cfg
)
69 channels_cfg
= tools
.config
.load_channels()
70 if not args
.system_channel
:
71 args
.system_channel
= channels_cfg
["channels"]["system_channel"]
72 if not args
.vendor_channel
:
73 args
.vendor_channel
= channels_cfg
["channels"]["vendor_channel"]
75 args
.rom_type
= channels_cfg
["channels"]["rom_type"]
76 if not args
.system_type
:
77 args
.system_type
= channels_cfg
["channels"]["system_type"]
79 if not args
.system_channel
or not args
.vendor_channel
:
80 logging
.error("ERROR: You must provide 'System OTA' and 'Vendor OTA' URLs.")
83 args
.system_ota
= args
.system_channel
+ "/" + args
.rom_type
+ \
84 "/waydroid_" + args
.arch
+ "/" + args
.system_type
+ ".json"
85 system_request
= helpers
.http
.retrieve(args
.system_ota
)
86 if system_request
[0] != 200:
88 "Failed to get system OTA channel: {}, error: {}".format(args
.system_ota
, system_request
[0]))
90 device_codename
= helpers
.props
.host_get(args
, "ro.product.device")
91 args
.vendor_type
= None
92 for vendor
in [device_codename
, get_vendor_type(args
)]:
93 vendor_ota
= args
.vendor_channel
+ "/waydroid_" + \
94 args
.arch
+ "/" + vendor
.replace(" ", "_") + ".json"
95 vendor_request
= helpers
.http
.retrieve(vendor_ota
)
96 if vendor_request
[0] == 200:
97 args
.vendor_type
= vendor
98 args
.vendor_ota
= vendor_ota
101 if not args
.vendor_type
:
103 "Failed to get vendor OTA channel: {}".format(vendor_ota
))
105 if args
.system_ota
!= cfg
["waydroid"].get("system_ota"):
106 cfg
["waydroid"]["system_datetime"] = tools
.config
.defaults
["system_datetime"]
107 if args
.vendor_ota
!= cfg
["waydroid"].get("vendor_ota"):
108 cfg
["waydroid"]["vendor_datetime"] = tools
.config
.defaults
["vendor_datetime"]
110 cfg
["waydroid"]["vendor_type"] = args
.vendor_type
111 cfg
["waydroid"]["system_ota"] = args
.system_ota
112 cfg
["waydroid"]["vendor_ota"] = args
.vendor_ota
113 tools
.config
.save(args
, cfg
)
117 if not is_initialized(args
) or args
.force
:
118 initializer_service
= None
120 initializer_service
= tools
.helpers
.ipc
.DBusContainerService("/Initializer", "id.waydro.Initializer")
121 except dbus
.DBusException
:
123 if not setup_config(args
):
126 if os
.path
.exists(tools
.config
.defaults
["lxc"] + "/waydroid"):
127 status
= helpers
.lxc
.status(args
)
128 if status
!= "STOPPED":
129 logging
.info("Stopping container")
131 container
= tools
.helpers
.ipc
.DBusContainerService()
132 args
.session
= container
.GetSession()
133 container
.Stop(False)
134 except Exception as e
:
136 tools
.actions
.container_manager
.stop(args
)
137 if args
.images_path
not in tools
.config
.defaults
["preinstalled_images_paths"]:
138 helpers
.images
.get(args
)
140 helpers
.images
.remove_overlay(args
)
141 if not os
.path
.isdir(tools
.config
.defaults
["rootfs"]):
142 os
.mkdir(tools
.config
.defaults
["rootfs"])
143 if not os
.path
.isdir(tools
.config
.defaults
["overlay"]):
144 os
.mkdir(tools
.config
.defaults
["overlay"])
145 os
.mkdir(tools
.config
.defaults
["overlay"]+"/vendor")
146 if not os
.path
.isdir(tools
.config
.defaults
["overlay_rw"]):
147 os
.mkdir(tools
.config
.defaults
["overlay_rw"])
148 os
.mkdir(tools
.config
.defaults
["overlay_rw"]+"/system")
149 os
.mkdir(tools
.config
.defaults
["overlay_rw"]+"/vendor")
150 helpers
.drivers
.probeAshmemDriver(args
)
151 helpers
.lxc
.setup_host_perms(args
)
152 helpers
.lxc
.set_lxc_config(args
)
153 helpers
.lxc
.make_base_props(args
)
154 if status
!= "STOPPED":
155 logging
.info("Starting container")
157 container
.Start(args
.session
)
158 except Exception as e
:
160 logging
.error("Failed to restart container. Please do so manually.")
162 if "running_init_in_service" not in args
or not args
.running_init_in_service
:
164 if initializer_service
:
165 initializer_service
.Done()
166 except dbus
.DBusException
:
169 logging
.info("Already initialized")
171 def wait_for_init(args
):
172 helpers
.ipc
.create_channel("remote_init_output")
174 mainloop
= GLib
.MainLoop()
175 dbus_obj
= DbusInitializer(mainloop
, dbus
.SystemBus(), '/Initializer', args
)
179 dbus_obj
.remove_from_connection()
181 class DbusInitializer(dbus
.service
.Object
):
182 def __init__(self
, looper
, bus
, object_path
, args
):
185 dbus
.service
.Object
.__init
__(self
, bus
, object_path
)
187 @dbus.service.method("id.waydro.Initializer", in_signature
='a{ss}', out_signature
='', sender_keyword
="sender", connection_keyword
="conn")
188 def Init(self
, params
, sender
=None, conn
=None):
189 channels_cfg
= tools
.config
.load_channels()
190 no_auth
= params
["system_channel"] == channels_cfg
["channels"]["system_channel"] and \
191 params
["vendor_channel"] == channels_cfg
["channels"]["vendor_channel"]
192 if no_auth
or ensure_polkit_auth(sender
, conn
, "id.waydro.Initializer.Init"):
193 threading
.Thread(target
=remote_init_server
, args
=(self
.args
, params
)).start()
195 raise PermissionError("Polkit: Authentication failed")
197 @dbus.service.method("id.waydro.Initializer", in_signature
='', out_signature
='')
199 if is_initialized(self
.args
):
202 def ensure_polkit_auth(sender
, conn
, privilege
):
203 dbus_info
= dbus
.Interface(conn
.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus/Bus", False), "org.freedesktop.DBus")
204 pid
= dbus_info
.GetConnectionUnixProcessID(sender
)
205 polkit
= dbus
.Interface(dbus
.SystemBus().get_object("org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", False), "org.freedesktop.PolicyKit1.Authority")
207 (is_auth
, _
, _
) = polkit
.CheckAuthorization(
209 "pid": dbus
.UInt32(pid
, variant_level
=1),
210 "start-time": dbus
.UInt64(0, variant_level
=1)}),
211 privilege
, {"AllowUserInteraction": "true"}
,
216 except dbus
.DBusException
:
217 raise PermissionError("Polkit: Authentication timed out")
219 def background_remote_init_process(args
):
220 with helpers
.ipc
.open_channel("remote_init_output", "wb") as channel_out
:
221 class StdoutRedirect(logging
.StreamHandler
):
223 channel_out
.write(str.encode(s
))
226 def emit(self
, record
):
227 if record
.levelno
>= logging
.INFO
:
228 self
.write(self
.format(record
) + self
.terminator
)
230 out
= StdoutRedirect()
231 sys
.stdout
= sys
.stderr
= out
232 logging
.getLogger().addHandler(out
)
234 ctl_queue
= queue
.Queue()
238 except Exception as e
:
244 poller
= select
.poll()
245 poller
.register(channel_out
, select
.POLLERR
)
247 # When reaching here the client was terminated
250 init_thread
= threading
.Thread(target
=try_init
, args
=(args
,))
251 init_thread
.daemon
= True
254 poll_thread
= threading
.Thread(target
=poll_pipe
)
255 poll_thread
.daemon
= True
258 # Join any one of the two threads
259 # Then exit the subprocess to kill the remaining thread.
260 # Can you believe this is the only way to kill a thread in python???
263 sys
.stdout
= sys
.__stdout
__
264 sys
.stderr
= sys
.__stderr
__
265 logging
.getLogger().removeHandler(out
)
267 def remote_init_server(args
, params
):
269 args
.images_path
= ""
271 args
.system_channel
= params
["system_channel"]
272 args
.vendor_channel
= params
["vendor_channel"]
273 args
.system_type
= params
["system_type"]
274 args
.running_init_in_service
= True
276 p
= multiprocessing
.Process(target
=background_remote_init_process
, args
=(args
,))
281 def remote_init_client(args
):
282 # Local imports cause Gtk is intrusive
284 gi
.require_version("Gtk", "3.0")
285 from gi
.repository
import Gtk
287 bus
= dbus
.SystemBus()
289 if is_initialized(args
):
291 tools
.helpers
.ipc
.DBusContainerService("/Initializer", "id.waydro.Initializer").Done()
292 except dbus
.DBusException
:
296 def notify_and_quit(caller
):
297 if is_initialized(args
):
299 tools
.helpers
.ipc
.DBusContainerService("/Initializer", "id.waydro.Initializer").Done()
300 except dbus
.DBusException
:
302 GLib
.idle_add(Gtk
.main_quit
)
304 class WaydroidInitWindow(Gtk
.Window
):
306 super().__init
__(title
="Initialize Waydroid")
307 channels_cfg
= tools
.config
.load_channels()
309 self
.set_default_size(600, 250)
310 self
.set_icon_name("waydroid")
312 grid
= Gtk
.Grid(row_spacing
=6, column_spacing
=6, margin
=10, column_homogeneous
=True)
313 grid
.set_hexpand(True)
314 grid
.set_vexpand(True)
317 sysOtaLabel
= Gtk
.Label("System OTA")
318 sysOtaEntry
= Gtk
.Entry()
319 sysOtaEntry
.set_text(channels_cfg
["channels"]["system_channel"])
320 grid
.attach(sysOtaLabel
, 0, 0, 1, 1)
321 grid
.attach_next_to(sysOtaEntry
,sysOtaLabel
, Gtk
.PositionType
.RIGHT
, 2, 1)
322 self
.sysOta
= sysOtaEntry
.get_buffer()
324 vndOtaLabel
= Gtk
.Label("Vendor OTA")
325 vndOtaEntry
= Gtk
.Entry()
326 vndOtaEntry
.set_text(channels_cfg
["channels"]["vendor_channel"])
327 grid
.attach(vndOtaLabel
, 0, 1, 1, 1)
328 grid
.attach_next_to(vndOtaEntry
, vndOtaLabel
, Gtk
.PositionType
.RIGHT
, 2, 1)
329 self
.vndOta
= vndOtaEntry
.get_buffer()
331 sysTypeLabel
= Gtk
.Label("Android Type")
332 sysTypeCombo
= Gtk
.ComboBoxText()
333 sysTypeCombo
.set_entry_text_column(0)
334 for t
in ["VANILLA", "GAPPS"]:
335 sysTypeCombo
.append_text(t
)
336 sysTypeCombo
.set_active(0)
337 grid
.attach(sysTypeLabel
, 0, 2, 1, 1)
338 grid
.attach_next_to(sysTypeCombo
, sysTypeLabel
, Gtk
.PositionType
.RIGHT
, 2, 1)
339 self
.sysType
= sysTypeCombo
341 downloadBtn
= Gtk
.Button("Download")
342 downloadBtn
.connect("clicked", self
.on_download_btn_clicked
)
343 grid
.attach(downloadBtn
, 1,3,1,1)
344 self
.downloadBtn
= downloadBtn
346 doneBtn
= Gtk
.Button("Done")
347 doneBtn
.connect("clicked", lambda x
: self
.destroy())
348 doneBtn
.get_style_context().add_class('suggested-action')
349 grid
.attach_next_to(doneBtn
, downloadBtn
, Gtk
.PositionType
.RIGHT
, 1, 1)
350 self
.doneBtn
= doneBtn
352 outScrolledWindow
= Gtk
.ScrolledWindow()
353 outScrolledWindow
.set_hexpand(True)
354 outScrolledWindow
.set_vexpand(True)
355 outTextView
= Gtk
.TextView()
356 outTextView
.set_property('editable', False)
357 outTextView
.set_property('cursor-visible', False)
358 outScrolledWindow
.add(outTextView
)
359 grid
.attach(outScrolledWindow
, 0, 4, 3, 1)
360 self
.outScrolledWindow
= outScrolledWindow
361 self
.outTextView
= outTextView
362 self
.outBuffer
= outTextView
.get_buffer()
363 self
.outBuffer
.create_mark("end", self
.outBuffer
.get_end_iter(), False)
365 self
.open_channel
= None
367 def scroll_to_bottom(self
):
368 self
.outTextView
.scroll_mark_onscreen(self
.outBuffer
.get_mark("end"))
370 def on_download_btn_clicked(self
, widget
):
371 widget
.set_sensitive(False)
373 self
.outTextView
.show()
374 init_params
= (self
.sysOta
.get_text(), self
.vndOta
.get_text(), self
.sysType
.get_active_text())
375 init_runner
= threading
.Thread(target
=self
.run_init
, args
=init_params
)
376 init_runner
.daemon
= True
379 def run_init(self
, systemOta
, vendorOta
, systemType
):
381 if s
.startswith('\r'):
382 last
= self
.outBuffer
.get_iter_at_line(self
.outBuffer
.get_line_count()-1)
384 self
.outBuffer
.delete(last
, self
.outBuffer
.get_end_iter())
385 self
.outBuffer
.insert(self
.outBuffer
.get_end_iter(), s
)
386 self
.scroll_to_bottom()
388 GLib
.idle_add(draw_sync
, s
)
390 if self
.open_channel
is not None:
391 self
.open_channel
.close()
392 # Wait for other end to reset
395 draw("Waiting for waydroid container service...\n")
398 "system_channel": self
.sysOta
.get_text(),
399 "vendor_channel": self
.vndOta
.get_text(),
400 "system_type": self
.sysType
.get_active_text()
402 tools
.helpers
.ipc
.DBusContainerService("/Initializer", "id.waydro.Initializer").Init(params
, timeout
=310)
403 except dbus
.DBusException
as e
:
404 if e
.get_dbus_name() == "org.freedesktop.DBus.Python.PermissionError":
405 draw(e
.get_dbus_message().splitlines()[-1] + "\n")
407 draw("The waydroid container service is not listening\n")
408 GLib
.idle_add(self
.downloadBtn
.set_sensitive
, True)
411 with helpers
.ipc
.open_channel("remote_init_output", "rb") as channel
:
412 self
.open_channel
= channel
413 GLib
.idle_add(self
.downloadBtn
.set_sensitive
, True)
417 data
= channel
.read(1)
431 draw("\nInterrupted\n")
433 if is_initialized(args
):
434 GLib
.idle_add(self
.doneBtn
.show
)
438 GLib
.set_prgname("Waydroid")
439 win
= WaydroidInitWindow()
440 win
.connect("destroy", notify_and_quit
)
443 win
.outTextView
.hide()