moving some loading functionality to dpmdk

This commit is contained in:
Chris Punches
2025-03-28 22:04:27 -04:00
parent 7b29824e40
commit 56e471e227
6 changed files with 384 additions and 66 deletions

View File

@@ -1,11 +1,9 @@
/**
* @file CommonModuleAPI.cpp
* @brief Implementation stub for DPM module interface
* @brief Implementation for DPMDK functions
*
* This file serves primarily as a documentation reference for the module
* interface. The actual implementations of dpm_get_config and dpm_log are
* provided by the DPM core, while dpm_module_execute, dpm_module_get_version,
* and dpm_get_description must be implemented by each module.
* Implements the module utility functions provided by the DPMDK library
* for module interoperability and loading.
*
* @copyright Copyright (c) 2025 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
@@ -28,5 +26,86 @@
* For bug reports or contributions, please contact the dhlp-contributors
* mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors
*/
#include "CommonModuleAPI.hpp"
// Only define these functions when not in standalone mode
#ifndef BUILD_STANDALONE
extern "C" bool dpm_module_exists(const char* module_name) {
if (!module_name) return false;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return false;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if file exists
struct stat buffer;
return (stat(module_file.c_str(), &buffer) == 0);
}
extern "C" int dpm_load_module(const char* module_name, void** module_handle) {
if (!module_name || !module_handle) return 1;
// Get the module path
const char* module_path = dpm_get_module_path();
if (!module_path) return 1;
// Build path to the module
std::string module_file = std::string(module_path) + "/" + module_name + ".so";
// Check if the file exists
if (!dpm_module_exists(module_name)) return 1;
// Load the module
*module_handle = dlopen(module_file.c_str(), RTLD_LAZY);
if (!*module_handle) return 1;
return 0;
}
extern "C" bool dpm_symbol_exists(void* module_handle, const char* symbol_name) {
if (!module_handle || !symbol_name) return false;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error) return false;
return (symbol != NULL);
}
extern "C" int dpm_execute_symbol(void* module_handle, const char* symbol_name, void* args) {
if (!module_handle || !symbol_name) return 1;
// Clear any error
dlerror();
// Look up the symbol
void* symbol = dlsym(module_handle, symbol_name);
// Check for errors
const char* error = dlerror();
if (error || !symbol) return 1;
// Cast to function pointer and call
typedef int (*FunctionPtr)(void*);
FunctionPtr func = (FunctionPtr)symbol;
return func(args);
}
extern "C" void dpm_unload_module(void* module_handle) {
if (module_handle) {
dlclose(module_handle);
}
}
#endif // BUILD_STANDALONE