]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dynamic.cpp
Added ability to update the helpop file on rehash (Bug #69)
[user/henk/code/inspircd.git] / src / dynamic.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 "globals.h"
20 #include "inspircd_config.h"
21 #include "dynamic.h"
22
23 #ifndef STATIC_LINK
24 #include <dlfcn.h>
25 #else
26 #include "modlist.h"
27 #endif
28
29 #include "inspstring.h"
30 #include "helperfuncs.h"
31
32 DLLManager::DLLManager(char *fname)
33 {
34 #ifdef STATIC_LINK
35         this->staticname[0] = '\0';
36         log(DEBUG,"Loading core-compiled module '%s'",fname);
37         for (int j = 0; modsyms[j].name; j++)
38         {
39                 log(DEBUG,"Check %s",modsyms[j].name);
40                 if (!strcmp(modsyms[j].name,fname))
41                 {
42                         log(DEBUG,"Found %s",fname);
43                         strlcpy(this->staticname,fname,1020);
44                         err = 0;
45                         return;
46                 }
47         }
48         err = "Module is not statically compiled into the ircd";
49 #else
50     // Try to open the library now and get any error message.
51         
52         h = dlopen( fname, RTLD_NOW );
53         err = (char*)dlerror();
54 #endif
55 }
56
57 DLLManager::~DLLManager()
58 {
59 #ifndef STATIC_LINK
60         // close the library if it isn't null
61         if (h != 0)
62         dlclose(h);
63 #endif
64 }
65
66
67
68 #ifdef STATIC_LINK
69
70 bool DLLManager::GetSymbol(initfunc* &v, char *sym_name)
71 {
72         log(DEBUG,"Symbol search...");
73         for (int j = 0; modsyms[j].name; j++)
74         {
75                 if (!strcmp(this->staticname,modsyms[j].name))
76                 {
77                         log(DEBUG,"Loading symbol...");
78                         v = modsyms[j].value;
79                         err = 0;
80                         return true;
81                 }
82         }
83         err = "Module symbol missing from the core";
84         return false;
85 }
86
87 #else
88
89 bool DLLManager::GetSymbol(void **v, char *sym_name)
90 {
91         // try extract a symbol from the library
92         // get any error message is there is any
93         
94         if(h != 0)
95         {
96                 *v = dlsym( h, sym_name );
97                 err = (char*)dlerror();
98                 if( err == 0 )
99                         return true;
100                 else
101                         return false;
102         }
103         else
104         {       
105                 return false;
106         }
107 }
108
109 #endif
110
111 DLLFactoryBase::DLLFactoryBase(char *fname, char *factory) : DLLManager(fname)
112 {
113         // try get the factory function if there is no error yet
114         
115         factory_func = 0;
116         
117         if(LastError() == 0)
118         {
119 #ifdef STATIC_LINK
120                 GetSymbol( factory_func, factory ? factory : (char*)"init_module" );
121 #else
122                 GetSymbol( (void **)&factory_func, factory ? factory : (char*)"init_module" );
123 #endif
124         }
125 }
126
127
128 DLLFactoryBase::~DLLFactoryBase()
129 {
130 }