Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(IPC): don't double lock shared memory
Browse files Browse the repository at this point in the history
Fixes #4678
  • Loading branch information
anthonybilinski committed Sep 25, 2017
1 parent b7921ef commit 0bf27a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ IPC::IPC(uint32_t profileId)

IPC::~IPC()
{
if (isCurrentOwner()) {
if (globalMemory.lock()) {
if (globalMemory.lock()) {
if (isCurrentOwnerNoLock()) {
global()->globalId = 0;
globalMemory.unlock();
}
globalMemory.unlock();
} else {
qWarning() << "Failed to lock in ~IPC";
}
}

Expand Down Expand Up @@ -135,13 +137,7 @@ time_t IPC::postEvent(const QString& name, const QByteArray& data, uint32_t dest
bool IPC::isCurrentOwner()
{
if (globalMemory.lock()) {
void* data = globalMemory.data();
if (!data) {
qWarning() << "isCurrentOwner failed to access the memory, returning false";
globalMemory.unlock();
return false;
}
bool isOwner = ((*(uint64_t*)data) == globalId);
const bool isOwner = isCurrentOwnerNoLock();
globalMemory.unlock();
return isOwner;
} else {
Expand Down Expand Up @@ -216,7 +212,7 @@ IPC::IPCEvent* IPC::fetchEvent()
memset(evt, 0, sizeof(IPCEvent));

if (evt->posted && !evt->processed && evt->sender != getpid()
&& (evt->dest == profileId || (evt->dest == 0 && isCurrentOwner())))
&& (evt->dest == profileId || (evt->dest == 0 && isCurrentOwnerNoLock())))
return evt;
}

Expand Down Expand Up @@ -281,6 +277,20 @@ void IPC::processEvents()
timer.start();
}

/**
* @brief Only called when global memory IS LOCKED.
* @return true if owner, false if not owner or if error
*/
bool IPC::isCurrentOwnerNoLock()
{
const void* const data = globalMemory.data();
if (!data) {
qWarning() << "isCurrentOwnerNoLock failed to access the memory, returning false";
return false;
}
return (*static_cast<const uint64_t*>(data) == globalId);
}

IPC::IPCMemory* IPC::global()
{
return static_cast<IPCMemory*>(globalMemory.data());
Expand Down
1 change: 1 addition & 0 deletions src/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public slots:
bool runEventHandler(IPCEventHandler handler, const QByteArray& arg);
IPCEvent* fetchEvent();
void processEvents();
bool isCurrentOwnerNoLock();

private:
QTimer timer;
Expand Down

0 comments on commit 0bf27a0

Please sign in to comment.