core: move allocateSHM use CFileDescriptor

make miscfunctions allocateSHMFilePair and openExclusiveShm use
CFileDescript and move the related usage to it aswell.
This commit is contained in:
Tom Englund 2024-11-21 05:13:09 +01:00
parent 983e67633f
commit 3da837ec4b
6 changed files with 44 additions and 62 deletions

View file

@ -147,19 +147,17 @@ void IKeyboard::updateKeymapFD() {
xkbKeymapString = cKeymapStr;
free(cKeymapStr);
int rw, ro;
if (!allocateSHMFilePair(xkbKeymapString.length() + 1, &rw, &ro))
CFileDescriptor rw, ro;
if (!allocateSHMFilePair(xkbKeymapString.length() + 1, rw, ro))
Debug::log(ERR, "IKeyboard: failed to allocate shm pair for the keymap");
else {
auto keymapFDDest = mmap(nullptr, xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw, 0);
close(rw);
if (keymapFDDest == MAP_FAILED) {
auto keymapFDDest = mmap(nullptr, xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw.get(), 0);
if (keymapFDDest == MAP_FAILED)
Debug::log(ERR, "IKeyboard: failed to mmap a shm pair for the keymap");
close(ro);
} else {
else {
memcpy(keymapFDDest, xkbKeymapString.c_str(), xkbKeymapString.length());
munmap(keymapFDDest, xkbKeymapString.length() + 1);
xkbKeymapFD = CFileDescriptor(ro);
xkbKeymapFD = std::move(ro);
}
}

View file

@ -853,50 +853,46 @@ 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;
}
if (ret < 0)
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_ref, CFileDescriptor& ro_fd_ref) {
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 = CFileDescriptor(shm_open(name.c_str(), O_RDONLY, 0));
if (!ro_fd.isValid()) {
shm_unlink(name.c_str());
close(fd);
return false;
}
@ -904,24 +900,18 @@ 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);
if (ret < 0)
return false;
}
*rw_fd_ptr = fd;
*ro_fd_ptr = ro_fd;
rw_fd_ref = std::move(fd);
ro_fd_ref = std::move(ro_fd);
return true;
}

View file

@ -7,9 +7,12 @@
#include <vector>
#include <format>
#include <expected>
#include <hyprutils/os/FileDescriptor.hpp>
#include "../SharedDefs.hpp"
#include "../macros.hpp"
using namespace Hyprutils::OS;
struct SCallstackFrameInfo {
void* adr = nullptr;
std::string desc;
@ -39,8 +42,8 @@ double normalizeAngleRad(double ang);
std::vector<SCallstackFrameInfo> getBacktrace();
void throwError(const std::string& err);
bool envEnabled(const std::string& env);
int allocateSHMFile(size_t len);
bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr);
CFileDescriptor allocateSHMFile(size_t len);
bool allocateSHMFilePair(size_t size, CFileDescriptor& rw_fd_ref, CFileDescriptor& ro_fd_ref);
float stringToPercentage(const std::string& VALUE, const float REL);
template <typename... Args>

View file

@ -33,25 +33,22 @@ void CInputMethodKeyboardGrabV2::sendKeyboardData(SP<IKeyboard> keyboard) {
pLastKeyboard = keyboard;
int keymapFD = allocateSHMFile(keyboard->xkbKeymapString.length() + 1);
if (keymapFD < 0) {
CFileDescriptor keymapFD = allocateSHMFile(keyboard->xkbKeymapString.length() + 1);
if (!keymapFD.isValid()) {
LOGM(ERR, "Failed to create a keymap file for keyboard grab");
return;
}
void* data = mmap(nullptr, keyboard->xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, keymapFD, 0);
void* data = mmap(nullptr, keyboard->xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, keymapFD.get(), 0);
if (data == MAP_FAILED) {
LOGM(ERR, "Failed to mmap a keymap file for keyboard grab");
close(keymapFD);
return;
}
memcpy(data, keyboard->xkbKeymapString.c_str(), keyboard->xkbKeymapString.length());
munmap(data, keyboard->xkbKeymapString.length() + 1);
resource->sendKeymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymapFD, keyboard->xkbKeymapString.length() + 1);
close(keymapFD);
resource->sendKeymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymapFD.get(), keyboard->xkbKeymapString.length() + 1);
sendMods(keyboard->modifiersState.depressed, keyboard->modifiersState.latched, keyboard->modifiersState.locked, keyboard->modifiersState.group);

View file

@ -77,30 +77,24 @@ CDMABUFFormatTable::CDMABUFFormatTable(SDMABUFTranche _rendererTranche, std::vec
tableSize = formatsVec.size() * sizeof(SDMABUFFormatTableEntry);
int fds[2] = {0};
allocateSHMFilePair(tableSize, &fds[0], &fds[1]);
CFileDescriptor fds[2] = {};
allocateSHMFilePair(tableSize, fds[0], fds[1]);
auto arr = (SDMABUFFormatTableEntry*)mmap(nullptr, tableSize, PROT_READ | PROT_WRITE, MAP_SHARED, fds[0], 0);
auto arr = (SDMABUFFormatTableEntry*)mmap(nullptr, tableSize, PROT_READ | PROT_WRITE, MAP_SHARED, fds[0].get(), 0);
if (arr == MAP_FAILED) {
LOGM(ERR, "mmap failed");
close(fds[0]);
close(fds[1]);
return;
}
close(fds[0]);
std::copy(formatsVec.begin(), formatsVec.end(), arr);
munmap(arr, tableSize);
tableFD = fds[1];
tableFD = std::move(fds[1]);
}
CDMABUFFormatTable::~CDMABUFFormatTable() {
close(tableFD);
}
CDMABUFFormatTable::~CDMABUFFormatTable() {}
CLinuxDMABuffer::CLinuxDMABuffer(uint32_t id, wl_client* client, Aquamarine::SDMABUFAttrs attrs) {
buffer = makeShared<CDMABuffer>(id, client, attrs);
@ -307,7 +301,7 @@ CLinuxDMABUFFeedbackResource::CLinuxDMABUFFeedbackResource(SP<CZwpLinuxDmabufFee
resource->setDestroy([this](CZwpLinuxDmabufFeedbackV1* r) { PROTO::linuxDma->destroyResource(this); });
auto& formatTable = PROTO::linuxDma->formatTable;
resource->sendFormatTable(formatTable->tableFD, formatTable->tableSize);
resource->sendFormatTable(formatTable->tableFD.get(), formatTable->tableSize);
sendDefaultFeedback();
}
@ -504,7 +498,7 @@ void CLinuxDMABufV1Protocol::resetFormatTable() {
auto newFormatTable = std::make_unique<CDMABUFFormatTable>(formatTable->rendererTranche, formatTable->monitorTranches);
for (auto const& feedback : m_vFeedbacks) {
feedback->resource->sendFormatTable(newFormatTable->tableFD, newFormatTable->tableSize);
feedback->resource->sendFormatTable(newFormatTable->tableFD.get(), newFormatTable->tableSize);
if (feedback->lastFeedbackWasScanout) {
PHLMONITOR mon;
auto HLSurface = CWLSurface::fromResource(feedback->surface);

View file

@ -51,7 +51,7 @@ class CDMABUFFormatTable {
CDMABUFFormatTable(SDMABUFTranche rendererTranche, std::vector<std::pair<PHLMONITORREF, SDMABUFTranche>> tranches);
~CDMABUFFormatTable();
int tableFD = -1;
CFileDescriptor tableFD;
size_t tableSize = 0;
SDMABUFTranche rendererTranche;
std::vector<std::pair<PHLMONITORREF, SDMABUFTranche>> monitorTranches;