Skip to content

Commit

Permalink
fix: kill all gnupg daemons in a proper way
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Jan 27, 2025
1 parent bd0bfe6 commit af1870f
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 266 deletions.
2 changes: 2 additions & 0 deletions src/GpgFrontendContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct GpgFrontendContext {
bool gather_external_gnupg_info;
bool unit_test_mode;

int rtn = GpgFrontend::kCrashCode;

/**
* @brief Construct a new Gpg Frontend Context object
*
Expand Down
23 changes: 6 additions & 17 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@

#include "GpgFrontendContext.h"
#include "core/GpgConstants.h"
#include "core/GpgCoreInit.h"
#include "core/module/ModuleInit.h"
#include "core/function/CacheManager.h"
#include "ui/GpgFrontendUIInit.h"

// main
#include "init.h"

namespace GpgFrontend {

constexpr int kCrashCode = ~0; ///<

/**
* @brief
*
Expand Down Expand Up @@ -79,26 +76,18 @@ auto StartApplication(const GFCxtWPtr& p_ctx) -> int {
// load module's translations
GpgFrontend::UI::InitModulesTranslations();




// finally create main window
return_from_event_loop_code = GpgFrontend::UI::RunGpgFrontendUI(app);

} while (return_from_event_loop_code == GpgFrontend::kRestartCode &&
restart_count++ < 99);

// first should shutdown the module system
GpgFrontend::Module::ShutdownGpgFrontendModules();

// then shutdown the core
GpgFrontend::DestroyGpgFrontendCore();
// clear cache of unsaved pages
GpgFrontend::CacheManager::GetInstance().SaveDurableCache(
"editor_unsaved_pages", QJsonDocument(QJsonArray()), true);

// deep restart mode
if (return_from_event_loop_code == GpgFrontend::kDeepRestartCode ||
return_from_event_loop_code == kCrashCode) {
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
};
// set return code
ctx->rtn = return_from_event_loop_code;

// exit the program
return return_from_event_loop_code;
Expand Down
1 change: 1 addition & 0 deletions src/core/GpgConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace GpgFrontend {
constexpr int kNonRestartCode = 0;
constexpr int kRestartCode = 1000; ///< only refresh ui
constexpr int kDeepRestartCode = 1001; // refresh core and ui
constexpr int kCrashCode = ~0; ///< application crash

// Channels
constexpr int kGpgFrontendDefaultChannel = 0; ///<
Expand Down
68 changes: 47 additions & 21 deletions src/core/GpgCoreInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#include "core/function/gpg/GpgAdvancedOperator.h"
#include "core/function/gpg/GpgContext.h"
#include "core/function/gpg/GpgKeyGetter.h"
#include "core/model/SettingsObject.h"
#include "core/module/ModuleManager.h"
#include "core/struct/settings_object/KeyDatabaseListSO.h"
#include "core/thread/Task.h"
#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/CommonUtils.h"
Expand All @@ -47,7 +45,13 @@

namespace GpgFrontend {

void DestroyGpgFrontendCore() { SingletonStorageCollection::Destroy(); }
void DestroyGpgFrontendCore() {
// stop all task runner
Thread::TaskRunnerGetter::GetInstance().StopAllTeakRunner();

// destroy all singleton objects
SingletonStorageCollection::Destroy();
}

auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool {
return gnupg_install_fs_path.isAbsolute() && gnupg_install_fs_path.exists() &&
Expand Down Expand Up @@ -260,39 +264,43 @@ auto RefreshGpgMEBackendEngine(const QString& gpgconf_path,
return true;
}

auto GetGnuPGPathByGpgConf(const QString& gpgconf_install_fs_path) -> QString {
auto GetComponentPathsByGpgConf(const QString& gpgconf_install_fs_path)
-> bool {
auto* process = new QProcess(QCoreApplication::instance());
process->setProgram(gpgconf_install_fs_path);
process->start();
process->waitForFinished(1000);
auto output_buffer = process->readAllStandardOutput();
process->deleteLater();

if (output_buffer.isEmpty()) return {};
if (output_buffer.isEmpty()) return false;

auto line_split_list = QString(output_buffer).split("\n");
for (const auto& line : line_split_list) {
auto info_split_list = line.split(":");

if (info_split_list.size() != 3) continue;

auto component_name = info_split_list[0].trimmed();
auto component_name = info_split_list[0].trimmed().toLower();
auto component_desc = info_split_list[1].trimmed();
auto component_path = info_split_list[2].trimmed();

if (component_name.toLower() == "gpg") {
#if defined(_WIN32) || defined(WIN32)
// replace some special substrings on windows platform
component_path.replace("%3a", ":");
// replace some special substrings on windows platform
component_path.replace("%3a", ":");
#endif
QFileInfo file_info(component_path);
if (file_info.exists() && file_info.isFile()) {
return file_info.absoluteFilePath();
}
return {};
}

QFileInfo file_info(component_path);
if (!file_info.exists() || !file_info.isFile()) continue;

Module::UpsertRTValue("core", "gnupg.component.paths." + component_name,
file_info.absoluteFilePath());

LOG_D() << "gpg components: " << component_name
<< "path: " << file_info.absoluteFilePath();
}
return "";

return true;
}

auto DecideGpgConfPath(const QString& default_gpgconf_path) -> QString {
Expand Down Expand Up @@ -349,8 +357,15 @@ auto DecideGpgConfPath(const QString& default_gpgconf_path) -> QString {
return "";
}

auto DecideGnuPGPath(const QString& gpgconf_path) -> QString {
return GetGnuPGPathByGpgConf(gpgconf_path);
auto DecideGnuPGPath(const QString& default_gnupg_path) -> QString {
QFileInfo info(default_gnupg_path);

if (default_gnupg_path.isEmpty() || !info.exists() || !info.isFile()) {
return Module::RetrieveRTValueTypedOrDefault<>(
"core", "gnupg.component.paths.gpg", QString{});
}

return default_gnupg_path;
}

auto InitBasicPath() -> bool {
Expand All @@ -364,12 +379,23 @@ auto InitBasicPath() -> bool {
LOG_I() << "default gnupg path found by gpgme: " << default_gnupg_path;

auto target_gpgconf_path = DecideGpgConfPath(default_gpgconf_path);

if (!GetComponentPathsByGpgConf(default_gpgconf_path)) {
LOG_E() << "Cannot get components paths by gpgconf!"
<< "GpgFrontend cannot start under this situation!";
CoreSignalStation::GetInstance()->SignalBadGnupgEnv(
QCoreApplication::tr("Cannot get Infos from GpgConf"));
return false;
}

auto target_gnupg_path = default_gnupg_path;

if (target_gpgconf_path != default_gpgconf_path) {
target_gnupg_path = DecideGnuPGPath(target_gpgconf_path);
target_gnupg_path = DecideGnuPGPath(target_gnupg_path);
}

LOG_I() << "gpgconf path used: " << target_gpgconf_path;
LOG_I() << "gnupg path provided by gpgconf: " << target_gnupg_path;
LOG_I() << "gnupg path used: " << target_gnupg_path;

if (target_gpgconf_path.isEmpty()) {
LOG_E() << "Cannot find gpgconf!"
Expand Down Expand Up @@ -577,7 +603,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int {
->PostTask(task);

if (!args.unit_test_mode && restart_all_gnupg_components_on_start) {
GpgAdvancedOperator::RestartGpgComponents();
GpgAdvancedOperator::RestartGpgComponents(nullptr);
}
return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/function/GlobalSettingStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,7 @@ auto GlobalSettingStation::GetIntegratedModulePath() const -> QString {
auto GlobalSettingStation::IsProtableMode() const -> bool {
return p_->IsProtableMode();
}
auto GetSettings() -> QSettings {
return GlobalSettingStation::GetInstance().GetSettings();
}
} // namespace GpgFrontend
3 changes: 3 additions & 0 deletions src/core/function/GlobalSettingStation.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,7 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation
class Impl;
SecureUniquePtr<Impl> p_;
};

auto GPGFRONTEND_CORE_EXPORT GetSettings() -> QSettings;

} // namespace GpgFrontend
Loading

0 comments on commit af1870f

Please sign in to comment.