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) 2003, 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
DLLManager(const std::string & name)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
~DLLManager()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
CallInit()70 Module* DLLManager::CallInit()
71 {
72 if (!lib)
73 return NULL;
74
75 const unsigned long* abi = GetSymbol<const unsigned long>(MODULE_STR_ABI);
76 if (!abi)
77 {
78 err.assign(libname + " is not a module (no ABI symbol)");
79 return NULL;
80 }
81 else if (*abi != MODULE_ABI)
82 {
83 const char* version = GetVersion();
84 err.assign(InspIRCd::Format("%s was built against %s (%lu) which is too %s to use with %s (%lu).",
85 libname.c_str(), version ? version : "an unknown version", *abi,
86 *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION, MODULE_ABI));
87 return NULL;
88 }
89
90 union
91 {
92 void* vptr;
93 Module* (*fptr)();
94 };
95
96 vptr = GetSymbol(MODULE_STR_INIT);
97 if (!vptr)
98 {
99 err.assign(libname + " is not a module (no init symbol)");
100 return NULL;
101 }
102
103 return (*fptr)();
104 }
105
GetSymbol(const char * name) const106 void* DLLManager::GetSymbol(const char* name) const
107 {
108 if (!lib)
109 return NULL;
110
111 #if defined _WIN32
112 return GetProcAddress(lib, name);
113 #else
114 return dlsym(lib, name);
115 #endif
116 }
117
RetrieveLastError()118 void DLLManager::RetrieveLastError()
119 {
120 #if defined _WIN32
121 char errmsg[500];
122 DWORD dwErrorCode = GetLastError();
123 if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0)
124 sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode);
125 SetLastError(ERROR_SUCCESS);
126 err = errmsg;
127 #else
128 const char* errmsg = dlerror();
129 err = errmsg ? errmsg : "Unknown error";
130 #endif
131
132 std::string::size_type p;
133 while ((p = err.find_last_of("\r\n")) != std::string::npos)
134 err.erase(p, 1);
135 }
136