From 2b3e7adc0b56a141741522de2956b43a7b6abb78 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Mon, 18 Aug 2025 18:02:30 -0400 Subject: [PATCH] feat: Prevent core, client and or server from running at the same time fixes: #8258 --- src/apps/deskflow-core/deskflow-core.cpp | 38 +++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/apps/deskflow-core/deskflow-core.cpp b/src/apps/deskflow-core/deskflow-core.cpp index 5260ffb1b..4e1195fc2 100644 --- a/src/apps/deskflow-core/deskflow-core.cpp +++ b/src/apps/deskflow-core/deskflow-core.cpp @@ -17,6 +17,7 @@ #include #endif +#include #include void showHelp() @@ -32,6 +33,15 @@ void showHelp() cApp.help(); } +bool isHelp(int argc, char **argv) +{ + for (int i = 0; i < argc; ++i) { + if (argv[i] == std::string("--help") || argv[i] == std::string("-h")) + return true; + } + return false; +} + bool isServer(int argc, char **argv) { return (argc > 1 && argv[1] == std::string("server")); @@ -44,6 +54,30 @@ bool isClient(int argc, char **argv) int main(int argc, char **argv) { + Arch arch; + arch.init(); + + Log log; + + if (isHelp(argc, argv)) { + showHelp(); + return 0; + } + + // Create a shared memory segment with a unique key + // This is to prevent a new instance from running if one is already running + QSharedMemory sharedMemory("deskflow-core"); + + // Attempt to attach first and detach in order to clean up stale shm chunks + // This can happen if the previous instance was killed or crashed + if (sharedMemory.attach()) + sharedMemory.detach(); + + // If we can create 1 byte of SHM we are the only instance + if (!sharedMemory.create(1)) { + LOG_WARN("an instance of deskflow core (server or client) is already running"); + return 0; + } #if SYSAPI_WIN32 // HACK to make sure settings gets the correct qApp path QCoreApplication m(argc, argv); @@ -52,10 +86,6 @@ int main(int argc, char **argv) ArchMiscWindows::setInstanceWin32(GetModuleHandle(nullptr)); #endif - Arch arch; - arch.init(); - - Log log; EventQueue events; if (isServer(argc, argv)) {