From b6f3d57a04f0dbc0014ad8c980416b9abed16c9a Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 30 Oct 2022 12:29:35 +0000 Subject: [PATCH] Tidied Process::isManagedByClion() routine --- src/Helpers/Process.cpp | 33 ++++++++++++++++++--------------- src/Helpers/Process.hpp | 4 ++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Helpers/Process.cpp b/src/Helpers/Process.cpp index 8c342c3d..dd1453b0 100644 --- a/src/Helpers/Process.cpp +++ b/src/Helpers/Process.cpp @@ -36,36 +36,39 @@ namespace Bloom return Process::getEffectiveUserId(processId) == 0; } - bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) { - if (!parentProcessId.has_value()) { - parentProcessId = Process::getParentProcessId(); + bool Process::isManagedByClion(std::optional<::pid_t> processId) { + if (!processId.has_value()) { + processId = Process::getProcessId(); } static auto cachedResultsByProcessId = std::map<::pid_t, bool>(); - if (cachedResultsByProcessId.contains(*parentProcessId)) { - return cachedResultsByProcessId.at(*parentProcessId); + if (cachedResultsByProcessId.contains(*processId)) { + return cachedResultsByProcessId.at(*processId); } - // Walk the process tree until we find CLion - auto processId = *parentProcessId; - while (processId != 0) { - const auto processInfo = Process::getProcessInfo(processId); + // Start with the parent process and walk the tree until we find CLion + const auto processInfo = Process::getProcessInfo(*processId); - if (!processInfo) { - break; - } + if (!processInfo) { + cachedResultsByProcessId[*processId] = false; + return false; + } + auto pid = processInfo->ppid; + + while (const auto processInfo = Process::getProcessInfo(pid)) { const auto commandLine = std::string(processInfo->cmd); + if (commandLine.find("clion.sh") != std::string::npos) { - cachedResultsByProcessId[*parentProcessId] = true; + cachedResultsByProcessId[*processId] = true; return true; } - processId = processInfo->ppid; + pid = processInfo->ppid; } - cachedResultsByProcessId[*parentProcessId] = false; + cachedResultsByProcessId[*processId] = false; return false; } diff --git a/src/Helpers/Process.hpp b/src/Helpers/Process.hpp index e432ea55..dd19b113 100644 --- a/src/Helpers/Process.hpp +++ b/src/Helpers/Process.hpp @@ -47,12 +47,12 @@ namespace Bloom /** * Returns true if the given process is managed by CLion. * - * @param parentProcessId + * @param processId * If not provided, this function will perform the check against the current process. * * @return */ - static bool isManagedByClion(std::optional<::pid_t> parentProcessId = std::nullopt); + static bool isManagedByClion(std::optional<::pid_t> processId = std::nullopt); private: using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>;