]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "configreader.h"
15 #include "dynamic.h"
16 #ifndef WIN32
17 #include <dlfcn.h>
18 #endif
19
20 #include "inspircd.h"
21
22 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
23 {
24         err = NULL;
25
26         if (!strstr(fname,".so"))
27         {
28                 err = "This doesn't look like a module file to me...";
29                 return;
30         }
31
32         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
33         if (!h)
34         {
35                 err = (char*)dlerror();
36                 return;
37         }
38 }
39
40 DLLManager::~DLLManager()
41 {
42         // close the library if it isn't null
43         if (h)
44                 dlclose(h);
45 }
46
47
48
49 bool DLLManager::GetSymbol(void** v, const char* sym_name)
50 {
51         // try extract a symbol from the library
52         // get any error message is there is any
53         
54         if (h)
55         {
56                 dlerror(); // clear value
57                 *v = dlsym(h, sym_name);
58                 err = (char*)dlerror();
59                 if (!*v || err)
60                         return false;
61         }
62         
63         if (err)
64         {
65                 return false;
66         }
67         else
68         {       
69                 return true;
70         }
71 }
72
73 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
74 {
75         // try get the factory function if there is no error yet
76         factory_func = 0;
77         
78         if (!LastError())
79         {
80                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
81                 {
82                         throw ModuleException("Missing init_module() entrypoint!");
83                 }
84         }
85 }
86
87 DLLFactoryBase::~DLLFactoryBase()
88 {
89 }
90