SingleApplication: Use geteuid / getpwuid to get username and reformat
sources
This commit is contained in:
11
3rdparty/singleapplication/CMakeLists.txt
vendored
11
3rdparty/singleapplication/CMakeLists.txt
vendored
@@ -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})
|
||||||
|
|||||||
2
3rdparty/singleapplication/config.h.in
vendored
Normal file
2
3rdparty/singleapplication/config.h.in
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#cmakedefine HAVE_GETEUID
|
||||||
|
#cmakedefine HAVE_GETPWUID
|
||||||
103
3rdparty/singleapplication/singleapplication.cpp
vendored
103
3rdparty/singleapplication/singleapplication.cpp
vendored
@@ -20,72 +20,84 @@
|
|||||||
// 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);
|
Q_D(SingleApplication);
|
||||||
|
|
||||||
// Store the current mode of the program
|
// Store the current mode of the program
|
||||||
d->options = options;
|
d->options = options;
|
||||||
|
|
||||||
// Generating an application ID used for identifying the shared memory
|
// Generating an application ID used for identifying the shared memory block and QLocalServer
|
||||||
// block and QLocalServer
|
|
||||||
d->genBlockServerName();
|
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 {
|
}
|
||||||
|
else {
|
||||||
// Attempt to attach to the memory segment
|
// Attempt to attach to the memory segment
|
||||||
if( ! d->memory->attach() ) {
|
if (! d->memory->attach()) {
|
||||||
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
|
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
|
||||||
qCritical() << d->memory->errorString();
|
qCritical() << d->memory->errorString();
|
||||||
delete d;
|
delete d;
|
||||||
::exit( EXIT_FAILURE );
|
::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();
|
||||||
}
|
}
|
||||||
@@ -93,24 +105,24 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
// Random sleep here limits the probability of a collision between two racing apps
|
// Random sleep here limits the probability of a collision between two racing apps
|
||||||
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
|
||||||
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
|
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
if( inst->primary == false) {
|
if (inst->primary == false) {
|
||||||
d->startPrimary();
|
d->startPrimary();
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if another instance can be started
|
// Check if another instance can be started
|
||||||
if( allowSecondary ) {
|
if (allowSecondary) {
|
||||||
inst->secondary += 1;
|
inst->secondary += 1;
|
||||||
inst->checksum = d->blockChecksum();
|
inst->checksum = d->blockChecksum();
|
||||||
d->instanceNumber = inst->secondary;
|
d->instanceNumber = inst->secondary;
|
||||||
d->startSecondary();
|
d->startSecondary();
|
||||||
if( d->options & Mode::SecondaryNotification ) {
|
if (d->options & Mode::SecondaryNotification) {
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance );
|
d->connectToPrimary(timeout, SingleApplicationPrivate::SecondaryInstance);
|
||||||
}
|
}
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
@@ -118,58 +130,55 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
|
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance );
|
d->connectToPrimary(timeout, SingleApplicationPrivate::NewInstance);
|
||||||
|
|
||||||
delete d;
|
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);
|
Q_D(SingleApplication);
|
||||||
|
|
||||||
// Nobody to connect to
|
// Nobody to connect to
|
||||||
if( isPrimary() ) return false;
|
if (isPrimary()) return false;
|
||||||
|
|
||||||
// Make sure the socket is connected
|
// Make sure the socket is connected
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect );
|
d->connectToPrimary(timeout, SingleApplicationPrivate::Reconnect);
|
||||||
|
|
||||||
d->socket->write( message );
|
d->socket->write(message);
|
||||||
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
bool dataWritten = d->socket->waitForBytesWritten(timeout);
|
||||||
d->socket->flush();
|
d->socket->flush();
|
||||||
return dataWritten;
|
return dataWritten;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
3rdparty/singleapplication/singleapplication.h
vendored
23
3rdparty/singleapplication/singleapplication.h
vendored
@@ -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,8 +45,7 @@ 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;
|
||||||
@@ -116,13 +126,14 @@ public:
|
|||||||
*/
|
*/
|
||||||
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:
|
||||||
SingleApplicationPrivate *d_ptr;
|
SingleApplicationPrivate *d_ptr;
|
||||||
Q_DECLARE_PRIVATE(SingleApplication)
|
Q_DECLARE_PRIVATE(SingleApplication)
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
|
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
|
||||||
|
|||||||
264
3rdparty/singleapplication/singleapplication_p.cpp
vendored
264
3rdparty/singleapplication/singleapplication_p.cpp
vendored
@@ -24,51 +24,61 @@
|
|||||||
// 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 ) {
|
if (socket != nullptr) {
|
||||||
socket->close();
|
socket->close();
|
||||||
delete socket;
|
delete socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
if( server != nullptr ) {
|
if (server != nullptr) {
|
||||||
server->close();
|
server->close();
|
||||||
delete server;
|
delete server;
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
@@ -78,123 +88,129 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
|
|||||||
memory->unlock();
|
memory->unlock();
|
||||||
|
|
||||||
delete memory;
|
delete memory;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::genBlockServerName()
|
void SingleApplicationPrivate::genBlockServerName() {
|
||||||
{
|
|
||||||
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) ) {
|
QCryptographicHash appData(QCryptographicHash::Sha256);
|
||||||
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
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) ) {
|
if (! (options & SingleApplication::Mode::ExcludeAppPath)) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
appData.addData(SingleApplication::app_t::applicationFilePath().toLower().toUtf8());
|
||||||
#else
|
#else
|
||||||
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
appData.addData(SingleApplication::app_t::applicationFilePath().toUtf8());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// User level block requires a user specific data in the hash
|
// User level block requires a user specific data in the hash
|
||||||
if( options & SingleApplication::Mode::User ) {
|
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
|
#ifdef Q_OS_WIN
|
||||||
wchar_t username [ UNLEN + 1 ];
|
wchar_t username [ UNLEN + 1 ];
|
||||||
// Specifies size of the buffer on input
|
// Specifies size of the buffer on input
|
||||||
DWORD usernameLength = UNLEN + 1;
|
DWORD usernameLength = UNLEN + 1;
|
||||||
if( GetUserNameW( username, &usernameLength ) ) {
|
if (GetUserNameW(username, &usernameLength)) {
|
||||||
appData.addData( QString::fromWCharArray(username).toUtf8() );
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements.
|
||||||
// server naming requirements.
|
|
||||||
blockServerName = appData.result().toBase64().replace("/", "_");
|
blockServerName = appData.result().toBase64().replace("/", "_");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::initializeMemoryBlock()
|
void SingleApplicationPrivate::initializeMemoryBlock() {
|
||||||
{
|
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
inst->secondary = 0;
|
inst->secondary = 0;
|
||||||
inst->primaryPid = -1;
|
inst->primaryPid = -1;
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::startPrimary()
|
void SingleApplicationPrivate::startPrimary() {
|
||||||
{
|
|
||||||
Q_Q(SingleApplication);
|
Q_Q(SingleApplication);
|
||||||
|
|
||||||
// Successful creation means that no main process exists
|
// Successful creation means that no main process exists
|
||||||
// So we start a QLocalServer to listen for connections
|
// So we start a QLocalServer to listen for connections
|
||||||
QLocalServer::removeServer( blockServerName );
|
QLocalServer::removeServer(blockServerName);
|
||||||
server = new QLocalServer();
|
server = new QLocalServer();
|
||||||
|
|
||||||
// Restrict access to the socket according to the
|
// Restrict access to the socket according to the
|
||||||
// SingleApplication::Mode::User flag on User level or no restrictions
|
// SingleApplication::Mode::User flag on User level or no restrictions
|
||||||
if( options & SingleApplication::Mode::User ) {
|
if (options & SingleApplication::Mode::User) {
|
||||||
server->setSocketOptions( QLocalServer::UserAccessOption );
|
server->setSocketOptions(QLocalServer::UserAccessOption);
|
||||||
} else {
|
}
|
||||||
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
else {
|
||||||
|
server->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
server->listen( blockServerName );
|
server->listen(blockServerName);
|
||||||
QObject::connect(
|
QObject::connect(server, &QLocalServer::newConnection, this, &SingleApplicationPrivate::slotConnectionEstablished);
|
||||||
server,
|
|
||||||
&QLocalServer::newConnection,
|
|
||||||
this,
|
|
||||||
&SingleApplicationPrivate::slotConnectionEstablished
|
|
||||||
);
|
|
||||||
|
|
||||||
// Reset the number of connections
|
// Reset the number of connections
|
||||||
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast <InstancesInfo*>(memory->data());
|
||||||
|
|
||||||
inst->primary = true;
|
inst->primary = true;
|
||||||
inst->primaryPid = q->applicationPid();
|
inst->primaryPid = q->applicationPid();
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
|
|
||||||
instanceNumber = 0;
|
instanceNumber = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::startSecondary()
|
void SingleApplicationPrivate::startSecondary() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
void SingleApplicationPrivate::connectToPrimary(int msecs, ConnectionType connectionType) {
|
||||||
{
|
|
||||||
// Connect to the Local Server of the Primary Instance if not already
|
// Connect to the Local Server of the Primary Instance if not already connected.
|
||||||
// connected.
|
if (socket == nullptr) {
|
||||||
if( socket == nullptr ) {
|
|
||||||
socket = new QLocalSocket();
|
socket = new QLocalSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already connected - we are done;
|
// If already connected - we are done;
|
||||||
if( socket->state() == QLocalSocket::ConnectedState )
|
if (socket->state() == QLocalSocket::ConnectedState)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// If not connect
|
// If not connect
|
||||||
if( socket->state() == QLocalSocket::UnconnectedState ||
|
if (socket->state() == QLocalSocket::UnconnectedState ||
|
||||||
socket->state() == QLocalSocket::ClosingState ) {
|
socket->state() == QLocalSocket::ClosingState) {
|
||||||
socket->connectToServer( blockServerName );
|
socket->connectToServer(blockServerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for being connected
|
// Wait for being connected
|
||||||
if( socket->state() == QLocalSocket::ConnectingState ) {
|
if (socket->state() == QLocalSocket::ConnectingState) {
|
||||||
socket->waitForConnected( msecs );
|
socket->waitForConnected(msecs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisation message according to the SingleApplication protocol
|
// Initialisation message according to the SingleApplication protocol
|
||||||
if( socket->state() == QLocalSocket::ConnectedState ) {
|
if (socket->state() == QLocalSocket::ConnectedState) {
|
||||||
// Notify the parent that a new instance had been started;
|
// Notify the parent that a new instance had been started;
|
||||||
QByteArray initMsg;
|
QByteArray initMsg;
|
||||||
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
||||||
@@ -216,47 +232,47 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
|
|||||||
#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(header);
|
||||||
socket->write( initMsg );
|
socket->write(initMsg);
|
||||||
socket->flush();
|
socket->flush();
|
||||||
socket->waitForBytesWritten( msecs );
|
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;
|
qint64 pid;
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
pid = inst->primaryPid;
|
pid = inst->primaryPid;
|
||||||
memory->unlock();
|
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();
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
||||||
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
||||||
|
|
||||||
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
||||||
[nextConnSocket, this]() {
|
[nextConnSocket, this]() {
|
||||||
auto &info = connectionMap[nextConnSocket];
|
auto &info = connectionMap[nextConnSocket];
|
||||||
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotClientConnectionClosed(nextConnSocket, info.instanceId);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -278,29 +294,30 @@ void SingleApplicationPrivate::slotConnectionEstablished()
|
|||||||
readInitMessageBody(nextConnSocket);
|
readInitMessageBody(nextConnSocket);
|
||||||
break;
|
break;
|
||||||
case StageConnected:
|
case StageConnected:
|
||||||
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotDataAvailable(nextConnSocket, info.instanceId);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
void SingleApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
|
||||||
{
|
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains(sock)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) {
|
if (sock->bytesAvailable() < (qint64) sizeof(quint64)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream headerStream( sock );
|
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
|
||||||
@@ -310,21 +327,22 @@ void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
|||||||
info.stage = StageBody;
|
info.stage = StageBody;
|
||||||
info.msgLen = msgLen;
|
info.msgLen = msgLen;
|
||||||
|
|
||||||
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
|
if (sock->bytesAvailable() >= (qint64) msgLen) {
|
||||||
readInitMessageBody( sock );
|
readInitMessageBody(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
void SingleApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
|
||||||
{
|
|
||||||
Q_Q(SingleApplication);
|
Q_Q(SingleApplication);
|
||||||
|
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains(sock)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionInfo &info = connectionMap[sock];
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) {
|
if (sock->bytesAvailable() < (qint64)info.msgLen) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,7 +351,7 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
QDataStream readStream(msgBytes);
|
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
|
||||||
@@ -344,7 +362,7 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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;
|
||||||
@@ -354,13 +372,11 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -368,26 +384,26 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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 &&
|
|
||||||
options & SingleApplication::Mode::SecondaryNotification ) )
|
|
||||||
{
|
|
||||||
Q_EMIT q->instanceStarted();
|
Q_EMIT q->instanceStarted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock->bytesAvailable() > 0) {
|
if (sock->bytesAvailable() > 0) {
|
||||||
Q_EMIT this->slotDataAvailable( sock, instanceId );
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
30
3rdparty/singleapplication/singleapplication_p.h
vendored
30
3rdparty/singleapplication/singleapplication_p.h
vendored
@@ -24,17 +24,22 @@
|
|||||||
// 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 {
|
||||||
@@ -45,16 +50,15 @@ struct InstancesInfo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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,
|
||||||
@@ -90,10 +94,10 @@ public:
|
|||||||
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
|
||||||
|
|||||||
@@ -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,9 +49,9 @@
|
|||||||
* @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);
|
Q_D(SingleCoreApplication);
|
||||||
|
|
||||||
// Store the current mode of the program
|
// Store the current mode of the program
|
||||||
@@ -52,40 +64,41 @@ SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allo
|
|||||||
#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 {
|
}
|
||||||
|
else {
|
||||||
// Attempt to attach to the memory segment
|
// Attempt to attach to the memory segment
|
||||||
if( ! d->memory->attach() ) {
|
if (!d->memory->attach()) {
|
||||||
qCritical() << "SingleCoreApplication: Unable to attach to shared memory block.";
|
qCritical() << "SingleCoreApplication: Unable to attach to shared memory block.";
|
||||||
qCritical() << d->memory->errorString();
|
qCritical() << d->memory->errorString();
|
||||||
delete d;
|
delete d;
|
||||||
::exit( EXIT_FAILURE );
|
::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();
|
||||||
}
|
}
|
||||||
@@ -93,24 +106,24 @@ SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allo
|
|||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
// Random sleep here limits the probability of a collision between two racing apps
|
// Random sleep here limits the probability of a collision between two racing apps
|
||||||
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
|
||||||
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
|
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
if( inst->primary == false) {
|
if (inst->primary == false) {
|
||||||
d->startPrimary();
|
d->startPrimary();
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if another instance can be started
|
// Check if another instance can be started
|
||||||
if( allowSecondary ) {
|
if (allowSecondary) {
|
||||||
inst->secondary += 1;
|
inst->secondary += 1;
|
||||||
inst->checksum = d->blockChecksum();
|
inst->checksum = d->blockChecksum();
|
||||||
d->instanceNumber = inst->secondary;
|
d->instanceNumber = inst->secondary;
|
||||||
d->startSecondary();
|
d->startSecondary();
|
||||||
if( d->options & Mode::SecondaryNotification ) {
|
if(d->options & Mode::SecondaryNotification) {
|
||||||
d->connectToPrimary( timeout, SingleCoreApplicationPrivate::SecondaryInstance );
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::SecondaryInstance);
|
||||||
}
|
}
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
@@ -118,58 +131,55 @@ SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allo
|
|||||||
|
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
d->connectToPrimary( timeout, SingleCoreApplicationPrivate::NewInstance );
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::NewInstance);
|
||||||
|
|
||||||
delete d;
|
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);
|
Q_D(SingleCoreApplication);
|
||||||
|
|
||||||
// Nobody to connect to
|
// Nobody to connect to
|
||||||
if( isPrimary() ) return false;
|
if(isPrimary()) return false;
|
||||||
|
|
||||||
// Make sure the socket is connected
|
// Make sure the socket is connected
|
||||||
d->connectToPrimary( timeout, SingleCoreApplicationPrivate::Reconnect );
|
d->connectToPrimary(timeout, SingleCoreApplicationPrivate::Reconnect);
|
||||||
|
|
||||||
d->socket->write( message );
|
d->socket->write(message);
|
||||||
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
bool dataWritten = d->socket->waitForBytesWritten(timeout);
|
||||||
d->socket->flush();
|
d->socket->flush();
|
||||||
return dataWritten;
|
return dataWritten;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,8 +45,7 @@ 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;
|
||||||
@@ -116,7 +126,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
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 );
|
||||||
|
|
||||||
|
|||||||
@@ -24,51 +24,61 @@
|
|||||||
// 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 ) {
|
if (socket != nullptr) {
|
||||||
socket->close();
|
socket->close();
|
||||||
delete socket;
|
delete socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
if( server != nullptr ) {
|
if (server != nullptr) {
|
||||||
server->close();
|
server->close();
|
||||||
delete server;
|
delete server;
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
@@ -78,123 +88,129 @@ SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate()
|
|||||||
memory->unlock();
|
memory->unlock();
|
||||||
|
|
||||||
delete memory;
|
delete memory;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::genBlockServerName()
|
void SingleCoreApplicationPrivate::genBlockServerName() {
|
||||||
{
|
|
||||||
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) ) {
|
QCryptographicHash appData(QCryptographicHash::Sha256);
|
||||||
appData.addData( SingleCoreApplication::app_t::applicationVersion().toUtf8() );
|
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) ) {
|
if (!(options & SingleCoreApplication::Mode::ExcludeAppPath)) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
appData.addData( SingleCoreApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
appData.addData(SingleCoreApplication::app_t::applicationFilePath().toLower().toUtf8());
|
||||||
#else
|
#else
|
||||||
appData.addData( SingleCoreApplication::app_t::applicationFilePath().toUtf8() );
|
appData.addData(SingleCoreApplication::app_t::applicationFilePath().toUtf8());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// User level block requires a user specific data in the hash
|
// User level block requires a user specific data in the hash
|
||||||
if( options & SingleCoreApplication::Mode::User ) {
|
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
|
#ifdef Q_OS_WIN
|
||||||
wchar_t username [ UNLEN + 1 ];
|
wchar_t username [ UNLEN + 1 ];
|
||||||
// Specifies size of the buffer on input
|
// Specifies size of the buffer on input
|
||||||
DWORD usernameLength = UNLEN + 1;
|
DWORD usernameLength = UNLEN + 1;
|
||||||
if( GetUserNameW( username, &usernameLength ) ) {
|
if (GetUserNameW(username, &usernameLength)) {
|
||||||
appData.addData( QString::fromWCharArray(username).toUtf8() );
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements.
|
||||||
// server naming requirements.
|
|
||||||
blockServerName = appData.result().toBase64().replace("/", "_");
|
blockServerName = appData.result().toBase64().replace("/", "_");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::initializeMemoryBlock()
|
void SingleCoreApplicationPrivate::initializeMemoryBlock() {
|
||||||
{
|
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
inst->secondary = 0;
|
inst->secondary = 0;
|
||||||
inst->primaryPid = -1;
|
inst->primaryPid = -1;
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::startPrimary()
|
void SingleCoreApplicationPrivate::startPrimary() {
|
||||||
{
|
|
||||||
Q_Q(SingleCoreApplication);
|
Q_Q(SingleCoreApplication);
|
||||||
|
|
||||||
// Successful creation means that no main process exists
|
// Successful creation means that no main process exists
|
||||||
// So we start a QLocalServer to listen for connections
|
// So we start a QLocalServer to listen for connections
|
||||||
QLocalServer::removeServer( blockServerName );
|
QLocalServer::removeServer(blockServerName);
|
||||||
server = new QLocalServer();
|
server = new QLocalServer();
|
||||||
|
|
||||||
// Restrict access to the socket according to the
|
// Restrict access to the socket according to the
|
||||||
// SingleCoreApplication::Mode::User flag on User level or no restrictions
|
// SingleCoreApplication::Mode::User flag on User level or no restrictions
|
||||||
if( options & SingleCoreApplication::Mode::User ) {
|
if (options & SingleCoreApplication::Mode::User) {
|
||||||
server->setSocketOptions( QLocalServer::UserAccessOption );
|
server->setSocketOptions(QLocalServer::UserAccessOption);
|
||||||
} else {
|
}
|
||||||
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
else {
|
||||||
|
server->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
server->listen( blockServerName );
|
server->listen(blockServerName);
|
||||||
QObject::connect(
|
QObject::connect(server, &QLocalServer::newConnection, this, &SingleCoreApplicationPrivate::slotConnectionEstablished);
|
||||||
server,
|
|
||||||
&QLocalServer::newConnection,
|
|
||||||
this,
|
|
||||||
&SingleCoreApplicationPrivate::slotConnectionEstablished
|
|
||||||
);
|
|
||||||
|
|
||||||
// Reset the number of connections
|
// Reset the number of connections
|
||||||
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast <InstancesInfo*>(memory->data());
|
||||||
|
|
||||||
inst->primary = true;
|
inst->primary = true;
|
||||||
inst->primaryPid = q->applicationPid();
|
inst->primaryPid = q->applicationPid();
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
|
|
||||||
instanceNumber = 0;
|
instanceNumber = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::startSecondary()
|
void SingleCoreApplicationPrivate::startSecondary() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
void SingleCoreApplicationPrivate::connectToPrimary(int msecs, ConnectionType connectionType) {
|
||||||
{
|
|
||||||
// Connect to the Local Server of the Primary Instance if not already
|
// Connect to the Local Server of the Primary Instance if not already connected.
|
||||||
// connected.
|
if (socket == nullptr) {
|
||||||
if( socket == nullptr ) {
|
|
||||||
socket = new QLocalSocket();
|
socket = new QLocalSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already connected - we are done;
|
// If already connected - we are done;
|
||||||
if( socket->state() == QLocalSocket::ConnectedState )
|
if (socket->state() == QLocalSocket::ConnectedState)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// If not connect
|
// If not connect
|
||||||
if( socket->state() == QLocalSocket::UnconnectedState ||
|
if (socket->state() == QLocalSocket::UnconnectedState ||
|
||||||
socket->state() == QLocalSocket::ClosingState ) {
|
socket->state() == QLocalSocket::ClosingState) {
|
||||||
socket->connectToServer( blockServerName );
|
socket->connectToServer(blockServerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for being connected
|
// Wait for being connected
|
||||||
if( socket->state() == QLocalSocket::ConnectingState ) {
|
if (socket->state() == QLocalSocket::ConnectingState) {
|
||||||
socket->waitForConnected( msecs );
|
socket->waitForConnected(msecs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisation message according to the SingleCoreApplication protocol
|
// Initialisation message according to the SingleCoreApplication protocol
|
||||||
if( socket->state() == QLocalSocket::ConnectedState ) {
|
if (socket->state() == QLocalSocket::ConnectedState) {
|
||||||
// Notify the parent that a new instance had been started;
|
// Notify the parent that a new instance had been started;
|
||||||
QByteArray initMsg;
|
QByteArray initMsg;
|
||||||
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
||||||
@@ -216,47 +232,47 @@ void SingleCoreApplicationPrivate::connectToPrimary( int msecs, ConnectionType c
|
|||||||
#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(header);
|
||||||
socket->write( initMsg );
|
socket->write(initMsg);
|
||||||
socket->flush();
|
socket->flush();
|
||||||
socket->waitForBytesWritten( msecs );
|
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;
|
qint64 pid;
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
pid = inst->primaryPid;
|
pid = inst->primaryPid;
|
||||||
memory->unlock();
|
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();
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
||||||
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
||||||
|
|
||||||
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
||||||
[nextConnSocket, this]() {
|
[nextConnSocket, this]() {
|
||||||
auto &info = connectionMap[nextConnSocket];
|
auto &info = connectionMap[nextConnSocket];
|
||||||
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotClientConnectionClosed(nextConnSocket, info.instanceId);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -278,29 +294,30 @@ void SingleCoreApplicationPrivate::slotConnectionEstablished()
|
|||||||
readInitMessageBody(nextConnSocket);
|
readInitMessageBody(nextConnSocket);
|
||||||
break;
|
break;
|
||||||
case StageConnected:
|
case StageConnected:
|
||||||
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotDataAvailable(nextConnSocket, info.instanceId);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
void SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
|
||||||
{
|
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains(sock)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) {
|
if (sock->bytesAvailable() < (qint64)sizeof(quint64)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream headerStream( sock );
|
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
|
||||||
@@ -310,21 +327,22 @@ void SingleCoreApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
|||||||
info.stage = StageBody;
|
info.stage = StageBody;
|
||||||
info.msgLen = msgLen;
|
info.msgLen = msgLen;
|
||||||
|
|
||||||
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
|
if (sock->bytesAvailable() >= (qint64) msgLen) {
|
||||||
readInitMessageBody( sock );
|
readInitMessageBody(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
void SingleCoreApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
|
||||||
{
|
|
||||||
Q_Q(SingleCoreApplication);
|
Q_Q(SingleCoreApplication);
|
||||||
|
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains(sock)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionInfo &info = connectionMap[sock];
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) {
|
if (sock->bytesAvailable() < (qint64)info.msgLen) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,7 +351,7 @@ void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
QDataStream readStream(msgBytes);
|
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
|
||||||
@@ -344,7 +362,7 @@ void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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;
|
||||||
@@ -354,13 +372,11 @@ void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -368,26 +384,26 @@ void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|||||||
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 &&
|
|
||||||
options & SingleCoreApplication::Mode::SecondaryNotification ) )
|
|
||||||
{
|
|
||||||
Q_EMIT q->instanceStarted();
|
Q_EMIT q->instanceStarted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock->bytesAvailable() > 0) {
|
if (sock->bytesAvailable() > 0) {
|
||||||
Q_EMIT this->slotDataAvailable( sock, instanceId );
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,17 +24,22 @@
|
|||||||
// 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 {
|
||||||
@@ -45,16 +50,15 @@ struct InstancesInfo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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,
|
||||||
@@ -90,10 +94,10 @@ public:
|
|||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user