Tidied Process::isManagedByClion() routine

This commit is contained in:
Nav
2022-10-30 12:29:35 +00:00
parent c299f282ca
commit b6f3d57a04
2 changed files with 20 additions and 17 deletions

View File

@@ -36,36 +36,39 @@ namespace Bloom
return Process::getEffectiveUserId(processId) == 0; return Process::getEffectiveUserId(processId) == 0;
} }
bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) { bool Process::isManagedByClion(std::optional<::pid_t> processId) {
if (!parentProcessId.has_value()) { if (!processId.has_value()) {
parentProcessId = Process::getParentProcessId(); processId = Process::getProcessId();
} }
static auto cachedResultsByProcessId = std::map<::pid_t, bool>(); static auto cachedResultsByProcessId = std::map<::pid_t, bool>();
if (cachedResultsByProcessId.contains(*parentProcessId)) { if (cachedResultsByProcessId.contains(*processId)) {
return cachedResultsByProcessId.at(*parentProcessId); return cachedResultsByProcessId.at(*processId);
} }
// Walk the process tree until we find CLion // Start with the parent process and walk the tree until we find CLion
auto processId = *parentProcessId; const auto processInfo = Process::getProcessInfo(*processId);
while (processId != 0) {
const auto processInfo = Process::getProcessInfo(processId);
if (!processInfo) { if (!processInfo) {
break; cachedResultsByProcessId[*processId] = false;
return false;
} }
auto pid = processInfo->ppid;
while (const auto processInfo = Process::getProcessInfo(pid)) {
const auto commandLine = std::string(processInfo->cmd); const auto commandLine = std::string(processInfo->cmd);
if (commandLine.find("clion.sh") != std::string::npos) { if (commandLine.find("clion.sh") != std::string::npos) {
cachedResultsByProcessId[*parentProcessId] = true; cachedResultsByProcessId[*processId] = true;
return true; return true;
} }
processId = processInfo->ppid; pid = processInfo->ppid;
} }
cachedResultsByProcessId[*parentProcessId] = false; cachedResultsByProcessId[*processId] = false;
return false; return false;
} }

View File

@@ -47,12 +47,12 @@ namespace Bloom
/** /**
* Returns true if the given process is managed by CLion. * 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. * If not provided, this function will perform the check against the current process.
* *
* @return * @return
*/ */
static bool isManagedByClion(std::optional<::pid_t> parentProcessId = std::nullopt); static bool isManagedByClion(std::optional<::pid_t> processId = std::nullopt);
private: private:
using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>; using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>;