2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
34 /** The extension that dynamic libraries end with. */
35 #define DLL_EXTENSION ".so"
37 DLLManager::DLLManager(const std::string& name)
41 static size_t extlen = strlen(DLL_EXTENSION);
42 if (name.length() <= extlen || name.compare(name.length() - extlen, name.length(), DLL_EXTENSION))
44 err.assign(name + " is not a module (no " DLL_EXTENSION " extension)");
49 lib = LoadLibraryA(name.c_str());
51 lib = dlopen(name.c_str(), RTLD_NOW|RTLD_LOCAL);
58 DLLManager::~DLLManager()
70 Module* DLLManager::CallInit()
75 const unsigned long* abi = GetSymbol<const unsigned long>(MODULE_STR_ABI);
78 err.assign(libname + " is not a module (no ABI symbol)");
81 else if (*abi != MODULE_ABI)
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));
96 vptr = GetSymbol(MODULE_STR_INIT);
99 err.assign(libname + " is not a module (no init symbol)");
106 void* DLLManager::GetSymbol(const char* name) const
112 return GetProcAddress(lib, name);
114 return dlsym(lib, name);
118 void DLLManager::RetrieveLastError()
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);
128 const char* errmsg = dlerror();
129 err = errmsg ? errmsg : "Unknown error";
132 std::string::size_type p;
133 while ((p = err.find_last_of("\r\n")) != std::string::npos)