2014-11-08 05:45:21 +08:00
|
|
|
/*
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
#include "src/ipc.h"
|
2015-02-20 20:26:41 +08:00
|
|
|
#include "src/misc/settings.h"
|
2014-11-06 22:12:10 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QCoreApplication>
|
2015-05-20 02:37:42 +08:00
|
|
|
#include <random>
|
2015-02-20 20:26:41 +08:00
|
|
|
#include <unistd.h>
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
IPC::IPC()
|
|
|
|
: globalMemory{"qtox-" IPC_PROTOCOL_VERSION}
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
|
|
|
qRegisterMetaType<IPCEventHandler>("IPCEventHandler");
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
timer.setInterval(EVENT_TIMER_MS);
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
connect(&timer, &QTimer::timeout, this, &IPC::processEvents);
|
2014-11-06 22:12:10 +08:00
|
|
|
|
|
|
|
// The first started instance gets to manage the shared memory by taking ownership
|
2015-02-20 20:26:41 +08:00
|
|
|
// Every time it processes events it updates the global shared timestamp "lastProcessed"
|
2014-11-06 22:12:10 +08:00
|
|
|
// If the timestamp isn't updated, that's a timeout and someone else can take ownership
|
|
|
|
// This is a safety measure, in case one of the clients crashes
|
|
|
|
// If the owner exits normally, it can set the timestamp to 0 first to immediately give ownership
|
|
|
|
|
2015-05-24 18:08:32 +08:00
|
|
|
std::default_random_engine randEngine((std::random_device())());
|
2015-05-20 02:37:42 +08:00
|
|
|
std::uniform_int_distribution<uint64_t> distribution;
|
|
|
|
globalId = distribution(randEngine);
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Our global ID is " << globalId;
|
2015-02-20 20:26:41 +08:00
|
|
|
if (globalMemory.create(sizeof(IPCMemory)))
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Creating the global shared memory and taking ownership";
|
2014-11-06 22:12:10 +08:00
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCMemory* mem = global();
|
|
|
|
memset(mem, 0, sizeof(IPCMemory));
|
|
|
|
mem->globalId = globalId;
|
|
|
|
mem->lastProcessed = time(0);
|
2014-11-06 22:12:10 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "Couldn't lock to take ownership";
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (globalMemory.attach())
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Attaching to the global shared memory";
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Failed to attach to the global shared memory, giving up";
|
2014-11-06 22:12:10 +08:00
|
|
|
return; // We won't be able to do any IPC without being attached, let's get outta here
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
timer.start();
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IPC::~IPC()
|
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
if (isCurrentOwner())
|
|
|
|
{
|
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
|
|
|
global()->globalId = 0;
|
|
|
|
globalMemory.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 20:49:27 +08:00
|
|
|
IPC& IPC::getInstance()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
|
|
Q_ASSERT(0 && "IPC can not be used on android");
|
|
|
|
#endif
|
|
|
|
static IPC instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
time_t IPC::postEvent(const QString &name, const QByteArray& data/*=QByteArray()*/, uint32_t dest/*=0*/)
|
|
|
|
{
|
|
|
|
QByteArray binName = name.toUtf8();
|
|
|
|
if (binName.length() > (int32_t)sizeof(IPCEvent::name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (data.length() > (int32_t)sizeof(IPCEvent::data))
|
|
|
|
return 0;
|
|
|
|
|
2014-11-06 22:21:42 +08:00
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCEvent* evt = 0;
|
|
|
|
IPCMemory* mem = global();
|
|
|
|
time_t result = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; !evt && i < EVENT_QUEUE_SIZE; i++)
|
|
|
|
{
|
|
|
|
if (mem->events[i].posted == 0)
|
|
|
|
evt = &mem->events[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evt)
|
|
|
|
{
|
|
|
|
memset(evt, 0, sizeof(IPCEvent));
|
|
|
|
memcpy(evt->name, binName.constData(), binName.length());
|
|
|
|
memcpy(evt->data, data.constData(), data.length());
|
|
|
|
mem->lastEvent = evt->posted = result = qMax(mem->lastEvent + 1, time(0));
|
|
|
|
evt->dest = dest;
|
|
|
|
evt->sender = getpid();
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "postEvent " << name << "to" << dest;
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2014-11-06 22:21:42 +08:00
|
|
|
globalMemory.unlock();
|
2015-02-20 20:26:41 +08:00
|
|
|
return result;
|
2014-11-06 22:21:42 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
else
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Failed to lock in postEvent()";
|
2015-02-20 20:26:41 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPC::isCurrentOwner()
|
|
|
|
{
|
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
2015-04-24 21:38:21 +08:00
|
|
|
void* data = globalMemory.data();
|
|
|
|
if (!data)
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "isCurrentOwner failed to access the memory, returning false";
|
2015-04-24 21:38:21 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isOwner = ((*(uint64_t*)data) == globalId);
|
2014-11-06 22:12:10 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
return isOwner;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "isCurrentOwner failed to lock, returning false";
|
2014-11-06 22:12:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
void IPC::registerEventHandler(const QString &name, IPCEventHandler handler)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
eventHandlers[name] = handler;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 18:06:05 +08:00
|
|
|
bool IPC::isEventAccepted(time_t time)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
bool result = false;
|
2014-11-06 22:12:10 +08:00
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
if (difftime(global()->lastProcessed, time) > 0)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCMemory* mem = global();
|
|
|
|
for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; i++)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
if (mem->events[i].posted == time && mem->events[i].processed)
|
|
|
|
{
|
|
|
|
result = mem->events[i].accepted;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
globalMemory.unlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "isEventAccepted failed to lock, returning false";
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
return result;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 18:06:05 +08:00
|
|
|
bool IPC::waitUntilAccepted(time_t postTime, int32_t timeout/*=-1*/)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
bool result = false;
|
|
|
|
time_t start = time(0);
|
2015-03-19 18:06:05 +08:00
|
|
|
while (!(result = isEventAccepted(postTime)))
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
qApp->processEvents();
|
|
|
|
if (timeout > 0 && difftime(time(0), start) >= timeout)
|
|
|
|
break;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
return result;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
IPC::IPCEvent *IPC::fetchEvent()
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCMemory* mem = global();
|
|
|
|
for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; i++)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCEvent* evt = &mem->events[i];
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
// Garbage-collect events that were not processed in EVENT_GC_TIMEOUT
|
|
|
|
// and events that were processed and EVENT_GC_TIMEOUT passed after
|
|
|
|
// so sending instance has time to react to those events.
|
|
|
|
if ((evt->processed && difftime(time(0), evt->processed) > EVENT_GC_TIMEOUT) ||
|
|
|
|
(!evt->processed && difftime(time(0), evt->posted) > EVENT_GC_TIMEOUT))
|
|
|
|
memset(evt, 0, sizeof(IPCEvent));
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
if (evt->posted && !evt->processed && evt->sender != getpid())
|
|
|
|
{
|
|
|
|
if (evt->dest == Settings::getInstance().getCurrentProfileId() || (evt->dest == 0 && isCurrentOwner()))
|
|
|
|
return evt;
|
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
return 0;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
bool IPC::runEventHandler(IPCEventHandler handler, const QByteArray& arg)
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
bool result = false;
|
|
|
|
if (QThread::currentThread() != qApp->thread())
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(this, "runEventHandler",
|
|
|
|
Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(bool, result),
|
|
|
|
Q_ARG(IPCEventHandler, handler),
|
|
|
|
Q_ARG(const QByteArray&, arg));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = handler(arg);
|
|
|
|
return result;
|
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
void IPC::processEvents()
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
if (globalMemory.lock())
|
|
|
|
{
|
|
|
|
IPCMemory* mem = global();
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
if (mem->globalId == globalId)
|
|
|
|
{
|
|
|
|
// We're the owner, let's process those events
|
|
|
|
mem->lastProcessed = time(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only the owner processes events. But if the previous owner's dead, we can take ownership now
|
|
|
|
if (difftime(time(0), mem->lastProcessed) >= OWNERSHIP_TIMEOUT_S)
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Previous owner timed out, taking ownership" << mem->globalId << "->" << globalId;
|
2015-02-20 20:26:41 +08:00
|
|
|
// Ignore events that were not meant for this instance
|
|
|
|
memset(mem, 0, sizeof(IPCMemory));
|
|
|
|
mem->globalId = globalId;
|
|
|
|
mem->lastProcessed = time(0);
|
|
|
|
}
|
|
|
|
// Non-main instance is limited to events destined for specific profile it runs
|
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
while (IPCEvent* evt = fetchEvent())
|
|
|
|
{
|
|
|
|
QString name = QString::fromUtf8(evt->name);
|
|
|
|
auto it = eventHandlers.find(name);
|
|
|
|
if (it != eventHandlers.end())
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Processing event: " << name << ":" << evt->posted << "=" << evt->accepted;
|
2015-03-19 18:06:05 +08:00
|
|
|
evt->accepted = runEventHandler(it.value(), evt->data);
|
|
|
|
if (evt->dest == 0)
|
|
|
|
{
|
|
|
|
// Global events should be processed only by instance that accepted event. Otherwise global
|
|
|
|
// event would be consumed by very first instance that gets to check it.
|
|
|
|
if (evt->accepted)
|
|
|
|
evt->processed = time(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
evt->processed = time(0);
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2015-03-19 18:06:05 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
}
|
|
|
|
timer.start();
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
IPC::IPCMemory *IPC::global()
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2015-02-20 20:26:41 +08:00
|
|
|
return (IPCMemory*)globalMemory.data();
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|