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"
34 #include <sys/types.h>
37 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
41 if (!strstr(fname,".so"))
43 err = "This doesn't look like a module file to me...";
47 this->staticname[0] = '\0';
48 ServerInstance->Log(DEBUG,"Loading core-compiled module '%s'",fname);
49 for (int j = 0; modsyms[j].name; j++)
51 ServerInstance->Log(DEBUG,"Check %s",modsyms[j].name);
52 if (!strcmp(modsyms[j].name,fname))
54 ServerInstance->Log(DEBUG,"Found %s",fname);
55 strlcpy(this->staticname,fname,1020);
60 err = "Module is not statically compiled into the ircd";
62 // Copy the library to a temp location, this makes recompiles
63 // a little safer if the ircd is running at the time as the
64 // shared libraries are mmap()ed and not doing this causes
66 FILE* x = fopen(fname,"rb");
69 err = strerror(errno);
72 ServerInstance->Log(DEBUG,"Opened module file %s",fname);
73 char tmpfile_template[255];
75 snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",ServerInstance->Config->TempDir,getpid());
76 int fd = mkstemp(tmpfile_template);
80 err = strerror(errno);
83 ServerInstance->Log(DEBUG,"Copying %s to %s",fname, tmpfile_template);
86 int n = fread(buffer, 1, 65535, x);
89 int written = write(fd,buffer,n);
93 err = strerror(errno);
98 ServerInstance->Log(DEBUG,"Copied entire file.");
99 // Try to open the library now and get any error message.
102 err = strerror(errno);
103 if (fclose(x) == EOF)
104 err = strerror(errno);
106 h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
109 ServerInstance->Log(DEBUG,"dlerror occured!");
110 err = (char*)dlerror();
114 ServerInstance->Log(DEBUG,"Finished loading '%s': %0x",tmpfile_template, h);
116 // We can delete the tempfile once it's loaded, leaving just the inode.
117 if (!err && !ServerInstance->Config->debugging)
119 ServerInstance->Log(DEBUG,"Deleteting %s",tmpfile_template);
120 if (unlink(tmpfile_template) == -1)
121 err = strerror(errno);
126 DLLManager::~DLLManager()
129 // close the library if it isn't null
139 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
141 for (int j = 0; modsyms[j].name; j++)
143 if (!strcmp(this->staticname,modsyms[j].name))
145 v = modsyms[j].value;
150 err = "Module symbol missing from the core";
156 bool DLLManager::GetSymbol(void** v, const char* sym_name)
158 // try extract a symbol from the library
159 // get any error message is there is any
163 dlerror(); // clear value
164 *v = dlsym(h, sym_name);
165 err = (char*)dlerror();
182 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
184 // try get the factory function if there is no error yet
190 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
192 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
195 throw ModuleException("Missing init_module() entrypoint!");
200 DLLFactoryBase::~DLLFactoryBase()