]> glassweightruler.freedombox.rocks Git - waydroid.git/commitdiff
Add -w flag for waiting for init
authorAlessandro Astone <ales.astone@gmail.com>
Fri, 6 May 2022 17:33:08 +0000 (19:33 +0200)
committerErfan Abdi <erfangplus@gmail.com>
Wed, 6 Jul 2022 16:22:21 +0000 (20:52 +0430)
This allows for example to start the container manager before init

tools/__init__.py
tools/actions/initializer.py
tools/helpers/__init__.py
tools/helpers/arguments.py
tools/helpers/ipc.py [new file with mode: 0644]

index 2afdf42532af1dc71022c377cdf227025d61c81f..ac67a03c6af7a036087dfe457a150f59ac68ac36 100644 (file)
@@ -31,10 +31,16 @@ def main():
         args.sudo_timer = True
         args.timeout = 1800
 
-        if not os.path.isfile(args.config):
+        if not actions.initializer.is_initialized(args):
             if args.action and (args.action != "init" and args.action != "log"):
-                print('ERROR: WayDroid is not initialized, run "waydroid init"')
-                return 0
+                if not args.wait_for_init:
+                    print('ERROR: WayDroid is not initialized, run "waydroid init"')
+                    return 0
+
+                print('WayDroid waiting for initialization...')
+                while helpers.ipc.listen(channel="init") != "done":
+                    pass
+
             elif os.geteuid() == 0 and args.action == "init":
                 if not os.path.exists(args.work):
                     os.mkdir(args.work)
index 4a80bb998b4cca1802a54e637fec7fee50059517..b7124d4fe15f156fac06ded68832e294c07a6c9b 100644 (file)
@@ -5,6 +5,8 @@ import os
 from tools import helpers
 import tools.config
 
+def is_initialized(args):
+    return os.path.isfile(args.config) and os.path.isdir(tools.config.defaults["rootfs"])
 
 def get_vendor_type(args):
     vndk_str = helpers.props.host_get(args, "ro.vndk.version")
@@ -81,7 +83,7 @@ def setup_config(args):
     tools.config.save(args, cfg)
 
 def init(args):
-    if not os.path.isfile(args.config) or not os.path.isdir(tools.config.defaults["rootfs"]) or args.force:
+    if not is_initialized(args) or args.force:
         setup_config(args)
         status = "STOPPED"
         if os.path.exists(tools.config.defaults["lxc"] + "/waydroid"):
@@ -101,5 +103,7 @@ def init(args):
             logging.info("Starting container")
             helpers.images.mount_rootfs(args, args.images_path)
             helpers.lxc.start(args)
+
+        helpers.ipc.notify(channel="init", msg="done")
     else:
         logging.info("Already initialized")
index 21357cf3bd5d325ef72ec65aee8e094fff6b3799..20d4d62b1041412766c95e40b65ff06278eac8c8 100644 (file)
@@ -8,3 +8,4 @@ import tools.helpers.images
 import tools.helpers.drivers
 import tools.helpers.mount
 import tools.helpers.http
+import tools.helpers.ipc
index 9243eb7f19488caa6a279b0b8bb2892c629d4312..f686032b5726eace8ae9153366bcaf1ea0599c49 100644 (file)
@@ -129,6 +129,8 @@ def arguments():
                         " logfiles (this may reduce performance)")
     parser.add_argument("-q", "--quiet", dest="quiet", action="store_true",
                         help="do not output any log messages")
+    parser.add_argument("-w", "--wait", dest="wait_for_init", action="store_true",
+                        help="wait for init before running")
 
     # Actions
     sub = parser.add_subparsers(title="action", dest="action")
diff --git a/tools/helpers/ipc.py b/tools/helpers/ipc.py
new file mode 100644 (file)
index 0000000..fbf522f
--- /dev/null
@@ -0,0 +1,26 @@
+# Copyright 2022 Alessandro Astone
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+# Currently implemented as FIFO
+import os
+
+BASE_DIR = "/var/run/"
+
+def listen(channel):
+    pipe = BASE_DIR + "waydroid-" + channel
+    if not os.path.exists(pipe):
+        os.mkfifo(pipe)
+    with open(pipe) as fifo:
+        while True:
+            data = fifo.read()
+            if len(data) != 0:
+                return data
+
+def notify(channel, msg):
+    pipe = BASE_DIR + "waydroid-" + channel
+    try:
+        fd = os.open(pipe, os.O_WRONLY | os.O_NONBLOCK)
+        with os.fdopen(fd, "w") as fifo:
+            fifo.write(msg)
+    except Exception:
+        pass