From 4c25c85c363f95438236ccfdc2f85c68dc83ce5f Mon Sep 17 00:00:00 2001 From: Nav Date: Mon, 26 Dec 2022 21:35:24 +0000 Subject: [PATCH] Moved Process helper functions to service class --- src/Application.cpp | 5 +-- src/CMakeLists.txt | 2 +- src/DebugServer/Gdb/CommandPackets/Detach.cpp | 4 +-- src/DebugServer/Gdb/GdbRspDebugServer.cpp | 5 +-- .../ProcessService.cpp} | 28 +++++++-------- .../ProcessService.hpp} | 4 +-- .../TargetControllerComponent.cpp | 36 ++++++++++++++++++- 7 files changed, 60 insertions(+), 24 deletions(-) rename src/{Helpers/Process.cpp => Services/ProcessService.cpp} (65%) rename src/{Helpers/Process.hpp => Services/ProcessService.hpp} (96%) diff --git a/src/Application.cpp b/src/Application.cpp index ba1905e5..74ad513c 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -7,9 +7,10 @@ #include #include +#include "src/Services/ProcessService.hpp" + #include "src/Logger/Logger.hpp" #include "src/Helpers/Paths.hpp" -#include "src/Helpers/Process.hpp" #include "src/Exceptions/InvalidConfig.hpp" @@ -515,7 +516,7 @@ namespace Bloom } void Application::onDebugSessionFinished(const Events::DebugSessionFinished& event) { - if (this->environmentConfig->shutdownPostDebugSession || Process::isManagedByClion()) { + if (this->environmentConfig->shutdownPostDebugSession || Services::ProcessService::isManagedByClion()) { this->shutdown(); } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 16849611..a299bb78 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources( # Services ${CMAKE_CURRENT_SOURCE_DIR}/Services/TargetControllerService.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Services/ProcessService.cpp # Helpers & other ${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/EventFdNotifier.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/ConditionVariableNotifier.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/Process.cpp ${CMAKE_CURRENT_SOURCE_DIR}/VersionNumber.cpp # Project & application configuration diff --git a/src/DebugServer/Gdb/CommandPackets/Detach.cpp b/src/DebugServer/Gdb/CommandPackets/Detach.cpp index 41d112d5..e2177381 100644 --- a/src/DebugServer/Gdb/CommandPackets/Detach.cpp +++ b/src/DebugServer/Gdb/CommandPackets/Detach.cpp @@ -3,7 +3,7 @@ #include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" -#include "src/Helpers/Process.hpp" +#include "src/Services/ProcessService.hpp" #include "src/Logger/Logger.hpp" @@ -24,7 +24,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets Logger::debug("Handling Detach packet"); try { - if (Process::isManagedByClion()) { + if (Services::ProcessService::isManagedByClion()) { targetControllerService.suspendTargetController(); } diff --git a/src/DebugServer/Gdb/GdbRspDebugServer.cpp b/src/DebugServer/Gdb/GdbRspDebugServer.cpp index d4d2e294..234c3493 100644 --- a/src/DebugServer/Gdb/GdbRspDebugServer.cpp +++ b/src/DebugServer/Gdb/GdbRspDebugServer.cpp @@ -36,7 +36,8 @@ // Response packets #include "ResponsePackets/TargetStopped.hpp" -#include "src/Helpers/Process.hpp" + +#include "src/Services/ProcessService.hpp" namespace Bloom::DebugServer::Gdb { @@ -126,7 +127,7 @@ namespace Bloom::DebugServer::Gdb std::bind(&GdbRspDebugServer::onTargetExecutionStopped, this, std::placeholders::_1) ); - if (Process::isManagedByClion()) { + if (Services::ProcessService::isManagedByClion()) { Logger::warning( "Bloom's process is being managed by CLion - Bloom will automatically shutdown upon detaching from GDB." ); diff --git a/src/Helpers/Process.cpp b/src/Services/ProcessService.cpp similarity index 65% rename from src/Helpers/Process.cpp rename to src/Services/ProcessService.cpp index 23020363..c660804b 100644 --- a/src/Helpers/Process.cpp +++ b/src/Services/ProcessService.cpp @@ -1,4 +1,4 @@ -#include "Process.hpp" +#include "ProcessService.hpp" #include #include @@ -6,22 +6,22 @@ #include "src/Exceptions/Exception.hpp" -namespace Bloom +namespace Bloom::Services { - ::pid_t Process::getProcessId() { + ::pid_t ProcessService::getProcessId() { return getpid(); } - ::pid_t Process::getParentProcessId() { + ::pid_t ProcessService::getParentProcessId() { return getppid(); } - ::uid_t Process::getEffectiveUserId(std::optional<::pid_t> processId) { + ::uid_t ProcessService::getEffectiveUserId(std::optional<::pid_t> processId) { 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) { throw Exceptions::Exception( @@ -32,13 +32,13 @@ namespace Bloom return static_cast<::uid_t>(processInfo->euid); } - bool Process::isRunningAsRoot(std::optional<::pid_t> processId) { - return Process::getEffectiveUserId(processId) == 0; + bool ProcessService::isRunningAsRoot(std::optional<::pid_t> processId) { + 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()) { - processId = Process::getProcessId(); + processId = ProcessService::getProcessId(); } 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 - const auto processInfo = Process::getProcessInfo(*processId); + const auto processInfo = ProcessService::getProcessInfo(*processId); if (!processInfo) { cachedResultsByProcessId[*processId] = false; @@ -58,7 +58,7 @@ namespace Bloom 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); if (commandLine.find("clion.sh") != std::string::npos) { @@ -73,7 +73,7 @@ namespace Bloom 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)>( ::openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, &processId), ::closeproc diff --git a/src/Helpers/Process.hpp b/src/Services/ProcessService.hpp similarity index 96% rename from src/Helpers/Process.hpp rename to src/Services/ProcessService.hpp index dd19b113..9fec38de 100644 --- a/src/Helpers/Process.hpp +++ b/src/Services/ProcessService.hpp @@ -5,9 +5,9 @@ #include #include -namespace Bloom +namespace Bloom::Services { - class Process + class ProcessService { public: /** diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 937fcce3..bd817ffb 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -6,7 +6,7 @@ #include "Responses/Error.hpp" -#include "src/Helpers/Process.hpp" +#include "src/Services/ProcessService.hpp" #include "src/Logger/Logger.hpp" #include "src/TargetController/Exceptions/DeviceFailure.hpp" @@ -439,6 +439,40 @@ namespace Bloom::TargetController 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() { if (this->getThreadState() == ThreadState::STOPPED) { return;