2016-07-05 01:19:48 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
2020-04-22 07:58:39 +08:00
|
|
|
// Copyright (c) Itay Grudev 2015 - 2020
|
2016-07-05 01:19:48 +08: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-12-28 02:22:44 +08:00
|
|
|
#include <QtCore/QElapsedTimer>
|
2016-08-10 09:42:46 +08:00
|
|
|
#include <QtCore/QByteArray>
|
2016-05-05 02:59:07 +08:00
|
|
|
#include <QtCore/QSharedMemory>
|
2017-02-02 12:28:00 +08:00
|
|
|
|
2016-03-05 02:15:13 +08:00
|
|
|
#include "singleapplication.h"
|
2016-08-10 09:42:46 +08:00
|
|
|
#include "singleapplication_p.h"
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2012-12-23 06:12:38 +08:00
|
|
|
/**
|
2015-02-27 03:19:38 +08:00
|
|
|
* @brief Constructor. Checks and fires up LocalServer or closes the program
|
2015-02-27 03:00:11 +08:00
|
|
|
* if another instance already exists
|
2012-12-23 06:12:38 +08:00
|
|
|
* @param argc
|
|
|
|
* @param argv
|
2020-09-09 06:00:04 +08: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
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2020-12-22 01:07:36 +08:00
|
|
|
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout, const QString &userData )
|
2016-05-05 04:05:59 +08:00
|
|
|
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) )
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2020-09-09 06:00:04 +08:00
|
|
|
Q_D( SingleApplication );
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2019-10-13 15:34:28 +08:00
|
|
|
#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.";
|
2019-10-11 21:50:52 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Store the current mode of the program
|
|
|
|
d->options = options;
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2020-12-11 08:18:00 +08:00
|
|
|
// Add any unique user data
|
|
|
|
if ( ! userData.isEmpty() )
|
|
|
|
d->addAppData( userData );
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Generating an application ID used for identifying the shared memory
|
|
|
|
// block and QLocalServer
|
2018-07-27 09:29:55 +08:00
|
|
|
d->genBlockServerName();
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2020-09-09 09:29:00 +08:00
|
|
|
// To mitigate QSharedMemory issues with large amount of processes
|
|
|
|
// attempting to attach at the same time
|
2020-10-16 02:41:00 +08:00
|
|
|
SingleApplicationPrivate::randomSleep();
|
2020-09-09 09:29:00 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
2018-07-27 09:29:55 +08: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.
|
2016-08-10 09:42:46 +08:00
|
|
|
d->memory = new QSharedMemory( d->blockServerName );
|
2016-05-05 03:41:23 +08:00
|
|
|
d->memory->attach();
|
|
|
|
delete d->memory;
|
2015-06-06 05:26:41 +08:00
|
|
|
#endif
|
2018-07-27 09:29:55 +08:00
|
|
|
// Guarantee thread safe behaviour with a shared memory block.
|
2016-08-10 09:42:46 +08:00
|
|
|
d->memory = new QSharedMemory( d->blockServerName );
|
|
|
|
|
|
|
|
// Create a shared memory block
|
2020-09-09 06:00:04 +08:00
|
|
|
if( d->memory->create( sizeof( InstancesInfo ) )){
|
2018-07-27 09:29:55 +08:00
|
|
|
// Initialize the shared memory block
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->lock() ){
|
|
|
|
qCritical() << "SingleApplication: Unable to lock memory block after create.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
d->initializeMemoryBlock();
|
2015-02-27 03:00:11 +08:00
|
|
|
} else {
|
2020-09-09 07:28:02 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
if( ! d->memory->lock() ){
|
|
|
|
qCritical() << "SingleApplication: Unable to lock memory block after attach.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-09 09:28:07 +08:00
|
|
|
qCritical() << "SingleApplication: Unable to create block.";
|
2020-09-09 07:28:02 +08:00
|
|
|
abortSafely();
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:50:27 +08:00
|
|
|
auto *inst = static_cast<InstancesInfo*>( d->memory->data() );
|
2019-12-28 02:22:44 +08:00
|
|
|
QElapsedTimer time;
|
2018-07-27 09:29:55 +08:00
|
|
|
time.start();
|
|
|
|
|
|
|
|
// Make sure the shared memory block is initialised and in consistent state
|
2020-09-09 06:00:04 +08:00
|
|
|
while( true ){
|
2020-09-09 06:15:17 +08:00
|
|
|
// If the shared memory block's checksum is valid continue
|
|
|
|
if( d->blockChecksum() == inst->checksum ) break;
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2020-09-09 06:15:17 +08:00
|
|
|
// 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();
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2020-09-09 06:15:17 +08: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
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->unlock() ){
|
|
|
|
qDebug() << "SingleApplication: Unable to unlock memory for random wait.";
|
|
|
|
qDebug() << d->memory->errorString();
|
|
|
|
}
|
2020-10-16 02:41:00 +08:00
|
|
|
SingleApplicationPrivate::randomSleep();
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->lock() ){
|
|
|
|
qCritical() << "SingleApplication: Unable to lock memory after random wait.";
|
|
|
|
abortSafely();
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
if( inst->primary == false ){
|
2018-07-27 09:29:55 +08:00
|
|
|
d->startPrimary();
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->unlock() ){
|
|
|
|
qDebug() << "SingleApplication: Unable to unlock memory after primary start.";
|
|
|
|
qDebug() << d->memory->errorString();
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
// Check if another instance can be started
|
2020-09-09 06:00:04 +08:00
|
|
|
if( allowSecondary ){
|
2018-07-27 09:29:55 +08:00
|
|
|
d->startSecondary();
|
2020-09-09 06:00:04 +08:00
|
|
|
if( d->options & Mode::SecondaryNotification ){
|
2018-07-27 09:29:55 +08:00
|
|
|
d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance );
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->unlock() ){
|
|
|
|
qDebug() << "SingleApplication: Unable to unlock memory after secondary start.";
|
|
|
|
qDebug() << d->memory->errorString();
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
return;
|
2015-02-27 03:00:11 +08:00
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! d->memory->unlock() ){
|
|
|
|
qDebug() << "SingleApplication: Unable to unlock memory at end of execution.";
|
|
|
|
qDebug() << d->memory->errorString();
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2017-12-04 01:04:02 +08:00
|
|
|
d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance );
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2016-05-05 03:51:06 +08:00
|
|
|
delete d;
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
::exit( EXIT_SUCCESS );
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
2015-02-27 03:00:11 +08:00
|
|
|
SingleApplication::~SingleApplication()
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2020-09-09 06:00:04 +08:00
|
|
|
Q_D( SingleApplication );
|
2016-05-05 02:59:07 +08:00
|
|
|
delete d;
|
|
|
|
}
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* Checks if the current application instance is primary.
|
|
|
|
* @return Returns true if the instance is primary, false otherwise.
|
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
bool SingleApplication::isPrimary() const
|
2016-05-05 02:59:07 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->server != nullptr;
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* Checks if the current application instance is secondary.
|
|
|
|
* @return Returns true if the instance is secondary, false otherwise.
|
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
bool SingleApplication::isSecondary() const
|
2016-05-05 02:59:07 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->server == nullptr;
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08: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-01-25 06:16:37 +08:00
|
|
|
quint32 SingleApplication::instanceId() const
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->instanceNumber;
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
qint64 SingleApplication::primaryPid() const
|
2017-10-02 19:17:41 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2017-10-02 19:17:41 +08:00
|
|
|
return d->primaryPid();
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* Returns the username the primary instance is running as.
|
|
|
|
* @return Returns the username the primary instance is running as.
|
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
QString SingleApplication::primaryUser() const
|
2020-03-03 09:22:54 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2020-03-03 09:22:54 +08:00
|
|
|
return d->primaryUser();
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* Returns the username the current instance is running as.
|
|
|
|
* @return Returns the username the current instance is running as.
|
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
QString SingleApplication::currentUser() const
|
2020-03-27 15:00:14 +08:00
|
|
|
{
|
2020-10-16 02:41:00 +08:00
|
|
|
return SingleApplicationPrivate::getUsername();
|
2020-03-27 15:00:14 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 06:00:04 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-05-17 22:50:27 +08:00
|
|
|
bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
|
2016-08-10 09:42:46 +08:00
|
|
|
{
|
2020-09-09 06:00:04 +08:00
|
|
|
Q_D( SingleApplication );
|
2016-08-10 09:42:46 +08:00
|
|
|
|
|
|
|
// Nobody to connect to
|
|
|
|
if( isPrimary() ) return false;
|
|
|
|
|
|
|
|
// Make sure the socket is connected
|
2020-09-09 09:28:07 +08:00
|
|
|
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
|
|
|
|
return false;
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2021-05-31 21:23:18 +08:00
|
|
|
return d->writeConfirmedMessage( timeout, message );
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
2020-09-09 07:28:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up the shared memory block and exits with a failure.
|
|
|
|
* This function halts program execution.
|
|
|
|
*/
|
|
|
|
void SingleApplication::abortSafely()
|
|
|
|
{
|
|
|
|
Q_D( SingleApplication );
|
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
qCritical() << "SingleApplication: " << d->memory->error() << d->memory->errorString();
|
2020-09-09 07:28:02 +08:00
|
|
|
delete d;
|
|
|
|
::exit( EXIT_FAILURE );
|
|
|
|
}
|
2020-12-11 08:18:00 +08:00
|
|
|
|
2021-01-25 06:16:37 +08:00
|
|
|
QStringList SingleApplication::userData() const
|
2020-12-11 08:18:00 +08:00
|
|
|
{
|
2021-01-25 06:16:37 +08:00
|
|
|
Q_D( const SingleApplication );
|
2020-12-11 08:18:00 +08:00
|
|
|
return d->appData();
|
|
|
|
}
|