]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Change to using Instance->Log (InspIRCd::Log) rather than log() macro
[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         ServerInstance->Log(DEBUG,"Loading core-compiled module '%s'",fname);
49         for (int j = 0; modsyms[j].name; j++)
50         {
51                 ServerInstance->Log(DEBUG,"Check %s",modsyms[j].name);
52                 if (!strcmp(modsyms[j].name,fname))
53                 {
54                         ServerInstance->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         ServerInstance->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         ServerInstance->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         ServerInstance->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                 ServerInstance->Log(DEBUG,"dlerror occured!");
110                 err = (char*)dlerror();
111                 return;
112         }
113
114         ServerInstance->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                 ServerInstance->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         for (int j = 0; modsyms[j].name; j++)
142         {
143                 if (!strcmp(this->staticname,modsyms[j].name))
144                 {
145                         v = modsyms[j].value;
146                         err = 0;
147                         return true;
148                 }
149         }
150         err = "Module symbol missing from the core";
151         return false;
152 }
153
154 #else
155
156 bool DLLManager::GetSymbol(void** v, const char* sym_name)
157 {
158         // try extract a symbol from the library
159         // get any error message is there is any
160         
161         if (h)
162         {
163                 dlerror(); // clear value
164                 *v = dlsym(h, sym_name);
165                 err = (char*)dlerror();
166                 if (!*v || err)
167                         return false;
168         }
169         
170         if (err)
171         {
172                 return false;
173         }
174         else
175         {       
176                 return true;
177         }
178 }
179
180 #endif
181
182 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
183 {
184         // try get the factory function if there is no error yet
185         factory_func = 0;
186         
187         if (!LastError())
188         {
189 #ifdef STATIC_LINK
190                 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
191 #else
192                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
193 #endif
194                 {
195                         throw ModuleException("Missing init_module() entrypoint!");
196                 }
197         }
198 }
199
200 DLLFactoryBase::~DLLFactoryBase()
201 {
202 }
203