2022-09-15 00:30:59 +01:00
|
|
|
#include "Process.hpp"
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string>
|
2022-09-15 20:18:26 +01:00
|
|
|
#include <map>
|
2022-09-15 00:30:59 +01:00
|
|
|
|
|
|
|
|
namespace Bloom
|
|
|
|
|
{
|
|
|
|
|
::pid_t Process::getProcessId() {
|
|
|
|
|
return getpid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::pid_t Process::getParentProcessId() {
|
|
|
|
|
return getppid();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) {
|
|
|
|
|
if (!parentProcessId.has_value()) {
|
|
|
|
|
parentProcessId = Process::getParentProcessId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static auto cachedResultsByProcessId = std::map<::pid_t, bool>();
|
2022-09-15 00:30:59 +01:00
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
if (cachedResultsByProcessId.contains(*parentProcessId)) {
|
|
|
|
|
return cachedResultsByProcessId.at(*parentProcessId);
|
2022-09-15 00:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Walk the process tree until we find CLion
|
2022-09-15 20:18:26 +01:00
|
|
|
auto processId = *parentProcessId;
|
2022-09-15 00:30:59 +01:00
|
|
|
while (processId != 0) {
|
|
|
|
|
const auto processInfo = Process::getProcessInfo(processId);
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
if (!processInfo) {
|
2022-09-15 00:30:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
const auto commandLine = std::string(processInfo->cmd);
|
2022-09-15 00:30:59 +01:00
|
|
|
if (commandLine.find("clion.sh") != std::string::npos) {
|
2022-09-15 20:18:26 +01:00
|
|
|
cachedResultsByProcessId[*parentProcessId] = true;
|
2022-09-15 00:30:59 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
processId = processInfo->ppid;
|
2022-09-15 00:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
cachedResultsByProcessId[*parentProcessId] = false;
|
2022-09-15 00:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 20:18:26 +01:00
|
|
|
Process::ProcT Process::getProcessInfo(::pid_t processId) {
|
|
|
|
|
auto proc = std::unique_ptr<::PROCTAB, decltype(&::closeproc)>(
|
|
|
|
|
::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId),
|
|
|
|
|
::closeproc
|
|
|
|
|
);
|
|
|
|
|
auto processInfo = ProcT(::readproc(proc.get(), NULL), ::freeproc);
|
2022-09-15 00:30:59 +01:00
|
|
|
|
|
|
|
|
if (processInfo == NULL) {
|
2022-09-15 20:18:26 +01:00
|
|
|
return ProcT(nullptr, ::freeproc);
|
2022-09-15 00:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|