]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Make loading modules considerably more robust and user friendly.
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29 #ifndef _WIN32
30 # include <dlfcn.h>
31 #endif
32
33 /** The extension that dynamic libraries end with. */
34 #define DLL_EXTENSION ".so"
35
36 DLLManager::DLLManager(const std::string& name)
37         : lib(NULL)
38         , libname(name)
39 {
40         static size_t extlen = strlen(DLL_EXTENSION);
41         if (name.length() <= extlen || name.compare(name.length() - extlen, name.length(), DLL_EXTENSION))
42         {
43                 err.assign(name + " is not a module (no " DLL_EXTENSION " extension)");
44                 return;
45         }
46
47 #ifdef _WIN32
48         lib = LoadLibraryA(name.c_str());
49 #else
50         lib = dlopen(name.c_str(), RTLD_NOW|RTLD_LOCAL);
51 #endif
52
53         if (!lib)
54                 RetrieveLastError();
55 }
56
57 DLLManager::~DLLManager()
58 {
59         if (!lib)
60                 return;
61
62 #ifdef _WIN32
63         FreeLibrary(lib)
64 #else
65         dlclose(lib);
66 #endif
67 }
68
69 Module* DLLManager::CallInit()
70 {
71         const uint32_t* abi = GetSymbol<const uint32_t>(MODULE_STR_ABI);
72         if (!abi)
73         {
74                 err.assign(libname + " is not a module (no ABI symbol)");
75                 return NULL;
76         }
77         else if (*abi != MODULE_ABI)
78         {
79                 const char* version = GetVersion();
80                 err.assign(InspIRCd::Format("%s was built against %s which is too %s to use with %s",
81                         libname.c_str(), version ? version : "an unknown version",
82                         *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION));
83                 return NULL;
84         }
85
86         union
87         {
88                 void* vptr;
89                 Module* (*fptr)();
90         };
91
92         vptr = GetSymbol(MODULE_STR_INIT);
93         if (!vptr)
94         {
95                 err.assign(libname + " is not a module (no init symbol)");
96                 return NULL;
97         }
98
99         return (*fptr)();
100 }
101
102 void* DLLManager::GetSymbol(const char* name) const
103 {
104         if (!lib)
105                 return NULL;
106
107 #if defined _WIN32
108         return GetProcAddress(lib, name);
109 #else
110         return dlsym(lib, name);
111 #endif
112 }
113
114 void DLLManager::RetrieveLastError()
115 {
116 #if defined _WIN32
117         char errmsg[500];
118         DWORD dwErrorCode = GetLastError();
119         if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0)
120                 sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode);
121         SetLastError(ERROR_SUCCESS);
122         err = errmsg;
123 #else
124         const char* errmsg = dlerror();
125         err = errmsg ? errmsg : "Unknown error";
126 #endif
127
128         std::string::size_type p;
129         while ((p = err.find_last_of("\r\n")) != std::string::npos)
130                 err.erase(p, 1);
131 }