summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2020-02-02 17:07:34 +0000
committerSadie Powell <sadie@witchery.services>2020-02-02 20:32:49 +0000
commitaed712ba8e087232fcd9f71db4311687a7ce4398 (patch)
treefe06900c63293fabab60faf863bf97a390a0df20
parentfda43fc0ff5ecf87d877cc341961c9da4affae76 (diff)
Make loading modules considerably more robust and user friendly.
-rw-r--r--include/dynamic.h63
-rw-r--r--include/moduledefs.h47
-rw-r--r--include/modules.h38
-rw-r--r--src/coremods/core_info/cmd_modules.cpp4
-rw-r--r--src/dynamic.cpp77
-rw-r--r--src/inspircd.cpp1
-rw-r--r--src/logger.cpp2
-rw-r--r--src/modulemanager.cpp10
8 files changed, 147 insertions, 95 deletions
diff --git a/include/dynamic.h b/include/dynamic.h
index 703120fc0..3a312a382 100644
--- a/include/dynamic.h
+++ b/include/dynamic.h
@@ -30,35 +30,34 @@
*/
class CoreExport DLLManager : public classbase
{
- protected:
- /** The last error string
- */
+ private:
+ /** The last error string. */
std::string err;
- /** Sets the last error string
- */
+ /** The module library handle. */
+#ifdef _WIN32
+ HMODULE lib;
+#else
+ void* lib;
+#endif
+
+ /** The filename of the module library. */
+ const std::string libname;
+
+ /** Sets the last error string. */
void RetrieveLastError();
public:
- /** This constructor loads the module using dlopen()
- * @param fname The filename to load. This should be within
- * the modules dir.
- */
- DLLManager(const char *fname);
- virtual ~DLLManager();
-
- /** Get the last error from dlopen() or dlsym().
+ /** Attempts to load the specified module.
+ * @param name The name of the library to load.
*/
- const std::string& LastError()
- {
- return err;
- }
+ DLLManager(const std::string& name);
- /** The module library handle.
- */
- void *h;
+ /** Unloads the module if one was loaded. */
+ ~DLLManager();
- /** Return a module by calling the init function
+ /** Attempts to create a new module instance from this shared library.
+ * @return Either a new instance of the Module class or NULL on error.
*/
Module* CallInit();
@@ -66,8 +65,24 @@ class CoreExport DLLManager : public classbase
* @param name The name of the symbol to retrieve.
* @return Either the value of the specified symbol or or NULL if it does not exist.
*/
- void* GetSymbol(const char* name);
+ void* GetSymbol(const char* name) const;
+
+ /** Retrieves the value of the specified symbol and casts it to the requested type.
+ * @param name The name of the symbol to retrieve.
+ * @return Either the value of the specified symbol or or NULL if it does not exist.
+ */
+ template <typename TReturn>
+ TReturn* GetSymbol(const char* name) const
+ {
+ return static_cast<TReturn*>(GetSymbol(name));
+ }
+
+ /** Retrieves the module version from the dynamic library. */
+ const char* GetVersion() const { return GetSymbol<const char>(MODULE_STR_VERSION); }
+
+ /** Retrieves the last error which occurred or an empty string if no errors have occurred. */
+ const std::string& LastError() const { return err; }
- /** Get detailed version information from the module file */
- std::string GetVersion();
+ /** Retrieves the filename of the underlying shared library. */
+ const std::string& LibraryName() const { return libname; }
};
diff --git a/include/moduledefs.h b/include/moduledefs.h
new file mode 100644
index 000000000..a2bac63cb
--- /dev/null
+++ b/include/moduledefs.h
@@ -0,0 +1,47 @@
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ * Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
+ *
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#pragma once
+
+class Module;
+
+/** The version of the InspIRCd ABI which is presently in use. */
+#define MODULE_ABI 3010
+
+/** Stringifies the value of a symbol. */
+#define MODULE_STRINGIFY_SYM1(DEF) MODULE_STRINGIFY_SYM2(DEF)
+#define MODULE_STRINGIFY_SYM2(DEF) #DEF
+
+/** The name of the symbol which contains the ABI that a module was compiled against. */
+#define MODULE_SYM_ABI inspircd_module_abi
+#define MODULE_STR_ABI MODULE_STRINGIFY_SYM1(MODULE_SYM_ABI)
+
+/** The name of the symbol which creates a new Module instance. */
+#define MODULE_SYM_INIT inspircd_module_init
+#define MODULE_STR_INIT MODULE_STRINGIFY_SYM1(MODULE_SYM_INIT)
+
+/** The name of the symbol which contains the version that a module was compiled against. */
+#define MODULE_SYM_VERSION inspircd_module_version
+#define MODULE_STR_VERSION MODULE_STRINGIFY_SYM1(MODULE_SYM_VERSION)
+
+/** 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 char MODULE_SYM_VERSION[] = INSPIRCD_VERSION; \
+ extern "C" DllExport Module* MODULE_SYM_INIT() { return new klass; }
diff --git a/include/modules.h b/include/modules.h
index 1c1f4a3ae..5e0c9ab07 100644
--- a/include/modules.h
+++ b/include/modules.h
@@ -30,13 +30,11 @@
#pragma once
+#include "moduledefs.h"
#include "dynamic.h"
#include "base.h"
#include "ctables.h"
#include "inspsocket.h"
-#include <string>
-#include <deque>
-#include <sstream>
#include "timer.h"
#include "mode.h"
@@ -101,19 +99,6 @@ struct ModResult {
}
};
-/** InspIRCd major version.
- * 1.2 -> 102; 2.1 -> 201; 2.12 -> 212
- */
-#define INSPIRCD_VERSION_MAJ 300
-
-/** InspIRCd API version.
- * If you change any API elements, increment this value. This counter should be
- * reset whenever the major version is changed. Modules can use these two values
- * and numerical comparisons in preprocessor macros if they wish to support
- * multiple versions of InspIRCd in one file.
- */
-#define INSPIRCD_VERSION_API 9
-
/**
* This #define allows us to call a method in all
* loaded modules in a readable simple way, e.g.:
@@ -1190,24 +1175,3 @@ class CoreExport ModuleManager : public fakederef<ModuleManager>
*/
void DelReferent(ServiceProvider* service);
};
-
-/** Do not mess with these functions unless you know the C preprocessor
- * well enough to explain why they are needed. The order is important.
- */
-#define MODULE_INIT_STR MODULE_INIT_STR_FN_2(MODULE_INIT_SYM)
-#define MODULE_INIT_STR_FN_2(x) MODULE_INIT_STR_FN_1(x)
-#define MODULE_INIT_STR_FN_1(x) #x
-#define MODULE_INIT_SYM MODULE_INIT_SYM_FN_2(INSPIRCD_VERSION_MAJ, INSPIRCD_VERSION_API)
-#define MODULE_INIT_SYM_FN_2(x,y) MODULE_INIT_SYM_FN_1(x,y)
-#define MODULE_INIT_SYM_FN_1(x,y) inspircd_module_ ## x ## _ ## y
-
-/** This definition is used as shorthand for the various classes
- * and functions needed to make a module loadable by the OS.
- * It defines the class factory and external init_module function.
- */
-#define MODULE_INIT(y) \
- extern "C" DllExport Module * MODULE_INIT_SYM() \
- { \
- return new y; \
- } \
- extern "C" DllExport const char inspircd_src_version[] = INSPIRCD_VERSION;
diff --git a/src/coremods/core_info/cmd_modules.cpp b/src/coremods/core_info/cmd_modules.cpp
index 7c9e49550..7936a34d7 100644
--- a/src/coremods/core_info/cmd_modules.cpp
+++ b/src/coremods/core_info/cmd_modules.cpp
@@ -76,8 +76,8 @@ CmdResult CommandModules::Handle(User* user, const Params& parameters)
if (!(V.Flags & mult))
flags[pos] = '-';
- std::string srcrev = m->ModuleDLLManager->GetVersion();
- user->WriteRemoteNumeric(RPL_MODLIST, m->ModuleSourceFile, srcrev.empty() ? "*" : srcrev, flags, V.description);
+ const char* srcrev = m->ModuleDLLManager->GetVersion();
+ user->WriteRemoteNumeric(RPL_MODLIST, m->ModuleSourceFile, srcrev ? "*" : srcrev, flags, V.description);
}
else
{
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 <dlfcn.h>
-#else
-#define dlopen(path, state) (void*)LoadLibraryA(path)
-#define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export)
-#define dlclose(handle) FreeLibrary((HMODULE)handle)
+# include <dlfcn.h>
#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<const uint32_t>(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<const char*>(GetSymbol("inspircd_src_version"));
- return srcver ? srcver : "";
+#if defined _WIN32
+ return GetProcAddress(lib, name);
+#else
+ return dlsym(lib, name);
+#endif
}
void DLLManager::RetrieveLastError()
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index dea4b4575..e2d3f8dff 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -36,7 +36,6 @@
#ifndef _WIN32
#include <unistd.h>
#include <sys/resource.h>
- #include <dlfcn.h>
#include <getopt.h>
#include <pwd.h> // setuid
#include <grp.h> // setgid
diff --git a/src/logger.cpp b/src/logger.cpp
index a05edc55f..8aad5e8f1 100644
--- a/src/logger.cpp
+++ b/src/logger.cpp
@@ -56,7 +56,7 @@
*/
const char LogStream::LogHeader[] =
- "Log started for " INSPIRCD_VERSION " (" MODULE_INIT_STR ")";
+ "Log started for " INSPIRCD_VERSION;
LogManager::LogManager()
: Logging(false)
diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp
index 218b9aa5a..e5309af57 100644
--- a/src/modulemanager.cpp
+++ b/src/modulemanager.cpp
@@ -69,13 +69,13 @@ bool ModuleManager::Load(const std::string& modname, bool defer)
newmod->ModuleSourceFile = filename;
newmod->ModuleDLLManager = newhandle;
Modules[filename] = newmod;
- std::string version = newhandle->GetVersion();
- if (version.empty())
- version.assign("unknown");
+ const char* version = newhandle->GetVersion();
+ if (!version)
+ version = "unknown";
if (defer)
{
ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
- filename.c_str(), version.c_str());
+ filename.c_str(), version);
}
else
{
@@ -88,7 +88,7 @@ bool ModuleManager::Load(const std::string& modname, bool defer)
Version v = newmod->GetVersion();
ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
- filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
+ filename.c_str(), version, (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
}
}
else