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 "configreader.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
33 #include <sys/types.h>
36 extern InspIRCd* ServerInstance;
38 DLLManager::DLLManager(const char *fname)
42 if (!strstr(fname,".so"))
44 err = "This doesn't look like a module file to me...";
48 this->staticname[0] = '\0';
49 log(DEBUG,"Loading core-compiled module '%s'",fname);
50 for (int j = 0; modsyms[j].name; j++)
52 log(DEBUG,"Check %s",modsyms[j].name);
53 if (!strcmp(modsyms[j].name,fname))
55 log(DEBUG,"Found %s",fname);
56 strlcpy(this->staticname,fname,1020);
61 err = "Module is not statically compiled into the ircd";
63 // Copy the library to a temp location, this makes recompiles
64 // a little safer if the ircd is running at the time as the
65 // shared libraries are mmap()ed and not doing this causes
67 FILE* x = fopen(fname,"rb");
70 err = strerror(errno);
73 log(DEBUG,"Opened module file %s",fname);
74 char tmpfile_template[255];
76 snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",ServerInstance->Config->TempDir,getpid());
77 int fd = mkstemp(tmpfile_template);
81 err = strerror(errno);
84 log(DEBUG,"Copying %s to %s",fname, tmpfile_template);
87 int n = fread(buffer, 1, 65535, x);
90 int written = write(fd,buffer,n);
94 err = strerror(errno);
99 log(DEBUG,"Copied entire file.");
100 // Try to open the library now and get any error message.
103 err = strerror(errno);
104 if (fclose(x) == EOF)
105 err = strerror(errno);
107 h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
110 log(DEBUG,"dlerror occured!");
111 err = (char*)dlerror();
115 log(DEBUG,"Finished loading '%s': %0x",tmpfile_template, h);
117 // We can delete the tempfile once it's loaded, leaving just the inode.
118 if (!err && !ServerInstance->Config->debugging)
120 log(DEBUG,"Deleteting %s",tmpfile_template);
121 if (unlink(tmpfile_template) == -1)
122 err = strerror(errno);
127 DLLManager::~DLLManager()
130 // close the library if it isn't null
140 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
142 log(DEBUG,"Symbol search...");
143 for (int j = 0; modsyms[j].name; j++)
145 if (!strcmp(this->staticname,modsyms[j].name))
147 log(DEBUG,"Loading symbol...");
148 v = modsyms[j].value;
153 err = "Module symbol missing from the core";
159 bool DLLManager::GetSymbol(void** v, const char* sym_name)
161 // try extract a symbol from the library
162 // get any error message is there is any
166 log(DEBUG,"Found symbol %s", sym_name);
167 dlerror(); // clear value
168 *v = dlsym(h, sym_name);
169 err = (char*)dlerror();
186 DLLFactoryBase::DLLFactoryBase(const char* fname, const char* symbol) : DLLManager(fname)
188 // try get the factory function if there is no error yet
194 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
196 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
199 throw ModuleException("Missing init_module() entrypoint!");
204 DLLFactoryBase::~DLLFactoryBase()