aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSarthak Kukreti <sarthakkukreti@google.com>2019-06-04 15:55:22 -0700
committerCommit Bot <commit-bot@chromium.org>2020-07-18 22:06:05 +0000
commit2078b13f65ccd31b45376b1599a1d50f976605f5 (patch)
treeba145c6e24836fdab5c94bc8584b6f0dbe0e5652
parent1ea6833dd52c54e39428ca07f4a34eb9d0461700 (diff)
downloadplatform_external_libbrillo-2078b13f65ccd31b45376b1599a1d50f976605f5.tar.gz
platform_external_libbrillo-2078b13f65ccd31b45376b1599a1d50f976605f5.tar.bz2
platform_external_libbrillo-2078b13f65ccd31b45376b1599a1d50f976605f5.zip
libbrillo: add retries for attach device
While it is unlikely, loopdev->AttachDevice should not indefinitely try to attach a loop device. Instead, set an arbitrary retry limit (10). BUG=chromium:968826 TEST=unittests. Change-Id: I124c11c241cfb2351a87681973f1f1d97e62ce36 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/1644362 Tested-by: Sarthak Kukreti <sarthakkukreti@chromium.org> Commit-Queue: Sarthak Kukreti <sarthakkukreti@chromium.org> Reviewed-by: Mike Frysinger <vapier@chromium.org> Reviewed-by: Gwendal Grignou <gwendal@chromium.org> Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: 3066f10027f9b8e4547a0ff352228eca60318128
-rw-r--r--brillo/blkdev_utils/loop_device.cc18
1 files changed, 17 insertions, 1 deletions
diff --git a/brillo/blkdev_utils/loop_device.cc b/brillo/blkdev_utils/loop_device.cc
index 2b2219d..0ba485a 100644
--- a/brillo/blkdev_utils/loop_device.cc
+++ b/brillo/blkdev_utils/loop_device.cc
@@ -35,6 +35,8 @@ constexpr char kDeviceIdPath[] = "dev";
constexpr char kLoopBackingFile[] = "loop/backing_file";
constexpr int kLoopDeviceIoctlFlags = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
constexpr int kLoopControlIoctlFlags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC;
+// Arbitrary retry limit for attempting to attach a loop device.
+constexpr int kMaxLoopDeviceAttachTries = 10;
// ioctl runner for LoopDevice and LoopDeviceManager
int LoopDeviceIoctl(const base::FilePath& device,
@@ -165,7 +167,9 @@ LoopDeviceManager::LoopDeviceManager(LoopIoctl ioctl_runner)
std::unique_ptr<LoopDevice> LoopDeviceManager::AttachDeviceToFile(
const base::FilePath& backing_file) {
int device_number = -1;
- while (true) {
+ int retries = kMaxLoopDeviceAttachTries;
+
+ while (retries >= 0) {
device_number =
loop_ioctl_.Run(base::FilePath(kLoopControl), LOOP_CTL_GET_FREE, 0,
kLoopControlIoctlFlags);
@@ -193,7 +197,19 @@ std::unique_ptr<LoopDevice> LoopDeviceManager::AttachDeviceToFile(
LOG(ERROR) << "ioctl(LOOP_SET_FD) failed";
return CreateLoopDevice(-1, base::FilePath());
}
+
+ // Other users could have set the file descriptor between get a valid
+ // loop device number and using it. Continue to loop until the device is
+ // created.
+ retries--;
+ }
+
+ if (retries < 0) {
+ LOG(ERROR) << "Failed to set up loop device after "
+ << kMaxLoopDeviceAttachTries << " retries.";
+ return CreateLoopDevice(-1, base::FilePath());
}
+
// All steps of setting up the loop device succeeded.
return CreateLoopDevice(device_number, backing_file);
}