Moved Process helper functions to service class

This commit is contained in:
Nav
2022-12-26 21:35:24 +00:00
parent d353b55f9b
commit 4c25c85c36
7 changed files with 60 additions and 24 deletions

View File

@@ -7,9 +7,10 @@
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <yaml-cpp/exceptions.h> #include <yaml-cpp/exceptions.h>
#include "src/Services/ProcessService.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "src/Helpers/Paths.hpp" #include "src/Helpers/Paths.hpp"
#include "src/Helpers/Process.hpp"
#include "src/Exceptions/InvalidConfig.hpp" #include "src/Exceptions/InvalidConfig.hpp"
@@ -515,7 +516,7 @@ namespace Bloom
} }
void Application::onDebugSessionFinished(const Events::DebugSessionFinished& event) { void Application::onDebugSessionFinished(const Events::DebugSessionFinished& event) {
if (this->environmentConfig->shutdownPostDebugSession || Process::isManagedByClion()) { if (this->environmentConfig->shutdownPostDebugSession || Services::ProcessService::isManagedByClion()) {
this->shutdown(); this->shutdown();
} }
} }

View File

@@ -6,6 +6,7 @@ target_sources(
# Services # Services
${CMAKE_CURRENT_SOURCE_DIR}/Services/TargetControllerService.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Services/TargetControllerService.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Services/ProcessService.cpp
# Helpers & other # Helpers & other
${CMAKE_CURRENT_SOURCE_DIR}/Logger/Logger.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Logger/Logger.cpp
@@ -14,7 +15,6 @@ 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

View File

@@ -3,7 +3,7 @@
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
#include "src/Helpers/Process.hpp" #include "src/Services/ProcessService.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
@@ -24,7 +24,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets
Logger::debug("Handling Detach packet"); Logger::debug("Handling Detach packet");
try { try {
if (Process::isManagedByClion()) { if (Services::ProcessService::isManagedByClion()) {
targetControllerService.suspendTargetController(); targetControllerService.suspendTargetController();
} }

View File

@@ -36,7 +36,8 @@
// Response packets // Response packets
#include "ResponsePackets/TargetStopped.hpp" #include "ResponsePackets/TargetStopped.hpp"
#include "src/Helpers/Process.hpp"
#include "src/Services/ProcessService.hpp"
namespace Bloom::DebugServer::Gdb namespace Bloom::DebugServer::Gdb
{ {
@@ -126,7 +127,7 @@ namespace Bloom::DebugServer::Gdb
std::bind(&GdbRspDebugServer::onTargetExecutionStopped, this, std::placeholders::_1) std::bind(&GdbRspDebugServer::onTargetExecutionStopped, this, std::placeholders::_1)
); );
if (Process::isManagedByClion()) { if (Services::ProcessService::isManagedByClion()) {
Logger::warning( Logger::warning(
"Bloom's process is being managed by CLion - Bloom will automatically shutdown upon detaching from GDB." "Bloom's process is being managed by CLion - Bloom will automatically shutdown upon detaching from GDB."
); );

View File

@@ -1,4 +1,4 @@
#include "Process.hpp" #include "ProcessService.hpp"
#include <unistd.h> #include <unistd.h>
#include <string> #include <string>
@@ -6,22 +6,22 @@
#include "src/Exceptions/Exception.hpp" #include "src/Exceptions/Exception.hpp"
namespace Bloom namespace Bloom::Services
{ {
::pid_t Process::getProcessId() { ::pid_t ProcessService::getProcessId() {
return getpid(); return getpid();
} }
::pid_t Process::getParentProcessId() { ::pid_t ProcessService::getParentProcessId() {
return getppid(); return getppid();
} }
::uid_t Process::getEffectiveUserId(std::optional<::pid_t> processId) { ::uid_t ProcessService::getEffectiveUserId(std::optional<::pid_t> processId) {
if (!processId.has_value()) { if (!processId.has_value()) {
processId = Process::getProcessId(); processId = ProcessService::getProcessId();
} }
const auto processInfo = Process::getProcessInfo(processId.value()); const auto processInfo = ProcessService::getProcessInfo(processId.value());
if (!processInfo) { if (!processInfo) {
throw Exceptions::Exception( throw Exceptions::Exception(
@@ -32,13 +32,13 @@ namespace Bloom
return static_cast<::uid_t>(processInfo->euid); return static_cast<::uid_t>(processInfo->euid);
} }
bool Process::isRunningAsRoot(std::optional<::pid_t> processId) { bool ProcessService::isRunningAsRoot(std::optional<::pid_t> processId) {
return Process::getEffectiveUserId(processId) == 0; return ProcessService::getEffectiveUserId(processId) == 0;
} }
bool Process::isManagedByClion(std::optional<::pid_t> processId) { bool ProcessService::isManagedByClion(std::optional<::pid_t> processId) {
if (!processId.has_value()) { if (!processId.has_value()) {
processId = Process::getProcessId(); processId = ProcessService::getProcessId();
} }
static auto cachedResultsByProcessId = std::map<::pid_t, bool>(); static auto cachedResultsByProcessId = std::map<::pid_t, bool>();
@@ -49,7 +49,7 @@ namespace Bloom
} }
// Start with the parent process and walk the tree until we find CLion // Start with the parent process and walk the tree until we find CLion
const auto processInfo = Process::getProcessInfo(*processId); const auto processInfo = ProcessService::getProcessInfo(*processId);
if (!processInfo) { if (!processInfo) {
cachedResultsByProcessId[*processId] = false; cachedResultsByProcessId[*processId] = false;
@@ -58,7 +58,7 @@ namespace Bloom
auto pid = processInfo->ppid; auto pid = processInfo->ppid;
while (const auto processInfo = Process::getProcessInfo(pid)) { while (const auto processInfo = ProcessService::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) {
@@ -73,7 +73,7 @@ namespace Bloom
return false; return false;
} }
Process::Proc Process::getProcessInfo(::pid_t processId) { ProcessService::Proc ProcessService::getProcessInfo(::pid_t processId) {
const auto proc = std::unique_ptr<::PROCTAB, decltype(&::closeproc)>( const auto proc = std::unique_ptr<::PROCTAB, decltype(&::closeproc)>(
::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId), ::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId),
::closeproc ::closeproc

View File

@@ -5,9 +5,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <proc/readproc.h> #include <proc/readproc.h>
namespace Bloom namespace Bloom::Services
{ {
class Process class ProcessService
{ {
public: public:
/** /**

View File

@@ -6,7 +6,7 @@
#include "Responses/Error.hpp" #include "Responses/Error.hpp"
#include "src/Helpers/Process.hpp" #include "src/Services/ProcessService.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "src/TargetController/Exceptions/DeviceFailure.hpp" #include "src/TargetController/Exceptions/DeviceFailure.hpp"
@@ -439,6 +439,40 @@ namespace Bloom::TargetController
TargetControllerComponent::responsesByCommandIdCv.notify_all(); TargetControllerComponent::responsesByCommandIdCv.notify_all();
} }
<<<<<<< HEAD
=======
void TargetControllerComponent::checkUdevRules() {
auto bloomRulesPath = std::string("/etc/udev/rules.d/99-bloom.rules");
auto latestBloomRulesPath = Paths::resourcesDirPath() + "/UDevRules/99-bloom.rules";
if (!std::filesystem::exists(bloomRulesPath)) {
Logger::warning("Bloom udev rules missing - attempting installation");
// We can only install them if we're running as root
if (!Services::ProcessService::isRunningAsRoot()) {
Logger::error("Bloom udev rules missing - cannot install udev rules without root privileges.\n"
"Running Bloom once with root privileges will allow it to automatically install the udev rules. "
"Alternatively, instructions on manually installing the udev rules can be found "
"here: " + Paths::homeDomainName() + "/docs/getting-started\nBloom may fail to connect to some "
"debug tools until this is resolved.");
return;
}
if (!std::filesystem::exists(latestBloomRulesPath)) {
// This shouldn't happen, but it can if someone has been messing with the installation files
Logger::error(
"Unable to install Bloom udev rules - \"" + latestBloomRulesPath + "\" does not exist."
);
return;
}
std::filesystem::copy(latestBloomRulesPath, bloomRulesPath);
Logger::warning("Bloom udev rules installed - a reconnect of the debug tool may be required "
"before the new udev rules come into effect.");
}
}
>>>>>>> b5402709 (Moved Process helper functions to service class)
void TargetControllerComponent::shutdown() { void TargetControllerComponent::shutdown() {
if (this->getThreadState() == ThreadState::STOPPED) { if (this->getThreadState() == ThreadState::STOPPED) {
return; return;