SingleApplication: Use geteuid / getpwuid to get username and reformat

sources
This commit is contained in:
Jonas Kvinge
2019-08-26 02:00:47 +02:00
parent 7fe1f4de93
commit b2160255d3
10 changed files with 1127 additions and 1034 deletions

View File

@@ -1,9 +1,17 @@
cmake_minimum_required(VERSION 2.8.11) cmake_minimum_required(VERSION 2.8.11)
include(CheckIncludeFiles)
include(CheckFunctionExists)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -U__STRICT_ANSI__ -Wall -Woverloaded-virtual -Wno-sign-compare -fpermissive") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -U__STRICT_ANSI__ -Wall -Woverloaded-virtual -Wno-sign-compare -fpermissive")
if(CMAKE_VERSION VERSION_GREATER 3.0)
check_function_exists(geteuid HAVE_GETEUID)
check_function_exists(getpwuid HAVE_GETPWUID)
endif()
set(SINGLEAPP-SOURCES singleapplication.cpp singleapplication_p.cpp) set(SINGLEAPP-SOURCES singleapplication.cpp singleapplication_p.cpp)
set(SINGLEAPP-MOC-HEADERS singleapplication.h singleapplication_p.h) set(SINGLEAPP-MOC-HEADERS singleapplication.h singleapplication_p.h)
QT5_WRAP_CPP(SINGLEAPP-SOURCES-MOC ${SINGLEAPP-MOC-HEADERS}) QT5_WRAP_CPP(SINGLEAPP-SOURCES-MOC ${SINGLEAPP-MOC-HEADERS})
@@ -15,3 +23,6 @@ set(SINGLECOREAPP-MOC-HEADERS singlecoreapplication.h singlecoreapplication_p.h)
QT5_WRAP_CPP(SINGLECOREAPP-SOURCES-MOC ${SINGLECOREAPP-MOC-HEADERS}) QT5_WRAP_CPP(SINGLECOREAPP-SOURCES-MOC ${SINGLECOREAPP-MOC-HEADERS})
ADD_LIBRARY(singlecoreapplication STATIC ${SINGLECOREAPP-SOURCES} ${SINGLECOREAPP-SOURCES-MOC}) ADD_LIBRARY(singlecoreapplication STATIC ${SINGLECOREAPP-SOURCES} ${SINGLECOREAPP-SOURCES-MOC})
target_link_libraries(singlecoreapplication Qt5::Core Qt5::Widgets Qt5::Network) target_link_libraries(singlecoreapplication Qt5::Core Qt5::Widgets Qt5::Network)
configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/config.h")
include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@@ -0,0 +1,2 @@
#cmakedefine HAVE_GETEUID
#cmakedefine HAVE_GETPWUID

View File

@@ -20,156 +20,165 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#include <QApplication> //
#include <QtCore/QTime> // W A R N I N G !!!
#include <QtCore/QThread> // -----------------
#include <QtCore/QDateTime> //
#include <QtCore/QByteArray> // This is a modified version of SingleApplication,
#include <QtCore/QSharedMemory> // The original version is at:
//
// https://github.com/itay-grudev/SingleApplication
//
//
#include <QtGlobal>
#include <QCoreApplication>
#include <QThread>
#include <QSharedMemory>
#include <QByteArray>
#include <QDateTime>
#include <QTime>
#include "singleapplication.h" #include "singleapplication.h"
#include "singleapplication_p.h" #include "singleapplication_p.h"
/** /**
* @brief Constructor. Checks and fires up LocalServer or closes the program * @brief Constructor.
* if another instance already exists * Checks and fires up LocalServer or closes the program if another instance already exists
* @param argc * @param argc
* @param argv * @param argv
* @param {bool} allowSecondaryInstances * @param {bool} allowSecondaryInstances
*/ */
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout ) SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) ) : app_t(argc, argv), d_ptr(new SingleApplicationPrivate(this)) {
{
Q_D(SingleApplication);
// Store the current mode of the program Q_D(SingleApplication);
d->options = options;
// Generating an application ID used for identifying the shared memory // Store the current mode of the program
// block and QLocalServer d->options = options;
d->genBlockServerName();
// Generating an application ID used for identifying the shared memory block and QLocalServer
d->genBlockServerName();
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the // By explicitly attaching it and then deleting it we make sure that the
// memory is deleted even after the process has crashed on Unix. // memory is deleted even after the process has crashed on Unix.
d->memory = new QSharedMemory( d->blockServerName ); d->memory = new QSharedMemory(d->blockServerName);
d->memory->attach(); d->memory->attach();
delete d->memory; delete d->memory;
#endif #endif
// Guarantee thread safe behaviour with a shared memory block. // Guarantee thread safe behaviour with a shared memory block.
d->memory = new QSharedMemory( d->blockServerName ); d->memory = new QSharedMemory(d->blockServerName);
// Create a shared memory block // Create a shared memory block
if( d->memory->create( sizeof( InstancesInfo ) ) ) { if (d->memory->create(sizeof(InstancesInfo))) {
// Initialize the shared memory block // Initialize the shared memory block
d->memory->lock(); d->memory->lock();
d->initializeMemoryBlock(); d->initializeMemoryBlock();
d->memory->unlock(); d->memory->unlock();
} else { }
// Attempt to attach to the memory segment else {
if( ! d->memory->attach() ) { // Attempt to attach to the memory segment
qCritical() << "SingleApplication: Unable to attach to shared memory block."; if (! d->memory->attach()) {
qCritical() << d->memory->errorString(); qCritical() << "SingleApplication: Unable to attach to shared memory block.";
delete d; qCritical() << d->memory->errorString();
::exit( EXIT_FAILURE ); delete d;
} ::exit(EXIT_FAILURE);
} }
}
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() ); InstancesInfo* inst = static_cast<InstancesInfo*>(d->memory->data());
QTime time; QTime time;
time.start(); time.start();
// Make sure the shared memory block is initialised and in consistent state // Make sure the shared memory block is initialised and in consistent state
while( true ) { while (true) {
d->memory->lock(); d->memory->lock();
if( d->blockChecksum() == inst->checksum ) break; if (d->blockChecksum() == inst->checksum) break;
if( time.elapsed() > 5000 ) { if (time.elapsed() > 5000) {
qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
d->initializeMemoryBlock(); d->initializeMemoryBlock();
}
d->memory->unlock();
// Random sleep here limits the probability of a collision between two racing apps
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
}
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, SingleApplicationPrivate::SecondaryInstance );
}
d->memory->unlock();
return;
} }
d->memory->unlock(); d->memory->unlock();
d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance ); // Random sleep here limits the probability of a collision between two racing apps
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
}
delete d; 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, SingleApplicationPrivate::SecondaryInstance);
}
d->memory->unlock();
return;
}
d->memory->unlock();
d->connectToPrimary(timeout, SingleApplicationPrivate::NewInstance);
delete d;
::exit(EXIT_SUCCESS);
::exit( EXIT_SUCCESS );
} }
/** /**
* @brief Destructor * @brief Destructor
*/ */
SingleApplication::~SingleApplication() SingleApplication::~SingleApplication() {
{ Q_D(SingleApplication);
Q_D(SingleApplication); delete d;
delete d;
} }
bool SingleApplication::isPrimary() bool SingleApplication::isPrimary() {
{ Q_D(SingleApplication);
Q_D(SingleApplication); return d->server != nullptr;
return d->server != nullptr;
} }
bool SingleApplication::isSecondary() bool SingleApplication::isSecondary() {
{ Q_D(SingleApplication);
Q_D(SingleApplication); return d->server == nullptr;
return d->server == nullptr;
} }
quint32 SingleApplication::instanceId() quint32 SingleApplication::instanceId() {
{ Q_D(SingleApplication);
Q_D(SingleApplication); return d->instanceNumber;
return d->instanceNumber;
} }
qint64 SingleApplication::primaryPid() qint64 SingleApplication::primaryPid() {
{ Q_D(SingleApplication);
Q_D(SingleApplication); return d->primaryPid();
return d->primaryPid();
} }
bool SingleApplication::sendMessage( QByteArray message, int timeout ) bool SingleApplication::sendMessage(QByteArray message, int timeout) {
{
Q_D(SingleApplication);
// Nobody to connect to Q_D(SingleApplication);
if( isPrimary() ) return false;
// Make sure the socket is connected // Nobody to connect to
d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ); if (isPrimary()) return false;
// Make sure the socket is connected
d->connectToPrimary(timeout, SingleApplicationPrivate::Reconnect);
d->socket->write(message);
bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush();
return dataWritten;
d->socket->write( message );
bool dataWritten = d->socket->waitForBytesWritten( timeout );
d->socket->flush();
return dataWritten;
} }

View File

@@ -20,12 +20,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
//
// 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
//
//
#ifndef SINGLEAPPLICATION_H #ifndef SINGLEAPPLICATION_H
#define SINGLEAPPLICATION_H #define SINGLEAPPLICATION_H
#include <QtCore/QtGlobal> #include <QtGlobal>
#include <QApplication> #include <QApplication>
#include <QtNetwork/QLocalSocket> #include <QLocalSocket>
class SingleApplicationPrivate; class SingleApplicationPrivate;
@@ -34,95 +45,95 @@ class SingleApplicationPrivate;
* Application * Application
* @see QCoreApplication * @see QCoreApplication
*/ */
class SingleApplication : public QApplication class SingleApplication : public QApplication {
{ Q_OBJECT
Q_OBJECT
typedef QApplication app_t; typedef QApplication app_t;
public: public:
/** /**
* @brief Mode of operation of SingleApplication. * @brief Mode of operation of SingleApplication.
* Whether the block should be user-wide or system-wide and whether the * Whether the block should be user-wide or system-wide and whether the
* primary instance should be notified when a secondary instance had been * primary instance should be notified when a secondary instance had been
* started. * started.
* @note Operating system can restrict the shared memory blocks to the same * @note Operating system can restrict the shared memory blocks to the same
* user, in which case the User/System modes will have no effect and the * user, in which case the User/System modes will have no effect and the
* block will be user wide. * block will be user wide.
* @enum * @enum
*/ */
enum Mode { enum Mode {
User = 1 << 0, User = 1 << 0,
System = 1 << 1, System = 1 << 1,
SecondaryNotification = 1 << 2, SecondaryNotification = 1 << 2,
ExcludeAppVersion = 1 << 3, ExcludeAppVersion = 1 << 3,
ExcludeAppPath = 1 << 4 ExcludeAppPath = 1 << 4
}; };
Q_DECLARE_FLAGS(Options, Mode) Q_DECLARE_FLAGS(Options, Mode)
/** /**
* @brief Intitializes a SingleApplication instance with argc command line * @brief Intitializes a SingleApplication instance with argc command line
* arguments in argv * arguments in argv
* @arg {int &} argc - Number of arguments in argv * @arg {int &} argc - Number of arguments in argv
* @arg {const char *[]} argv - Supplied command line arguments * @arg {const char *[]} argv - Supplied command line arguments
* @arg {bool} allowSecondary - Whether to start the instance as secondary * @arg {bool} allowSecondary - Whether to start the instance as secondary
* if there is already a primary instance. * if there is already a primary instance.
* @arg {Mode} mode - Whether for the SingleApplication block to be applied * @arg {Mode} mode - Whether for the SingleApplication block to be applied
* User wide or System wide. * User wide or System wide.
* @arg {int} timeout - Timeout to wait in milliseconds. * @arg {int} timeout - Timeout to wait in milliseconds.
* @note argc and argv may be changed as Qt removes arguments that it * @note argc and argv may be changed as Qt removes arguments that it
* recognizes * recognizes
* @note Mode::SecondaryNotification only works if set on both the primary * @note Mode::SecondaryNotification only works if set on both the primary
* instance and the secondary instance. * instance and the secondary instance.
* @note The timeout is just a hint for the maximum time of blocking * @note The timeout is just a hint for the maximum time of blocking
* operations. It does not guarantee that the SingleApplication * operations. It does not guarantee that the SingleApplication
* initialisation will be completed in given time, though is a good hint. * initialisation will be completed in given time, though is a good hint.
* Usually 4*timeout would be the worst case (fail) scenario. * Usually 4*timeout would be the worst case (fail) scenario.
* @see See the corresponding QAPPLICATION_CLASS constructor for reference * @see See the corresponding QAPPLICATION_CLASS constructor for reference
*/ */
explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 ); explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 );
~SingleApplication(); ~SingleApplication();
/** /**
* @brief Returns if the instance is the primary instance * @brief Returns if the instance is the primary instance
* @returns {bool} * @returns {bool}
*/ */
bool isPrimary(); bool isPrimary();
/** /**
* @brief Returns if the instance is a secondary instance * @brief Returns if the instance is a secondary instance
* @returns {bool} * @returns {bool}
*/ */
bool isSecondary(); bool isSecondary();
/** /**
* @brief Returns a unique identifier for the current instance * @brief Returns a unique identifier for the current instance
* @returns {qint32} * @returns {qint32}
*/ */
quint32 instanceId(); quint32 instanceId();
/** /**
* @brief Returns the process ID (PID) of the primary instance * @brief Returns the process ID (PID) of the primary instance
* @returns {qint64} * @returns {qint64}
*/ */
qint64 primaryPid(); qint64 primaryPid();
/** /**
* @brief Sends a message to the primary instance. Returns true on success. * @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting * @param {int} timeout - Timeout for connecting
* @returns {bool} * @returns {bool}
* @note sendMessage() will return false if invoked from the primary * @note sendMessage() will return false if invoked from the primary
* instance. * instance.
*/ */
bool sendMessage( QByteArray message, int timeout = 1000 ); bool sendMessage( QByteArray message, int timeout = 1000 );
Q_SIGNALS: signals:
void instanceStarted(); void instanceStarted();
void receivedMessage( quint32 instanceId, QByteArray message ); void receivedMessage( quint32 instanceId, QByteArray message );
private:
SingleApplicationPrivate *d_ptr;
Q_DECLARE_PRIVATE(SingleApplication)
private:
SingleApplicationPrivate *d_ptr;
Q_DECLARE_PRIVATE(SingleApplication)
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options) Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)

View File

@@ -24,370 +24,386 @@
// W A R N I N G !!! // W A R N I N G !!!
// ----------------- // -----------------
// //
// This file is not part of the SingleApplication API. It is used purely as an // This is a modified version of SingleApplication,
// implementation detail. This header file may change from version to // The original version is at:
// version without notice, or may even be removed.
// //
// https://github.com/itay-grudev/SingleApplication
//
//
#include "config.h"
#include <QtGlobal>
#include <cstdlib> #include <cstdlib>
#include <cstddef> #include <cstddef>
#include <QtGlobal> #ifdef Q_OS_UNIX
#include <QtCore/QDir> # include <unistd.h>
#include <QtCore/QByteArray> # include <sys/types.h>
#include <QtCore/QSemaphore> # include <pwd.h>
#include <QtCore/QDataStream> #endif
#include <QtCore/QStandardPaths>
#include <QtCore/QCryptographicHash> #include <QDir>
#include <QtNetwork/QLocalServer> #include <QByteArray>
#include <QtNetwork/QLocalSocket> #include <QDataStream>
#include <QCryptographicHash>
#include <QLocalServer>
#include <QLocalSocket>
#include "singleapplication.h" #include "singleapplication.h"
#include "singleapplication_p.h" #include "singleapplication_p.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> # include <windows.h>
#include <lmcons.h> # include <lmcons.h>
#endif #endif
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr ) SingleApplicationPrivate::SingleApplicationPrivate(SingleApplication *q_ptr)
: q_ptr( q_ptr ) : q_ptr(q_ptr) {
{
server = nullptr; server = nullptr;
socket = nullptr; socket = nullptr;
memory = nullptr; memory = nullptr;
instanceNumber = -1; instanceNumber = -1;
} }
SingleApplicationPrivate::~SingleApplicationPrivate() SingleApplicationPrivate::~SingleApplicationPrivate() {
{
if( socket != nullptr ) {
socket->close();
delete socket;
}
memory->lock(); if (socket != nullptr) {
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data()); socket->close();
if( server != nullptr ) { delete socket;
server->close(); }
delete server;
inst->primary = false;
inst->primaryPid = -1;
inst->checksum = blockChecksum();
}
memory->unlock();
delete memory; memory->lock();
} InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
if (server != nullptr) {
void SingleApplicationPrivate::genBlockServerName() server->close();
{ delete server;
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
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
appData.addData(qgetenv("USER"));
#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::initializeMemoryBlock()
{
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
inst->primary = false; inst->primary = false;
inst->secondary = 0;
inst->primaryPid = -1; inst->primaryPid = -1;
inst->checksum = blockChecksum(); inst->checksum = blockChecksum();
}
memory->unlock();
delete memory;
} }
void SingleApplicationPrivate::startPrimary() void SingleApplicationPrivate::genBlockServerName() {
{
Q_Q(SingleApplication);
// Successful creation means that no main process exists QCryptographicHash appData(QCryptographicHash::Sha256);
// So we start a QLocalServer to listen for connections appData.addData("SingleApplication", 17);
QLocalServer::removeServer( blockServerName ); appData.addData(SingleApplication::app_t::applicationName().toUtf8());
server = new QLocalServer(); appData.addData(SingleApplication::app_t::organizationName().toUtf8());
appData.addData(SingleApplication::app_t::organizationDomain().toUtf8());
// Restrict access to the socket according to the if (!(options & SingleApplication::Mode::ExcludeAppVersion)) {
// SingleApplication::Mode::User flag on User level or no restrictions appData.addData(SingleApplication::app_t::applicationVersion().toUtf8());
if( options & SingleApplication::Mode::User ) { }
server->setSocketOptions( QLocalServer::UserAccessOption );
} else { if (! (options & SingleApplication::Mode::ExcludeAppPath)) {
server->setSocketOptions( QLocalServer::WorldAccessOption ); #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_UNIX
QByteArray username;
#if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID)
uid_t uid = geteuid();
if (uid != -1) {
struct passwd *pw = getpwuid(uid);
if (pw) {
username = pw->pw_name;
}
} }
qDebug() << username;
#endif
if (username.isEmpty()) username = qgetenv("USER");
appData.addData(username);
#endif
#ifdef Q_OS_WIN
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());
}
#endif
}
server->listen( blockServerName ); // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements.
QObject::connect( blockServerName = appData.result().toBase64().replace("/", "_");
server,
&QLocalServer::newConnection,
this,
&SingleApplicationPrivate::slotConnectionEstablished
);
// Reset the number of connections
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
inst->primary = true;
inst->primaryPid = q->applicationPid();
inst->checksum = blockChecksum();
instanceNumber = 0;
} }
void SingleApplicationPrivate::startSecondary() void SingleApplicationPrivate::initializeMemoryBlock() {
{
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
inst->primary = false;
inst->secondary = 0;
inst->primaryPid = -1;
inst->checksum = blockChecksum();
} }
void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType ) void SingleApplicationPrivate::startPrimary() {
{
// 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; Q_Q(SingleApplication);
if( socket->state() == QLocalSocket::ConnectedState )
return;
// If not connect // Successful creation means that no main process exists
if( socket->state() == QLocalSocket::UnconnectedState || // So we start a QLocalServer to listen for connections
socket->state() == QLocalSocket::ClosingState ) { QLocalServer::removeServer(blockServerName);
socket->connectToServer( blockServerName ); server = new QLocalServer();
}
// Wait for being connected // Restrict access to the socket according to the
if( socket->state() == QLocalSocket::ConnectingState ) { // SingleApplication::Mode::User flag on User level or no restrictions
socket->waitForConnected( msecs ); if (options & SingleApplication::Mode::User) {
} server->setSocketOptions(QLocalServer::UserAccessOption);
}
else {
server->setSocketOptions(QLocalServer::WorldAccessOption);
}
// Initialisation message according to the SingleApplication protocol server->listen(blockServerName);
if( socket->state() == QLocalSocket::ConnectedState ) { QObject::connect(server, &QLocalServer::newConnection, this, &SingleApplicationPrivate::slotConnectionEstablished);
// Notify the parent that a new instance had been started;
QByteArray initMsg; // Reset the number of connections
QDataStream writeStream(&initMsg, QIODevice::WriteOnly); InstancesInfo* inst = static_cast <InstancesInfo*>(memory->data());
inst->primary = true;
inst->primaryPid = q->applicationPid();
inst->checksum = blockChecksum();
instanceNumber = 0;
}
void SingleApplicationPrivate::startSecondary() {}
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);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
writeStream.setVersion(QDataStream::Qt_5_6); writeStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
writeStream << blockServerName.toLatin1(); writeStream << blockServerName.toLatin1();
writeStream << static_cast<quint8>(connectionType); writeStream << static_cast<quint8>(connectionType);
writeStream << instanceNumber; writeStream << instanceNumber;
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length())); quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
writeStream << checksum; writeStream << checksum;
// The header indicates the message length that follows // The header indicates the message length that follows
QByteArray header; QByteArray header;
QDataStream headerStream(&header, QIODevice::WriteOnly); QDataStream headerStream(&header, QIODevice::WriteOnly);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
headerStream.setVersion(QDataStream::Qt_5_6); headerStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
headerStream << static_cast <quint64>( initMsg.length() ); headerStream << static_cast <quint64>(initMsg.length());
socket->write(header);
socket->write(initMsg);
socket->flush();
socket->waitForBytesWritten(msecs);
}
socket->write( header );
socket->write( initMsg );
socket->flush();
socket->waitForBytesWritten( msecs );
}
} }
quint16 SingleApplicationPrivate::blockChecksum() quint16 SingleApplicationPrivate::blockChecksum() {
{
return qChecksum( return qChecksum(static_cast <const char *>(memory->data()), offsetof(InstancesInfo, checksum));
static_cast <const char *>( memory->data() ),
offsetof( InstancesInfo, checksum )
);
} }
qint64 SingleApplicationPrivate::primaryPid() qint64 SingleApplicationPrivate::primaryPid() {
{
qint64 pid;
memory->lock(); qint64 pid;
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
pid = inst->primaryPid; memory->lock();
memory->unlock(); InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
pid = inst->primaryPid;
memory->unlock();
return pid;
return pid;
} }
/** /**
* @brief Executed when a connection has been made to the LocalServer * @brief Executed when a connection has been made to the LocalServer
*/ */
void SingleApplicationPrivate::slotConnectionEstablished() void SingleApplicationPrivate::slotConnectionEstablished() {
{
QLocalSocket *nextConnSocket = server->nextPendingConnection();
connectionMap.insert(nextConnSocket, ConnectionInfo());
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose, QLocalSocket *nextConnSocket = server->nextPendingConnection();
[nextConnSocket, this]() { connectionMap.insert(nextConnSocket, ConnectionInfo());
auto &info = connectionMap[nextConnSocket];
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
}
);
QObject::connect(nextConnSocket, &QLocalSocket::disconnected, QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
[nextConnSocket, this](){ [nextConnSocket, this]() {
connectionMap.remove(nextConnSocket); auto &info = connectionMap[nextConnSocket];
nextConnSocket->deleteLater(); Q_EMIT this->slotClientConnectionClosed(nextConnSocket, info.instanceId);
} }
); );
QObject::connect(nextConnSocket, &QLocalSocket::disconnected,
[nextConnSocket, this](){
connectionMap.remove(nextConnSocket);
nextConnSocket->deleteLater();
}
);
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
[nextConnSocket, this]() {
auto &info = connectionMap[nextConnSocket];
switch(info.stage) {
case StageHeader:
readInitMessageHeader(nextConnSocket);
break;
case StageBody:
readInitMessageBody(nextConnSocket);
break;
case StageConnected:
Q_EMIT this->slotDataAvailable(nextConnSocket, info.instanceId);
break;
default:
break;
};
}
);
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
[nextConnSocket, this]() {
auto &info = connectionMap[nextConnSocket];
switch(info.stage) {
case StageHeader:
readInitMessageHeader(nextConnSocket);
break;
case StageBody:
readInitMessageBody(nextConnSocket);
break;
case StageConnected:
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
break;
default:
break;
};
}
);
} }
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock ) void SingleApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
{
if (!connectionMap.contains( sock )) {
return;
}
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) { if (!connectionMap.contains(sock)) {
return; return;
} }
QDataStream headerStream( sock ); if (sock->bytesAvailable() < (qint64) sizeof(quint64)) {
return;
}
QDataStream headerStream(sock);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
headerStream.setVersion( QDataStream::Qt_5_6 ); headerStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
// Read the header to know the message length // Read the header to know the message length
quint64 msgLen = 0; quint64 msgLen = 0;
headerStream >> msgLen; headerStream >> msgLen;
ConnectionInfo &info = connectionMap[sock]; ConnectionInfo &info = connectionMap[sock];
info.stage = StageBody; info.stage = StageBody;
info.msgLen = msgLen; info.msgLen = msgLen;
if (sock->bytesAvailable() >= (qint64) msgLen) {
readInitMessageBody(sock);
}
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
readInitMessageBody( sock );
}
} }
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock ) void SingleApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
{
Q_Q(SingleApplication);
if (!connectionMap.contains( sock )) { Q_Q(SingleApplication);
return;
}
ConnectionInfo &info = connectionMap[sock]; if (!connectionMap.contains(sock)) {
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) { return;
return; }
}
// Read the message body ConnectionInfo &info = connectionMap[sock];
QByteArray msgBytes = sock->read(info.msgLen); if (sock->bytesAvailable() < (qint64)info.msgLen) {
QDataStream readStream(msgBytes); return;
}
// Read the message body
QByteArray msgBytes = sock->read(info.msgLen);
QDataStream readStream(msgBytes);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
readStream.setVersion( QDataStream::Qt_5_6 ); readStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
// server name // server name
QByteArray latin1Name; QByteArray latin1Name;
readStream >> latin1Name; readStream >> latin1Name;
// connection type // connection type
ConnectionType connectionType = InvalidConnection; ConnectionType connectionType = InvalidConnection;
quint8 connTypeVal = InvalidConnection; quint8 connTypeVal = InvalidConnection;
readStream >> connTypeVal; readStream >> connTypeVal;
connectionType = static_cast <ConnectionType>( connTypeVal ); connectionType = static_cast <ConnectionType>(connTypeVal);
// instance id // instance id
quint32 instanceId = 0; quint32 instanceId = 0;
readStream >> instanceId; readStream >> instanceId;
// checksum // checksum
quint16 msgChecksum = 0; quint16 msgChecksum = 0;
readStream >> msgChecksum; readStream >> msgChecksum;
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) ); const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16)));
bool isValid = readStream.status() == QDataStream::Ok && bool isValid = readStream.status() == QDataStream::Ok && QLatin1String(latin1Name) == blockServerName && msgChecksum == actualChecksum;
QLatin1String(latin1Name) == blockServerName &&
msgChecksum == actualChecksum;
if( !isValid ) { if (!isValid) {
sock->close(); sock->close();
return; return;
} }
info.instanceId = instanceId; info.instanceId = instanceId;
info.stage = StageConnected; info.stage = StageConnected;
if( connectionType == NewInstance || if (connectionType == NewInstance || (connectionType == SecondaryInstance && options & SingleApplication::Mode::SecondaryNotification)) {
( connectionType == SecondaryInstance && Q_EMIT q->instanceStarted();
options & SingleApplication::Mode::SecondaryNotification ) ) }
{
Q_EMIT q->instanceStarted(); if (sock->bytesAvailable() > 0) {
} Q_EMIT this->slotDataAvailable(sock, instanceId);
}
if (sock->bytesAvailable() > 0) {
Q_EMIT this->slotDataAvailable( sock, instanceId );
}
} }
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId ) void SingleApplicationPrivate::slotDataAvailable(QLocalSocket *dataSocket, quint32 instanceId) {
{
Q_Q(SingleApplication); Q_Q(SingleApplication);
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() ); Q_EMIT q->receivedMessage(instanceId, dataSocket->readAll());
} }
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId ) void SingleApplicationPrivate::slotClientConnectionClosed(QLocalSocket *closedSocket, quint32 instanceId) {
{
if( closedSocket->bytesAvailable() > 0 ) if (closedSocket->bytesAvailable() > 0)
Q_EMIT slotDataAvailable( closedSocket, instanceId ); Q_EMIT slotDataAvailable(closedSocket, instanceId);
} }

View File

@@ -24,76 +24,80 @@
// W A R N I N G !!! // W A R N I N G !!!
// ----------------- // -----------------
// //
// This file is not part of the SingleApplication API. It is used purely as an // This is a modified version of SingleApplication,
// implementation detail. This header file may change from version to // The original version is at:
// version without notice, or may even be removed. //
// https://github.com/itay-grudev/SingleApplication
//
// //
#ifndef SINGLEAPPLICATION_P_H #ifndef SINGLEAPPLICATION_P_H
#define SINGLEAPPLICATION_P_H #define SINGLEAPPLICATION_P_H
#include <QtCore/QSharedMemory> #include <QtGlobal>
#include <QtNetwork/QLocalServer> #include <QLocalSocket>
#include <QtNetwork/QLocalSocket> #include <QLocalServer>
#include <QSharedMemory>
#include <QMap>
#include "singleapplication.h" #include "singleapplication.h"
struct InstancesInfo { struct InstancesInfo {
bool primary; bool primary;
quint32 secondary; quint32 secondary;
qint64 primaryPid; qint64 primaryPid;
quint16 checksum; quint16 checksum;
}; };
struct ConnectionInfo { struct ConnectionInfo {
explicit ConnectionInfo() : explicit ConnectionInfo() : msgLen(0), instanceId(0), stage(0) {}
msgLen(0), instanceId(0), stage(0) {} qint64 msgLen;
qint64 msgLen; quint32 instanceId;
quint32 instanceId; quint8 stage;
quint8 stage;
}; };
class SingleApplicationPrivate : public QObject { class SingleApplicationPrivate : public QObject {
Q_OBJECT Q_OBJECT
public: public:
enum ConnectionType : quint8 { enum ConnectionType : quint8 {
InvalidConnection = 0, InvalidConnection = 0,
NewInstance = 1, NewInstance = 1,
SecondaryInstance = 2, SecondaryInstance = 2,
Reconnect = 3 Reconnect = 3
}; };
enum ConnectionStage : quint8 { enum ConnectionStage : quint8 {
StageHeader = 0, StageHeader = 0,
StageBody = 1, StageBody = 1,
StageConnected = 2, StageConnected = 2,
}; };
Q_DECLARE_PUBLIC(SingleApplication) Q_DECLARE_PUBLIC(SingleApplication)
SingleApplicationPrivate( SingleApplication *q_ptr ); SingleApplicationPrivate( SingleApplication *q_ptr );
~SingleApplicationPrivate(); ~SingleApplicationPrivate();
void genBlockServerName(); void genBlockServerName();
void initializeMemoryBlock(); void initializeMemoryBlock();
void startPrimary(); void startPrimary();
void startSecondary(); void startSecondary();
void connectToPrimary(int msecs, ConnectionType connectionType ); void connectToPrimary(int msecs, ConnectionType connectionType );
quint16 blockChecksum(); quint16 blockChecksum();
qint64 primaryPid(); qint64 primaryPid();
void readInitMessageHeader(QLocalSocket *socket); void readInitMessageHeader(QLocalSocket *socket);
void readInitMessageBody(QLocalSocket *socket); void readInitMessageBody(QLocalSocket *socket);
SingleApplication *q_ptr; SingleApplication *q_ptr;
QSharedMemory *memory; QSharedMemory *memory;
QLocalSocket *socket; QLocalSocket *socket;
QLocalServer *server; QLocalServer *server;
quint32 instanceNumber; quint32 instanceNumber;
QString blockServerName; QString blockServerName;
SingleApplication::Options options; SingleApplication::Options options;
QMap<QLocalSocket*, ConnectionInfo> connectionMap; QMap<QLocalSocket*, ConnectionInfo> connectionMap;
public Q_SLOTS: public slots:
void slotConnectionEstablished(); void slotConnectionEstablished();
void slotDataAvailable( QLocalSocket*, quint32 ); void slotDataAvailable(QLocalSocket*, quint32);
void slotClientConnectionClosed( QLocalSocket*, quint32 ); void slotClientConnectionClosed(QLocalSocket*, quint32);
}; };
#endif // SINGLEAPPLICATION_P_H #endif // SINGLEAPPLICATION_P_H

View File

@@ -20,12 +20,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
//
// 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
//
//
#include <QtGlobal>
#include <QCoreApplication> #include <QCoreApplication>
#include <QtCore/QTime> #include <QThread>
#include <QtCore/QThread> #include <QSharedMemory>
#include <QtCore/QDateTime> #include <QByteArray>
#include <QtCore/QByteArray> #include <QDateTime>
#include <QtCore/QSharedMemory> #include <QTime>
#include "singlecoreapplication.h" #include "singlecoreapplication.h"
#include "singlecoreapplication_p.h" #include "singlecoreapplication_p.h"
@@ -37,139 +49,137 @@
* @param argv * @param argv
* @param {bool} allowSecondaryInstances * @param {bool} allowSecondaryInstances
*/ */
SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout ) SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
: app_t( argc, argv ), d_ptr( new SingleCoreApplicationPrivate( this ) ) : app_t(argc, argv), d_ptr(new SingleCoreApplicationPrivate(this)) {
{
Q_D(SingleCoreApplication);
// Store the current mode of the program Q_D(SingleCoreApplication);
d->options = options;
// Generating an application ID used for identifying the shared memory // Store the current mode of the program
// block and QLocalServer d->options = options;
d->genBlockServerName();
// Generating an application ID used for identifying the shared memory
// block and QLocalServer
d->genBlockServerName();
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the // By explicitly attaching it and then deleting it we make sure that the
// memory is deleted even after the process has crashed on Unix. // memory is deleted even after the process has crashed on Unix.
d->memory = new QSharedMemory( d->blockServerName ); d->memory = new QSharedMemory(d->blockServerName);
d->memory->attach(); d->memory->attach();
delete d->memory; delete d->memory;
#endif #endif
// Guarantee thread safe behaviour with a shared memory block. // Guarantee thread safe behaviour with a shared memory block.
d->memory = new QSharedMemory( d->blockServerName ); d->memory = new QSharedMemory(d->blockServerName);
// Create a shared memory block // Create a shared memory block
if( d->memory->create( sizeof( InstancesInfo ) ) ) { if (d->memory->create(sizeof(InstancesInfo))) {
// Initialize the shared memory block // Initialize the shared memory block
d->memory->lock(); d->memory->lock();
d->initializeMemoryBlock(); d->initializeMemoryBlock();
d->memory->unlock(); d->memory->unlock();
} else { }
// Attempt to attach to the memory segment else {
if( ! d->memory->attach() ) { // Attempt to attach to the memory segment
qCritical() << "SingleCoreApplication: Unable to attach to shared memory block."; if (!d->memory->attach()) {
qCritical() << d->memory->errorString(); qCritical() << "SingleCoreApplication: Unable to attach to shared memory block.";
delete d; qCritical() << d->memory->errorString();
::exit( EXIT_FAILURE ); delete d;
} ::exit(EXIT_FAILURE);
} }
}
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() ); InstancesInfo* inst = static_cast<InstancesInfo*>(d->memory->data());
QTime time; QTime time;
time.start(); time.start();
// Make sure the shared memory block is initialised and in consistent state // Make sure the shared memory block is initialised and in consistent state
while( true ) { while (true) {
d->memory->lock(); d->memory->lock();
if( d->blockChecksum() == inst->checksum ) break; if(d->blockChecksum() == inst->checksum) break;
if( time.elapsed() > 5000 ) { if (time.elapsed() > 5000) {
qWarning() << "SingleCoreApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; qWarning() << "SingleCoreApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
d->initializeMemoryBlock(); d->initializeMemoryBlock();
}
d->memory->unlock();
// Random sleep here limits the probability of a collision between two racing apps
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
}
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 );
}
d->memory->unlock();
return;
} }
d->memory->unlock(); d->memory->unlock();
d->connectToPrimary( timeout, SingleCoreApplicationPrivate::NewInstance ); // Random sleep here limits the probability of a collision between two racing apps
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
}
delete d; 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);
}
d->memory->unlock();
return;
}
d->memory->unlock();
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::NewInstance);
delete d;
::exit(EXIT_SUCCESS);
::exit( EXIT_SUCCESS );
} }
/** /**
* @brief Destructor * @brief Destructor
*/ */
SingleCoreApplication::~SingleCoreApplication() SingleCoreApplication::~SingleCoreApplication() {
{ Q_D(SingleCoreApplication);
Q_D(SingleCoreApplication); delete d;
delete d;
} }
bool SingleCoreApplication::isPrimary() bool SingleCoreApplication::isPrimary() {
{ Q_D(SingleCoreApplication);
Q_D(SingleCoreApplication); return d->server != nullptr;
return d->server != nullptr;
} }
bool SingleCoreApplication::isSecondary() bool SingleCoreApplication::isSecondary() {
{ Q_D(SingleCoreApplication);
Q_D(SingleCoreApplication); return d->server == nullptr;
return d->server == nullptr;
} }
quint32 SingleCoreApplication::instanceId() quint32 SingleCoreApplication::instanceId() {
{ Q_D(SingleCoreApplication);
Q_D(SingleCoreApplication); return d->instanceNumber;
return d->instanceNumber;
} }
qint64 SingleCoreApplication::primaryPid() qint64 SingleCoreApplication::primaryPid() {
{ Q_D(SingleCoreApplication);
Q_D(SingleCoreApplication); return d->primaryPid();
return d->primaryPid();
} }
bool SingleCoreApplication::sendMessage( QByteArray message, int timeout ) bool SingleCoreApplication::sendMessage(QByteArray message, int timeout) {
{
Q_D(SingleCoreApplication);
// Nobody to connect to Q_D(SingleCoreApplication);
if( isPrimary() ) return false;
// Make sure the socket is connected // Nobody to connect to
d->connectToPrimary( timeout, SingleCoreApplicationPrivate::Reconnect ); if(isPrimary()) return false;
// Make sure the socket is connected
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::Reconnect);
d->socket->write(message);
bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush();
return dataWritten;
d->socket->write( message );
bool dataWritten = d->socket->waitForBytesWritten( timeout );
d->socket->flush();
return dataWritten;
} }

View File

@@ -20,12 +20,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
//
// 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
//
//
#ifndef SINGLECOREAPPLICATION_H #ifndef SINGLECOREAPPLICATION_H
#define SINGLECOREAPPLICATION_H #define SINGLECOREAPPLICATION_H
#include <QtCore/QtGlobal> #include <QtGlobal>
#include <QCoreApplication> #include <QCoreApplication>
#include <QtNetwork/QLocalSocket> #include <QLocalSocket>
class SingleCoreApplicationPrivate; class SingleCoreApplicationPrivate;
@@ -34,95 +45,94 @@ class SingleCoreApplicationPrivate;
* Application * Application
* @see QCoreApplication * @see QCoreApplication
*/ */
class SingleCoreApplication : public QCoreApplication class SingleCoreApplication : public QCoreApplication {
{ Q_OBJECT
Q_OBJECT
typedef QCoreApplication app_t; typedef QCoreApplication app_t;
public: public:
/** /**
* @brief Mode of operation of SingleCoreApplication. * @brief Mode of operation of SingleCoreApplication.
* Whether the block should be user-wide or system-wide and whether the * Whether the block should be user-wide or system-wide and whether the
* primary instance should be notified when a secondary instance had been * primary instance should be notified when a secondary instance had been
* started. * started.
* @note Operating system can restrict the shared memory blocks to the same * @note Operating system can restrict the shared memory blocks to the same
* user, in which case the User/System modes will have no effect and the * user, in which case the User/System modes will have no effect and the
* block will be user wide. * block will be user wide.
* @enum * @enum
*/ */
enum Mode { enum Mode {
User = 1 << 0, User = 1 << 0,
System = 1 << 1, System = 1 << 1,
SecondaryNotification = 1 << 2, SecondaryNotification = 1 << 2,
ExcludeAppVersion = 1 << 3, ExcludeAppVersion = 1 << 3,
ExcludeAppPath = 1 << 4 ExcludeAppPath = 1 << 4
}; };
Q_DECLARE_FLAGS(Options, Mode) Q_DECLARE_FLAGS(Options, Mode)
/** /**
* @brief Intitializes a SingleCoreApplication instance with argc command line * @brief Intitializes a SingleCoreApplication instance with argc command line
* arguments in argv * arguments in argv
* @arg {int &} argc - Number of arguments in argv * @arg {int &} argc - Number of arguments in argv
* @arg {const char *[]} argv - Supplied command line arguments * @arg {const char *[]} argv - Supplied command line arguments
* @arg {bool} allowSecondary - Whether to start the instance as secondary * @arg {bool} allowSecondary - Whether to start the instance as secondary
* if there is already a primary instance. * if there is already a primary instance.
* @arg {Mode} mode - Whether for the SingleCoreApplication block to be applied * @arg {Mode} mode - Whether for the SingleCoreApplication block to be applied
* User wide or System wide. * User wide or System wide.
* @arg {int} timeout - Timeout to wait in milliseconds. * @arg {int} timeout - Timeout to wait in milliseconds.
* @note argc and argv may be changed as Qt removes arguments that it * @note argc and argv may be changed as Qt removes arguments that it
* recognizes * recognizes
* @note Mode::SecondaryNotification only works if set on both the primary * @note Mode::SecondaryNotification only works if set on both the primary
* instance and the secondary instance. * instance and the secondary instance.
* @note The timeout is just a hint for the maximum time of blocking * @note The timeout is just a hint for the maximum time of blocking
* operations. It does not guarantee that the SingleCoreApplication * operations. It does not guarantee that the SingleCoreApplication
* initialisation will be completed in given time, though is a good hint. * initialisation will be completed in given time, though is a good hint.
* Usually 4*timeout would be the worst case (fail) scenario. * Usually 4*timeout would be the worst case (fail) scenario.
* @see See the corresponding QAPPLICATION_CLASS constructor for reference * @see See the corresponding QAPPLICATION_CLASS constructor for reference
*/ */
explicit SingleCoreApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 ); explicit SingleCoreApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 );
~SingleCoreApplication(); ~SingleCoreApplication();
/** /**
* @brief Returns if the instance is the primary instance * @brief Returns if the instance is the primary instance
* @returns {bool} * @returns {bool}
*/ */
bool isPrimary(); bool isPrimary();
/** /**
* @brief Returns if the instance is a secondary instance * @brief Returns if the instance is a secondary instance
* @returns {bool} * @returns {bool}
*/ */
bool isSecondary(); bool isSecondary();
/** /**
* @brief Returns a unique identifier for the current instance * @brief Returns a unique identifier for the current instance
* @returns {qint32} * @returns {qint32}
*/ */
quint32 instanceId(); quint32 instanceId();
/** /**
* @brief Returns the process ID (PID) of the primary instance * @brief Returns the process ID (PID) of the primary instance
* @returns {qint64} * @returns {qint64}
*/ */
qint64 primaryPid(); qint64 primaryPid();
/** /**
* @brief Sends a message to the primary instance. Returns true on success. * @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting * @param {int} timeout - Timeout for connecting
* @returns {bool} * @returns {bool}
* @note sendMessage() will return false if invoked from the primary * @note sendMessage() will return false if invoked from the primary
* instance. * instance.
*/ */
bool sendMessage( QByteArray message, int timeout = 1000 ); bool sendMessage( QByteArray message, int timeout = 1000 );
Q_SIGNALS: signals:
void instanceStarted(); void instanceStarted();
void receivedMessage( quint32 instanceId, QByteArray message ); void receivedMessage( quint32 instanceId, QByteArray message );
private: private:
SingleCoreApplicationPrivate *d_ptr; SingleCoreApplicationPrivate *d_ptr;
Q_DECLARE_PRIVATE(SingleCoreApplication) Q_DECLARE_PRIVATE(SingleCoreApplication)
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleCoreApplication::Options) Q_DECLARE_OPERATORS_FOR_FLAGS(SingleCoreApplication::Options)

View File

@@ -24,370 +24,386 @@
// W A R N I N G !!! // W A R N I N G !!!
// ----------------- // -----------------
// //
// This file is not part of the SingleCoreApplication API. It is used purely as an // This is a modified version of SingleApplication,
// implementation detail. This header file may change from version to // The original version is at:
// version without notice, or may even be removed.
// //
// https://github.com/itay-grudev/SingleApplication
//
//
#include "config.h"
#include <QtGlobal>
#include <cstdlib> #include <cstdlib>
#include <cstddef> #include <cstddef>
#include <QtGlobal> #ifdef Q_OS_UNIX
#include <QtCore/QDir> # include <unistd.h>
#include <QtCore/QByteArray> # include <sys/types.h>
#include <QtCore/QSemaphore> # include <pwd.h>
#include <QtCore/QDataStream> #endif
#include <QtCore/QStandardPaths>
#include <QtCore/QCryptographicHash> #include <QDir>
#include <QtNetwork/QLocalServer> #include <QByteArray>
#include <QtNetwork/QLocalSocket> #include <QDataStream>
#include <QCryptographicHash>
#include <QLocalServer>
#include <QLocalSocket>
#include "singlecoreapplication.h" #include "singlecoreapplication.h"
#include "singlecoreapplication_p.h" #include "singlecoreapplication_p.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> # include <windows.h>
#include <lmcons.h> # include <lmcons.h>
#endif #endif
SingleCoreApplicationPrivate::SingleCoreApplicationPrivate( SingleCoreApplication *q_ptr ) SingleCoreApplicationPrivate::SingleCoreApplicationPrivate(SingleCoreApplication *q_ptr)
: q_ptr( q_ptr ) : q_ptr(q_ptr) {
{
server = nullptr; server = nullptr;
socket = nullptr; socket = nullptr;
memory = nullptr; memory = nullptr;
instanceNumber = -1; instanceNumber = -1;
} }
SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() {
{
if( socket != nullptr ) {
socket->close();
delete socket;
}
memory->lock(); if (socket != nullptr) {
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data()); socket->close();
if( server != nullptr ) { delete socket;
server->close(); }
delete server;
inst->primary = false;
inst->primaryPid = -1;
inst->checksum = blockChecksum();
}
memory->unlock();
delete memory; memory->lock();
} InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
if (server != nullptr) {
void SingleCoreApplicationPrivate::genBlockServerName() server->close();
{ delete server;
QCryptographicHash appData( QCryptographicHash::Sha256 );
appData.addData( "SingleApplication", 17 );
appData.addData( SingleCoreApplication::app_t::applicationName().toUtf8() );
appData.addData( SingleCoreApplication::app_t::organizationName().toUtf8() );
appData.addData( SingleCoreApplication::app_t::organizationDomain().toUtf8() );
if( ! (options & SingleCoreApplication::Mode::ExcludeAppVersion) ) {
appData.addData( SingleCoreApplication::app_t::applicationVersion().toUtf8() );
}
if( ! (options & SingleCoreApplication::Mode::ExcludeAppPath) ) {
#ifdef Q_OS_WIN
appData.addData( SingleCoreApplication::app_t::applicationFilePath().toLower().toUtf8() );
#else
appData.addData( SingleCoreApplication::app_t::applicationFilePath().toUtf8() );
#endif
}
// User level block requires a user specific data in the hash
if( options & SingleCoreApplication::Mode::User ) {
#ifdef Q_OS_WIN
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
appData.addData(qgetenv("USER"));
#endif
}
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
// server naming requirements.
blockServerName = appData.result().toBase64().replace("/", "_");
}
void SingleCoreApplicationPrivate::initializeMemoryBlock()
{
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
inst->primary = false; inst->primary = false;
inst->secondary = 0;
inst->primaryPid = -1; inst->primaryPid = -1;
inst->checksum = blockChecksum(); inst->checksum = blockChecksum();
}
memory->unlock();
delete memory;
} }
void SingleCoreApplicationPrivate::startPrimary() void SingleCoreApplicationPrivate::genBlockServerName() {
{
Q_Q(SingleCoreApplication);
// Successful creation means that no main process exists QCryptographicHash appData(QCryptographicHash::Sha256);
// So we start a QLocalServer to listen for connections appData.addData("SingleApplication", 17);
QLocalServer::removeServer( blockServerName ); appData.addData(SingleCoreApplication::app_t::applicationName().toUtf8());
server = new QLocalServer(); appData.addData(SingleCoreApplication::app_t::organizationName().toUtf8());
appData.addData(SingleCoreApplication::app_t::organizationDomain().toUtf8());
// Restrict access to the socket according to the if (!(options & SingleCoreApplication::Mode::ExcludeAppVersion)) {
// SingleCoreApplication::Mode::User flag on User level or no restrictions appData.addData(SingleCoreApplication::app_t::applicationVersion().toUtf8());
if( options & SingleCoreApplication::Mode::User ) { }
server->setSocketOptions( QLocalServer::UserAccessOption );
} else { if (!(options & SingleCoreApplication::Mode::ExcludeAppPath)) {
server->setSocketOptions( QLocalServer::WorldAccessOption ); #ifdef Q_OS_WIN
appData.addData(SingleCoreApplication::app_t::applicationFilePath().toLower().toUtf8());
#else
appData.addData(SingleCoreApplication::app_t::applicationFilePath().toUtf8());
#endif
}
// User level block requires a user specific data in the hash
if (options & SingleCoreApplication::Mode::User) {
#ifdef Q_OS_UNIX
QByteArray username;
#if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID)
uid_t uid = geteuid();
if (uid != -1) {
struct passwd *pw = getpwuid(uid);
if (pw) {
username = pw->pw_name;
}
} }
qDebug() << username;
#endif
if (username.isEmpty()) username = qgetenv("USER");
appData.addData(username);
#endif
#ifdef Q_OS_WIN
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());
}
#endif
}
server->listen( blockServerName ); // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements.
QObject::connect( blockServerName = appData.result().toBase64().replace("/", "_");
server,
&QLocalServer::newConnection,
this,
&SingleCoreApplicationPrivate::slotConnectionEstablished
);
// Reset the number of connections
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
inst->primary = true;
inst->primaryPid = q->applicationPid();
inst->checksum = blockChecksum();
instanceNumber = 0;
} }
void SingleCoreApplicationPrivate::startSecondary() void SingleCoreApplicationPrivate::initializeMemoryBlock() {
{
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
inst->primary = false;
inst->secondary = 0;
inst->primaryPid = -1;
inst->checksum = blockChecksum();
} }
void SingleCoreApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType ) void SingleCoreApplicationPrivate::startPrimary() {
{
// 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; Q_Q(SingleCoreApplication);
if( socket->state() == QLocalSocket::ConnectedState )
return;
// If not connect // Successful creation means that no main process exists
if( socket->state() == QLocalSocket::UnconnectedState || // So we start a QLocalServer to listen for connections
socket->state() == QLocalSocket::ClosingState ) { QLocalServer::removeServer(blockServerName);
socket->connectToServer( blockServerName ); server = new QLocalServer();
}
// Wait for being connected // Restrict access to the socket according to the
if( socket->state() == QLocalSocket::ConnectingState ) { // SingleCoreApplication::Mode::User flag on User level or no restrictions
socket->waitForConnected( msecs ); if (options & SingleCoreApplication::Mode::User) {
} server->setSocketOptions(QLocalServer::UserAccessOption);
}
else {
server->setSocketOptions(QLocalServer::WorldAccessOption);
}
// Initialisation message according to the SingleCoreApplication protocol server->listen(blockServerName);
if( socket->state() == QLocalSocket::ConnectedState ) { QObject::connect(server, &QLocalServer::newConnection, this, &SingleCoreApplicationPrivate::slotConnectionEstablished);
// Notify the parent that a new instance had been started;
QByteArray initMsg; // Reset the number of connections
QDataStream writeStream(&initMsg, QIODevice::WriteOnly); InstancesInfo* inst = static_cast <InstancesInfo*>(memory->data());
inst->primary = true;
inst->primaryPid = q->applicationPid();
inst->checksum = blockChecksum();
instanceNumber = 0;
}
void SingleCoreApplicationPrivate::startSecondary() {}
void SingleCoreApplicationPrivate::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 SingleCoreApplication protocol
if (socket->state() == QLocalSocket::ConnectedState) {
// Notify the parent that a new instance had been started;
QByteArray initMsg;
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
writeStream.setVersion(QDataStream::Qt_5_6); writeStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
writeStream << blockServerName.toLatin1(); writeStream << blockServerName.toLatin1();
writeStream << static_cast<quint8>(connectionType); writeStream << static_cast<quint8>(connectionType);
writeStream << instanceNumber; writeStream << instanceNumber;
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length())); quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
writeStream << checksum; writeStream << checksum;
// The header indicates the message length that follows // The header indicates the message length that follows
QByteArray header; QByteArray header;
QDataStream headerStream(&header, QIODevice::WriteOnly); QDataStream headerStream(&header, QIODevice::WriteOnly);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
headerStream.setVersion(QDataStream::Qt_5_6); headerStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
headerStream << static_cast <quint64>( initMsg.length() ); headerStream << static_cast <quint64>(initMsg.length());
socket->write(header);
socket->write(initMsg);
socket->flush();
socket->waitForBytesWritten(msecs);
}
socket->write( header );
socket->write( initMsg );
socket->flush();
socket->waitForBytesWritten( msecs );
}
} }
quint16 SingleCoreApplicationPrivate::blockChecksum() quint16 SingleCoreApplicationPrivate::blockChecksum() {
{
return qChecksum( return qChecksum(static_cast <const char*> (memory->data()), offsetof(InstancesInfo, checksum));
static_cast <const char *>( memory->data() ),
offsetof( InstancesInfo, checksum )
);
} }
qint64 SingleCoreApplicationPrivate::primaryPid() qint64 SingleCoreApplicationPrivate::primaryPid() {
{
qint64 pid;
memory->lock(); qint64 pid;
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
pid = inst->primaryPid; memory->lock();
memory->unlock(); InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
pid = inst->primaryPid;
memory->unlock();
return pid;
return pid;
} }
/** /**
* @brief Executed when a connection has been made to the LocalServer * @brief Executed when a connection has been made to the LocalServer
*/ */
void SingleCoreApplicationPrivate::slotConnectionEstablished() void SingleCoreApplicationPrivate::slotConnectionEstablished() {
{
QLocalSocket *nextConnSocket = server->nextPendingConnection();
connectionMap.insert(nextConnSocket, ConnectionInfo());
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose, QLocalSocket *nextConnSocket = server->nextPendingConnection();
[nextConnSocket, this]() { connectionMap.insert(nextConnSocket, ConnectionInfo());
auto &info = connectionMap[nextConnSocket];
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
}
);
QObject::connect(nextConnSocket, &QLocalSocket::disconnected, QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
[nextConnSocket, this](){ [nextConnSocket, this]() {
connectionMap.remove(nextConnSocket); auto &info = connectionMap[nextConnSocket];
nextConnSocket->deleteLater(); Q_EMIT this->slotClientConnectionClosed(nextConnSocket, info.instanceId);
} }
); );
QObject::connect(nextConnSocket, &QLocalSocket::disconnected,
[nextConnSocket, this](){
connectionMap.remove(nextConnSocket);
nextConnSocket->deleteLater();
}
);
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
[nextConnSocket, this]() {
auto &info = connectionMap[nextConnSocket];
switch(info.stage) {
case StageHeader:
readInitMessageHeader(nextConnSocket);
break;
case StageBody:
readInitMessageBody(nextConnSocket);
break;
case StageConnected:
Q_EMIT this->slotDataAvailable(nextConnSocket, info.instanceId);
break;
default:
break;
};
}
);
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
[nextConnSocket, this]() {
auto &info = connectionMap[nextConnSocket];
switch(info.stage) {
case StageHeader:
readInitMessageHeader(nextConnSocket);
break;
case StageBody:
readInitMessageBody(nextConnSocket);
break;
case StageConnected:
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
break;
default:
break;
};
}
);
} }
void SingleCoreApplicationPrivate::readInitMessageHeader( QLocalSocket *sock ) void SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
{
if (!connectionMap.contains( sock )) {
return;
}
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) { if (!connectionMap.contains(sock)) {
return; return;
} }
QDataStream headerStream( sock ); if (sock->bytesAvailable() < (qint64)sizeof(quint64)) {
return;
}
QDataStream headerStream(sock);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
headerStream.setVersion( QDataStream::Qt_5_6 ); headerStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
// Read the header to know the message length // Read the header to know the message length
quint64 msgLen = 0; quint64 msgLen = 0;
headerStream >> msgLen; headerStream >> msgLen;
ConnectionInfo &info = connectionMap[sock]; ConnectionInfo &info = connectionMap[sock];
info.stage = StageBody; info.stage = StageBody;
info.msgLen = msgLen; info.msgLen = msgLen;
if (sock->bytesAvailable() >= (qint64) msgLen) {
readInitMessageBody(sock);
}
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
readInitMessageBody( sock );
}
} }
void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock ) void SingleCoreApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
{
Q_Q(SingleCoreApplication);
if (!connectionMap.contains( sock )) { Q_Q(SingleCoreApplication);
return;
}
ConnectionInfo &info = connectionMap[sock]; if (!connectionMap.contains(sock)) {
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) { return;
return; }
}
// Read the message body ConnectionInfo &info = connectionMap[sock];
QByteArray msgBytes = sock->read(info.msgLen); if (sock->bytesAvailable() < (qint64)info.msgLen) {
QDataStream readStream(msgBytes); return;
}
// Read the message body
QByteArray msgBytes = sock->read(info.msgLen);
QDataStream readStream(msgBytes);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
readStream.setVersion( QDataStream::Qt_5_6 ); readStream.setVersion(QDataStream::Qt_5_6);
#endif #endif
// server name // server name
QByteArray latin1Name; QByteArray latin1Name;
readStream >> latin1Name; readStream >> latin1Name;
// connection type // connection type
ConnectionType connectionType = InvalidConnection; ConnectionType connectionType = InvalidConnection;
quint8 connTypeVal = InvalidConnection; quint8 connTypeVal = InvalidConnection;
readStream >> connTypeVal; readStream >> connTypeVal;
connectionType = static_cast <ConnectionType>( connTypeVal ); connectionType = static_cast <ConnectionType>(connTypeVal);
// instance id // instance id
quint32 instanceId = 0; quint32 instanceId = 0;
readStream >> instanceId; readStream >> instanceId;
// checksum // checksum
quint16 msgChecksum = 0; quint16 msgChecksum = 0;
readStream >> msgChecksum; readStream >> msgChecksum;
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) ); const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16)));
bool isValid = readStream.status() == QDataStream::Ok && bool isValid = readStream.status() == QDataStream::Ok && QLatin1String(latin1Name) == blockServerName && msgChecksum == actualChecksum;
QLatin1String(latin1Name) == blockServerName &&
msgChecksum == actualChecksum;
if( !isValid ) { if (!isValid) {
sock->close(); sock->close();
return; return;
} }
info.instanceId = instanceId; info.instanceId = instanceId;
info.stage = StageConnected; info.stage = StageConnected;
if( connectionType == NewInstance || if (connectionType == NewInstance || (connectionType == SecondaryInstance && options & SingleCoreApplication::Mode::SecondaryNotification)) {
( connectionType == SecondaryInstance && Q_EMIT q->instanceStarted();
options & SingleCoreApplication::Mode::SecondaryNotification ) ) }
{
Q_EMIT q->instanceStarted(); if (sock->bytesAvailable() > 0) {
} Q_EMIT this->slotDataAvailable(sock, instanceId);
}
if (sock->bytesAvailable() > 0) {
Q_EMIT this->slotDataAvailable( sock, instanceId );
}
} }
void SingleCoreApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId ) void SingleCoreApplicationPrivate::slotDataAvailable(QLocalSocket *dataSocket, quint32 instanceId) {
{
Q_Q(SingleCoreApplication); Q_Q(SingleCoreApplication);
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() ); Q_EMIT q->receivedMessage(instanceId, dataSocket->readAll());
} }
void SingleCoreApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId ) void SingleCoreApplicationPrivate::slotClientConnectionClosed(QLocalSocket *closedSocket, quint32 instanceId) {
{
if( closedSocket->bytesAvailable() > 0 ) if (closedSocket->bytesAvailable() > 0)
Q_EMIT slotDataAvailable( closedSocket, instanceId ); Q_EMIT slotDataAvailable(closedSocket, instanceId);
} }

View File

@@ -24,76 +24,80 @@
// W A R N I N G !!! // W A R N I N G !!!
// ----------------- // -----------------
// //
// This file is not part of the SingleCoreApplication API. It is used purely as an // This is a modified version of SingleApplication,
// implementation detail. This header file may change from version to // The original version is at:
// version without notice, or may even be removed. //
// https://github.com/itay-grudev/SingleApplication
//
// //
#ifndef SINGLECOREAPPLICATION_P_H #ifndef SINGLECOREAPPLICATION_P_H
#define SINGLECOREAPPLICATION_P_H #define SINGLECOREAPPLICATION_P_H
#include <QtCore/QSharedMemory> #include <QtGlobal>
#include <QtNetwork/QLocalServer> #include <QLocalSocket>
#include <QtNetwork/QLocalSocket> #include <QLocalServer>
#include <QSharedMemory>
#include <QMap>
#include "singlecoreapplication.h" #include "singlecoreapplication.h"
struct InstancesInfo { struct InstancesInfo {
bool primary; bool primary;
quint32 secondary; quint32 secondary;
qint64 primaryPid; qint64 primaryPid;
quint16 checksum; quint16 checksum;
}; };
struct ConnectionInfo { struct ConnectionInfo {
explicit ConnectionInfo() : explicit ConnectionInfo() : msgLen(0), instanceId(0), stage(0) {}
msgLen(0), instanceId(0), stage(0) {} qint64 msgLen;
qint64 msgLen; quint32 instanceId;
quint32 instanceId; quint8 stage;
quint8 stage;
}; };
class SingleCoreApplicationPrivate : public QObject { class SingleCoreApplicationPrivate : public QObject {
Q_OBJECT Q_OBJECT
public: public:
enum ConnectionType : quint8 { enum ConnectionType : quint8 {
InvalidConnection = 0, InvalidConnection = 0,
NewInstance = 1, NewInstance = 1,
SecondaryInstance = 2, SecondaryInstance = 2,
Reconnect = 3 Reconnect = 3
}; };
enum ConnectionStage : quint8 { enum ConnectionStage : quint8 {
StageHeader = 0, StageHeader = 0,
StageBody = 1, StageBody = 1,
StageConnected = 2, StageConnected = 2,
}; };
Q_DECLARE_PUBLIC(SingleCoreApplication) Q_DECLARE_PUBLIC(SingleCoreApplication)
SingleCoreApplicationPrivate( SingleCoreApplication *q_ptr ); SingleCoreApplicationPrivate( SingleCoreApplication *q_ptr );
~SingleCoreApplicationPrivate(); ~SingleCoreApplicationPrivate();
void genBlockServerName(); void genBlockServerName();
void initializeMemoryBlock(); void initializeMemoryBlock();
void startPrimary(); void startPrimary();
void startSecondary(); void startSecondary();
void connectToPrimary(int msecs, ConnectionType connectionType ); void connectToPrimary(int msecs, ConnectionType connectionType );
quint16 blockChecksum(); quint16 blockChecksum();
qint64 primaryPid(); qint64 primaryPid();
void readInitMessageHeader(QLocalSocket *socket); void readInitMessageHeader(QLocalSocket *socket);
void readInitMessageBody(QLocalSocket *socket); void readInitMessageBody(QLocalSocket *socket);
SingleCoreApplication *q_ptr; SingleCoreApplication *q_ptr;
QSharedMemory *memory; QSharedMemory *memory;
QLocalSocket *socket; QLocalSocket *socket;
QLocalServer *server; QLocalServer *server;
quint32 instanceNumber; quint32 instanceNumber;
QString blockServerName; QString blockServerName;
SingleCoreApplication::Options options; SingleCoreApplication::Options options;
QMap<QLocalSocket*, ConnectionInfo> connectionMap; QMap<QLocalSocket*, ConnectionInfo> connectionMap;
public Q_SLOTS: public slots:
void slotConnectionEstablished(); void slotConnectionEstablished();
void slotDataAvailable( QLocalSocket*, quint32 ); void slotDataAvailable(QLocalSocket*, quint32);
void slotClientConnectionClosed( QLocalSocket*, quint32 ); void slotClientConnectionClosed(QLocalSocket*, quint32);
}; };
#endif // SINGLECOREAPPLICATION_P_H #endif // SINGLECOREAPPLICATION_P_H