feat: Prevent core, client and or server from running at the same time
fixes: #8258
This commit is contained in:
parent
26983dc0e4
commit
2b3e7adc0b
1 changed files with 34 additions and 4 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <QSharedMemory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void showHelp()
|
void showHelp()
|
||||||
|
|
@ -32,6 +33,15 @@ void showHelp()
|
||||||
cApp.help();
|
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)
|
bool isServer(int argc, char **argv)
|
||||||
{
|
{
|
||||||
return (argc > 1 && argv[1] == std::string("server"));
|
return (argc > 1 && argv[1] == std::string("server"));
|
||||||
|
|
@ -44,6 +54,30 @@ bool isClient(int argc, char **argv)
|
||||||
|
|
||||||
int main(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
|
#if SYSAPI_WIN32
|
||||||
// HACK to make sure settings gets the correct qApp path
|
// HACK to make sure settings gets the correct qApp path
|
||||||
QCoreApplication m(argc, argv);
|
QCoreApplication m(argc, argv);
|
||||||
|
|
@ -52,10 +86,6 @@ int main(int argc, char **argv)
|
||||||
ArchMiscWindows::setInstanceWin32(GetModuleHandle(nullptr));
|
ArchMiscWindows::setInstanceWin32(GetModuleHandle(nullptr));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Arch arch;
|
|
||||||
arch.init();
|
|
||||||
|
|
||||||
Log log;
|
|
||||||
EventQueue events;
|
EventQueue events;
|
||||||
|
|
||||||
if (isServer(argc, argv)) {
|
if (isServer(argc, argv)) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue