]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Convert more modules
[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 "inspircd.h"
15 #include "configreader.h"
16 #include "dynamic.h"
17 #ifndef WIN32
18 #include <dlfcn.h>
19 #endif
20
21 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
22 {
23         err = NULL;
24
25         if (!strstr(fname,".so"))
26         {
27                 err = "This doesn't look like a module file to me...";
28                 return;
29         }
30
31         h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
32         if (!h)
33         {
34                 err = (char*)dlerror();
35                 return;
36         }
37 }
38
39 DLLManager::~DLLManager()
40 {
41         // close the library if it isn't null
42         if (h)
43                 dlclose(h);
44 }
45
46
47
48 bool DLLManager::GetSymbol(void** v, const char* sym_name)
49 {
50         // try extract a symbol from the library
51         // get any error message is there is any
52         
53         if (h)
54         {
55                 dlerror(); // clear value
56                 *v = dlsym(h, sym_name);
57                 err = (char*)dlerror();
58                 if (!*v || err)
59                         return false;
60         }
61         
62         if (err)
63         {
64                 return false;
65         }
66         else
67         {       
68                 return true;
69         }
70 }
71
72 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
73 {
74         // try get the factory function if there is no error yet
75         factory_func = 0;
76         
77         if (!LastError())
78         {
79                 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
80                 {
81                         throw ModuleException("Missing init_module() entrypoint!");
82                 }
83         }
84 }
85
86 DLLFactoryBase::~DLLFactoryBase()
87 {
88 }
89