New process helper class

This commit is contained in:
Nav
2022-09-15 00:30:59 +01:00
parent 98963ef4a8
commit 753cfd7c11
4 changed files with 97 additions and 0 deletions

View File

@@ -95,6 +95,7 @@ target_link_libraries(Bloom -lstdc++fs)
target_link_libraries(Bloom -lpthread) target_link_libraries(Bloom -lpthread)
target_link_libraries(Bloom -lusb-1.0) target_link_libraries(Bloom -lusb-1.0)
target_link_libraries(Bloom -lhidapi-libusb) target_link_libraries(Bloom -lhidapi-libusb)
target_link_libraries(Bloom -lprocps)
target_link_libraries(Bloom ${YAML_CPP_LIBRARIES}) target_link_libraries(Bloom ${YAML_CPP_LIBRARIES})
target_link_libraries(Bloom Qt6::Core) target_link_libraries(Bloom Qt6::Core)
target_link_libraries(Bloom Qt6::Gui) target_link_libraries(Bloom Qt6::Gui)

View File

@@ -11,6 +11,7 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EpollInstance.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EpollInstance.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EventFdNotifier.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EventFdNotifier.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Helpers/ConditionVariableNotifier.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/ConditionVariableNotifier.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Helpers/Process.cpp
${CMAKE_CURRENT_SOURCE_DIR}/VersionNumber.cpp ${CMAKE_CURRENT_SOURCE_DIR}/VersionNumber.cpp
# Project & application configuration # Project & application configuration

59
src/Helpers/Process.cpp Normal file
View File

@@ -0,0 +1,59 @@
#include "Process.hpp"
#include <unistd.h>
#include <string>
namespace Bloom
{
::pid_t Process::getProcessId() {
return getpid();
}
::pid_t Process::getParentProcessId() {
return getppid();
}
bool Process::isProcessManagedByClion(::pid_t processId) {
static auto cachedResult = std::optional<bool>();
if (cachedResult.has_value()) {
return cachedResult.value();
}
// Walk the process tree until we find CLion
while (processId != 0) {
const auto processInfo = Process::getProcessInfo(processId);
if (!processInfo.has_value()) {
break;
}
auto* processInfoPtr = processInfo.value();
const auto commandLine = std::string(processInfoPtr->cmd);
if (commandLine.find("clion.sh") != std::string::npos) {
freeproc(processInfoPtr);
cachedResult = true;
return true;
}
processId = processInfoPtr->ppid;
freeproc(processInfoPtr);
}
cachedResult = true;
return false;
}
std::optional<::proc_t*> Process::getProcessInfo(::pid_t processId) {
auto* proc = ::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId);
auto* processInfo = ::readproc(proc, NULL);
if (processInfo == NULL) {
return std::nullopt;
}
closeproc(proc);
return processInfo;
}
}

36
src/Helpers/Process.hpp Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include <optional>
#include <proc/readproc.h>
namespace Bloom
{
class Process
{
public:
/**
* Returns the process ID of the current process.
*
* @return
*/
static ::pid_t getProcessId();
/**
* Returns the process ID of the current process's parent.
*
* @return
*/
static ::pid_t getParentProcessId();
/**
* Returns true if the given process is managed by CLion.
*
* @param processId
* @return
*/
static bool isProcessManagedByClion(::pid_t processId);
private:
static std::optional<::proc_t*> getProcessInfo(::pid_t processId);
};
}