| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/helper/helpercontrol.cpp`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "helpercontrol.h"cxx . . $ `<file>/src/helper/helpercontrol.h`/ |
| 5 | |
| 6 | #include "common/icryptolayer.h"cxx . . $ `<file>/src/common/icryptolayer.h`/ |
| 7 | #include "helperids.h"cxx . . $ `<file>/src/helper/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 | HelperControlcxx . . $ HelperControl#::HelperControlcxx . . $ HelperControl#HelperControl(fa3ab6c77c92806e).(stdcxx . . $ std/::shared_ptrcxx . . $ std/shared_ptr#<ICryptoLayercxx . . $ ICryptoLayer#> cryptoLayer, |
| 23 | QObjectcxx . . $ QObject# *parent) noexcept |
| 24 | : QObjectcxx . . $ QObject#QObject(749de1b6ae65bddd).(parent) |
| 25 | , cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(cryptoLayer)) |
| 26 | , autoExitTimeout_cxx . . $ HelperControl#autoExitTimeout_.(IHelperControlcxx . . $ IHelperControl#::MinExitTimeoutcxx . . $ IHelperControl#MinExitTimeout#::valuecxx . . $ units/unit_value_t#value(ca235afb30d3d25b).()) |
| 27 | , deferAutoExitSignalInterval_cxx . . $ HelperControl#deferAutoExitSignalInterval_.(IHelperControlcxx . . $ IHelperControl#::MinExitTimeoutcxx . . $ IHelperControl#MinExitTimeout#::valuecxx . . $ units/unit_value_t#value(ca235afb30d3d25b).() *cxx . . $ units/`operator*`(2b6c1f0c52efed). .667) |
| 28 | { |
| 29 | connectcxx . . $ QObject#connect(dad3a9587f3dce3c).(&deferHelperHealthCheckTimer_cxx . . $ HelperControl#deferHelperHealthCheckTimer_., &QTimercxx . . $ QTimer#::timeoutcxx . . $ QTimer#timeout(32604d697c0ceadc)., this, |
| 30 | &HelperControlcxx . . $ HelperControl#::helperHealthCheckTimeoutcxx . . $ HelperControl#helperHealthCheckTimeout(49f6e7a06ebc5aa8).); |
| 31 | connectcxx . . $ QObject#connect(dad3a9587f3dce3c).(&deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_., &QTimercxx . . $ QTimer#::timeoutcxx . . $ QTimer#timeout(32604d697c0ceadc)., this, |
| 32 | &HelperControlcxx . . $ HelperControl#::helperExitDeferrerTimeoutcxx . . $ HelperControl#helperExitDeferrerTimeout(49f6e7a06ebc5aa8).); |
| 33 | } |
| 34 | |
| 35 | void HelperControlcxx . . $ HelperControl#::initcxx . . $ HelperControl#init(9920060bc6d69dd4).(unitscxx . . $ units/::timecxx . . $ units/time/::millisecond_tcxx . . $ units/time/millisecond_t# autoExitTimeout) |
| 36 | { |
| 37 | autoExitTimeout_cxx . . $ HelperControl#autoExitTimeout_. =cxx . . $ units/unit_t#`operator=`(4868c006e9872056). stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(autoExitTimeout, |
| 38 | IHelperControlcxx . . $ IHelperControl#::MinExitTimeoutcxx . . $ IHelperControl#MinExitTimeout#::valuecxx . . $ units/unit_value_t#value(ca235afb30d3d25b).()); |
| 39 | deferAutoExitSignalInterval_cxx . . $ HelperControl#deferAutoExitSignalInterval_. =cxx . . $ units/unit_t#`operator=`(ed7ebe278f64a19). autoExitTimeout *cxx . . $ units/`operator*`(2b6c1f0c52efed). .667; |
| 40 | |
| 41 | cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).initcxx . . $ ICryptoLayer#init(49f6e7a06ebc5aa8).(); |
| 42 | createHelperInterfacecxx . . $ HelperControl#createHelperInterface(49f6e7a06ebc5aa8).(); |
| 43 | |
| 44 | killOtherHelperInstancecxx . . $ HelperControl#killOtherHelperInstance(49f6e7a06ebc5aa8).(); |
| 45 | |
| 46 | auto helperPublicKey = startHelpercxx . . $ HelperControl#startHelper(d93fc752717fb136).(); |
| 47 | if (!helperPublicKey.has_valuecxx . . $ std/optional#has_value(3482b152b9333168).()) |
| 48 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Cannot start helper"); |
| 49 | |
| 50 | cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).usePublicKeycxx . . $ ICryptoLayer#usePublicKey(45672f3c6961a244).(helperPublicKey.valuecxx . . $ std/optional#value(db8caaf2f996d67e).()); |
| 51 | |
| 52 | // Check the helper health every 15 seconds. |
| 53 | deferHelperHealthCheckTimer_cxx . . $ HelperControl#deferHelperHealthCheckTimer_..setIntervalcxx . . $ QTimer#setInterval(d4f767463ce0a6b3).(15000); |
| 54 | deferHelperHealthCheckTimer_cxx . . $ HelperControl#deferHelperHealthCheckTimer_..startcxx . . $ QTimer#start(49f6e7a06ebc5aa8).(); |
| 55 | } |
| 56 | |
| 57 | void HelperControlcxx . . $ HelperControl#::stopcxx . . $ HelperControl#stop(49f6e7a06ebc5aa8).() |
| 58 | { |
| 59 | deferHelperHealthCheckTimer_cxx . . $ HelperControl#deferHelperHealthCheckTimer_..stopcxx . . $ QTimer#stop(49f6e7a06ebc5aa8).(); |
| 60 | deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_..stopcxx . . $ QTimer#stop(49f6e7a06ebc5aa8).(); |
| 61 | stopHelpercxx . . $ HelperControl#stopHelper(49f6e7a06ebc5aa8).(); |
| 62 | } |
| 63 | |
| 64 | void HelperControlcxx . . $ HelperControl#::helperHealthCheckTimeoutcxx . . $ HelperControl#helperHealthCheckTimeout(49f6e7a06ebc5aa8).() |
| 65 | { |
| 66 | if (deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_..isActivecxx . . $ QTimer#isActive(50ce9a9e25b4a850).() && !helperHasBeenStartedcxx . . $ HelperControl#helperHasBeenStarted(50ce9a9e25b4a850).()) { |
| 67 | SPDLOG_WARNcxx . . $ `<external>/13613935168682455490/spdlog.h:329:13`!("The Helper has not been started. Starting it now."); |
| 68 | |
| 69 | deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_..stopcxx . . $ QTimer#stop(49f6e7a06ebc5aa8).(); |
| 70 | auto helperPublicKey = startHelpercxx . . $ HelperControl#startHelper(d93fc752717fb136).(); |
| 71 | |
| 72 | if (!helperPublicKey.has_valuecxx . . $ std/optional#has_value(3482b152b9333168).()) { |
| 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_WARNcxx . . $ `<external>/13613935168682455490/spdlog.h:329:13`!("Cannot restart helper!"); |
| 76 | } |
| 77 | |
| 78 | cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).usePublicKeycxx . . $ ICryptoLayer#usePublicKey(45672f3c6961a244).(helperPublicKey.valuecxx . . $ std/optional#value(db8caaf2f996d67e).()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void HelperControlcxx . . $ HelperControl#::helperExitDeferrerTimeoutcxx . . $ HelperControl#helperExitDeferrerTimeout(49f6e7a06ebc5aa8).() |
| 83 | { |
| 84 | helperInterface_cxx . . $ HelperControl#helperInterface_.->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).asyncCallcxx . . $ QDBusAbstractInterface#asyncCall(942db5d47cd93614).(QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!("delayAutoExit")); |
| 85 | } |
| 86 | |
| 87 | bool HelperControlcxx . . $ HelperControl#::helperHasBeenStartedcxx . . $ HelperControl#helperHasBeenStarted(50ce9a9e25b4a850).() const |
| 88 | { |
| 89 | QDBusReplycxx . . $ QDBusReply#<bool> reply = helperInterface_cxx . . $ QDBusReply#QDBusReply(65337ba3304bf734).->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).callcxx . . $ QDBusAbstractInterface#call(ac112feac6c1e626).(QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!("started")); |
| 90 | return reply.isValidcxx . . $ QDBusReply#isValid(50ce9a9e25b4a850).() && reply.valuecxx . . $ QDBusReply#value(8f5363d873dd4781).(); |
| 91 | } |
| 92 | |
| 93 | void HelperControlcxx . . $ HelperControl#::createHelperInterfacecxx . . $ HelperControl#createHelperInterface(49f6e7a06ebc5aa8).() |
| 94 | { |
| 95 | helperInterface_cxx . . $ HelperControl#helperInterface_. =cxx . . $ std/unique_ptr#`operator=`(1b63e42eaa758a73). stdcxx . . $ std/::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<QDBusInterfacecxx . . $ QDBusInterface#>( |
| 96 | QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_SERVICEcxx . . $ `src/helper/helperids.h:13:9`!), QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_PATHcxx . . $ `src/helper/helperids.h:15:9`!), |
| 97 | QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_INTERFACEcxx . . $ `src/helper/helperids.h:14:9`!), QDBusConnectioncxx . . $ QDBusConnection#::systemBuscxx . . $ QDBusConnection#systemBus(45d475f1888103b2).()); |
| 98 | |
| 99 | if (!helperInterface_cxx . . $ HelperControl#helperInterface_.->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).isValidcxx . . $ QDBusAbstractInterface#isValid(50ce9a9e25b4a850).()) |
| 100 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(stdcxx . . $ std/::formatcxx . . $ std/format(7a9e6f4918cdc192).( |
| 101 | "Cannot connect to D-Bus interface {}: {}"cxx . . $ std/basic_format_string#basic_format_string(edcd8b2679e9bd1a)., DBUS_HELPER_INTERFACEcxx . . $ `src/helper/helperids.h:14:9`!, |
| 102 | helperInterface_cxx . . $ HelperControl#helperInterface_.->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).lastErrorcxx . . $ QDBusAbstractInterface#lastError(1d9100e4f448fa26).().messagecxx . . $ QDBusError#message(6ef7b6c27b00b43d).().toStdStringcxx . . $ QString#toStdString(219584694fe2b248).())); |
| 103 | } |
| 104 | |
| 105 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<QByteArraycxx . . $ QByteArray#> HelperControlcxx . . $ HelperControl#::startHelpercxx . . $ HelperControl#startHelper(d93fc752717fb136).() |
| 106 | { |
| 107 | QDBusReplycxx . . $ QDBusReply#<QDBusVariantcxx . . $ QDBusVariant#> reply = helperInterface_cxx . . $ QDBusReply#QDBusReply(65337ba3304bf734).->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).callcxx . . $ QDBusAbstractInterface#call(47baaf22534148ef).( |
| 108 | QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!("start"), cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).publicKeycxx . . $ ICryptoLayer#publicKey(b3c286cec66e09f4).(), |
| 109 | autoExitTimeout_cxx . . $ HelperControl#autoExitTimeout_..tocxx . . $ units/unit_t#to(88b827abdede45d2).<int>()); |
| 110 | |
| 111 | if (!(reply.isValidcxx . . $ QDBusReply#isValid(50ce9a9e25b4a850).() && |
| 112 | reply.valuecxx . . $ QDBusReply#value(8f5363d873dd4781).().variantcxx . . $ QDBusVariant#variant(23280f95d1ccaa1f).().typeIdcxx . . $ QVariant#typeId(455f465bc33b4cdf).() == QMetaTypecxx . . $ QMetaType#::QByteArraycxx . . $ QMetaType#QByteArray.)) |
| 113 | return stdcxx . . $ std/optional#optional(eb2253fdec8870b5).::nulloptcxx . . $ std/nullopt.; |
| 114 | |
| 115 | deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_..setIntervalcxx . . $ QTimer#setInterval(d4f767463ce0a6b3).( |
| 116 | deferAutoExitSignalInterval_cxx . . $ HelperControl#deferAutoExitSignalInterval_..tocxx . . $ units/unit_t#to(88b827abdede45d2).<int>()); |
| 117 | deferHelperAutoExitSignalTimer_cxx . . $ HelperControl#deferHelperAutoExitSignalTimer_..startcxx . . $ QTimer#start(49f6e7a06ebc5aa8).(); |
| 118 | |
| 119 | return replycxx . . $ std/optional#optional(e3f0d6382bd52f24)..valuecxx . . $ QDBusReply#value(8f5363d873dd4781).().variantcxx . . $ QDBusVariant#variant(23280f95d1ccaa1f).().valuecxx . . $ QVariant#value(1e6269539e51e84a).<QByteArraycxx . . $ QByteArray#>(); |
| 120 | } |
| 121 | |
| 122 | void HelperControlcxx . . $ HelperControl#::stopHelpercxx . . $ HelperControl#stopHelper(49f6e7a06ebc5aa8).() |
| 123 | { |
| 124 | auto signature = cryptoLayer_cxx . . $ HelperControl#cryptoLayer_.->cxx . . $ std/__shared_ptr_access#`operator->`(34d6acfd341ba7ab).signaturecxx . . $ ICryptoLayer#signature(ba1ae99d10185369).({cxx . . $ QByteArray#QByteArray(de40a1f2d6082f12)."exit"}); |
| 125 | helperInterface_cxx . . $ HelperControl#helperInterface_.->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).asyncCallcxx . . $ QDBusAbstractInterface#asyncCall(1ecb6b7bdc99c898).(QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!("exit"), signature); |
| 126 | } |
| 127 | |
| 128 | void HelperControlcxx . . $ HelperControl#::killOtherHelperInstancecxx . . $ HelperControl#killOtherHelperInstance(49f6e7a06ebc5aa8).() |
| 129 | { |
| 130 | if (helperHasBeenStartedcxx . . $ HelperControl#helperHasBeenStarted(50ce9a9e25b4a850).()) { |
| 131 | SPDLOG_WARNcxx . . $ `<external>/13613935168682455490/spdlog.h:329:13`!("Helper instance detected. Killing it now."); |
| 132 | |
| 133 | if (!startHelperKillercxx . . $ HelperControl#startHelperKiller(c9857a73d68e59ba).() || helperHasBeenStartedcxx . . $ HelperControl#helperHasBeenStarted(50ce9a9e25b4a850).()) |
| 134 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to kill other helper instance"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool HelperControlcxx . . $ HelperControl#::startHelperKillercxx . . $ HelperControl#startHelperKiller(c9857a73d68e59ba).() |
| 139 | { |
| 140 | QDBusInterfacecxx . . $ QDBusInterface# ifacecxx . . $ QDBusInterface#QDBusInterface(aded780112dba5f5).(QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_KILLER_SERVICEcxx . . $ `src/helper/helperids.h:8:9`!), |
| 141 | QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_KILLER_PATHcxx . . $ `src/helper/helperids.h:10:9`!), |
| 142 | QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!(DBUS_HELPER_KILLER_INTERFACEcxx . . $ `src/helper/helperids.h:9:9`!), |
| 143 | QDBusConnectioncxx . . $ QDBusConnection#::systemBuscxx . . $ QDBusConnection#systemBus(45d475f1888103b2).()); |
| 144 | if (!iface.isValidcxx . . $ QDBusAbstractInterface#isValid(50ce9a9e25b4a850).()) { |
| 145 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot connect to D-Bus interface {}: {}", |
| 146 | DBUS_HELPER_KILLER_INTERFACEcxx . . $ `src/helper/helperids.h:9:9`!, |
| 147 | iface.lastError().message().toStdString()); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | QDBusReplycxx . . $ QDBusReply#<bool> reply = ifacecxx . . $ QDBusReply#QDBusReply(65337ba3304bf734)..callcxx . . $ QDBusAbstractInterface#call(ac112feac6c1e626).(QStringLiteralcxx . . $ `<external>/13954984326995544643/qstring.h:1789:9`!("start")); |
| 152 | if (!reply.isValidcxx . . $ QDBusReply#isValid(50ce9a9e25b4a850).()) { |
| 153 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Helper killer error: {}", |
| 154 | iface.lastError().message().toStdString()); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | return reply.valuecxx . . $ QDBusReply#value(8f5363d873dd4781).(); |
| 159 | } |