2019-01-11 01:04:13 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) Itay Grudev 2015 - 2018
|
|
|
|
//
|
|
|
|
// 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-02-08 03:40:30 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#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>
|
2020-05-29 17:37:46 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
# include <QRandomGenerator>
|
|
|
|
#else
|
|
|
|
# include <QDateTime>
|
|
|
|
#endif
|
2019-01-11 01:04:13 +01:00
|
|
|
|
|
|
|
#include "singlecoreapplication.h"
|
|
|
|
#include "singlecoreapplication_p.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor. Checks and fires up LocalServer or closes the program
|
|
|
|
* if another instance already exists
|
|
|
|
* @param argc
|
|
|
|
* @param argv
|
|
|
|
* @param {bool} allowSecondaryInstances
|
|
|
|
*/
|
2019-08-26 02:00:47 +02:00
|
|
|
SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
|
|
|
|
: app_t(argc, argv), d_ptr(new SingleCoreApplicationPrivate(this)) {
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
2019-08-26 02:00:47 +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
|
2019-08-26 02:00:47 +02:00
|
|
|
// 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))) {
|
|
|
|
// Initialize the shared memory block
|
|
|
|
d->memory->lock();
|
|
|
|
d->initializeMemoryBlock();
|
|
|
|
d->memory->unlock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Attempt to attach to the memory segment
|
|
|
|
if (!d->memory->attach()) {
|
|
|
|
qCritical() << "SingleCoreApplication: Unable to attach to shared memory block.";
|
|
|
|
qCritical() << d->memory->errorString();
|
|
|
|
delete d;
|
|
|
|
::exit(EXIT_FAILURE);
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +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
|
|
|
|
while (true) {
|
|
|
|
d->memory->lock();
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
if(d->blockChecksum() == inst->checksum) break;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
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
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
d->memory->unlock();
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
// Random sleep here limits the probability of a collision between two racing apps
|
2020-05-29 17:37:46 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u));
|
|
|
|
#else
|
2019-08-26 02:00:47 +02:00
|
|
|
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
|
2020-05-29 17:37:46 +02:00
|
|
|
QThread::sleep(8 + static_cast<unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
|
|
|
|
#endif
|
2019-08-26 02:00:47 +02:00
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
if (inst->primary == false) {
|
|
|
|
d->startPrimary();
|
|
|
|
d->memory->unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if another instance can be started
|
|
|
|
if (allowSecondary) {
|
|
|
|
inst->secondary += 1;
|
|
|
|
inst->checksum = d->blockChecksum();
|
|
|
|
d->instanceNumber = inst->secondary;
|
|
|
|
d->startSecondary();
|
|
|
|
if(d->options & Mode::SecondaryNotification) {
|
|
|
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::SecondaryInstance);
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
2019-08-26 02:00:47 +02:00
|
|
|
d->memory->unlock();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
d->memory->unlock();
|
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);
|
2019-01-11 01:04:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Destructor
|
|
|
|
*/
|
2019-08-26 02:00:47 +02:00
|
|
|
SingleCoreApplication::~SingleCoreApplication() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
delete d;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
bool SingleCoreApplication::isPrimary() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->server != nullptr;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
bool SingleCoreApplication::isSecondary() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->server == nullptr;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
quint32 SingleCoreApplication::instanceId() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->instanceNumber;
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
qint64 SingleCoreApplication::primaryPid() {
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
return d->primaryPid();
|
2019-01-11 01:04:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
bool SingleCoreApplication::sendMessage(QByteArray message, int timeout) {
|
|
|
|
|
|
|
|
Q_D(SingleCoreApplication);
|
|
|
|
|
|
|
|
// Nobody to connect to
|
|
|
|
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
|
2020-02-17 20:21:45 +01:00
|
|
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::Reconnect);
|
2019-01-11 01:04:13 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
d->socket->write(message);
|
|
|
|
bool dataWritten = d->socket->waitForBytesWritten(timeout);
|
|
|
|
d->socket->flush();
|
|
|
|
return dataWritten;
|
2019-01-11 01:04:13 +01:00
|
|
|
|
|
|
|
}
|