// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2019 Juan Palacios #include "helpercontrol.h" #include "common/icryptolayer.h" #include "helperids.h" #include #include #include #include #include #include #include #include #include #include #include #include #include HelperControl::HelperControl(std::shared_ptr cryptoLayer, QObject *parent) noexcept : QObject(parent) , cryptoLayer_(std::move(cryptoLayer)) , autoExitTimeout_(IHelperControl::MinExitTimeout::value()) , deferAutoExitSignalInterval_(IHelperControl::MinExitTimeout::value() * .667) { connect(&deferHelperHealthCheckTimer_, &QTimer::timeout, this, &HelperControl::helperHealthCheckTimeout); connect(&deferHelperAutoExitSignalTimer_, &QTimer::timeout, this, &HelperControl::helperExitDeferrerTimeout); } void HelperControl::init(units::time::millisecond_t autoExitTimeout) { autoExitTimeout_ = std::max(autoExitTimeout, IHelperControl::MinExitTimeout::value()); deferAutoExitSignalInterval_ = autoExitTimeout * .667; cryptoLayer_->init(); createHelperInterface(); killOtherHelperInstance(); auto helperPublicKey = startHelper(); if (!helperPublicKey.has_value()) throw std::runtime_error("Cannot start helper"); cryptoLayer_->usePublicKey(helperPublicKey.value()); // Check the helper health every 15 seconds. deferHelperHealthCheckTimer_.setInterval(15000); deferHelperHealthCheckTimer_.start(); } void HelperControl::stop() { deferHelperHealthCheckTimer_.stop(); deferHelperAutoExitSignalTimer_.stop(); stopHelper(); } void HelperControl::helperHealthCheckTimeout() { if (deferHelperAutoExitSignalTimer_.isActive() && !helperHasBeenStarted()) { SPDLOG_WARN("The Helper has not been started. Starting it now."); deferHelperAutoExitSignalTimer_.stop(); auto helperPublicKey = startHelper(); if (!helperPublicKey.has_value()) { // NOTE If the helper died and cannot be started again, it's better to not // crash the application throwing an exception. SPDLOG_WARN("Cannot restart helper!"); } cryptoLayer_->usePublicKey(helperPublicKey.value()); } } void HelperControl::helperExitDeferrerTimeout() { helperInterface_->asyncCall(QStringLiteral("delayAutoExit")); } bool HelperControl::helperHasBeenStarted() const { QDBusReply reply = helperInterface_->call(QStringLiteral("started")); return reply.isValid() && reply.value(); } void HelperControl::createHelperInterface() { helperInterface_ = std::make_unique( QStringLiteral(DBUS_HELPER_SERVICE), QStringLiteral(DBUS_HELPER_PATH), QStringLiteral(DBUS_HELPER_INTERFACE), QDBusConnection::systemBus()); if (!helperInterface_->isValid()) throw std::runtime_error(std::format( "Cannot connect to D-Bus interface {}: {}", DBUS_HELPER_INTERFACE, helperInterface_->lastError().message().toStdString())); } std::optional HelperControl::startHelper() { QDBusReply reply = helperInterface_->call( QStringLiteral("start"), cryptoLayer_->publicKey(), autoExitTimeout_.to()); if (!(reply.isValid() && reply.value().variant().typeId() == QMetaType::QByteArray)) return std::nullopt; deferHelperAutoExitSignalTimer_.setInterval( deferAutoExitSignalInterval_.to()); deferHelperAutoExitSignalTimer_.start(); return reply.value().variant().value(); } void HelperControl::stopHelper() { auto signature = cryptoLayer_->signature({"exit"}); helperInterface_->asyncCall(QStringLiteral("exit"), signature); } void HelperControl::killOtherHelperInstance() { if (helperHasBeenStarted()) { SPDLOG_WARN("Helper instance detected. Killing it now."); if (!startHelperKiller() || helperHasBeenStarted()) throw std::runtime_error("Failed to kill other helper instance"); } } bool HelperControl::startHelperKiller() { QDBusInterface iface(QStringLiteral(DBUS_HELPER_KILLER_SERVICE), QStringLiteral(DBUS_HELPER_KILLER_PATH), QStringLiteral(DBUS_HELPER_KILLER_INTERFACE), QDBusConnection::systemBus()); if (!iface.isValid()) { SPDLOG_DEBUG("Cannot connect to D-Bus interface {}: {}", DBUS_HELPER_KILLER_INTERFACE, iface.lastError().message().toStdString()); return false; } QDBusReply reply = iface.call(QStringLiteral("start")); if (!reply.isValid()) { SPDLOG_DEBUG("Helper killer error: {}", iface.lastError().message().toStdString()); return false; } return reply.value(); }