]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Add extra /map info (connection uptime, and lag time) to /MAP for opers. Adds feature...
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "configreader.h"
15 #include "dynamic.h"
16
17 #ifndef STATIC_LINK
18 #include <dlfcn.h>
19 #else
20 #include "modlist.h"
21 #endif
22
23 #include "inspircd.h"
24
25 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
26 {
27         err = NULL;
28
29         if (!strstr(fname,".so"))
30         {
31                 err = "This doesn't look like a module file to me...";
32                 return;
33         }
34 #ifdef STATIC_LINK
35         this->staticname[0] = '\0';
36         for (int j = 0; modsyms[j].name; j++)
37         {
38                 if (!strcmp(modsyms[j].name,fname))
39                 {
40                         strlcpy(this->staticname,fname,1020);
41                         err = 0;
42                         return;
43                 }
44         }
45         err = "Module is not statically compiled into the ircd";
46 #else
47         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
48         if (!h)
49         {
50                 err = (char*)dlerror();
51                 return;
52         }
53 #endif
54 }
55
56 DLLManager::~DLLManager()
57 {
58 #ifndef STATIC_LINK
59         // close the library if it isn't null
60         if (h)
61                 dlclose(h);
62 #endif
63 }
64
65
66
67 #ifdef STATIC_LINK
68
69 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
70 {
71         for (int j = 0; modsyms[j].name; j++)
72         {
73                 if (!strcmp(this->staticname,modsyms[j].name))
74                 {
75                         v = modsyms[j].value;
76                         err = 0;
77                         return true;
78                 }
79         }
80         err = "Module symbol missing from the core";
81         return false;
82 }
83
84 #else
85
86 bool DLLManager::GetSymbol(void** v, const char* sym_name)
87 {
88         // try extract a symbol from the library
89         // get any error message is there is any
90         
91         if (h)
92         {
93                 dlerror(); // clear value
94                 *v = dlsym(h, sym_name);
95                 err = (char*)dlerror();
96                 if (!*v || err)
97                         return false;
98         }
99         
100         if (err)
101         {
102                 return false;
103         }
104         else
105         {       
106                 return true;
107         }
108 }
109
110 #endif
111
112 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
113 {
114         // try get the factory function if there is no error yet
115         factory_func = 0;
116         
117         if (!LastError())
118         {
119 #ifdef STATIC_LINK
120                 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
121 #else
122                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
123 #endif
124                 {
125                         throw ModuleException("Missing init_module() entrypoint!");
126                 }
127         }
128 }
129
130 DLLFactoryBase::~DLLFactoryBase()
131 {
132 }
133