1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "socketengine.h"
18 #include "command_parse.h"
20 #include "exitcodes.h"
24 bool ModuleManager::Load(const char* filename)
26 /* Don't allow people to specify paths for modules, it doesn't work as expected */
27 if (strchr(filename, '/'))
29 /* Do we have a glob pattern in the filename?
30 * The user wants to load multiple modules which
33 if (strchr(filename,'*') || (strchr(filename,'?')))
36 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
39 /* Try and locate and load all modules matching the pattern */
41 while (0 != (entry = readdir(library)))
43 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
45 if (!this->Load(entry->d_name))
51 /* Loadmodule will now return false if any one of the modules failed
52 * to load (but wont abort when it encounters a bad one) and when 1 or
53 * more modules were actually loaded.
55 return (n_match > 0 ? false : true);
59 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
60 std::string filename_str = filename;
62 if (!ServerConfig::FileExists(modfile))
64 LastModuleError = "Module file could not be found: " + filename_str;
65 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
69 if (Modules.find(filename_str) != Modules.end())
71 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
72 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
76 Module* newmod = NULL;
77 DLLManager* newhandle = new DLLManager(modfile);
81 newmod = newhandle->callInit();
85 newmod->ModuleSourceFile = filename_str;
86 newmod->ModuleDLLManager = newhandle;
87 Version v = newmod->GetVersion();
89 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
90 filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
92 Modules[filename_str] = newmod;
96 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
97 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
102 catch (CoreException& modexcept)
104 // failure in module constructor
107 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
108 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
113 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
115 /* We give every module a chance to re-prioritize when we introduce a new one,
116 * not just the one thats loading, as the new module could affect the preference
119 for(int tries = 0; tries < 20; tries++)
121 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
122 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
123 n->second->Prioritize();
125 if (prioritizationState == PRIO_STATE_LAST)
128 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
131 ServerInstance->BuildISupport();
135 bool ModuleManager::CanUnload(Module* mod)
137 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
139 if (modfind == Modules.end() || modfind->second != mod)
141 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
142 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
145 if (mod->GetVersion().Flags & VF_STATIC)
147 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
148 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
151 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(mod);
152 if (intercount.first > 0)
154 LastModuleError = "Failed to unload module " + mod->ModuleSourceFile + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
155 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
161 void ModuleManager::DoSafeUnload(Module* mod)
163 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
165 std::vector<reference<ExtensionItem> > items;
166 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
167 /* Give the module a chance to tidy out all its metadata */
168 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
170 mod->OnCleanup(TYPE_CHANNEL,c->second);
171 c->second->doUnhookExtensions(items);
172 const UserMembList* users = c->second->GetUsers();
173 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
174 mi->second->doUnhookExtensions(items);
176 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
178 mod->OnCleanup(TYPE_USER,u->second);
179 u->second->doUnhookExtensions(items);
181 for(char m='A'; m <= 'z'; m++)
184 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
185 if (mh && mh->creator == mod)
186 ServerInstance->Modes->DelMode(mh);
187 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
188 if (mh && mh->creator == mod)
189 ServerInstance->Modes->DelMode(mh);
192 /* Tidy up any dangling resolvers */
193 ServerInstance->Res->CleanResolvers(mod);
195 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
199 Modules.erase(modfind);
200 ServerInstance->GlobalCulls.AddItem(mod);
202 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
204 ServerInstance->BuildISupport();
208 struct UnloadAction : public HandlerBase0<void>
211 UnloadAction(Module* m) : mod(m) {}
214 DLLManager* dll = mod->ModuleDLLManager;
215 ServerInstance->Modules->DoSafeUnload(mod);
216 ServerInstance->GlobalCulls.Apply();
218 ServerInstance->GlobalCulls.AddItem(this);
222 struct ReloadAction : public HandlerBase0<void>
225 HandlerBase1<void, bool>* const callback;
226 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
227 : mod(m), callback(c) {}
230 DLLManager* dll = mod->ModuleDLLManager;
231 std::string name = mod->ModuleSourceFile;
232 ServerInstance->Modules->DoSafeUnload(mod);
233 ServerInstance->GlobalCulls.Apply();
235 bool rv = ServerInstance->Modules->Load(name.c_str());
237 ServerInstance->GlobalCulls.AddItem(this);
242 bool ModuleManager::Unload(Module* mod)
246 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
250 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
253 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
255 callback->Call(false);
258 /* We must load the modules AFTER initializing the socket engine, now */
259 void ModuleManager::LoadAll()
263 printf("\nLoading core commands");
266 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
269 dirent* entry = NULL;
270 while (0 != (entry = readdir(library)))
272 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
277 if (!Load(entry->d_name))
279 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
280 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
281 ServerInstance->Exit(EXIT_STATUS_MODULE);
289 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
290 for(ConfigIter i = tags.first; i != tags.second; ++i)
292 ConfigTag* tag = i->second;
293 std::string name = tag->getString("name");
294 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
296 if (!this->Load(name.c_str()))
298 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
299 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
300 ServerInstance->Exit(EXIT_STATUS_MODULE);
305 void ModuleManager::UnloadAll()
307 /* We do this more than once, so that any service providers get a
308 * chance to be unhooked by the modules using them, but then get
309 * a chance to be removed themsleves.
311 * Note: this deliberately does NOT delete the DLLManager objects
313 for (int tries = 0; tries < 4; tries++)
315 std::map<std::string, Module*>::iterator i = Modules.begin();
316 while (i != Modules.end())
318 std::map<std::string, Module*>::iterator me = i++;
319 if (CanUnload(me->second))
321 DoSafeUnload(me->second);
324 ServerInstance->GlobalCulls.Apply();