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