Simplifies error handling in region open and adds test for
VSOC_GET_FD_SCOPED_PERMISSION
Test: run locally
Change-Id: I6bfcd6495016c014882a9654d9e6a6034c73cf9b
diff --git a/guest/vsoc/lib/guest_region.cpp b/guest/vsoc/lib/guest_region.cpp
index 796116e..a148711 100644
--- a/guest/vsoc/lib/guest_region.cpp
+++ b/guest/vsoc/lib/guest_region.cpp
@@ -88,13 +88,14 @@
vsoc_reg_off_t end_offset) {
if (!region_fd_->IsOpen()) {
LOG(FATAL) << "Can't create permission before opening controller region";
- return VSOC_PERM_ERROR;
+ return -EINVAL;
}
int managed_region_fd =
open(device_path_from_name(managed_region_name).c_str(), O_RDWR);
if (managed_region_fd < 0) {
+ int errno_ = errno;
LOG(FATAL) << "Can't open managed region: " << managed_region_name;
- return VSOC_PERM_ERROR;
+ return -errno_;
}
fd_scoped_permission_arg perm;
@@ -106,15 +107,12 @@
LOG(INFO) << "owner offset: " << perm.perm.owner_offset;
int retval = region_fd_->Ioctl(VSOC_CREATE_FD_SCOPED_PERMISSION, &perm);
if (retval) {
- retval = errno;
- close(managed_region_fd);
- if (retval == EBUSY) {
- return VSOC_PERM_OWNED;
- } else {
- LOG(FATAL) << "Unable to create fd scoped permission (" <<
- strerror(retval) << ")";
- return VSOC_PERM_ERROR;
+ int errno_ = errno;
+ if (errno != EBUSY) {
+ LOG(FATAL) << "Unable to create fd scoped permission (" <<
+ strerror(errno) << ")";
}
+ return -errno_;
}
return managed_region_fd;
}
diff --git a/guest/vsoc/lib/guest_region.h b/guest/vsoc/lib/guest_region.h
index 1f4b902..c009ef8 100644
--- a/guest/vsoc/lib/guest_region.h
+++ b/guest/vsoc/lib/guest_region.h
@@ -30,15 +30,6 @@
namespace vsoc {
-enum {
- // Means an unrecoverable error ocurred, aborting is usually the best handling
- // strategy in this case.
- VSOC_PERM_ERROR = -2,
- // Means that the permission could not be created because someone else
- // reserved the memory first. Find another area of memory and try again.
- VSOC_PERM_OWNED = -1,
-};
-
/**
* Accessor class for VSoC regions designed for use from processes on the
* host. This mainly affects the implementation of Open.
@@ -118,7 +109,7 @@
*
* On success returns an open fd with the requested permission asociated to
* it. If another thread/process acquired ownership of *owner_ptr before this
- * one returns VSOC_PERM_OWNED. Returns VSOC_PERM_ERROR otherwise.
+ * one returns -EBUSY, returns a different negative number otherwise.
*/
int CreateFdScopedPermission(uint32_t* owner_ptr,
uint32_t owned_val,
diff --git a/guest/vsoc/lib/guest_region_e2e_test.cpp b/guest/vsoc/lib/guest_region_e2e_test.cpp
index f2a21ad..b662c04 100644
--- a/guest/vsoc/lib/guest_region_e2e_test.cpp
+++ b/guest/vsoc/lib/guest_region_e2e_test.cpp
@@ -165,8 +165,10 @@
begin_offset,
end_offset);
EXPECT_TRUE(perm_fd >= 0);
+ fd_scoped_permission perm;
+ ASSERT_TRUE(ioctl(perm_fd, VSOC_GET_FD_SCOPED_PERMISSION, &perm) == 0);
void* mapped_ptr = mmap(NULL,
- end_offset - begin_offset,
+ perm.end_offset - perm.begin_offset,
PROT_WRITE | PROT_READ,
MAP_SHARED,
perm_fd,