2024-05-21 01:14:38 +08:00
|
|
|
// Copyright (c) Itay Grudev 2023
|
2018-07-27 09:29:55 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2023-03-24 18:09:12 +08:00
|
|
|
// Permission is not granted to use this software or any of the associated files
|
|
|
|
// as sample data for the purposes of building machine learning models.
|
|
|
|
//
|
2018-07-27 09:29:55 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
//
|
|
|
|
// W A R N I N G !!!
|
|
|
|
// -----------------
|
|
|
|
//
|
|
|
|
// This file is not part of the SingleApplication API. It is used purely as an
|
|
|
|
// implementation detail. This header file may change from version to
|
|
|
|
// version without notice, or may even be removed.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#include <QtCore/QDir>
|
2020-09-09 09:23:42 +08:00
|
|
|
#include <QtCore/QThread>
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <QtCore/QByteArray>
|
|
|
|
#include <QtCore/QDataStream>
|
2020-09-09 09:23:42 +08:00
|
|
|
#include <QtCore/QElapsedTimer>
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <QtCore/QCryptographicHash>
|
|
|
|
#include <QtNetwork/QLocalServer>
|
|
|
|
#include <QtNetwork/QLocalSocket>
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
#include "message_coder.h"
|
|
|
|
|
2020-09-09 09:23:42 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
#include <QtCore/QRandomGenerator>
|
|
|
|
#else
|
|
|
|
#include <QtCore/QDateTime>
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
#include "singleapplication.h"
|
|
|
|
#include "singleapplication_p.h"
|
|
|
|
|
2019-09-23 01:39:45 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2020-10-06 18:56:20 +08:00
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX 1
|
|
|
|
#endif
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#include <lmcons.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr )
|
2024-05-21 01:14:38 +08:00
|
|
|
: q_ptr( q_ptr ), server( nullptr ), socket( nullptr ), instanceNumber( 0 ), connectionMap()
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SingleApplicationPrivate::~SingleApplicationPrivate()
|
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
if( socket != nullptr ){
|
2018-07-27 09:29:55 +08:00
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:52:48 +08:00
|
|
|
QString SingleApplicationPrivate::getUsername()
|
|
|
|
{
|
2020-03-03 09:22:54 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
wchar_t username[UNLEN + 1];
|
|
|
|
// Specifies size of the buffer on input
|
|
|
|
DWORD usernameLength = UNLEN + 1;
|
|
|
|
if( GetUserNameW( username, &usernameLength ) )
|
2020-03-27 14:52:48 +08:00
|
|
|
return QString::fromWCharArray( username );
|
2020-03-27 15:19:41 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
return QString::fromLocal8Bit( qgetenv( "USERNAME" ) );
|
|
|
|
#else
|
2020-03-27 14:52:48 +08:00
|
|
|
return qEnvironmentVariable( "USERNAME" );
|
2020-03-03 09:22:54 +08:00
|
|
|
#endif
|
2020-03-27 15:19:41 +08:00
|
|
|
#endif
|
2020-03-03 09:22:54 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
2020-03-27 14:52:48 +08:00
|
|
|
QString username;
|
2020-03-03 09:22:54 +08:00
|
|
|
uid_t uid = geteuid();
|
|
|
|
struct passwd *pw = getpwuid( uid );
|
|
|
|
if( pw )
|
2020-03-27 14:52:48 +08:00
|
|
|
username = QString::fromLocal8Bit( pw->pw_name );
|
2020-09-09 07:28:02 +08:00
|
|
|
if ( username.isEmpty() ){
|
2020-03-27 15:19:41 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
username = QString::fromLocal8Bit( qgetenv( "USER" ) );
|
|
|
|
#else
|
2020-03-27 14:52:48 +08:00
|
|
|
username = qEnvironmentVariable( "USER" );
|
2020-03-27 15:19:41 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-03-03 09:22:54 +08:00
|
|
|
return username;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
void SingleApplicationPrivate::genBlockServerName()
|
|
|
|
{
|
|
|
|
QCryptographicHash appData( QCryptographicHash::Sha256 );
|
2022-02-13 19:16:05 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 3, 0)
|
2018-07-27 09:29:55 +08:00
|
|
|
appData.addData( "SingleApplication", 17 );
|
2022-02-13 19:16:05 +08:00
|
|
|
#else
|
2024-05-21 01:14:38 +08:00
|
|
|
appData.addData( QByteArrayView{"SingleApplication"} );
|
2022-02-13 19:16:05 +08:00
|
|
|
#endif
|
2018-07-27 09:29:55 +08:00
|
|
|
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
|
|
|
|
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
|
|
|
|
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
|
|
|
|
|
2020-12-11 08:18:00 +08:00
|
|
|
if ( ! appDataList.isEmpty() )
|
2022-04-03 23:07:16 +08:00
|
|
|
appData.addData( appDataList.join(QString()).toUtf8() );
|
2020-12-11 08:18:00 +08:00
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ){
|
2018-07-27 09:29:55 +08:00
|
|
|
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ){
|
2021-09-20 21:37:01 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
2018-07-27 09:29:55 +08:00
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
2021-09-20 21:38:28 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
2021-09-20 21:22:02 +08:00
|
|
|
// If the application is running as an AppImage then the APPIMAGE env var should be used
|
|
|
|
// instead of applicationPath() as each instance is launched with its own executable path
|
2021-09-20 20:56:31 +08:00
|
|
|
const QByteArray appImagePath = qgetenv( "APPIMAGE" );
|
2021-09-20 21:22:02 +08:00
|
|
|
if( appImagePath.isEmpty() ){ // Not running as AppImage: use path to executable file
|
2021-09-20 20:41:02 +08:00
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
2021-09-20 21:22:02 +08:00
|
|
|
} else { // Running as AppImage: Use absolute path to AppImage file
|
2021-09-20 21:16:20 +08:00
|
|
|
appData.addData( appImagePath );
|
2021-09-20 21:32:42 +08:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
2018-07-27 09:29:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// User level block requires a user specific data in the hash
|
2020-09-09 07:28:02 +08:00
|
|
|
if( options & SingleApplication::Mode::User ){
|
2020-03-27 14:52:48 +08:00
|
|
|
appData.addData( getUsername().toUtf8() );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
|
|
|
// server naming requirements.
|
2022-04-03 23:07:16 +08:00
|
|
|
blockServerName = QString::fromUtf8(appData.result().toBase64().replace("/", "_"));
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
bool SingleApplicationPrivate::startPrimary( uint timeout )
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
|
|
|
// Successful creation means that no main process exists
|
|
|
|
// So we start a QLocalServer to listen for connections
|
|
|
|
QLocalServer::removeServer( blockServerName );
|
|
|
|
server = new QLocalServer();
|
|
|
|
|
|
|
|
// Restrict access to the socket according to the
|
|
|
|
// SingleApplication::Mode::User flag on User level or no restrictions
|
2020-09-09 07:28:02 +08:00
|
|
|
if( options & SingleApplication::Mode::User ){
|
2020-12-11 08:18:00 +08:00
|
|
|
server->setSocketOptions( QLocalServer::UserAccessOption );
|
2018-07-27 09:29:55 +08:00
|
|
|
} else {
|
2020-12-11 08:18:00 +08:00
|
|
|
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QObject::connect(
|
|
|
|
server,
|
|
|
|
&QLocalServer::newConnection,
|
|
|
|
this,
|
|
|
|
&SingleApplicationPrivate::slotConnectionEstablished
|
|
|
|
);
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
if( server->listen( blockServerName ) )
|
|
|
|
return true;
|
2020-09-09 07:28:02 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
delete server;
|
|
|
|
return false;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
bool SingleApplicationPrivate::connectToPrimary( uint timeout ){
|
|
|
|
if( socket == nullptr )
|
|
|
|
socket = new QLocalSocket( this );
|
2020-09-09 09:28:07 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
if( socket->state() == QLocalSocket::ConnectedState )
|
|
|
|
return true;
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
if( socket->state() != QLocalSocket::ConnectingState )
|
|
|
|
socket->connectToServer( blockServerName );
|
2021-05-31 22:14:39 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
return socket->waitForConnected( timeout );
|
2021-05-31 22:14:39 +08:00
|
|
|
}
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
void SingleApplicationPrivate::notifySecondaryStart( uint timeout )
|
2021-05-31 22:14:39 +08:00
|
|
|
{
|
2024-05-21 01:14:38 +08:00
|
|
|
// SingleApplicationMessage message( SingleApplicationMessage::NewInstance, 0, QByteArray() );
|
|
|
|
// sendApplicationMessage( message, timeout );
|
|
|
|
sendApplicationMessage( SingleApplication::MessageType::NewInstance, QByteArray(), timeout );
|
|
|
|
}
|
2021-05-31 22:14:39 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
//bool SingleApplicationPrivate::sendApplicationMessage( SingleApplicationMessage message, uint timeout )
|
|
|
|
//{
|
|
|
|
// SingleApplicationMessage response;
|
|
|
|
// return sendApplicationMessage( message, timeout, response );
|
|
|
|
//}
|
2018-12-15 02:41:55 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
bool SingleApplicationPrivate::sendApplicationMessage( SingleApplication::MessageType messageType, QByteArray content, uint timeout )
|
|
|
|
{
|
|
|
|
QElapsedTimer elapsedTime;
|
|
|
|
elapsedTime.start();
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
if( ! connectToPrimary( timeout * 2 / 3 ))
|
2021-05-31 21:23:18 +08:00
|
|
|
return false;
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
MessageCoder coder( socket );
|
|
|
|
coder.sendMessage( messageType, instanceNumber, content );
|
2022-04-06 04:46:42 +08:00
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
socket->flush();
|
2024-05-21 01:14:38 +08:00
|
|
|
return socket->waitForBytesWritten( qMax(timeout - elapsedTime.elapsed(), 1) );
|
2021-05-31 21:23:18 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
// TODO: Wait for an ACK message
|
|
|
|
// if( socket->waitForReadyRead( timeout )){
|
|
|
|
// QByteArray responseBytes = socket->readAll();
|
|
|
|
// response = SingleApplicationMessage( socket->readAll() );
|
|
|
|
//
|
|
|
|
// // The response message is invalid
|
|
|
|
// if( response.invalid )
|
|
|
|
// return false;
|
|
|
|
//
|
|
|
|
// // The response message didn't contain the primary instance id
|
|
|
|
// if( response.instanceId != 0 )
|
|
|
|
// return false;
|
|
|
|
//
|
|
|
|
// // This isn't an acknowledge message
|
|
|
|
// if( response.type != SingleApplicationMessage::Acknowledge )
|
|
|
|
// return false;
|
|
|
|
//
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return false;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
qint64 SingleApplicationPrivate::primaryPid() const
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
|
|
|
qint64 pid;
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
// TODO: Reimplement with message response
|
2018-07-27 09:29:55 +08:00
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
QString SingleApplicationPrivate::primaryUser() const
|
2020-03-03 09:22:54 +08:00
|
|
|
{
|
|
|
|
QByteArray username;
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
// TODO: Reimplement with message response
|
2020-03-03 09:22:54 +08:00
|
|
|
|
|
|
|
return QString::fromUtf8( username );
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
/**
|
|
|
|
* @brief Executed when a connection has been made to the LocalServer
|
|
|
|
*/
|
|
|
|
void SingleApplicationPrivate::slotConnectionEstablished()
|
|
|
|
{
|
|
|
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
connectionMap.insert( nextConnSocket, ConnectionInfo() );
|
|
|
|
connectionMap[nextConnSocket].coder = new MessageCoder( nextConnSocket );
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
QObject::connect( nextConnSocket, &QLocalSocket::disconnected, nextConnSocket, &QLocalSocket::deleteLater );
|
2020-12-23 09:49:50 +08:00
|
|
|
|
2024-05-21 01:14:38 +08:00
|
|
|
QObject::connect( nextConnSocket, &QLocalSocket::destroyed, this,
|
2020-12-24 04:42:56 +08:00
|
|
|
[nextConnSocket, this](){
|
2024-05-21 01:14:38 +08:00
|
|
|
connectionMap.remove( nextConnSocket );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-09-09 09:23:42 +08:00
|
|
|
|
|
|
|
void SingleApplicationPrivate::randomSleep()
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
|
|
|
|
QThread::msleep( QRandomGenerator::global()->bounded( 8u, 18u ));
|
|
|
|
#else
|
|
|
|
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
2021-04-10 19:37:33 +08:00
|
|
|
QThread::msleep( qrand() % 11 + 8);
|
2020-09-09 09:23:42 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-12-11 08:18:00 +08:00
|
|
|
|
|
|
|
void SingleApplicationPrivate::addAppData(const QString &data)
|
|
|
|
{
|
|
|
|
appDataList.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SingleApplicationPrivate::appData() const
|
|
|
|
{
|
|
|
|
return appDataList;
|
|
|
|
}
|