]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Update copyright headers.
[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
30 #ifndef _WIN32
31 #include <dlfcn.h>
32 #else
33 #define dlopen(path, state) (void*)LoadLibraryA(path)
34 #define dlsym(handle, export) (void*)GetProcAddress((HMODULE)handle, export)
35 #define dlclose(handle) FreeLibrary((HMODULE)handle)
36 #endif
37
38 DLLManager::DLLManager(const char *fname)
39 {
40         if (!strstr(fname,".so"))
41         {
42                 err = "This doesn't look like a module file to me...";
43                 h = NULL;
44                 return;
45         }
46
47         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
48         if (!h)
49         {
50                 RetrieveLastError();
51         }
52 }
53
54 DLLManager::~DLLManager()
55 {
56         /* close the library */
57         if (h)
58                 dlclose(h);
59 }
60
61 Module* DLLManager::CallInit()
62 {
63         union
64         {
65                 void* vptr;
66                 Module* (*fptr)();
67         };
68
69         vptr = GetSymbol(MODULE_INIT_STR);
70         if (!vptr)
71                 return NULL;
72
73         return (*fptr)();
74 }
75
76 void* DLLManager::GetSymbol(const char* name)
77 {
78         return h ? dlsym(h, name) : NULL;
79 }
80
81 std::string DLLManager::GetVersion()
82 {
83         const char* srcver = static_cast<const char*>(GetSymbol("inspircd_src_version"));
84         return srcver ? srcver : "";
85 }
86
87 void DLLManager::RetrieveLastError()
88 {
89 #if defined _WIN32
90         char errmsg[500];
91         DWORD dwErrorCode = GetLastError();
92         if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0)
93                 sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode);
94         SetLastError(ERROR_SUCCESS);
95         err = errmsg;
96 #else
97         const char* errmsg = dlerror();
98         err = errmsg ? errmsg : "Unknown error";
99 #endif
100
101         std::string::size_type p;
102         while ((p = err.find_last_of("\r\n")) != std::string::npos)
103                 err.erase(p, 1);
104 }