1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
20 #include "inspircd_io.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
33 #include <sys/types.h>
36 extern ServerConfig* Config;
38 DLLManager::DLLManager(char *fname)
41 this->staticname[0] = '\0';
42 log(DEBUG,"Loading core-compiled module '%s'",fname);
43 for (int j = 0; modsyms[j].name; j++)
45 log(DEBUG,"Check %s",modsyms[j].name);
46 if (!strcmp(modsyms[j].name,fname))
48 log(DEBUG,"Found %s",fname);
49 strlcpy(this->staticname,fname,1020);
54 err = "Module is not statically compiled into the ircd";
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 :)
63 h = dlopen(fname, RTLD_NOW );
64 err = (char*)dlerror();
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
71 FILE* x = fopen(fname,"rb");
72 char tmpfile_template[255];
74 snprintf(tmpfile_template, 255, "/tmp/inspircd_file.so.%d.XXXXXXXXXX",getpid());
75 int fd = mkstemp(tmpfile_template);
78 int n = fread(buffer, 1, 65535, x);
83 // Try to open the library now and get any error message.
85 h = dlopen(tmpfile_template, RTLD_NOW );
86 err = (char*)dlerror();
88 // We can delete the tempfile once it's loaded, leaving just the inode.
89 if (!Config->debugging)
90 unlink(tmpfile_template);
95 DLLManager::~DLLManager()
98 // close the library if it isn't null
108 bool DLLManager::GetSymbol(initfunc* &v, char *sym_name)
110 log(DEBUG,"Symbol search...");
111 for (int j = 0; modsyms[j].name; j++)
113 if (!strcmp(this->staticname,modsyms[j].name))
115 log(DEBUG,"Loading symbol...");
116 v = modsyms[j].value;
121 err = "Module symbol missing from the core";
127 bool DLLManager::GetSymbol(void **v, char *sym_name)
129 // try extract a symbol from the library
130 // get any error message is there is any
134 *v = dlsym( h, sym_name );
135 err = (char*)dlerror();
149 DLLFactoryBase::DLLFactoryBase(char *fname, char *factory) : DLLManager(fname)
151 // try get the factory function if there is no error yet
158 GetSymbol( factory_func, factory ? factory : (char*)"init_module" );
160 GetSymbol( (void **)&factory_func, factory ? factory : (char*)"init_module" );
166 DLLFactoryBase::~DLLFactoryBase()