Skip to content

Commit

Permalink
Report errors if loopback device creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
vosst committed Apr 12, 2017
1 parent d8612d3 commit 9ef45b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/anbox/cmds/container_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,17 @@ bool anbox::cmds::ContainerManager::setup_mounts() {
if (!fs::exists(android_rootfs_dir))
fs::create_directory(android_rootfs_dir);

auto loop_device = common::LoopDeviceAllocator::new_device();
if (!loop_device)
std::shared_ptr<common::LoopDevice> loop_device;

try {
loop_device = common::LoopDeviceAllocator::new_device();
} catch (const std::exception& e) {
ERROR("Could not create loopback device: %s", e.what());
return false;
} catch (...) {
ERROR("Could not create loopback device");
return false;
}

if (!loop_device->attach_file(android_img_path)) {
ERROR("Failed to attach Android rootfs image to loopback device");
Expand Down
5 changes: 4 additions & 1 deletion src/anbox/common/loop_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include "anbox/common/loop_device.h"
#include "anbox/defer_action.h"

#include <system_error>

#include <linux/loop.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>

Expand All @@ -27,7 +30,7 @@ namespace common {
std::shared_ptr<LoopDevice> LoopDevice::create(const boost::filesystem::path &path) {
const auto fd = ::open(path.c_str(), O_RDWR);
if (fd < 0)
return nullptr;
throw std::system_error{errno, std::system_category()};

return std::shared_ptr<LoopDevice>(new LoopDevice(Fd{fd}, path));
}
Expand Down
13 changes: 6 additions & 7 deletions src/anbox/common/loop_device_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
#include <boost/filesystem.hpp>

#include <linux/loop.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <system_error>

namespace fs = boost::filesystem;

namespace {
Expand All @@ -38,19 +41,15 @@ namespace common {
std::shared_ptr<LoopDevice> LoopDeviceAllocator::new_device() {
const auto ctl_fd = ::open(loop_control_path, O_RDWR);
if (ctl_fd < 0)
return nullptr;
throw std::system_error{errno, std::system_category()};

DeferAction close_ctl_fd{[&]() { ::close(ctl_fd); }};

const auto device_nr = ::ioctl(ctl_fd, LOOP_CTL_GET_FREE);
if (device_nr < 0)
return nullptr;

const auto path = utils::string_format("%s%d", base_loop_path, device_nr);
if (!fs::exists(path))
return nullptr;
throw std::system_error{errno, std::system_category()};

return LoopDevice::create(path);
return LoopDevice::create(utils::string_format("%s%d", base_loop_path, device_nr));
}
} // namespace common
} // namespace anbox

0 comments on commit 9ef45b4

Please sign in to comment.