2014-11-08 05:45:21 +08:00
|
|
|
/*
|
2019-06-24 22:01:18 +08:00
|
|
|
Copyright © 2014-2019 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-11-08 05:45:21 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-08 05:45:21 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-08 05:45:21 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-08 05:45:21 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-08 05:45:21 +08:00
|
|
|
*/
|
|
|
|
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/ipc.h"
|
2014-11-06 22:12:10 +08:00
|
|
|
#include <QCoreApplication>
|
2015-10-11 19:49:22 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QThread>
|
2017-06-01 15:50:59 +08:00
|
|
|
#include <ctime>
|
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
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @var time_t IPC::lastEvent
|
|
|
|
* @brief When last event was posted.
|
|
|
|
*
|
|
|
|
* @var time_t IPC::lastProcessed
|
|
|
|
* @brief When processEvents() ran last time
|
|
|
|
*/
|
2016-07-27 06:21:22 +08:00
|
|
|
|
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @class IPC
|
|
|
|
* @brief Inter-process communication
|
|
|
|
*/
|
2016-07-27 06:21:22 +08:00
|
|
|
|
2017-04-18 04:42:13 +08:00
|
|
|
IPC::IPC(uint32_t profileId)
|
2017-06-12 00:36:37 +08:00
|
|
|
: profileId{profileId}
|
|
|
|
, 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);
|
2017-09-01 04:09:38 +08:00
|
|
|
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
|
2017-02-26 19:52:45 +08:00
|
|
|
// If the owner exits normally, it can set the timestamp to 0 first to immediately give
|
|
|
|
// ownership
|
2014-11-06 22:12:10 +08:00
|
|
|
|
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-09-06 06:11:47 +08:00
|
|
|
qDebug() << "Our global IPC ID is " << globalId;
|
2017-02-26 19:52:45 +08:00
|
|
|
if (globalMemory.create(sizeof(IPCMemory))) {
|
|
|
|
if (globalMemory.lock()) {
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCMemory* mem = global();
|
|
|
|
memset(mem, 0, sizeof(IPCMemory));
|
|
|
|
mem->globalId = globalId;
|
2018-09-07 17:56:52 +08:00
|
|
|
mem->lastProcessed = time(nullptr);
|
2014-11-06 22:12:10 +08:00
|
|
|
globalMemory.unlock();
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "Couldn't lock to take ownership";
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
} else if (globalMemory.attach()) {
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "Attaching to the global shared memory";
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2017-11-22 01:25:39 +08:00
|
|
|
qDebug() << "Failed to attach to the global shared memory, giving up. Error:"
|
|
|
|
<< globalMemory.error();
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
processEvents();
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IPC::~IPC()
|
|
|
|
{
|
2018-12-19 06:37:59 +08:00
|
|
|
if (!globalMemory.lock()) {
|
2017-09-23 06:52:21 +08:00
|
|
|
qWarning() << "Failed to lock in ~IPC";
|
2018-12-19 06:37:59 +08:00
|
|
|
return;
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
|
|
|
|
if (isCurrentOwnerNoLock()) {
|
|
|
|
global()->globalId = 0;
|
|
|
|
}
|
|
|
|
globalMemory.unlock();
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief Post IPC event.
|
|
|
|
* @param name Name to set in IPC event.
|
|
|
|
* @param data Data to set in IPC event (default QByteArray()).
|
|
|
|
* @param dest Settings::getCurrentProfileId() or 0 (main instance, default).
|
2018-12-19 06:37:59 +08:00
|
|
|
* @return Time the event finished or 0 on error.
|
2016-08-01 16:20:56 +08:00
|
|
|
*/
|
2017-02-26 19:52:45 +08:00
|
|
|
time_t IPC::postEvent(const QString& name, const QByteArray& data, uint32_t dest)
|
2015-02-20 20:26:41 +08:00
|
|
|
{
|
|
|
|
QByteArray binName = name.toUtf8();
|
2018-12-19 06:37:59 +08:00
|
|
|
if (binName.length() > (int32_t)sizeof(IPCEvent::name)) {
|
2015-02-20 20:26:41 +08:00
|
|
|
return 0;
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
if (data.length() > (int32_t)sizeof(IPCEvent::data)) {
|
2015-02-20 20:26:41 +08:00
|
|
|
return 0;
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
if (!globalMemory.lock()) {
|
|
|
|
qDebug() << "Failed to lock in postEvent()";
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
IPCEvent* evt = nullptr;
|
|
|
|
IPCMemory* mem = global();
|
|
|
|
time_t result = 0;
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
for (uint32_t i = 0; !evt && i < EVENT_QUEUE_SIZE; ++i) {
|
|
|
|
if (mem->events[i].posted == 0) {
|
|
|
|
evt = &mem->events[i];
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
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(nullptr));
|
|
|
|
evt->dest = dest;
|
|
|
|
evt->sender = getpid();
|
|
|
|
qDebug() << "postEvent " << name << "to" << dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
globalMemory.unlock();
|
|
|
|
return result;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPC::isCurrentOwner()
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (globalMemory.lock()) {
|
2017-09-23 06:52:21 +08:00
|
|
|
const bool isOwner = isCurrentOwnerNoLock();
|
2014-11-06 22:12:10 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
return isOwner;
|
2017-02-26 19:52:45 +08:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 12:55:23 +08:00
|
|
|
/**
|
|
|
|
* @brief Register a handler for an IPC event
|
|
|
|
* @param handler The handler callback. Should not block for more than a second, at worst
|
|
|
|
*/
|
2017-02-26 19:52:45 +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;
|
2018-12-19 06:37:59 +08:00
|
|
|
if (!globalMemory.lock()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (difftime(global()->lastProcessed, time) > 0) {
|
|
|
|
IPCMemory* mem = global();
|
|
|
|
for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; ++i) {
|
|
|
|
if (mem->events[i].posted == time && mem->events[i].processed) {
|
|
|
|
result = mem->events[i].accepted;
|
|
|
|
break;
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
globalMemory.unlock();
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
return result;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +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;
|
2018-09-07 17:56:52 +08:00
|
|
|
time_t start = time(nullptr);
|
2015-12-22 20:48:52 +08:00
|
|
|
forever
|
|
|
|
{
|
2015-10-13 02:28:03 +08:00
|
|
|
result = isEventAccepted(postTime);
|
2018-12-19 06:37:59 +08:00
|
|
|
if (result || (timeout > 0 && difftime(time(nullptr), start) >= timeout)) {
|
2015-02-20 20:26:41 +08:00
|
|
|
break;
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2015-10-13 02:28:03 +08:00
|
|
|
|
|
|
|
qApp->processEvents();
|
|
|
|
QThread::msleep(0);
|
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
|
|
|
}
|
|
|
|
|
2017-11-22 01:25:39 +08:00
|
|
|
bool IPC::isAttached() const
|
|
|
|
{
|
|
|
|
return globalMemory.isAttached();
|
|
|
|
}
|
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
void IPC::setProfileId(uint32_t profileId)
|
|
|
|
{
|
|
|
|
this->profileId = profileId;
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief Only called when global memory IS LOCKED.
|
|
|
|
* @return nullptr if no evnts present, IPC event otherwise
|
|
|
|
*/
|
2017-05-21 01:20:28 +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();
|
2017-02-26 19:52:45 +08:00
|
|
|
for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; ++i) {
|
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.
|
2018-09-07 17:56:52 +08:00
|
|
|
if ((evt->processed && difftime(time(nullptr), evt->processed) > EVENT_GC_TIMEOUT)
|
2018-12-19 06:37:59 +08:00
|
|
|
|| (!evt->processed && difftime(time(nullptr), evt->posted) > EVENT_GC_TIMEOUT)) {
|
2015-02-20 20:26:41 +08:00
|
|
|
memset(evt, 0, sizeof(IPCEvent));
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
if (evt->posted && !evt->processed && evt->sender != getpid()
|
2018-12-19 06:37:59 +08:00
|
|
|
&& (evt->dest == profileId || (evt->dest == 0 && isCurrentOwnerNoLock()))) {
|
2016-07-27 06:21:22 +08:00
|
|
|
return evt;
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
2016-07-27 06:21:22 +08:00
|
|
|
|
|
|
|
return nullptr;
|
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;
|
2018-12-19 06:37:59 +08:00
|
|
|
if (QThread::currentThread() == qApp->thread()) {
|
2016-07-27 06:21:22 +08:00
|
|
|
result = handler(arg);
|
2018-12-19 06:37:59 +08:00
|
|
|
} else {
|
2017-02-26 19:52:45 +08:00
|
|
|
QMetaObject::invokeMethod(this, "runEventHandler", Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(bool, result), Q_ARG(IPCEventHandler, handler),
|
2015-02-20 20:26:41 +08:00
|
|
|
Q_ARG(const QByteArray&, arg));
|
2018-12-19 06:37:59 +08:00
|
|
|
}
|
2016-07-27 06:21:22 +08:00
|
|
|
|
|
|
|
return result;
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
void IPC::processEvents()
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2018-12-19 06:37:59 +08:00
|
|
|
if (!globalMemory.lock()) {
|
|
|
|
timer.start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCMemory* mem = global();
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
if (mem->globalId == globalId) {
|
|
|
|
// We're the owner, let's process those events
|
|
|
|
mem->lastProcessed = time(nullptr);
|
|
|
|
} else {
|
|
|
|
// Only the owner processes events. But if the previous owner's dead, we can take
|
|
|
|
// ownership now
|
|
|
|
if (difftime(time(nullptr), mem->lastProcessed) >= OWNERSHIP_TIMEOUT_S) {
|
|
|
|
qDebug() << "Previous owner timed out, taking ownership" << mem->globalId << "->"
|
|
|
|
<< globalId;
|
|
|
|
// Ignore events that were not meant for this instance
|
|
|
|
memset(mem, 0, sizeof(IPCMemory));
|
|
|
|
mem->globalId = globalId;
|
2018-09-07 17:56:52 +08:00
|
|
|
mem->lastProcessed = time(nullptr);
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
// Non-main instance is limited to events destined for specific profile it runs
|
|
|
|
}
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2018-12-19 06:37:59 +08:00
|
|
|
while (IPCEvent* evt = fetchEvent()) {
|
|
|
|
QString name = QString::fromUtf8(evt->name);
|
|
|
|
auto it = eventHandlers.find(name);
|
|
|
|
if (it != eventHandlers.end()) {
|
|
|
|
evt->accepted = runEventHandler(it.value(), evt->data);
|
|
|
|
qDebug() << "Processed event:" << name << "posted:" << evt->posted
|
|
|
|
<< "accepted:" << evt->accepted;
|
|
|
|
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) {
|
2018-09-07 17:56:52 +08:00
|
|
|
evt->processed = time(nullptr);
|
2016-04-19 20:06:06 +08:00
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
} else {
|
|
|
|
evt->processed = time(nullptr);
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
} else {
|
|
|
|
qDebug() << "Received event:" << name << "without handler";
|
|
|
|
qDebug() << "Available handlers:" << eventHandlers.keys();
|
2015-02-20 20:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 06:37:59 +08:00
|
|
|
|
|
|
|
globalMemory.unlock();
|
2015-02-20 20:26:41 +08:00
|
|
|
timer.start();
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 06:52:21 +08:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
IPC::IPCMemory* IPC::global()
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
2016-04-19 20:06:06 +08:00
|
|
|
return static_cast<IPCMemory*>(globalMemory.data());
|
2014-11-06 22:12:10 +08:00
|
|
|
}
|