Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,36 @@
#include <QtCore/QByteArray>
#include <QtCore/QSharedMemory>

#ifdef Q_OS_UNIX
#include <signal.h>
#include <errno.h>
#endif
#ifdef Q_OS_WIN
#include <windows.h>
#endif

#include "singleapplication.h"
#include "singleapplication_p.h"

static bool isProcessRunning( qint64 pid )
{
if ( pid <= 0 )
return false;

#ifdef Q_OS_UNIX
return kill( static_cast<pid_t>( pid ), 0 ) == 0 || errno != ESRCH;
#endif
#ifdef Q_OS_WIN
HANDLE hProcess = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE, static_cast<DWORD>( pid ) );
if ( hProcess == NULL )
return false;
DWORD exitCode;
bool running = GetExitCodeProcess( hProcess, &exitCode ) && exitCode == STILL_ACTIVE;
CloseHandle( hProcess );
return running;
#endif
}

/**
* @brief Constructor. Checks and fires up LocalServer or closes the program
* if another instance already exists
Expand Down Expand Up @@ -138,6 +165,13 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
}
}

// If the recorded primary PID is no longer running (e.g. force-killed),
// take over as the primary instance
if( inst->primary && !isProcessRunning( inst->primaryPid ) ){
qWarning() << "SingleApplication: Primary instance (PID" << inst->primaryPid << ") is no longer running. Taking over.";
d->initializeMemoryBlock();
}

if( inst->primary == false ){
d->startPrimary();
if( ! d->memory->unlock() ){
Expand Down
Loading