Tidied Process class and moved Application::isRunningAsRoot() function to Process class

This commit is contained in:
Nav
2022-10-05 20:58:25 +01:00
parent f5d75f2ea6
commit 51a3d2fbbb
5 changed files with 49 additions and 21 deletions

View File

@@ -87,10 +87,6 @@ namespace Bloom
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool Application::isRunningAsRoot() {
return geteuid() == 0;
}
std::map<std::string, std::function<int()>> Application::getCommandHandlersByCommandName() { std::map<std::string, std::function<int()>> Application::getCommandHandlersByCommandName() {
return std::map<std::string, std::function<int()>> { return std::map<std::string, std::function<int()>> {
{ {

View File

@@ -44,13 +44,6 @@ namespace Bloom
*/ */
int run(); int run();
/**
* Checks if the current effective user running Bloom has root privileges.
*
* @return
*/
static bool isRunningAsRoot();
private: private:
std::vector<std::string> arguments; std::vector<std::string> arguments;

View File

@@ -4,6 +4,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include "src/Exceptions/Exception.hpp"
namespace Bloom namespace Bloom
{ {
::pid_t Process::getProcessId() { ::pid_t Process::getProcessId() {
@@ -14,6 +16,26 @@ namespace Bloom
return getppid(); return getppid();
} }
::uid_t Process::getEffectiveUserId(std::optional<::pid_t> processId) {
if (!processId.has_value()) {
processId = Process::getProcessId();
}
const auto processInfo = Process::getProcessInfo(processId.value());
if (!processInfo) {
throw Exceptions::Exception(
"Failed to fetch process info for process ID " + std::to_string(processId.value())
);
}
return static_cast<::uid_t>(processInfo->euid);
}
bool Process::isRunningAsRoot(std::optional<::pid_t> processId) {
return Process::getEffectiveUserId(processId) == 0;
}
bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) { bool Process::isManagedByClion(std::optional<::pid_t> parentProcessId) {
if (!parentProcessId.has_value()) { if (!parentProcessId.has_value()) {
parentProcessId = Process::getParentProcessId(); parentProcessId = Process::getParentProcessId();
@@ -48,16 +70,11 @@ namespace Bloom
} }
Process::Proc Process::getProcessInfo(::pid_t processId) { Process::Proc Process::getProcessInfo(::pid_t processId) {
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
); );
auto processInfo = Proc(::readproc(proc.get(), NULL), ::freeproc);
if (processInfo == NULL) { return Proc(::readproc(proc.get(), NULL), ::freeproc);
return Proc(nullptr, ::freeproc);
}
return processInfo;
} }
} }

View File

@@ -2,6 +2,7 @@
#include <optional> #include <optional>
#include <memory> #include <memory>
#include <sys/types.h>
#include <proc/readproc.h> #include <proc/readproc.h>
namespace Bloom namespace Bloom
@@ -23,6 +24,26 @@ namespace Bloom
*/ */
static ::pid_t getParentProcessId(); static ::pid_t getParentProcessId();
/**
* Returns the effective user ID of the given process.
*
* @param processId
* If not provided, this function will use the current process ID.
*
* @return
*/
static ::uid_t getEffectiveUserId(std::optional<::pid_t> processId = std::nullopt);
/**
* Returns true if the given process is running as root.
*
* @param processId
* If not provided, this function will perform the check against the current process.
*
* @return
*/
static bool isRunningAsRoot(std::optional<::pid_t> processId = std::nullopt);
/** /**
* Returns true if the given process is managed by CLion. * Returns true if the given process is managed by CLion.
* *
@@ -33,7 +54,6 @@ namespace Bloom
*/ */
static bool isManagedByClion(std::optional<::pid_t> parentProcessId = std::nullopt); static bool isManagedByClion(std::optional<::pid_t> parentProcessId = std::nullopt);
private: private:
using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>; using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>;
static Proc getProcessInfo(::pid_t processId); static Proc getProcessInfo(::pid_t processId);

View File

@@ -5,8 +5,10 @@
#include <typeindex> #include <typeindex>
#include <algorithm> #include <algorithm>
#include "src/Application.hpp" #include "Responses/Error.hpp"
#include "src/Helpers/Paths.hpp" #include "src/Helpers/Paths.hpp"
#include "src/Helpers/Process.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "src/TargetController/Exceptions/DeviceFailure.hpp" #include "src/TargetController/Exceptions/DeviceFailure.hpp"
@@ -446,7 +448,7 @@ namespace Bloom::TargetController
Logger::warning("Bloom udev rules missing - attempting installation"); Logger::warning("Bloom udev rules missing - attempting installation");
// We can only install them if we're running as root // We can only install them if we're running as root
if (!Application::isRunningAsRoot()) { if (!Process::isRunningAsRoot()) {
Logger::error("Bloom udev rules missing - cannot install udev rules without root privileges.\n" 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. " "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 " "Alternatively, instructions on manually installing the udev rules can be found "