2016-09-24 16:19:23 +02:00
|
|
|
#include "./singleinstance.h"
|
|
|
|
|
2023-05-03 21:18:03 +02:00
|
|
|
#include "resources/config.h"
|
|
|
|
|
2016-09-24 16:19:23 +02:00
|
|
|
#include <c++utilities/conversion/binaryconversion.h>
|
2018-04-02 20:23:54 +02:00
|
|
|
#include <c++utilities/io/ansiescapecodes.h>
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2017-05-01 03:34:43 +02:00
|
|
|
#include <QCoreApplication>
|
2023-04-20 00:33:52 +02:00
|
|
|
#include <QFile>
|
2016-09-24 16:19:23 +02:00
|
|
|
#include <QLocalServer>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <QStringBuilder>
|
2023-04-20 00:33:52 +02:00
|
|
|
#include <QThread>
|
2016-09-24 16:19:23 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2017-02-11 02:42:45 +01:00
|
|
|
#include <memory>
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2022-06-17 21:46:06 +02:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
// needs to be included after windows.h
|
|
|
|
#include <sddl.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace CppUtilities;
|
|
|
|
using namespace CppUtilities::EscapeCodes;
|
2016-09-24 16:19:23 +02:00
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
2022-06-17 21:46:06 +02:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
static QString getCurrentProcessSIDAsString()
|
|
|
|
{
|
|
|
|
auto res = QString();
|
|
|
|
auto processToken = HANDLE();
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &processToken)) {
|
|
|
|
std::cerr << Phrases::Error << "Unable to determine current user: OpenProcessToken failed with " << GetLastError() << Phrases::EndFlush;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bufferSize = DWORD();
|
|
|
|
if (!GetTokenInformation(processToken, TokenUser, nullptr, 0, &bufferSize) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
|
|
|
|
std::cerr << Phrases::Error << "Unable to determine current user: GetTokenInformation failed with " << GetLastError() << Phrases::EndFlush;
|
|
|
|
CloseHandle(processToken);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
auto buffer = std::vector<BYTE>();
|
|
|
|
buffer.resize(bufferSize);
|
|
|
|
|
|
|
|
auto userToken = reinterpret_cast<PTOKEN_USER>(buffer.data());
|
|
|
|
if (!GetTokenInformation(processToken, TokenUser, userToken, bufferSize, &bufferSize)) {
|
|
|
|
std::cerr << Phrases::Error << "Unable to determine current user: GetTokenInformation failed with " << GetLastError() << Phrases::EndFlush;
|
|
|
|
CloseHandle(processToken);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stringSid = LPWSTR();
|
|
|
|
if (!ConvertSidToStringSidW(userToken->User.Sid, &stringSid)) {
|
|
|
|
std::cerr << Phrases::Error << "Unable to determine current user: ConvertSidToStringSid failed with " << GetLastError() << Phrases::EndFlush;
|
|
|
|
CloseHandle(processToken);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res = QString::fromWCharArray(stringSid);
|
|
|
|
LocalFree(stringSid);
|
|
|
|
CloseHandle(processToken);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-20 00:33:52 +02:00
|
|
|
SingleInstance::SingleInstance(int argc, const char *const *argv, bool skipSingleInstanceBehavior, bool skipPassing, QObject *parent)
|
2017-05-01 03:34:43 +02:00
|
|
|
: QObject(parent)
|
|
|
|
, m_server(nullptr)
|
2016-09-24 16:19:23 +02:00
|
|
|
{
|
2023-04-20 00:33:52 +02:00
|
|
|
// just do nothing if supposed to skip single instance behavior
|
|
|
|
if (skipSingleInstanceBehavior) {
|
2020-10-19 19:03:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-20 00:33:52 +02:00
|
|
|
// check for running instance; if there is one pass parameters and exit
|
|
|
|
static const auto appId = applicationId();
|
|
|
|
if (!skipPassing && passArgsToRunningInstance(argc, argv, appId)) {
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2023-04-20 00:33:52 +02:00
|
|
|
// create local server; at this point no previous instance is running anymore
|
|
|
|
// -> cleanup possible leftover (previous instance might have crashed)
|
2020-10-19 19:04:18 +02:00
|
|
|
QLocalServer::removeServer(appId);
|
2023-04-20 00:33:52 +02:00
|
|
|
// -> setup server
|
2020-10-19 19:04:18 +02:00
|
|
|
m_server = new QLocalServer(this);
|
|
|
|
connect(m_server, &QLocalServer::newConnection, this, &SingleInstance::handleNewConnection);
|
|
|
|
if (!m_server->listen(appId)) {
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Error << "Unable to launch as single instance application as " << appId.toStdString() << Phrases::EndFlush;
|
2023-04-20 00:33:52 +02:00
|
|
|
} else {
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Info << "Single instance application ID: " << appId.toStdString() << Phrases::EndFlush;
|
2020-10-19 19:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 00:33:52 +02:00
|
|
|
const QString &SingleInstance::applicationId()
|
|
|
|
{
|
2023-05-03 21:18:03 +02:00
|
|
|
static const auto envOverride = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SINGLE_INSTANCE_ID");
|
|
|
|
if (!envOverride.isEmpty()) {
|
|
|
|
return envOverride;
|
|
|
|
}
|
2023-04-20 00:33:52 +02:00
|
|
|
static const auto id = QString(QCoreApplication::applicationName() % QChar('-') % QCoreApplication::organizationName() % QChar('-') %
|
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
getCurrentProcessSIDAsString()
|
|
|
|
#else
|
|
|
|
QString::number(getuid())
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SingleInstance::passArgsToRunningInstance(int argc, const char *const *argv, const QString &appId, bool waitUntilGone)
|
2020-10-19 19:04:18 +02:00
|
|
|
{
|
2023-04-20 00:33:52 +02:00
|
|
|
if (argc < 0 || argc > 0xFFFF) {
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Error << "Unable to pass the specified number of arguments" << Phrases::EndFlush;
|
2023-04-20 00:33:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto socket = QLocalSocket();
|
2016-09-24 16:19:23 +02:00
|
|
|
socket.connectToServer(appId, QLocalSocket::ReadWrite);
|
2023-04-20 00:33:52 +02:00
|
|
|
const auto fullServerName = socket.fullServerName();
|
|
|
|
if (!socket.waitForConnected(1000)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Info << "Application already running, sending args to previous instance" << Phrases::EndFlush;
|
2023-04-20 00:33:52 +02:00
|
|
|
char buffer[2];
|
|
|
|
BE::getBytes(static_cast<std::uint16_t>(argc), buffer);
|
|
|
|
auto error = socket.write(buffer, 2) < 0;
|
|
|
|
*buffer = '\0';
|
|
|
|
for (const char *const *end = argv + argc; argv != end && !error; ++argv) {
|
|
|
|
error = socket.write(*argv) < 0 || socket.write(buffer, 1) < 0;
|
|
|
|
}
|
2023-05-01 21:10:09 +02:00
|
|
|
error = error || !socket.waitForBytesWritten(1000);
|
2023-04-20 00:33:52 +02:00
|
|
|
socket.disconnectFromServer();
|
|
|
|
if (socket.state() != QLocalSocket::UnconnectedState) {
|
|
|
|
error = !socket.waitForDisconnected(1000) || error;
|
|
|
|
}
|
|
|
|
if (error) {
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Error << "Unable to pass args to previous instance: " << socket.errorString().toStdString() << Phrases::EndFlush;
|
2023-04-20 00:33:52 +02:00
|
|
|
}
|
2023-05-01 21:10:09 +02:00
|
|
|
if (waitUntilGone && QFile::exists(fullServerName)) {
|
|
|
|
const auto fullServerNameStd = fullServerName.toStdString();
|
|
|
|
std::cerr << Phrases::Info << "Waiting for previous instance to shutdown (" << fullServerNameStd << " still exists)" << Phrases::EndFlush;
|
|
|
|
do {
|
2023-04-20 00:33:52 +02:00
|
|
|
QThread::msleep(500);
|
2023-05-01 21:10:09 +02:00
|
|
|
} while (QFile::exists(fullServerName));
|
2016-09-24 16:19:23 +02:00
|
|
|
}
|
2023-04-20 00:33:52 +02:00
|
|
|
return !error;
|
2016-09-24 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SingleInstance::handleNewConnection()
|
|
|
|
{
|
2018-04-02 20:23:54 +02:00
|
|
|
const QLocalSocket *const socket = m_server->nextPendingConnection();
|
2016-09-24 16:19:23 +02:00
|
|
|
connect(socket, &QLocalSocket::readChannelFinished, this, &SingleInstance::readArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleInstance::readArgs()
|
|
|
|
{
|
2018-04-02 20:23:54 +02:00
|
|
|
auto *const socket = static_cast<QLocalSocket *>(sender());
|
2023-05-01 21:10:09 +02:00
|
|
|
const auto argData = socket->readAll();
|
|
|
|
if (argData.size() < 2) {
|
2023-05-11 17:57:22 +02:00
|
|
|
std::cerr << Phrases::Error << "Another application instance sent invalid argument data (payload only " << argData.size() << " bytes)."
|
|
|
|
<< Phrases::EndFlush;
|
2016-09-24 16:19:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
|
2016-09-25 20:54:09 +02:00
|
|
|
// reconstruct argc and argv array
|
2023-05-18 00:53:37 +02:00
|
|
|
const auto argc = BE::toInt<std::uint16_t>(argData.data());
|
2023-05-01 21:10:09 +02:00
|
|
|
auto args = std::vector<const char *>();
|
2016-09-24 16:19:23 +02:00
|
|
|
args.reserve(argc + 1);
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << Phrases::Info << "Evaluating " << argc << " arguments from another instance: " << Phrases::End;
|
|
|
|
for (const char *argv = argData.data() + 2, *end = argData.data() + argData.size(), *i = argv; i != end && *argv;) {
|
2017-05-01 03:34:43 +02:00
|
|
|
if (!*i) {
|
2016-09-24 16:19:23 +02:00
|
|
|
args.push_back(argv);
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << ' ' << argv;
|
2016-09-24 16:19:23 +02:00
|
|
|
argv = ++i;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push_back(nullptr);
|
2023-05-01 21:10:09 +02:00
|
|
|
std::cerr << '\n';
|
2016-09-24 16:19:23 +02:00
|
|
|
|
2016-09-25 20:54:09 +02:00
|
|
|
emit newInstance(static_cast<int>(args.size() - 1), args.data());
|
2016-09-24 16:19:23 +02:00
|
|
|
}
|
2020-10-19 19:04:18 +02:00
|
|
|
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|