summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHung-ying Tyan <tyanh@google.com>2020-09-02 05:09:20 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2020-09-02 05:09:20 +0000
commit57c23dd518ebf9c8f632f96c3e573289749499a2 (patch)
treec50b30c69ae98d91ab9cfafb9e2e3b9b94382b4c
parent57f65da1fd5e82e82bbbf400765d18f510761b6c (diff)
parenta6a44f726e647e2f1b567fb5bfc8620972590098 (diff)
downloadvold-android10-gsi.tar.gz
Merge "Merge branch android10-qpr3-release" into android10-gsiandroid10-gsi
-rw-r--r--Loop.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/Loop.cpp b/Loop.cpp
index fa8f8ba2..f5d2481c 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -31,6 +31,9 @@
#include <linux/kdev_t.h>
+#include <chrono>
+#include <thread>
+
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
@@ -41,12 +44,29 @@
#include "VoldUtil.h"
#include "sehandle.h"
+using namespace std::literals;
using android::base::StringPrintf;
using android::base::unique_fd;
static const char* kVoldPrefix = "vold:";
static constexpr size_t kLoopDeviceRetryAttempts = 3u;
+static bool wait_for_file(const std::string& filename,
+ const std::chrono::milliseconds relative_timeout) {
+ auto start_time = std::chrono::steady_clock::now();
+
+ while (true) {
+ int rv = access(filename.c_str(), F_OK);
+ if (!rv || errno != ENOENT) return true;
+
+ std::this_thread::sleep_for(50ms);
+
+ auto now = std::chrono::steady_clock::now();
+ auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+ if (time_elapsed > relative_timeout) return false;
+ }
+}
+
int Loop::create(const std::string& target, std::string& out_device) {
unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
if (ctl_fd.get() == -1) {
@@ -74,6 +94,12 @@ int Loop::create(const std::string& target, std::string& out_device) {
PLOG(ERROR) << "Failed to open " << target;
return -errno;
}
+
+ if (!wait_for_file(out_device, 2s)) {
+ LOG(ERROR) << "Failed to find " << out_device;
+ return -ENOENT;
+ }
+
unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
if (device_fd.get() == -1) {
PLOG(ERROR) << "Failed to open " << out_device;