From aed712ba8e087232fcd9f71db4311687a7ce4398 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 2 Feb 2020 17:07:34 +0000 Subject: Make loading modules considerably more robust and user friendly. --- src/dynamic.cpp | 77 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 25 deletions(-) (limited to 'src/dynamic.cpp') diff --git a/src/dynamic.cpp b/src/dynamic.cpp index a6f758d33..a3ba43ff2 100644 --- a/src/dynamic.cpp +++ b/src/dynamic.cpp @@ -26,62 +26,89 @@ #include "inspircd.h" - #ifndef _WIN32 -#include -#else -#define dlopen(path, state) (void*)LoadLibraryA(path) -#define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export) -#define dlclose(handle) FreeLibrary((HMODULE)handle) +# include #endif -DLLManager::DLLManager(const char *fname) +/** The extension that dynamic libraries end with. */ +#define DLL_EXTENSION ".so" + +DLLManager::DLLManager(const std::string& name) + : lib(NULL) + , libname(name) { - if (!strstr(fname,".so")) + static size_t extlen = strlen(DLL_EXTENSION); + if (name.length() <= extlen || name.compare(name.length() - extlen, name.length(), DLL_EXTENSION)) { - err = "This doesn't look like a module file to me..."; - h = NULL; + err.assign(name + " is not a module (no " DLL_EXTENSION " extension)"); return; } - h = dlopen(fname, RTLD_NOW|RTLD_LOCAL); - if (!h) - { +#ifdef _WIN32 + lib = LoadLibraryA(name.c_str()); +#else + lib = dlopen(name.c_str(), RTLD_NOW|RTLD_LOCAL); +#endif + + if (!lib) RetrieveLastError(); - } } DLLManager::~DLLManager() { - /* close the library */ - if (h) - dlclose(h); + if (!lib) + return; + +#ifdef _WIN32 + FreeLibrary(lib) +#else + dlclose(lib); +#endif } Module* DLLManager::CallInit() { + const uint32_t* abi = GetSymbol(MODULE_STR_ABI); + if (!abi) + { + err.assign(libname + " is not a module (no ABI symbol)"); + return NULL; + } + else if (*abi != MODULE_ABI) + { + const char* version = GetVersion(); + err.assign(InspIRCd::Format("%s was built against %s which is too %s to use with %s", + libname.c_str(), version ? version : "an unknown version", + *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION)); + return NULL; + } + union { void* vptr; Module* (*fptr)(); }; - vptr = GetSymbol(MODULE_INIT_STR); + vptr = GetSymbol(MODULE_STR_INIT); if (!vptr) + { + err.assign(libname + " is not a module (no init symbol)"); return NULL; + } return (*fptr)(); } -void* DLLManager::GetSymbol(const char* name) +void* DLLManager::GetSymbol(const char* name) const { - return h ? dlsym(h, name) : NULL; -} + if (!lib) + return NULL; -std::string DLLManager::GetVersion() -{ - const char* srcver = static_cast(GetSymbol("inspircd_src_version")); - return srcver ? srcver : ""; +#if defined _WIN32 + return GetProcAddress(lib, name); +#else + return dlsym(lib, name); +#endif } void DLLManager::RetrieveLastError() -- cgit v1.3.1-10-gc9f91 From 0c5b85df8c5ae969831551ddefb8e07cb6da5f08 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Mon, 3 Feb 2020 21:43:15 +0000 Subject: Include the ABI version with the incompatible module error message. --- include/moduledefs.h | 4 ++-- src/dynamic.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/dynamic.cpp') diff --git a/include/moduledefs.h b/include/moduledefs.h index a2bac63cb..4b917bf26 100644 --- a/include/moduledefs.h +++ b/include/moduledefs.h @@ -22,7 +22,7 @@ class Module; /** The version of the InspIRCd ABI which is presently in use. */ -#define MODULE_ABI 3010 +#define MODULE_ABI 3010UL /** Stringifies the value of a symbol. */ #define MODULE_STRINGIFY_SYM1(DEF) MODULE_STRINGIFY_SYM2(DEF) @@ -42,6 +42,6 @@ class Module; /** Defines the interface that a shared library must expose in order to be a module. */ #define MODULE_INIT(klass) \ - extern "C" DllExport const uint32_t MODULE_SYM_ABI = MODULE_ABI; \ + extern "C" DllExport const unsigned long MODULE_SYM_ABI = MODULE_ABI; \ extern "C" DllExport const char MODULE_SYM_VERSION[] = INSPIRCD_VERSION; \ extern "C" DllExport Module* MODULE_SYM_INIT() { return new klass; } diff --git a/src/dynamic.cpp b/src/dynamic.cpp index a3ba43ff2..e0d7f6d80 100644 --- a/src/dynamic.cpp +++ b/src/dynamic.cpp @@ -68,7 +68,7 @@ DLLManager::~DLLManager() Module* DLLManager::CallInit() { - const uint32_t* abi = GetSymbol(MODULE_STR_ABI); + const unsigned long* abi = GetSymbol(MODULE_STR_ABI); if (!abi) { err.assign(libname + " is not a module (no ABI symbol)"); @@ -77,9 +77,9 @@ Module* DLLManager::CallInit() else if (*abi != MODULE_ABI) { const char* version = GetVersion(); - err.assign(InspIRCd::Format("%s was built against %s which is too %s to use with %s", - libname.c_str(), version ? version : "an unknown version", - *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION)); + err.assign(InspIRCd::Format("%s was built against %s (%lu) which is too %s to use with %s (%lu).", + libname.c_str(), version ? version : "an unknown version", *abi, + *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION, MODULE_ABI)); return NULL; } -- cgit v1.3.1-10-gc9f91