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 "configreader.h"
30 DLLManager::DLLManager(InspIRCd* ServerInstance, const char *fname)
34 if (!strstr(fname,".so"))
36 err = "This doesn't look like a module file to me...";
40 this->staticname[0] = '\0';
41 ServerInstance->Log(DEBUG,"Loading core-compiled module '%s'",fname);
42 for (int j = 0; modsyms[j].name; j++)
44 ServerInstance->Log(DEBUG,"Check %s",modsyms[j].name);
45 if (!strcmp(modsyms[j].name,fname))
47 ServerInstance->Log(DEBUG,"Found %s",fname);
48 strlcpy(this->staticname,fname,1020);
53 err = "Module is not statically compiled into the ircd";
55 // Copy the library to a temp location, this makes recompiles
56 // a little safer if the ircd is running at the time as the
57 // shared libraries are mmap()ed and not doing this causes
59 FILE* x = fopen(fname,"rb");
62 err = strerror(errno);
65 ServerInstance->Log(DEBUG,"Opened module file %s",fname);
66 char tmpfile_template[255];
68 snprintf(tmpfile_template, 255, "%s/inspircd_file.so.%d.XXXXXXXXXX",ServerInstance->Config->TempDir,getpid());
69 int fd = mkstemp(tmpfile_template);
73 err = strerror(errno);
76 ServerInstance->Log(DEBUG,"Copying %s to %s",fname, tmpfile_template);
79 int n = fread(buffer, 1, 65535, x);
82 int written = write(fd,buffer,n);
86 err = strerror(errno);
91 ServerInstance->Log(DEBUG,"Copied entire file.");
92 // Try to open the library now and get any error message.
95 err = strerror(errno);
97 err = strerror(errno);
99 h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
102 ServerInstance->Log(DEBUG,"dlerror occured!");
103 err = (char*)dlerror();
107 ServerInstance->Log(DEBUG,"Finished loading '%s': %0x",tmpfile_template, h);
109 // We can delete the tempfile once it's loaded, leaving just the inode.
110 if (!err && !ServerInstance->Config->debugging)
112 ServerInstance->Log(DEBUG,"Deleteting %s",tmpfile_template);
113 if (unlink(tmpfile_template) == -1)
114 err = strerror(errno);
119 DLLManager::~DLLManager()
122 // close the library if it isn't null
132 bool DLLManager::GetSymbol(initfunc* &v, const char *sym_name)
134 for (int j = 0; modsyms[j].name; j++)
136 if (!strcmp(this->staticname,modsyms[j].name))
138 v = modsyms[j].value;
143 err = "Module symbol missing from the core";
149 bool DLLManager::GetSymbol(void** v, const char* sym_name)
151 // try extract a symbol from the library
152 // get any error message is there is any
156 dlerror(); // clear value
157 *v = dlsym(h, sym_name);
158 err = (char*)dlerror();
175 DLLFactoryBase::DLLFactoryBase(InspIRCd* Instance, const char* fname, const char* symbol) : DLLManager(Instance, fname)
177 // try get the factory function if there is no error yet
183 if (!GetSymbol( factory_func, symbol ? symbol : "init_module"))
185 if (!GetSymbol( (void **)&factory_func, symbol ? symbol : "init_module"))
188 throw ModuleException("Missing init_module() entrypoint!");
193 DLLFactoryBase::~DLLFactoryBase()