]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Tidy up, make a lot of char*'s const
[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         char tmpfile_template[255];
74         char buffer[65536];
75         snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",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         while (!feof(x))
84         {
85                 int n = fread(buffer, 1, 65535, x);
86                 if (n)
87                 {
88                         int written = write(fd,buffer,n);
89                         if (written != n)
90                         {
91                                 fclose(x);
92                                 err = strerror(errno);
93                                 return;
94                         }
95                 }
96         }
97         // Try to open the library now and get any error message.
98         
99         h = dlopen(tmpfile_template, RTLD_NOW );
100
101         if (!h)
102         {
103                 err = dlerror();
104                 return;
105         }
106         if (close(fd) == -1)
107                 err = strerror(errno);
108         if (fclose(x) == EOF)
109                 err = strerror(errno);
110
111         // We can delete the tempfile once it's loaded, leaving just the inode.
112         if (!err && !Config->debugging)
113         {
114                 if (unlink(tmpfile_template) == -1)
115                         err = strerror(errno);
116         }
117 #endif
118 }
119
120 DLLManager::~DLLManager()
121 {
122 #ifndef STATIC_LINK
123         // close the library if it isn't null
124         if (h != 0)
125         dlclose(h);
126 #endif
127 }
128
129
130
131 #ifdef STATIC_LINK
132
133 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
134 {
135         log(DEBUG,"Symbol search...");
136         for (int j = 0; modsyms[j].name; j++)
137         {
138                 if (!strcmp(this->staticname,modsyms[j].name))
139                 {
140                         log(DEBUG,"Loading symbol...");
141                         v = modsyms[j].value;
142                         err = 0;
143                         return true;
144                 }
145         }
146         err = "Module symbol missing from the core";
147         return false;
148 }
149
150 #else
151
152 bool DLLManager::GetSymbol(void** v, const char* sym_name)
153 {
154         // try extract a symbol from the library
155         // get any error message is there is any
156         
157         if (h)
158         {
159                 *v = dlsym(h, sym_name);
160                 err = dlerror();
161                 return !err;
162         }
163         else
164         {       
165                 return false;
166         }
167 }
168
169 #endif
170
171 DLLFactoryBase::DLLFactoryBase(const char* fname, const char* factory) : DLLManager(fname)
172 {
173         // try get the factory function if there is no error yet
174         factory_func = 0;
175         
176         if (!LastError())
177         {
178 #ifdef STATIC_LINK
179                 GetSymbol( factory_func, factory ? factory : "init_module" );
180 #else
181                 GetSymbol( (void **)&factory_func, factory ? factory : "init_module" );
182 #endif
183         }
184 }
185
186
187 DLLFactoryBase::~DLLFactoryBase()
188 {
189 }