]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Make loading modules considerably more robust and user friendly.
authorSadie Powell <sadie@witchery.services>
Sun, 2 Feb 2020 17:07:34 +0000 (17:07 +0000)
committerSadie Powell <sadie@witchery.services>
Sun, 2 Feb 2020 20:32:49 +0000 (20:32 +0000)
include/dynamic.h
include/moduledefs.h [new file with mode: 0644]
include/modules.h
src/coremods/core_info/cmd_modules.cpp
src/dynamic.cpp
src/inspircd.cpp
src/logger.cpp
src/modulemanager.cpp

index 703120fc05bd18cbe33d2ad439ea6e3e13bd0f60..3a312a382a04ddf7ced5466d76ab4c8a297cd87f 100644 (file)
  */
 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 (file)
index 0000000..a2bac63
--- /dev/null
@@ -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; }
index 1c1f4a3aed68fec30c3c7bce080eeb7c4b12b025..5e0c9ab077cd58aca711e95c4cafa888c86623cf 100644 (file)
 
 #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;
index 7c9e49550946d5983207cd35305f8df2930db7b0..7936a34d7324c9fa15fc4456521d1f076f46257e 100644 (file)
@@ -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
                {
index a6f758d33bfd437ff5dd8d3009dbb586fc01dd27..a3ba43ff24a8045c454ae88fefe16d18ec8d26b1 100644 (file)
 
 
 #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()
index dea4b45755fcfa595b91fc2c5164f91c7ae83448..e2d3f8dff24227d4635bc84ea9b2e129b3ef0487 100644 (file)
@@ -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
index a05edc55f12a702743ef5c64b89a3f3e29d7a503..8aad5e8f19677a7bf86cb02242d0b756279ca0cc 100644 (file)
@@ -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)
index 218b9aa5a8a07c86ee13c38a9b5f3c88a361a4fd..e5309af57dc10921f4bab7820c043ea3ef824975 100644 (file)
@@ -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