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;
}
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;
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;
}

View File

@@ -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)>;