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,33 +20,44 @@
// 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
@@ -65,7 +76,8 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
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.";
@@ -123,43 +135,39 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
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
@@ -172,4 +180,5 @@ bool SingleApplication::sendMessage( QByteArray message, int timeout )
bool dataWritten = d->socket->waitForBytesWritten(timeout); bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush(); d->socket->flush();
return dataWritten; 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,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)

View File

@@ -24,23 +24,32 @@
// 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"
@@ -51,16 +60,17 @@
#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;
@@ -78,10 +88,11 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
memory->unlock(); memory->unlock();
delete memory; delete memory;
} }
void SingleApplicationPrivate::genBlockServerName() void SingleApplicationPrivate::genBlockServerName() {
{
QCryptographicHash appData(QCryptographicHash::Sha256); QCryptographicHash appData(QCryptographicHash::Sha256);
appData.addData("SingleApplication", 17); appData.addData("SingleApplication", 17);
appData.addData(SingleApplication::app_t::applicationName().toUtf8()); appData.addData(SingleApplication::app_t::applicationName().toUtf8());
@@ -102,37 +113,48 @@ void SingleApplicationPrivate::genBlockServerName()
// 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
@@ -144,17 +166,13 @@ void SingleApplicationPrivate::startPrimary()
// 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 { }
else {
server->setSocketOptions(QLocalServer::WorldAccessOption); 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());
@@ -164,16 +182,14 @@ void SingleApplicationPrivate::startPrimary()
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();
} }
@@ -223,18 +239,17 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
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();
@@ -243,13 +258,14 @@ qint64 SingleApplicationPrivate::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());
@@ -285,10 +301,11 @@ void SingleApplicationPrivate::slotConnectionEstablished()
}; };
} }
); );
} }
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock ) void SingleApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
{
if (!connectionMap.contains(sock)) { if (!connectionMap.contains(sock)) {
return; return;
} }
@@ -313,10 +330,11 @@ void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
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)) {
@@ -356,9 +374,7 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
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();
@@ -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);
} }

View File

@@ -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,8 +50,7 @@ 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;
@@ -90,7 +94,7 @@ 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);

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"
@@ -38,8 +50,8 @@
* @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
@@ -65,7 +77,8 @@ SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allo
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.";
@@ -123,43 +136,39 @@ SingleCoreApplication::SingleCoreApplication( int &argc, char *argv[], bool allo
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
@@ -172,4 +181,5 @@ bool SingleCoreApplication::sendMessage( QByteArray message, int timeout )
bool dataWritten = d->socket->waitForBytesWritten(timeout); bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush(); d->socket->flush();
return dataWritten; 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,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 );

View File

@@ -24,23 +24,32 @@
// 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"
@@ -51,16 +60,17 @@
#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;
@@ -78,10 +88,11 @@ SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate()
memory->unlock(); memory->unlock();
delete memory; delete memory;
} }
void SingleCoreApplicationPrivate::genBlockServerName() void SingleCoreApplicationPrivate::genBlockServerName() {
{
QCryptographicHash appData(QCryptographicHash::Sha256); QCryptographicHash appData(QCryptographicHash::Sha256);
appData.addData("SingleApplication", 17); appData.addData("SingleApplication", 17);
appData.addData(SingleCoreApplication::app_t::applicationName().toUtf8()); appData.addData(SingleCoreApplication::app_t::applicationName().toUtf8());
@@ -102,37 +113,48 @@ void SingleCoreApplicationPrivate::genBlockServerName()
// 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
@@ -144,17 +166,13 @@ void SingleCoreApplicationPrivate::startPrimary()
// 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 { }
else {
server->setSocketOptions(QLocalServer::WorldAccessOption); 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());
@@ -164,16 +182,14 @@ void SingleCoreApplicationPrivate::startPrimary()
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();
} }
@@ -223,18 +239,17 @@ void SingleCoreApplicationPrivate::connectToPrimary( int msecs, ConnectionType c
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();
@@ -243,13 +258,14 @@ qint64 SingleCoreApplicationPrivate::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());
@@ -285,10 +301,11 @@ void SingleCoreApplicationPrivate::slotConnectionEstablished()
}; };
} }
); );
} }
void SingleCoreApplicationPrivate::readInitMessageHeader( QLocalSocket *sock ) void SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
{
if (!connectionMap.contains(sock)) { if (!connectionMap.contains(sock)) {
return; return;
} }
@@ -313,10 +330,11 @@ void SingleCoreApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
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)) {
@@ -356,9 +374,7 @@ void SingleCoreApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
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();
@@ -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);
} }

View File

@@ -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,8 +50,7 @@ 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;
@@ -90,7 +94,7 @@ 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);