diff --git a/paths.cpp b/paths.cpp index c27ecbb..674db81 100644 --- a/paths.cpp +++ b/paths.cpp @@ -9,6 +9,11 @@ #include #include +#include +#include +#include +#include +#include std::vector Paths::getPath(std::string envVar, std::string deflt, std::string defltHomeRelPath) @@ -39,3 +44,34 @@ Paths::getPath(std::string envVar, std::string deflt, std::string defltHomeRelPa return pathList; } + +// Behaves like mkstemp, but for shared memory. +int shm_mkstemp(char *fileBase) +{ + const char charSet[] = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"; + int size = strlen(fileBase); + if (size < 6) { + errno = EINVAL; + return -1; + } + + if (strcmp(fileBase + size - 6, "XXXXXX") != 0) { + errno = EINVAL; + return -1; + } + + while (1) { + for (int c = size - 6; c < size; c++) { + // Note the -1 to avoid the trailing '\0' in charSet. + fileBase[c] = charSet[rand() % (sizeof(charSet) - 1)]; + } + int fd = shm_open(fileBase, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + return fd; + } else if (errno != EEXIST) { + return -1; + } + } +} diff --git a/paths.h b/paths.h index 514fb41..0b6a6aa 100644 --- a/paths.h +++ b/paths.h @@ -20,4 +20,6 @@ class Paths std::string defltHomeRelPath); }; +int shm_mkstemp(char *fileBase); + #endif diff --git a/remotepluginclient.cpp b/remotepluginclient.cpp index 4509819..c589511 100644 --- a/remotepluginclient.cpp +++ b/remotepluginclient.cpp @@ -6,6 +6,7 @@ */ #include "remotepluginclient.h" +#include "paths.h" #include #include @@ -40,6 +41,8 @@ RemotePluginClient::RemotePluginClient() : { char tmpFileBase[60]; + srand(time(NULL)); + sprintf(tmpFileBase, "/tmp/rplugin_crq_XXXXXX"); if (mkstemp(tmpFileBase) < 0) { cleanup(); @@ -68,18 +71,13 @@ RemotePluginClient::RemotePluginClient() : throw((std::string)"Failed to create FIFO"); } - sprintf(tmpFileBase, "/tmp/rplugin_shc_XXXXXX"); - if (mkstemp(tmpFileBase) < 0) { - cleanup(); - throw((std::string)"Failed to obtain temporary filename"); - } - m_shmControlFileName = strdup(tmpFileBase); - - m_shmControlFd = open(m_shmControlFileName, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + sprintf(tmpFileBase, "/dssi-vst-rplugin_shc_XXXXXX"); + m_shmControlFd = shm_mkstemp(tmpFileBase); if (m_shmControlFd < 0) { cleanup(); throw((std::string)"Failed to open or create shared memory file"); } + m_shmControlFileName = strdup(tmpFileBase); ftruncate(m_shmControlFd, sizeof(ShmControl)); m_shmControl = static_cast(mmap(0, sizeof(ShmControl), PROT_READ | PROT_WRITE, MAP_SHARED, m_shmControlFd, 0)); if (!m_shmControl) { @@ -95,18 +93,13 @@ RemotePluginClient::RemotePluginClient() : throw((std::string)"Failed to initialize shared memory semaphore"); } - sprintf(tmpFileBase, "/tmp/rplugin_shm_XXXXXX"); - if (mkstemp(tmpFileBase) < 0) { - cleanup(); - throw((std::string)"Failed to obtain temporary filename"); - } - m_shmFileName = strdup(tmpFileBase); - - m_shmFd = open(m_shmFileName, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + sprintf(tmpFileBase, "/dssi-vst-rplugin_shm_XXXXXX"); + m_shmFd = shm_mkstemp(tmpFileBase); if (m_shmFd < 0) { cleanup(); throw((std::string)"Failed to open or create shared memory file"); } + m_shmFileName = strdup(tmpFileBase); } RemotePluginClient::~RemotePluginClient() @@ -195,12 +188,12 @@ RemotePluginClient::cleanup() m_controlResponseFileName = 0; } if (m_shmFileName) { - unlink(m_shmFileName); + shm_unlink(m_shmFileName); free(m_shmFileName); m_shmFileName = 0; } if (m_shmControlFileName) { - unlink(m_shmControlFileName); + shm_unlink(m_shmControlFileName); free(m_shmControlFileName); m_shmControlFileName = 0; } diff --git a/remotepluginserver.cpp b/remotepluginserver.cpp index fc2e7b0..6b53344 100644 --- a/remotepluginserver.cpp +++ b/remotepluginserver.cpp @@ -62,11 +62,11 @@ RemotePluginServer::RemotePluginServer(std::string fileIdentifiers) : } bool b = false; - sprintf(tmpFileBase, "/tmp/rplugin_shc_%s", + sprintf(tmpFileBase, "/dssi-vst-rplugin_shc_%s", fileIdentifiers.substr(12, 6).c_str()); m_shmControlFileName = strdup(tmpFileBase); - m_shmControlFd = open(m_shmControlFileName, O_RDWR); + m_shmControlFd = shm_open(m_shmControlFileName, O_RDWR, 0); if (m_shmControlFd < 0) { tryWrite(m_controlResponseFd, &b, sizeof(bool)); cleanup(); @@ -80,11 +80,11 @@ RemotePluginServer::RemotePluginServer(std::string fileIdentifiers) : throw((std::string)"Failed to mmap shared memory file"); } - sprintf(tmpFileBase, "/tmp/rplugin_shm_%s", + sprintf(tmpFileBase, "/dssi-vst-rplugin_shm_%s", fileIdentifiers.substr(18, 6).c_str()); m_shmFileName = strdup(tmpFileBase); - if ((m_shmFd = open(m_shmFileName, O_RDWR)) < 0) { + if ((m_shmFd = shm_open(m_shmFileName, O_RDWR, 0)) < 0) { tryWrite(m_controlResponseFd, &b, sizeof(bool)); cleanup(); throw((std::string)"Failed to open shared memory file");