| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "helpercontrol.h" |
| 5 | |
| 6 | #include "common/icryptolayer.h" |
| 7 | #include "helperids.h" |
| 8 | #include <QDBusConnection> |
| 9 | #include <QDBusError> |
| 10 | #include <QDBusInterface> |
| 11 | #include <QDBusReply> |
| 12 | #include <QDBusVariant> |
| 13 | #include <QString> |
| 14 | #include <QVariant> |
| 15 | #include <algorithm> |
| 16 | #include <format> |
| 17 | #include <limits> |
| 18 | #include <spdlog/spdlog.h> |
| 19 | #include <stdexcept> |
| 20 | #include <utility> |
| 21 | |
| 22 | HelperControl::HelperControl(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 23 | QObject *parent) noexcept |
| 24 | : QObject(parent) |
| 25 | , cryptoLayer_(std::move(cryptoLayer)) |
| 26 | , autoExitTimeout_(IHelperControl::MinExitTimeout::value()) |
| 27 | , deferAutoExitSignalInterval_(IHelperControl::MinExitTimeout::value() * .667) |
| 28 | { |
| 29 | connect(&deferHelperHealthCheckTimer_, &QTimer::timeout, this, |
| 30 | &HelperControl::helperHealthCheckTimeout); |
| 31 | connect(&deferHelperAutoExitSignalTimer_, &QTimer::timeout, this, |
| 32 | &HelperControl::helperExitDeferrerTimeout); |
| 33 | } |
| 34 | |
| 35 | void HelperControl::init(units::time::millisecond_t autoExitTimeout) |
| 36 | { |
| 37 | autoExitTimeout_ = std::max(autoExitTimeout, |
| 38 | IHelperControl::MinExitTimeout::value()); |
| 39 | deferAutoExitSignalInterval_ = autoExitTimeout * .667; |
| 40 | |
| 41 | cryptoLayer_->init(); |
| 42 | createHelperInterface(); |
| 43 | |
| 44 | killOtherHelperInstance(); |
| 45 | |
| 46 | auto helperPublicKey = startHelper(); |
| 47 | if (!helperPublicKey.has_value()) |
| 48 | throw std::runtime_error("Cannot start helper"); |
| 49 | |
| 50 | cryptoLayer_->usePublicKey(helperPublicKey.value()); |
| 51 | |
| 52 | // Check the helper health every 15 seconds. |
| 53 | deferHelperHealthCheckTimer_.setInterval(15000); |
| 54 | deferHelperHealthCheckTimer_.start(); |
| 55 | } |
| 56 | |
| 57 | void HelperControl::stop() |
| 58 | { |
| 59 | deferHelperHealthCheckTimer_.stop(); |
| 60 | deferHelperAutoExitSignalTimer_.stop(); |
| 61 | stopHelper(); |
| 62 | } |
| 63 | |
| 64 | void HelperControl::helperHealthCheckTimeout() |
| 65 | { |
| 66 | if (deferHelperAutoExitSignalTimer_.isActive() && !helperHasBeenStarted()) { |
| 67 | SPDLOG_WARN("The Helper has not been started. Starting it now."); |
| 68 | |
| 69 | deferHelperAutoExitSignalTimer_.stop(); |
| 70 | auto helperPublicKey = startHelper(); |
| 71 | |
| 72 | if (!helperPublicKey.has_value()) { |
| 73 | // NOTE If the helper died and cannot be started again, it's better to not |
| 74 | // crash the application throwing an exception. |
| 75 | SPDLOG_WARN("Cannot restart helper!"); |
| 76 | } |
| 77 | |
| 78 | cryptoLayer_->usePublicKey(helperPublicKey.value()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void HelperControl::helperExitDeferrerTimeout() |
| 83 | { |
| 84 | helperInterface_->asyncCall(QStringLiteral("delayAutoExit")); |
| 85 | } |
| 86 | |
| 87 | bool HelperControl::helperHasBeenStarted() const |
| 88 | { |
| 89 | QDBusReply<bool> reply = helperInterface_->call(QStringLiteral("started")); |
| 90 | return reply.isValid() && reply.value(); |
| 91 | } |
| 92 | |
| 93 | void HelperControl::createHelperInterface() |
| 94 | { |
| 95 | helperInterface_ = std::make_unique<QDBusInterface>( |
| 96 | QStringLiteral(DBUS_HELPER_SERVICE), QStringLiteral(DBUS_HELPER_PATH), |
| 97 | QStringLiteral(DBUS_HELPER_INTERFACE), QDBusConnection::systemBus()); |
| 98 | |
| 99 | if (!helperInterface_->isValid()) |
| 100 | throw std::runtime_error(std::format( |
| 101 | "Cannot connect to D-Bus interface {}: {}", DBUS_HELPER_INTERFACE, |
| 102 | helperInterface_->lastError().message().toStdString())); |
| 103 | } |
| 104 | |
| 105 | std::optional<QByteArray> HelperControl::startHelper() |
| 106 | { |
| 107 | QDBusReply<QDBusVariant> reply = helperInterface_->call( |
| 108 | QStringLiteral("start"), cryptoLayer_->publicKey(), |
| 109 | autoExitTimeout_.to<int>()); |
| 110 | |
| 111 | if (!(reply.isValid() && |
| 112 | reply.value().variant().typeId() == QMetaType::QByteArray)) |
| 113 | return std::nullopt; |
| 114 | |
| 115 | deferHelperAutoExitSignalTimer_.setInterval( |
| 116 | deferAutoExitSignalInterval_.to<int>()); |
| 117 | deferHelperAutoExitSignalTimer_.start(); |
| 118 | |
| 119 | return reply.value().variant().value<QByteArray>(); |
| 120 | } |
| 121 | |
| 122 | void HelperControl::stopHelper() |
| 123 | { |
| 124 | auto signature = cryptoLayer_->signature({"exit"}); |
| 125 | helperInterface_->asyncCall(QStringLiteral("exit"), signature); |
| 126 | } |
| 127 | |
| 128 | void HelperControl::killOtherHelperInstance() |
| 129 | { |
| 130 | if (helperHasBeenStarted()) { |
| 131 | SPDLOG_WARN("Helper instance detected. Killing it now."); |
| 132 | |
| 133 | if (!startHelperKiller() || helperHasBeenStarted()) |
| 134 | throw std::runtime_error("Failed to kill other helper instance"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool HelperControl::startHelperKiller() |
| 139 | { |
| 140 | QDBusInterface iface(QStringLiteral(DBUS_HELPER_KILLER_SERVICE), |
| 141 | QStringLiteral(DBUS_HELPER_KILLER_PATH), |
| 142 | QStringLiteral(DBUS_HELPER_KILLER_INTERFACE), |
| 143 | QDBusConnection::systemBus()); |
| 144 | if (!iface.isValid()) { |
| 145 | SPDLOG_DEBUG("Cannot connect to D-Bus interface {}: {}", |
| 146 | DBUS_HELPER_KILLER_INTERFACE, |
| 147 | iface.lastError().message().toStdString()); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | QDBusReply<bool> reply = iface.call(QStringLiteral("start")); |
| 152 | if (!reply.isValid()) { |
| 153 | SPDLOG_DEBUG("Helper killer error: {}", |
| 154 | iface.lastError().message().toStdString()); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | return reply.value(); |
| 159 | } |