updated qtsingleapplication to v3.1.5

This commit is contained in:
Jeremy Borgman
2020-10-31 19:46:18 -05:00
committed by borgmanJeremy
parent a0156be7b6
commit 8211b6aff0
31 changed files with 1863 additions and 544 deletions

View File

@@ -1,6 +1,6 @@
// The MIT License (MIT)
//
// Copyright (c) Itay Grudev 2015 - 2016
// Copyright (c) Itay Grudev 2015 - 2020
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -20,358 +20,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <cstdlib>
#include <QMessageBox>
#include <QTimer>
#include <QtCore/QByteArray>
#include <QtCore/QCryptographicHash>
#include <QtCore/QDataStream>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSemaphore>
#include <QtCore/QElapsedTimer>
#include <QtCore/QSharedMemory>
#include <QtCore/QStandardPaths>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket>
#ifdef Q_OS_UNIX
#include <signal.h>
#include <unistd.h>
#endif
#ifdef Q_OS_WIN
#include <lmcons.h>
#include <windows.h>
#endif
#include "singleapplication.h"
#include "singleapplication_p.h"
SingleApplicationPrivate::SingleApplicationPrivate(SingleApplication* q_ptr)
: q_ptr(q_ptr)
{
server = nullptr;
socket = nullptr;
}
SingleApplicationPrivate::~SingleApplicationPrivate()
{
if (socket != nullptr) {
socket->close();
delete socket;
}
memory->lock();
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
if (server != nullptr) {
server->close();
delete server;
inst->primary = false;
inst->primaryPid = -1;
}
memory->unlock();
delete memory;
}
void SingleApplicationPrivate::genBlockServerName(int timeout)
{
QCryptographicHash appData(QCryptographicHash::Sha256);
appData.addData("SingleApplication", 17);
appData.addData(SingleApplication::app_t::applicationName().toUtf8());
appData.addData(SingleApplication::app_t::organizationName().toUtf8());
appData.addData(SingleApplication::app_t::organizationDomain().toUtf8());
if (!(options & SingleApplication::Mode::ExcludeAppVersion)) {
appData.addData(
SingleApplication::app_t::applicationVersion().toUtf8());
}
if (!(options & SingleApplication::Mode::ExcludeAppPath)) {
#ifdef Q_OS_WIN
appData.addData(
SingleApplication::app_t::applicationFilePath().toLower().toUtf8());
#else
appData.addData(
SingleApplication::app_t::applicationFilePath().toUtf8());
#endif
}
// User level block requires a user specific data in the hash
if (options & SingleApplication::Mode::User) {
#ifdef Q_OS_WIN
Q_UNUSED(timeout);
wchar_t username[UNLEN + 1];
// Specifies size of the buffer on input
DWORD usernameLength = UNLEN + 1;
if (GetUserNameW(username, &usernameLength)) {
appData.addData(QString::fromWCharArray(username).toUtf8());
} else {
appData.addData(
QStandardPaths::standardLocations(QStandardPaths::HomeLocation)
.join("")
.toUtf8());
}
#endif
#ifdef Q_OS_UNIX
QProcess process;
process.start(QStringLiteral("whoami"), QStringList{});
if (process.waitForFinished(timeout) &&
process.exitCode() == QProcess::NormalExit) {
appData.addData(process.readLine());
} else {
appData.addData(QDir(QStandardPaths::standardLocations(
QStandardPaths::HomeLocation)
.first())
.absolutePath()
.toUtf8());
}
#endif
}
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
// server naming requirements.
blockServerName = appData.result().toBase64().replace("/", "_");
}
void SingleApplicationPrivate::startPrimary(bool resetMemory)
{
Q_Q(SingleApplication);
#ifdef Q_OS_UNIX
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
crashHandler();
#endif
// Successful creation means that no main process exists
// So we start a QLocalServer to listen for connections
QLocalServer::removeServer(blockServerName);
server = new QLocalServer();
// Restrict access to the socket according to the
// SingleApplication::Mode::User flag on User level or no restrictions
if (options & SingleApplication::Mode::User) {
server->setSocketOptions(QLocalServer::UserAccessOption);
} else {
server->setSocketOptions(QLocalServer::WorldAccessOption);
}
server->listen(blockServerName);
QObject::connect(server,
&QLocalServer::newConnection,
this,
&SingleApplicationPrivate::slotConnectionEstablished);
// Reset the number of connections
memory->lock();
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
if (resetMemory) {
inst->secondary = 0;
}
inst->primary = true;
inst->primaryPid = q->applicationPid();
memory->unlock();
instanceNumber = 0;
}
void SingleApplicationPrivate::startSecondary()
{
#ifdef Q_OS_UNIX
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
crashHandler();
#endif
}
void SingleApplicationPrivate::connectToPrimary(int msecs,
ConnectionType connectionType)
{
// Connect to the Local Server of the Primary Instance if not already
// connected.
if (socket == nullptr) {
socket = new QLocalSocket();
}
// If already connected - we are done;
if (socket->state() == QLocalSocket::ConnectedState)
return;
// If not connect
if (socket->state() == QLocalSocket::UnconnectedState ||
socket->state() == QLocalSocket::ClosingState) {
socket->connectToServer(blockServerName);
}
// Wait for being connected
if (socket->state() == QLocalSocket::ConnectingState) {
socket->waitForConnected(msecs);
}
// Initialisation message according to the SingleApplication protocol
if (socket->state() == QLocalSocket::ConnectedState) {
// Notify the parent that a new instance had been started;
QByteArray initMsg;
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
writeStream.setVersion(QDataStream::Qt_5_2);
writeStream << blockServerName.toLatin1();
writeStream << static_cast<quint8>(connectionType);
writeStream << instanceNumber;
quint16 checksum = qChecksum(initMsg.constData(),
static_cast<quint32>(initMsg.length()));
writeStream << checksum;
socket->write(initMsg);
socket->flush();
socket->waitForBytesWritten(msecs);
}
}
qint64 SingleApplicationPrivate::primaryPid()
{
qint64 pid;
memory->lock();
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
pid = inst->primaryPid;
memory->unlock();
return pid;
}
#ifdef Q_OS_UNIX
void SingleApplicationPrivate::crashHandler()
{
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
signal(SIGHUP, SingleApplicationPrivate::terminate); // 1
signal(SIGINT, SingleApplicationPrivate::terminate); // 2
signal(SIGQUIT, SingleApplicationPrivate::terminate); // 3
signal(SIGILL, SingleApplicationPrivate::terminate); // 4
signal(SIGABRT, SingleApplicationPrivate::terminate); // 6
signal(SIGFPE, SingleApplicationPrivate::terminate); // 8
signal(SIGBUS, SingleApplicationPrivate::terminate); // 10
signal(SIGSEGV, SingleApplicationPrivate::terminate); // 11
signal(SIGSYS, SingleApplicationPrivate::terminate); // 12
signal(SIGPIPE, SingleApplicationPrivate::terminate); // 13
signal(SIGALRM, SingleApplicationPrivate::terminate); // 14
signal(SIGTERM, SingleApplicationPrivate::terminate); // 15
signal(SIGXCPU, SingleApplicationPrivate::terminate); // 24
signal(SIGXFSZ, SingleApplicationPrivate::terminate); // 25
}
void SingleApplicationPrivate::terminate(int signum)
{
delete ((SingleApplication*)QCoreApplication::instance())->d_ptr;
::exit(128 + signum);
}
#endif
/**
* @brief Executed when a connection has been made to the LocalServer
*/
void SingleApplicationPrivate::slotConnectionEstablished()
{
Q_Q(SingleApplication);
QLocalSocket* nextConnSocket = server->nextPendingConnection();
quint32 instanceId = 0;
ConnectionType connectionType = InvalidConnection;
if (nextConnSocket->waitForReadyRead(100)) {
// read all data from message in same order/format as written
QByteArray msgBytes =
nextConnSocket->read(nextConnSocket->bytesAvailable() -
static_cast<qint64>(sizeof(quint16)));
QByteArray checksumBytes = nextConnSocket->read(sizeof(quint16));
QDataStream readStream(msgBytes);
readStream.setVersion(QDataStream::Qt_5_2);
// server name
QByteArray latin1Name;
readStream >> latin1Name;
// connectioon type
quint8 connType = InvalidConnection;
readStream >> connType;
connectionType = static_cast<ConnectionType>(connType);
// instance id
readStream >> instanceId;
// checksum
quint16 msgChecksum = 0;
QDataStream checksumStream(checksumBytes);
checksumStream.setVersion(QDataStream::Qt_5_2);
checksumStream >> msgChecksum;
const quint16 actualChecksum = qChecksum(
msgBytes.constData(), static_cast<quint32>(msgBytes.length()));
if (readStream.status() != QDataStream::Ok ||
QLatin1String(latin1Name) != blockServerName ||
msgChecksum != actualChecksum) {
connectionType = InvalidConnection;
}
}
if (connectionType == InvalidConnection) {
nextConnSocket->close();
delete nextConnSocket;
return;
}
QObject::connect(nextConnSocket,
&QLocalSocket::aboutToClose,
this,
[nextConnSocket, instanceId, this]() {
emit this->slotClientConnectionClosed(nextConnSocket,
instanceId);
});
QObject::connect(nextConnSocket,
&QLocalSocket::readyRead,
this,
[nextConnSocket, instanceId, this]() {
emit this->slotDataAvailable(nextConnSocket,
instanceId);
});
if (connectionType == NewInstance ||
(connectionType == SecondaryInstance &&
options & SingleApplication::Mode::SecondaryNotification)) {
emit q->instanceStarted();
}
if (nextConnSocket->bytesAvailable() > 0) {
emit this->slotDataAvailable(nextConnSocket, instanceId);
}
}
void SingleApplicationPrivate::slotDataAvailable(QLocalSocket* dataSocket,
quint32 instanceId)
{
Q_Q(SingleApplication);
emit q->receivedMessage(instanceId, dataSocket->readAll());
}
void SingleApplicationPrivate::slotClientConnectionClosed(
QLocalSocket* closedSocket,
quint32 instanceId)
{
if (closedSocket->bytesAvailable() > 0)
emit slotDataAvailable(closedSocket, instanceId);
closedSocket->deleteLater();
}
/**
* @brief Constructor. Checks and fires up LocalServer or closes the program
* if another instance already exists
* @param argc
* @param argv
* @param {bool} allowSecondaryInstances
* @param allowSecondary Whether to enable secondary instance support
* @param options Optional flags to toggle specific behaviour
* @param timeout Maximum time blocking functions are allowed during app load
*/
SingleApplication::SingleApplication(int& argc,
char* argv[],
@@ -383,115 +46,213 @@ SingleApplication::SingleApplication(int& argc,
{
Q_D(SingleApplication);
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
// On Android and iOS since the library is not supported fallback to
// standard QApplication behaviour by simply returning at this point.
qWarning()
<< "SingleApplication is not supported on Android and iOS systems.";
return;
#endif
// Store the current mode of the program
d->options = options;
// Generating an application ID used for identifying the shared memory
// block and QLocalServer
d->genBlockServerName(timeout);
d->genBlockServerName();
// To mitigate QSharedMemory issues with large amount of processes
// attempting to attach at the same time
d->randomSleep();
// Guarantee thread safe behaviour with a shared memory block. Also by
// explicitly attaching it and then deleting it we make sure that the
// memory is deleted even if the process had crashed on Unix.
#ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the
// memory is deleted even after the process has crashed on Unix.
d->memory = new QSharedMemory(d->blockServerName);
d->memory->attach();
delete d->memory;
#endif
// Guarantee thread safe behaviour with a shared memory block.
d->memory = new QSharedMemory(d->blockServerName);
// Create a shared memory block
if (d->memory->create(sizeof(InstancesInfo))) {
d->startPrimary(true);
return;
// Initialize the shared memory block
if (!d->memory->lock()) {
qCritical()
<< "SingleApplication: Unable to lock memory block after create.";
abortSafely();
}
d->initializeMemoryBlock();
} else {
// Attempt to attach to the memory segment
if (d->memory->attach()) {
d->memory->lock();
InstancesInfo* inst =
static_cast<InstancesInfo*>(d->memory->data());
if (!inst->primary) {
d->startPrimary(false);
d->memory->unlock();
return;
if (d->memory->error() == QSharedMemory::AlreadyExists) {
// Attempt to attach to the memory segment
if (!d->memory->attach()) {
qCritical() << "SingleApplication: Unable to attach to shared "
"memory block.";
abortSafely();
}
// Check if another instance can be started
if (allowSecondary) {
inst->secondary += 1;
d->instanceNumber = inst->secondary;
d->startSecondary();
if (d->options & Mode::SecondaryNotification) {
d->connectToPrimary(
timeout, SingleApplicationPrivate::SecondaryInstance);
}
d->memory->unlock();
return;
if (!d->memory->lock()) {
qCritical() << "SingleApplication: Unable to lock memory block "
"after attach.";
abortSafely();
}
d->memory->unlock();
} else {
qCritical() << "SingleApplication: Unable to create block.";
abortSafely();
}
}
d->connectToPrimary(timeout, SingleApplicationPrivate::NewInstance);
delete d;
auto* inst = static_cast<InstancesInfo*>(d->memory->data());
QElapsedTimer time;
time.start();
// show message box with inforation that Flameshot is already launched
QMessageBox msgBox;
msgBox.setText(QObject::tr(
"Hi, I'm already running!\nYou can find me in the system tray."));
int cnt = 3;
QTimer cntDown;
QObject::connect(
&cntDown, &QTimer::timeout, [&msgBox, &cnt, &cntDown]() -> void {
if (--cnt < 0) {
cntDown.stop();
msgBox.close();
} else {
msgBox.setWindowTitle(QString("Flameshot (%1)").arg(cnt + 1));
}
});
cntDown.start(1000);
msgBox.exec();
// Make sure the shared memory block is initialised and in consistent state
while (true) {
// If the shared memory block's checksum is valid continue
if (d->blockChecksum() == inst->checksum)
break;
// If more than 5s have elapsed, assume the primary instance crashed and
// assume it's position
if (time.elapsed() > 5000) {
qWarning() << "SingleApplication: Shared memory block has been in "
"an inconsistent state from more than 5s. Assuming "
"primary instance failure.";
d->initializeMemoryBlock();
}
// Otherwise wait for a random period and try again. The random sleep
// here limits the probability of a collision between two racing apps
// and allows the app to initialise faster
if (!d->memory->unlock()) {
qDebug()
<< "SingleApplication: Unable to unlock memory for random wait.";
qDebug() << d->memory->errorString();
}
d->randomSleep();
if (!d->memory->lock()) {
qCritical()
<< "SingleApplication: Unable to lock memory after random wait.";
abortSafely();
}
}
if (inst->primary == false) {
d->startPrimary();
if (!d->memory->unlock()) {
qDebug() << "SingleApplication: Unable to unlock memory after "
"primary start.";
qDebug() << d->memory->errorString();
}
return;
}
// Check if another instance can be started
if (allowSecondary) {
d->startSecondary();
if (d->options & Mode::SecondaryNotification) {
d->connectToPrimary(timeout,
SingleApplicationPrivate::SecondaryInstance);
}
if (!d->memory->unlock()) {
qDebug() << "SingleApplication: Unable to unlock memory after "
"secondary start.";
qDebug() << d->memory->errorString();
}
return;
}
if (!d->memory->unlock()) {
qDebug()
<< "SingleApplication: Unable to unlock memory at end of execution.";
qDebug() << d->memory->errorString();
}
d->connectToPrimary(timeout, SingleApplicationPrivate::NewInstance);
delete d;
::exit(EXIT_SUCCESS);
}
/**
* @brief Destructor
*/
SingleApplication::~SingleApplication()
{
Q_D(SingleApplication);
delete d;
}
/**
* Checks if the current application instance is primary.
* @return Returns true if the instance is primary, false otherwise.
*/
bool SingleApplication::isPrimary()
{
Q_D(SingleApplication);
return d->server != nullptr;
}
/**
* Checks if the current application instance is secondary.
* @return Returns true if the instance is secondary, false otherwise.
*/
bool SingleApplication::isSecondary()
{
Q_D(SingleApplication);
return d->server == nullptr;
}
/**
* Allows you to identify an instance by returning unique consecutive instance
* ids. It is reset when the first (primary) instance of your app starts and
* only incremented afterwards.
* @return Returns a unique instance id.
*/
quint32 SingleApplication::instanceId()
{
Q_D(SingleApplication);
return d->instanceNumber;
}
/**
* Returns the OS PID (Process Identifier) of the process running the primary
* instance. Especially useful when SingleApplication is coupled with OS.
* specific APIs.
* @return Returns the primary instance PID.
*/
qint64 SingleApplication::primaryPid()
{
Q_D(SingleApplication);
return d->primaryPid();
}
bool SingleApplication::sendMessage(QByteArray message, int timeout)
/**
* Returns the username the primary instance is running as.
* @return Returns the username the primary instance is running as.
*/
QString SingleApplication::primaryUser()
{
Q_D(SingleApplication);
return d->primaryUser();
}
/**
* Returns the username the current instance is running as.
* @return Returns the username the current instance is running as.
*/
QString SingleApplication::currentUser()
{
Q_D(SingleApplication);
return d->getUsername();
}
/**
* Sends message to the Primary Instance.
* @param message The message to send.
* @param timeout the maximum timeout in milliseconds for blocking functions.
* @return true if the message was sent successfuly, false otherwise.
*/
bool SingleApplication::sendMessage(const QByteArray& message, int timeout)
{
Q_D(SingleApplication);
@@ -500,10 +261,25 @@ bool SingleApplication::sendMessage(QByteArray message, int timeout)
return false;
// Make sure the socket is connected
d->connectToPrimary(timeout, SingleApplicationPrivate::Reconnect);
if (!d->connectToPrimary(timeout, SingleApplicationPrivate::Reconnect))
return false;
d->socket->write(message);
bool dataWritten = d->socket->flush();
d->socket->waitForBytesWritten(timeout);
bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush();
return dataWritten;
}
/**
* Cleans up the shared memory block and exits with a failure.
* This function halts program execution.
*/
void SingleApplication::abortSafely()
{
Q_D(SingleApplication);
qCritical() << "SingleApplication: " << d->memory->error()
<< d->memory->errorString();
delete d;
::exit(EXIT_FAILURE);
}