2019-01-11 01:04:13 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
2020-10-05 21:31:03 +02:00
|
|
|
// Copyright (c) Itay Grudev 2015 - 2020
|
2019-01-11 01:04:13 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
//
|
|
|
|
// W A R N I N G !!!
|
|
|
|
// -----------------
|
|
|
|
//
|
|
|
|
// This is a modified version of SingleApplication,
|
|
|
|
// The original version is at:
|
|
|
|
//
|
|
|
|
// https://github.com/itay-grudev/SingleApplication
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2020-06-14 23:54:18 +02:00
|
|
|
#include <cstdlib>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
#include <QtGlobal>
|
2019-01-11 01:04:13 +01:00
|
|
|
#include <QCoreApplication>
|
2019-08-26 02:00:47 +02:00
|
|
|
#include <QThread>
|
|
|
|
#include <QSharedMemory>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QLocalSocket>
|
2019-08-26 02:00:47 +02:00
|
|
|
#include <QByteArray>
|
2019-12-21 18:11:54 +01:00
|
|
|
#include <QElapsedTimer>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QtDebug>
|
2019-01-11 01:04:13 +01:00
|
|
|
|
|
|
|
#include "singlecoreapplication.h"
|
|
|
|
#include "singlecoreapplication_p.h"
|
|
|
|
|
|
|
|
/**
|
2020-10-05 21:31:03 +02:00
|
|
|
* @brief Constructor. Checks and fires up LocalServer or closes the program if another instance already exists
|
2019-01-11 01:04:13 +01:00
|
|
|
* @param argc
|
|
|
|
* @param argv
|
2020-10-05 21:31:03 +02:00
|
|
|
* @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
|
2019-01-11 01:04:13 +01:00
|
|
|
*/
|
2019-08-26 02:00:47 +02:00
|
|
|
SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
|
2020-10-05 21:31:03 +02:00
|
|
|
: app_t(argc, argv),
|
|
|
|
d_ptr(new SingleCoreApplicationPrivate(this)) {
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
Q_D(SingleCoreApplication);
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
// Store the current mode of the program
|
2020-10-05 21:31:03 +02:00
|
|
|
d->options_ = options;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-05-29 17:37:46 +02:00
|
|
|
// Generating an application ID used for identifying the shared memory block and QLocalServer
|
2019-08-26 02:00:47 +02:00
|
|
|
d->genBlockServerName();
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
// To mitigate QSharedMemory issues with large amount of processes attempting to attach at the same time
|
|
|
|
d->randomSleep();
|
|
|
|
|
2019-01-11 01:04:13 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
2020-10-05 21:31:03 +02:00
|
|
|
// 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_;
|
2019-01-11 01:04:13 +01:00
|
|
|
#endif
|
2020-10-05 21:31:03 +02:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
// Guarantee thread safe behaviour with a shared memory block.
|
2020-10-05 21:31:03 +02:00
|
|
|
d->memory_ = new QSharedMemory(d->blockServerName_);
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
// Create a shared memory block
|
2020-10-05 21:31:03 +02:00
|
|
|
if (d->memory_->create(sizeof(InstancesInfo))) {
|
2019-08-26 02:00:47 +02:00
|
|
|
// Initialize the shared memory block
|
2020-10-05 21:31:03 +02:00
|
|
|
if (!d->memory_->lock()) {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to lock memory block after create.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
d->initializeMemoryBlock();
|
|
|
|
}
|
|
|
|
else {
|
2020-10-05 21:31:03 +02:00
|
|
|
if (d->memory_->error() == QSharedMemory::AlreadyExists) {
|
|
|
|
// Attempt to attach to the memory segment
|
|
|
|
if (!d->memory_->attach()) {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to attach to shared memory block.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
|
|
|
if (!d->memory_->lock()) {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to lock memory block after attach.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to create block.";
|
|
|
|
abortSafely();
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
InstancesInfo *inst = static_cast<InstancesInfo*>(d->memory_->data());
|
2019-12-21 18:11:54 +01:00
|
|
|
QElapsedTimer time;
|
2019-08-26 02:00:47 +02:00
|
|
|
time.start();
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
// Make sure the shared memory block is initialised and in consistent state
|
2020-10-05 21:31:03 +02:00
|
|
|
forever {
|
|
|
|
// If the shared memory block's checksum is valid continue
|
2020-10-04 02:18:10 +02:00
|
|
|
if (d->blockChecksum() == inst->checksum) break;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
// If more than 5s have elapsed, assume the primary instance crashed and assume it's position
|
2019-08-26 02:00:47 +02:00
|
|
|
if (time.elapsed() > 5000) {
|
|
|
|
qWarning() << "SingleCoreApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
|
|
|
|
d->initializeMemoryBlock();
|
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
// 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() << "SingleCoreApplication: Unable to unlock memory for random wait.";
|
|
|
|
qDebug() << d->memory_->errorString();
|
|
|
|
}
|
|
|
|
d->randomSleep();
|
|
|
|
if (!d->memory_->lock()) {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to lock memory after random wait.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2021-06-22 13:54:58 +02:00
|
|
|
if (!inst->primary) {
|
2019-08-26 02:00:47 +02:00
|
|
|
d->startPrimary();
|
2020-10-05 21:31:03 +02:00
|
|
|
if (!d->memory_->unlock()) {
|
|
|
|
qDebug() << "SingleCoreApplication: Unable to unlock memory after primary start.";
|
|
|
|
qDebug() << d->memory_->errorString();
|
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if another instance can be started
|
|
|
|
if (allowSecondary) {
|
|
|
|
d->startSecondary();
|
2020-10-05 21:31:03 +02:00
|
|
|
if (d->options_ & Mode::SecondaryNotification) {
|
2019-08-26 02:00:47 +02:00
|
|
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::SecondaryInstance);
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
2020-10-05 21:31:03 +02:00
|
|
|
if (!d->memory_->unlock()) {
|
|
|
|
qDebug() << "SingleCoreApplication: Unable to unlock memory after secondary start.";
|
|
|
|
qDebug() << d->memory_->errorString();
|
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
if (!d->memory_->unlock()) {
|
|
|
|
qDebug() << "SingleCoreApplication: Unable to unlock memory at end of execution.";
|
|
|
|
qDebug() << d->memory_->errorString();
|
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::NewInstance);
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
delete d;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
::exit(EXIT_SUCCESS);
|
2020-10-05 21:31:03 +02:00
|
|
|
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
SingleCoreApplication::~SingleCoreApplication() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
delete d;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* Checks if the current application instance is primary.
|
|
|
|
* @return Returns true if the instance is primary, false otherwise.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
bool SingleCoreApplication::isPrimary() {
|
2019-08-26 02:00:47 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
2020-10-05 21:31:03 +02:00
|
|
|
return d->server_ != nullptr;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* Checks if the current application instance is secondary.
|
|
|
|
* @return Returns true if the instance is secondary, false otherwise.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
bool SingleCoreApplication::isSecondary() {
|
2019-08-26 02:00:47 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
2020-10-05 21:31:03 +02:00
|
|
|
return d->server_ == nullptr;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
quint32 SingleCoreApplication::instanceId() {
|
2019-08-26 02:00:47 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
2020-10-05 21:31:03 +02:00
|
|
|
return d->instanceNumber_;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* Returns the OS PID (Process Identifier) of the process running the primary instance.
|
|
|
|
* Especially useful when SingleCoreApplication is coupled with OS. specific APIs.
|
|
|
|
* @return Returns the primary instance PID.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
qint64 SingleCoreApplication::primaryPid() {
|
2019-08-26 02:00:47 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->primaryPid();
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* Returns the username the primary instance is running as.
|
|
|
|
* @return Returns the username the primary instance is running as.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
QString SingleCoreApplication::primaryUser() {
|
2020-10-05 21:31:03 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->primaryUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the username the current instance is running as.
|
|
|
|
* @return Returns the username the current instance is running as.
|
|
|
|
*/
|
2021-06-22 16:15:04 +02:00
|
|
|
QString SingleCoreApplication::currentUser() {
|
2020-10-05 21:31:03 +02:00
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
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.
|
2021-07-02 18:45:53 +02:00
|
|
|
* @return true if the message was sent successfully, false otherwise.
|
2020-10-05 21:31:03 +02:00
|
|
|
*/
|
2021-06-20 19:04:08 +02:00
|
|
|
bool SingleCoreApplication::sendMessage(const QByteArray &message, const int timeout) {
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
|
|
|
|
// Nobody to connect to
|
2020-10-04 02:18:10 +02:00
|
|
|
if (isPrimary()) return false;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
// Make sure the socket is connected
|
2021-08-23 21:21:08 +02:00
|
|
|
if (!d->connectToPrimary(timeout, SingleCoreApplicationPrivate::Reconnect)) {
|
2020-10-05 21:31:03 +02:00
|
|
|
return false;
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
d->socket_->write(message);
|
|
|
|
const bool dataWritten = d->socket_->waitForBytesWritten(timeout);
|
|
|
|
d->socket_->flush();
|
2019-08-26 02:00:47 +02:00
|
|
|
return dataWritten;
|
2020-10-05 21:31:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up the shared memory block and exits with a failure.
|
|
|
|
* This function halts program execution.
|
|
|
|
*/
|
|
|
|
void SingleCoreApplication::abortSafely() {
|
|
|
|
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
|
|
|
|
qCritical() << "SingleCoreApplication: " << d->memory_->error() << d->memory_->errorString();
|
|
|
|
delete d;
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|