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