improved module validation
parent
2ec8908fa9
commit
6a91f30ce2
|
@ -27,5 +27,5 @@ public:
|
||||||
DPMError validate_module_interface(void* module_handle, std::vector<std::string>& missing_symbols) const;
|
DPMError validate_module_interface(void* module_handle, std::vector<std::string>& missing_symbols) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string module_path_;
|
std::string _module_path;
|
||||||
};
|
};
|
|
@ -5,21 +5,21 @@ namespace fs = std::filesystem;
|
||||||
ModuleLoader::ModuleLoader(std::string module_path)
|
ModuleLoader::ModuleLoader(std::string module_path)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
module_path_ = fs::absolute(module_path).string();
|
_module_path = fs::absolute(module_path).string();
|
||||||
if (!module_path_.empty() && module_path_.back() != '/') {
|
if (!_module_path.empty() && _module_path.back() != '/') {
|
||||||
module_path_ += '/';
|
_module_path += '/';
|
||||||
}
|
}
|
||||||
} catch (const fs::filesystem_error&) {
|
} catch (const fs::filesystem_error&) {
|
||||||
module_path_ = module_path;
|
_module_path = module_path;
|
||||||
if (!module_path_.empty() && module_path_.back() != '/') {
|
if (!_module_path.empty() && _module_path.back() != '/') {
|
||||||
module_path_ += '/';
|
_module_path += '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DPMError ModuleLoader::get_module_path(std::string& path) const
|
DPMError ModuleLoader::get_module_path(std::string& path) const
|
||||||
{
|
{
|
||||||
path = module_path_;
|
path = _module_path;
|
||||||
return DPMError::SUCCESS;
|
return DPMError::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ DPMError ModuleLoader::list_available_modules(std::vector<std::string>& modules)
|
||||||
modules.clear();
|
modules.clear();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const auto& entry : fs::directory_iterator(module_path_)) {
|
for (const auto& entry : fs::directory_iterator(_module_path)) {
|
||||||
if (entry.is_regular_file()) {
|
if (entry.is_regular_file()) {
|
||||||
std::string filename = entry.path().filename().string();
|
std::string filename = entry.path().filename().string();
|
||||||
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") {
|
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".so") {
|
||||||
|
@ -45,7 +45,7 @@ DPMError ModuleLoader::list_available_modules(std::vector<std::string>& modules)
|
||||||
|
|
||||||
DPMError ModuleLoader::load_module(const std::string& module_name, void*& module_handle) const
|
DPMError ModuleLoader::load_module(const std::string& module_name, void*& module_handle) const
|
||||||
{
|
{
|
||||||
std::string module_so_path = module_path_ + module_name + ".so";
|
std::string module_so_path = _module_path + module_name + ".so";
|
||||||
|
|
||||||
module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY);
|
module_handle = dlopen(module_so_path.c_str(), RTLD_LAZY);
|
||||||
if (!module_handle) {
|
if (!module_handle) {
|
||||||
|
@ -65,6 +65,13 @@ DPMError ModuleLoader::execute_module(const std::string& module_name, const std:
|
||||||
return load_error;
|
return load_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> missing_symbols;
|
||||||
|
DPMError validate_error = validate_module_interface(module_handle, missing_symbols);
|
||||||
|
if (validate_error != DPMError::SUCCESS) {
|
||||||
|
dlclose(module_handle);
|
||||||
|
return DPMError::INVALID_MODULE;
|
||||||
|
}
|
||||||
|
|
||||||
using ExecuteFn = int (*)(const char*, int, char**);
|
using ExecuteFn = int (*)(const char*, int, char**);
|
||||||
ExecuteFn execute_fn = (ExecuteFn)dlsym(module_handle, "dpm_module_execute");
|
ExecuteFn execute_fn = (ExecuteFn)dlsym(module_handle, "dpm_module_execute");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue