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