]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
And fix a bug
[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 "inspircd.h"
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <stdio.h>
36
37 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
38 {
39         err = NULL;
40
41         if (!strstr(fname,".so"))
42         {
43                 err = "This doesn't look like a module file to me...";
44                 return;
45         }
46 #ifdef STATIC_LINK
47         this->staticname[0] = '\0';
48         log(DEBUG,"Loading core-compiled module '%s'",fname);
49         for (int j = 0; modsyms[j].name; j++)
50         {
51                 log(DEBUG,"Check %s",modsyms[j].name);
52                 if (!strcmp(modsyms[j].name,fname))
53                 {
54                         log(DEBUG,"Found %s",fname);
55                         strlcpy(this->staticname,fname,1020);
56                         err = 0;
57                         return;
58                 }
59         }
60         err = "Module is not statically compiled into the ircd";
61 #else
62         // Copy the library to a temp location, this makes recompiles
63         // a little safer if the ircd is running at the time as the
64         // shared libraries are mmap()ed and not doing this causes
65         // segfaults.
66         FILE* x = fopen(fname,"rb");
67         if (!x)
68         {
69                 err = strerror(errno);
70                 return;
71         }
72         log(DEBUG,"Opened module file %s",fname);
73         char tmpfile_template[255];
74         char buffer[65536];
75         snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",ServerInstance->Config->TempDir,getpid());
76         int fd = mkstemp(tmpfile_template);
77         if (fd == -1)
78         {
79                 fclose(x);
80                 err = strerror(errno);
81                 return;
82         }
83         log(DEBUG,"Copying %s to %s",fname, tmpfile_template);
84         while (!feof(x))
85         {
86                 int n = fread(buffer, 1, 65535, x);
87                 if (n)
88                 {
89                         int written = write(fd,buffer,n);
90                         if (written != n)
91                         {
92                                 fclose(x);
93                                 err = strerror(errno);
94                                 return;
95                         }
96                 }
97         }
98         log(DEBUG,"Copied entire file.");
99         // Try to open the library now and get any error message.
100
101         if (close(fd) == -1)
102                 err = strerror(errno);
103         if (fclose(x) == EOF)
104                 err = strerror(errno);
105
106         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
107         if (!h)
108         {
109                 log(DEBUG,"dlerror occured!");
110                 err = (char*)dlerror();
111                 return;
112         }
113
114         log(DEBUG,"Finished loading '%s': %0x",tmpfile_template, h);
115
116         // We can delete the tempfile once it's loaded, leaving just the inode.
117         if (!err && !ServerInstance->Config->debugging)
118         {
119                 log(DEBUG,"Deleteting %s",tmpfile_template);
120                 if (unlink(tmpfile_template) == -1)
121                         err = strerror(errno);
122         }
123 #endif
124 }
125
126 DLLManager::~DLLManager()
127 {
128 #ifndef STATIC_LINK
129         // close the library if it isn't null
130         if (h)
131                 dlclose(h);
132 #endif
133 }
134
135
136
137 #ifdef STATIC_LINK
138
139 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
140 {
141         log(DEBUG,"Symbol search...");
142         for (int j = 0; modsyms[j].name; j++)
143         {
144                 if (!strcmp(this->staticname,modsyms[j].name))
145                 {
146                         log(DEBUG,"Loading symbol...");
147                         v = modsyms[j].value;
148                         err = 0;
149                         return true;
150                 }
151         }
152         err = "Module symbol missing from the core";
153         return false;
154 }
155
156 #else
157
158 bool DLLManager::GetSymbol(void** v, const char* sym_name)
159 {
160         // try extract a symbol from the library
161         // get any error message is there is any
162         
163         if (h)
164         {
165                 log(DEBUG,"Found symbol %s", sym_name);
166                 dlerror(); // clear value
167                 *v = dlsym(h, sym_name);
168                 err = (char*)dlerror();
169                 if (!*v || err)
170                         return false;
171         }
172         
173         if (err)
174         {
175                 return false;
176         }
177         else
178         {       
179                 return true;
180         }
181 }
182
183 #endif
184
185 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
186 {
187         // try get the factory function if there is no error yet
188         factory_func = 0;
189         
190         if (!LastError())
191         {
192 #ifdef STATIC_LINK
193                 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
194 #else
195                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
196 #endif
197                 {
198                         throw ModuleException("Missing init_module() entrypoint!");
199                 }
200         }
201 }
202
203 DLLFactoryBase::~DLLFactoryBase()
204 {
205 }
206