summaryrefslogtreecommitdiff
path: root/src/helpers/MiscFunctions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers/MiscFunctions.cpp')
-rw-r--r--src/helpers/MiscFunctions.cpp44
1 files changed, 19 insertions, 25 deletions
diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp
index 5ebb0842..f532617c 100644
--- a/src/helpers/MiscFunctions.cpp
+++ b/src/helpers/MiscFunctions.cpp
@@ -836,50 +836,48 @@ bool envEnabled(const std::string& env) {
return std::string(ENV) == "1";
}
-std::pair<int, std::string> openExclusiveShm() {
+std::pair<CFileDescriptor, std::string> openExclusiveShm() {
// Only absolute paths can be shared across different shm_open() calls
std::string name = "/" + g_pTokenManager->getRandomUUID();
for (size_t i = 0; i < 69; ++i) {
- int fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fd >= 0)
- return {fd, name};
+ CFileDescriptor fd{shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600)};
+ if (fd.isValid())
+ return {std::move(fd), name};
}
- return {-1, ""};
+ return {{}, ""};
}
-int allocateSHMFile(size_t len) {
+CFileDescriptor allocateSHMFile(size_t len) {
auto [fd, name] = openExclusiveShm();
- if (fd < 0)
- return -1;
+ if (!fd.isValid())
+ return {};
shm_unlink(name.c_str());
int ret;
do {
- ret = ftruncate(fd, len);
+ ret = ftruncate(fd.get(), len);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
- close(fd);
- return -1;
+ return {};
}
- return fd;
+ return std::move(fd);
}
-bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr) {
+bool allocateSHMFilePair(size_t size, CFileDescriptor& rw_fd_ptr, CFileDescriptor& ro_fd_ptr) {
auto [fd, name] = openExclusiveShm();
- if (fd < 0) {
+ if (!fd.isValid()) {
return false;
}
// CLOEXEC is guaranteed to be set by shm_open
- int ro_fd = shm_open(name.c_str(), O_RDONLY, 0);
- if (ro_fd < 0) {
+ CFileDescriptor ro_fd{shm_open(name.c_str(), O_RDONLY, 0)};
+ if (!ro_fd.isValid()) {
shm_unlink(name.c_str());
- close(fd);
return false;
}
@@ -887,24 +885,20 @@ bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr) {
// Make sure the file cannot be re-opened in read-write mode (e.g. via
// "/proc/self/fd/" on Linux)
- if (fchmod(fd, 0) != 0) {
- close(fd);
- close(ro_fd);
+ if (fchmod(fd.get(), 0) != 0) {
return false;
}
int ret;
do {
- ret = ftruncate(fd, size);
+ ret = ftruncate(fd.get(), size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
- close(fd);
- close(ro_fd);
return false;
}
- *rw_fd_ptr = fd;
- *ro_fd_ptr = ro_fd;
+ rw_fd_ptr = std::move(fd);
+ ro_fd_ptr = std::move(ro_fd);
return true;
}