]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Added some hax :( to prevent invalid symbols. It seems that instead of returning...
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "configreader.h"
21 #include "globals.h"
22 #include "dynamic.h"
23
24 #ifndef STATIC_LINK
25 #include <dlfcn.h>
26 #else
27 #include "modlist.h"
28 #endif
29
30 #include "inspstring.h"
31 #include "helperfuncs.h"
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <stdio.h>
35
36 extern ServerConfig* Config;
37
38 DLLManager::DLLManager(const char *fname)
39 {
40         err = NULL;
41
42         if (!strstr(fname,".so"))
43         {
44                 err = "This doesn't look like a module file to me...";
45                 return;
46         }
47 #ifdef STATIC_LINK
48         this->staticname[0] = '\0';
49         log(DEBUG,"Loading core-compiled module '%s'",fname);
50         for (int j = 0; modsyms[j].name; j++)
51         {
52                 log(DEBUG,"Check %s",modsyms[j].name);
53                 if (!strcmp(modsyms[j].name,fname))
54                 {
55                         log(DEBUG,"Found %s",fname);
56                         strlcpy(this->staticname,fname,1020);
57                         err = 0;
58                         return;
59                 }
60         }
61         err = "Module is not statically compiled into the ircd";
62 #else
63         // Copy the library to a temp location, this makes recompiles
64         // a little safer if the ircd is running at the time as the
65         // shared libraries are mmap()ed and not doing this causes
66         // segfaults.
67         /*FILE* x = fopen(fname,"rb");
68         if (!x)
69         {
70                 err = strerror(errno);
71                 return;
72         }
73         log(DEBUG,"Opened module file %s",fname);
74         char tmpfile_template[255];
75         char buffer[65536];
76         snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",Config->TempDir,getpid());
77         int fd = mkstemp(tmpfile_template);
78         if (fd == -1)
79         {
80                 fclose(x);
81                 err = strerror(errno);
82                 return;
83         }
84         log(DEBUG,"Copying %s to %s",fname, tmpfile_template);
85         while (!feof(x))
86         {
87                 int n = fread(buffer, 1, 65535, x);
88                 if (n)
89                 {
90                         int written = write(fd,buffer,n);
91                         if (written != n)
92                         {
93                                 fclose(x);
94                                 err = strerror(errno);
95                                 return;
96                         }
97                 }
98         }
99         log(DEBUG,"Copied entire file.");
100         // Try to open the library now and get any error message.
101
102         if (close(fd) == -1)
103                 err = strerror(errno);
104         if (fclose(x) == EOF)
105                 err = strerror(errno);*/
106
107         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
108         if (!h)
109         {
110                 log(DEBUG,"dlerror occured!");
111                 err = dlerror();
112                 return;
113         }
114
115         /*log(DEBUG,"Finished loading '%s': %0x",tmpfile_template, h);
116
117         // We can delete the tempfile once it's loaded, leaving just the inode.
118         if (!err && !Config->debugging)
119         {
120                 log(DEBUG,"Deleteting %s",tmpfile_template);
121                 if (unlink(tmpfile_template) == -1)
122                         err = strerror(errno);
123         }*/
124 #endif
125 }
126
127 DLLManager::~DLLManager()
128 {
129 #ifndef STATIC_LINK
130         // close the library if it isn't null
131         if (h)
132                 dlclose(h);
133 #endif
134 }
135
136
137
138 #ifdef STATIC_LINK
139
140 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
141 {
142         log(DEBUG,"Symbol search...");
143         for (int j = 0; modsyms[j].name; j++)
144         {
145                 if (!strcmp(this->staticname,modsyms[j].name))
146                 {
147                         log(DEBUG,"Loading symbol...");
148                         v = modsyms[j].value;
149                         err = 0;
150                         return true;
151                 }
152         }
153         err = "Module symbol missing from the core";
154         return false;
155 }
156
157 #else
158
159 bool DLLManager::GetSymbol(void** v, const char* sym_name)
160 {
161         // try extract a symbol from the library
162         // get any error message is there is any
163         
164         if (h)
165         {
166                 log(DEBUG,"Found symbol %s", sym_name);
167                 dlerror(); // clear value
168                 *v = dlsym(h, "init_module");
169                 err = dlerror();
170
171                 log(DEBUG,"%s",err);
172                 if (!*v || err)
173                         return false;
174
175                 log(DEBUG,"%0x %0x",dlsym(h, "init_module"), *v);
176         }
177         
178         if (err)
179         {
180                 log(DEBUG,"Not found symbol");
181                 return false;
182         }
183         else
184         {       
185                 return true;
186         }
187 }
188
189 #endif
190
191 DLLFactoryBase::DLLFactoryBase(const char* fname, const char* factory) : DLLManager(fname)
192 {
193         // try get the factory function if there is no error yet
194         factory_func = 0;
195         
196         if (!LastError())
197         {
198 #ifdef STATIC_LINK
199                 if (!GetSymbol( factory_func, factory ? factory : "init_module" ))
200 #else
201                 if (!GetSymbol( (void **)&factory_func, factory ? factory : "init_module" ))
202 #endif
203                 {
204                         throw ModuleException("Missing init_module() entrypoint!");
205                 }
206         }
207 }
208
209 DLLFactoryBase::~DLLFactoryBase()
210 {
211 }
212