Android doesn't use devtmpfs. Instead device nodes under /dev are
created by userspace daemon ueventd. There could be a noticeable delay
between LOOP_CTL_GET_FREE issued and loop device created, so we need to
wait until it is created and then continue.
The timeout (5s) and time quantum (20ms) is picked randomly, it bares no
special meaning but they worked fine empirically.

Bug: 243495405
From 35860d567aad464299ee1ab623d27ffa129a3763 Mon Sep 17 00:00:00 2001
From: Yi-Yo Chiang <[email protected]>
Date: Tue, 23 Aug 2022 23:30:14 +0800
Subject: [PATCH] losetup: Wait for ueventd to create loop device on Android

Android doesn't use devtmpfs. Instead device nodes under /dev are
created by userspace daemon ueventd. There could be a noticeable delay
between LOOP_CTL_GET_FREE issued and loop device created, so we need to
wait until it is created and then continue.
The timeout (5s) and time quantum (20ms) is picked randomly, it bares no
special meaning but they worked fine empirically.

Bug: 243495405
Change-Id: Ia56d347bf52f5730e7f46c30bf4f3f4d117149f8
---
 toys/other/losetup.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/toys/other/losetup.c b/toys/other/losetup.c
index ce84c1ef..0973273c 100644
--- a/toys/other/losetup.c
+++ b/toys/other/losetup.c
@@ -71,6 +71,16 @@ static int loopback_setup(char *device, char *file)
       }
       close(cfd);
     }
+    if (CFG_TOYBOX_ON_ANDROID && device) {
+      // ANDROID SPECIFIC: /dev is not devtmpfs, instead an userspace daemon
+      // ueventd is responsible for creating the loop devices under /dev.
+      // Wait for the uevent to be processed to avoid race.
+      long long timeout = millitime() + 5000;
+      do {
+        if (!access(device, F_OK) || errno != ENOENT) break;
+        msleep(20);
+      } while (millitime() < timeout);
+    }
   }
 
   if (device) lfd = open(device, TT.openflags);
-- 
2.37.1.595.g718a3a8f04-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to