Cleaning up initial commit and restructured some of the execution flow, fixed returns and reference mgmt

This commit is contained in:
Chris Punches
2025-02-23 03:26:49 -05:00
parent 1ce163ef29
commit 932c40275f
7 changed files with 292 additions and 105 deletions

View File

@@ -1,8 +1,4 @@
#include "ModuleLoader.hpp"
#include "dpm_interface.hpp"
#include <filesystem>
#include <dlfcn.h>
#include <iostream>
namespace fs = std::filesystem;
@@ -13,18 +9,37 @@ ModuleLoader::ModuleLoader(std::string module_path) : module_path_(std::move(mod
}
}
DPMError ModuleLoader::check_module_path() const
DPMError ModuleLoader::get_module_path(std::string& path) const
{
if (!fs::exists(module_path_)) {
path = module_path_;
return DPMError::SUCCESS;
}
DPMError ModuleLoader::get_absolute_module_path(std::string& abs_path) const
{
try {
abs_path = fs::absolute(module_path_).string();
return DPMError::SUCCESS;
} catch (const fs::filesystem_error&) {
abs_path = module_path_;
return DPMError::PATH_NOT_FOUND;
}
}
if (!fs::is_directory(module_path_)) {
return DPMError::PATH_NOT_DIRECTORY;
}
DPMError ModuleLoader::list_available_modules(std::vector<std::string>& modules) const
{
modules.clear();
try {
fs::directory_iterator(module_path_);
fs::path absolute_path = fs::absolute(module_path_);
for (const auto& entry : fs::directory_iterator(absolute_path)) {
if (entry.is_regular_file()) {
std::string filename = entry.path().filename().string();
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") {
modules.push_back(filename.substr(0, filename.size() - 3));
}
}
}
} catch (const fs::filesystem_error&) {
return DPMError::PERMISSION_DENIED;
}
@@ -32,75 +47,105 @@ DPMError ModuleLoader::check_module_path() const
return DPMError::SUCCESS;
}
std::string ModuleLoader::get_absolute_module_path() const
DPMError ModuleLoader::load_module(const std::string& module_name, void*& module_handle) const
{
try {
return fs::absolute(module_path_).string();
} catch (const fs::filesystem_error&) {
return module_path_; // Return relative path if conversion fails
}
}
std::pair<std::vector<std::string>, DPMError> ModuleLoader::list_available_modules() const
{
std::vector<std::string> modules;
try {
fs::path absolute_path = fs::absolute(module_path_);
for (const auto& entry : fs::directory_iterator(absolute_path)) {
if (entry.is_regular_file()) {
std::string filename = entry.path().filename().string();
// Check if it's a .so file
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") {
// Remove the .so extension
modules.push_back(filename.substr(0, filename.size() - 3));
}
}
}
} catch (const fs::filesystem_error&) {
return {modules, DPMError::PERMISSION_DENIED};
}
return {modules, DPMError::SUCCESS};
}
void* ModuleLoader::load_module(const std::string& module_name) const
{
// Construct path to module shared object
std::string module_so_path = module_path_ + module_name + ".so";
// Load the module
void* module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY);
module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY);
if (!module_handle) {
std::cerr << "Failed to load module: " << dlerror() << std::endl;
return nullptr;
return DPMError::MODULE_LOAD_FAILED;
}
// Clear any existing errors
dlerror();
return module_handle;
return DPMError::SUCCESS;
}
int ModuleLoader::execute_module(void* module_handle, const std::string& command) const
DPMError ModuleLoader::execute_module(void* module_handle, const std::string& command) const
{
if (!module_handle) {
std::cerr << "Invalid module handle" << std::endl;
return 1;
return DPMError::INVALID_MODULE;
}
// Find the execution entry point
using ExecuteFn = int (*)(const char*, int, char**);
ExecuteFn execute_fn = (ExecuteFn)dlsym(module_handle, "dpm_module_execute");
const char* error = dlerror();
if (error != nullptr) {
std::cerr << "Failed to find module entry point: " << error << std::endl;
return 1;
return DPMError::MODULE_LOAD_FAILED;
}
// Execute the module with just the provided command string
int result = execute_fn(command.c_str(), 0, nullptr);
execute_fn(command.c_str(), 0, nullptr);
return DPMError::SUCCESS;
}
return result;
DPMError ModuleLoader::get_module_version(void* module_handle, std::string& version) const
{
if (!module_handle) {
version = "ERROR";
return DPMError::INVALID_MODULE;
}
dlerror();
using GetVersionFn = const char* (*)();
GetVersionFn get_version = (GetVersionFn)dlsym(module_handle, "dpm_module_get_version");
const char* error = dlerror();
if (error != nullptr) {
version = "unknown";
return DPMError::MODULE_LOAD_FAILED;
}
const char* ver = get_version();
version = ver ? ver : "unknown";
return DPMError::SUCCESS;
}
DPMError ModuleLoader::get_module_description(void* module_handle, std::string& description) const
{
if (!module_handle) {
description = "ERROR";
return DPMError::INVALID_MODULE;
}
dlerror();
using GetDescriptionFn = const char* (*)();
GetDescriptionFn get_description = (GetDescriptionFn)dlsym(module_handle, "dpm_get_description");
const char* error = dlerror();
if (error != nullptr) {
description = "unknown";
return DPMError::MODULE_LOAD_FAILED;
}
const char* desc = get_description();
description = desc ? desc : "unknown";
return DPMError::SUCCESS;
}
DPMError ModuleLoader::validate_module_interface(void* module_handle, std::vector<std::string>& missing_symbols) const
{
if (!module_handle) {
return DPMError::INVALID_MODULE;
}
missing_symbols.clear();
size_t num_symbols = module_interface::required_symbols.size();
for (size_t i = 0; i < num_symbols; i++) {
dlerror();
void* sym = dlsym(module_handle, module_interface::required_symbols[i].c_str());
const char* error = dlerror();
if (error != nullptr) {
missing_symbols.push_back(module_interface::required_symbols[i]);
}
}
if (missing_symbols.empty()) {
return DPMError::SUCCESS;
}
return DPMError::INVALID_MODULE;
}