From 47a1519699a59765fcdfa05b0d830401c8016fb3 Mon Sep 17 00:00:00 2001 From: Caio Carvalho Date: Fri, 23 Mar 2018 18:19:33 -0300 Subject: [PATCH] - Including Watchdog Timer in CoreBackendManager to ping DBus service. - Including ExternalCommandHelper::ping slot to receive pings. - Including timer to watch last ping in the service to unregister it after reaching 42 seconds without pings. --- src/backend/corebackendmanager.cpp | 17 ++++++++++++++++ src/util/externalcommandhelper.cpp | 31 ++++++++++++++++++++++++++++++ src/util/externalcommandhelper.h | 7 +++++++ 3 files changed, 55 insertions(+) diff --git a/src/backend/corebackendmanager.cpp b/src/backend/corebackendmanager.cpp index 5b6c00c..aaea5d4 100644 --- a/src/backend/corebackendmanager.cpp +++ b/src/backend/corebackendmanager.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -78,6 +79,22 @@ void CoreBackendManager::startExternalCommandHelper() auto conn = QObject::connect(job(), &KAuth::ExecuteJob::newData, exitLoop); loop.exec(); QObject::disconnect(conn); + + // Watchdog Timer for the DBus service + QTimer *timer = new QTimer; + + auto sendDBusPing = [&] () { + QDBusInterface iface(QStringLiteral("org.kde.kpmcore.helperinterface"), + QStringLiteral("/Helper"), + QStringLiteral("org.kde.kpmcore.externalcommand"), + QDBusConnection::systemBus()); + + if (iface.isValid()) + iface.call(QStringLiteral("ping"), CoreBackendManager::self()->Uuid()); + }; + + QObject::connect(timer, &QTimer::timeout, sendDBusPing); + timer->start(20000); // 20 seconds } void CoreBackendManager::stopExternalCommandHelper() diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp index 6b0ebaa..5eef104 100644 --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -17,6 +17,7 @@ #include "externalcommandhelper.h" +#include #include #include #include @@ -49,6 +50,13 @@ ActionReply ExternalCommandHelper::init(const QVariantMap& args) HelperSupport::progressStep(QVariantMap()); m_loop.exec(); reply.addData(QStringLiteral("success"), true); + + m_pingTime = new QDateTime(QDateTime::currentDateTime()); + + QTimer *timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, &ExternalCommandHelper::checkPing); + timer->start(20000); // check ping every 20 secs + return reply; } @@ -237,6 +245,29 @@ void ExternalCommandHelper::exit(const QString& Uuid) QDBusConnection::systemBus().unregisterObject(QStringLiteral("/Helper")); } +void ExternalCommandHelper::ping(const QString &Uuid) +{ + if (!isCallerAuthorized(Uuid)) + return; + + // update ping + m_pingTime->setDate(QDate::currentDate()); + m_pingTime->setTime(QTime::currentTime()); +} + +void ExternalCommandHelper::checkPing() +{ + qint64 mSecsSinceLastPing = m_pingTime->msecsTo(QDateTime::currentDateTime()); + + qDebug() << (((double)mSecsSinceLastPing) / 1000) << " seconds since the last ping."; + + if (mSecsSinceLastPing >= 42000) { // more than 42 seconds since the last ping + qDebug() << "Ending DBus service"; + + exit(m_callerUuid); + } +} + void ExternalCommandHelper::onReadOutput() { // const QByteArray s = cmd.readAllStandardOutput(); diff --git a/src/util/externalcommandhelper.h b/src/util/externalcommandhelper.h index 0a7590f..4aea8d4 100644 --- a/src/util/externalcommandhelper.h +++ b/src/util/externalcommandhelper.h @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -44,6 +45,10 @@ public Q_SLOTS: Q_SCRIPTABLE QVariantMap start(const QString& Uuid, const QString& command, const QStringList& arguments, const QByteArray& input, const QStringList& environment); Q_SCRIPTABLE bool copyblocks(const QString& Uuid, const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize); Q_SCRIPTABLE void exit(const QString& Uuid); + Q_SCRIPTABLE void ping(const QString& Uuid); + +private Q_SLOTS: + void checkPing(); private: void onReadOutput(); @@ -54,6 +59,8 @@ private: QString m_command; QString m_sourceDevice; QProcess m_cmd; + + QDateTime *m_pingTime; // QByteArray output; };