]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
More error descriptives
[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 "inspircd_io.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(char *fname)
39 {
40         if (!strstr(fname,".so"))
41         {
42                 err = "This doesn't look like a module file to me...";
43                 return;
44         }
45 #ifdef STATIC_LINK
46         this->staticname[0] = '\0';
47         log(DEBUG,"Loading core-compiled module '%s'",fname);
48         for (int j = 0; modsyms[j].name; j++)
49         {
50                 log(DEBUG,"Check %s",modsyms[j].name);
51                 if (!strcmp(modsyms[j].name,fname))
52                 {
53                         log(DEBUG,"Found %s",fname);
54                         strlcpy(this->staticname,fname,1020);
55                         err = 0;
56                         return;
57                 }
58         }
59         err = "Module is not statically compiled into the ircd";
60 #else
61 #ifdef IS_CYGWIN
62         // Cygwin behaviour is handled slightly differently
63         // With the advent of dynamic modules. Because Windows
64         // wont let you overwrite a file which is currently in
65         // Use, we can safely attempt to load the module from its
66         // Current location :)
67
68         h = dlopen(fname, RTLD_NOW );
69         err = (char*)dlerror();
70
71 #else
72         // Copy the library to a temp location, this makes recompiles
73         // a little safer if the ircd is running at the time as the
74         // shared libraries are mmap()ed and not doing this causes
75         // segfaults.
76         FILE* x = fopen(fname,"rb");
77         if (!x)
78         {
79                 err = "Module file not found or cannot access, game over man!";
80                 return;
81         }
82         char tmpfile_template[255];
83         char buffer[65536];
84         snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",Config->TempDir,getpid());
85         int fd = mkstemp(tmpfile_template);
86         while (!feof(x))
87         {
88                 int n = fread(buffer, 1, 65535, x);
89                 if (n)
90                         write(fd,buffer,n);
91         }
92         
93         // Try to open the library now and get any error message.
94         
95         h = dlopen(tmpfile_template, RTLD_NOW );
96         err = (char*)dlerror();
97         close(fd);
98         // We can delete the tempfile once it's loaded, leaving just the inode.
99         if (!Config->debugging)
100                 unlink(tmpfile_template);
101 #endif
102 #endif
103 }
104
105 DLLManager::~DLLManager()
106 {
107 #ifndef STATIC_LINK
108         // close the library if it isn't null
109         if (h != 0)
110         dlclose(h);
111 #endif
112 }
113
114
115
116 #ifdef STATIC_LINK
117
118 bool DLLManager::GetSymbol(initfunc* &v, char *sym_name)
119 {
120         log(DEBUG,"Symbol search...");
121         for (int j = 0; modsyms[j].name; j++)
122         {
123                 if (!strcmp(this->staticname,modsyms[j].name))
124                 {
125                         log(DEBUG,"Loading symbol...");
126                         v = modsyms[j].value;
127                         err = 0;
128                         return true;
129                 }
130         }
131         err = "Module symbol missing from the core";
132         return false;
133 }
134
135 #else
136
137 bool DLLManager::GetSymbol(void **v, char *sym_name)
138 {
139         // try extract a symbol from the library
140         // get any error message is there is any
141         
142         if(h != 0)
143         {
144                 *v = dlsym( h, sym_name );
145                 err = (char*)dlerror();
146                 if( err == 0 )
147                         return true;
148                 else
149                         return false;
150         }
151         else
152         {       
153                 return false;
154         }
155 }
156
157 #endif
158
159 DLLFactoryBase::DLLFactoryBase(char *fname, char *factory) : DLLManager(fname)
160 {
161         // try get the factory function if there is no error yet
162         
163         factory_func = 0;
164         
165         if(LastError() == 0)
166         {
167 #ifdef STATIC_LINK
168                 GetSymbol( factory_func, factory ? factory : (char*)"init_module" );
169 #else
170                 GetSymbol( (void **)&factory_func, factory ? factory : (char*)"init_module" );
171 #endif
172         }
173 }
174
175
176 DLLFactoryBase::~DLLFactoryBase()
177 {
178 }