]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Move whowas containers into whowas class to avoid all cpp files including cmd_whowas...
[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         ServerInstance->Log(DEBUG,"Loading core-compiled module '%s'",fname);
37         for (int j = 0; modsyms[j].name; j++)
38         {
39                 ServerInstance->Log(DEBUG,"Check %s",modsyms[j].name);
40                 if (!strcmp(modsyms[j].name,fname))
41                 {
42                         ServerInstance->Log(DEBUG,"Found %s",fname);
43                         strlcpy(this->staticname,fname,1020);
44                         err = 0;
45                         return;
46                 }
47         }
48         err = "Module is not statically compiled into the ircd";
49 #else
50         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
51         if (!h)
52         {
53                 err = (char*)dlerror();
54                 ServerInstance->Log(DEBUG,"dlerror '%s' occured!", err);
55                 return;
56         }
57
58         ServerInstance->Log(DEBUG,"Finished loading '%s': %0x", fname, h);
59 #endif
60 }
61
62 DLLManager::~DLLManager()
63 {
64 #ifndef STATIC_LINK
65         // close the library if it isn't null
66         if (h)
67                 dlclose(h);
68 #endif
69 }
70
71
72
73 #ifdef STATIC_LINK
74
75 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
76 {
77         for (int j = 0; modsyms[j].name; j++)
78         {
79                 if (!strcmp(this->staticname,modsyms[j].name))
80                 {
81                         v = modsyms[j].value;
82                         err = 0;
83                         return true;
84                 }
85         }
86         err = "Module symbol missing from the core";
87         return false;
88 }
89
90 #else
91
92 bool DLLManager::GetSymbol(void** v, const char* sym_name)
93 {
94         // try extract a symbol from the library
95         // get any error message is there is any
96         
97         if (h)
98         {
99                 dlerror(); // clear value
100                 *v = dlsym(h, sym_name);
101                 err = (char*)dlerror();
102                 if (!*v || err)
103                         return false;
104         }
105         
106         if (err)
107         {
108                 return false;
109         }
110         else
111         {       
112                 return true;
113         }
114 }
115
116 #endif
117
118 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
119 {
120         // try get the factory function if there is no error yet
121         factory_func = 0;
122         
123         if (!LastError())
124         {
125 #ifdef STATIC_LINK
126                 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
127 #else
128                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
129 #endif
130                 {
131                         throw ModuleException("Missing init_module() entrypoint!");
132                 }
133         }
134 }
135
136 DLLFactoryBase::~DLLFactoryBase()
137 {
138 }
139