From 8bcccf9f0f0f69986e315d4288dfdbf119dd19db Mon Sep 17 00:00:00 2001 From: Vaxry Date: Wed, 1 May 2024 16:41:17 +0100 Subject: ime-v2: move to new impl --- src/helpers/MiscFunctions.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/helpers/MiscFunctions.cpp') diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index b8061bef..22aa48f4 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -2,9 +2,12 @@ #include "../defines.hpp" #include #include "../Compositor.hpp" +#include "../managers/TokenManager.hpp" #include #include #include +#include +#include #include #include #ifdef HAS_EXECINFO @@ -834,3 +837,35 @@ bool envEnabled(const std::string& env) { return false; return std::string(ENV) == "1"; } + +std::pair openExclusiveShm() { + 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}; + } + + return {-1, ""}; +} + +int allocateSHMFile(size_t len) { + auto [fd, name] = openExclusiveShm(); + if (fd < 0) + return -1; + + shm_unlink(name.c_str()); + + int ret; + do { + ret = ftruncate(fd, len); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} -- cgit v1.2.3