2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
33 /** The extension that dynamic libraries end with. */
34 #define DLL_EXTENSION ".so"
36 DLLManager::DLLManager(const std::string& name)
40 static size_t extlen = strlen(DLL_EXTENSION);
41 if (name.length() <= extlen || name.compare(name.length() - extlen, name.length(), DLL_EXTENSION))
43 err.assign(name + " is not a module (no " DLL_EXTENSION " extension)");
48 lib = LoadLibraryA(name.c_str());
50 lib = dlopen(name.c_str(), RTLD_NOW|RTLD_LOCAL);
57 DLLManager::~DLLManager()
69 Module* DLLManager::CallInit()
71 const unsigned long* abi = GetSymbol<const unsigned long>(MODULE_STR_ABI);
74 err.assign(libname + " is not a module (no ABI symbol)");
77 else if (*abi != MODULE_ABI)
79 const char* version = GetVersion();
80 err.assign(InspIRCd::Format("%s was built against %s (%lu) which is too %s to use with %s (%lu).",
81 libname.c_str(), version ? version : "an unknown version", *abi,
82 *abi < MODULE_ABI ? "old" : "new", INSPIRCD_VERSION, MODULE_ABI));
92 vptr = GetSymbol(MODULE_STR_INIT);
95 err.assign(libname + " is not a module (no init symbol)");
102 void* DLLManager::GetSymbol(const char* name) const
108 return GetProcAddress(lib, name);
110 return dlsym(lib, name);
114 void DLLManager::RetrieveLastError()
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);
124 const char* errmsg = dlerror();
125 err = errmsg ? errmsg : "Unknown error";
128 std::string::size_type p;
129 while ((p = err.find_last_of("\r\n")) != std::string::npos)