1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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"
28 bool ModuleManager::Load(const char* filename)
30 /* Don't allow people to specify paths for modules, it doesn't work as expected */
31 if (strchr(filename, '/'))
33 /* Do we have a glob pattern in the filename?
34 * The user wants to load multiple modules which
37 if (strchr(filename,'*') || (strchr(filename,'?')))
40 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
43 /* Try and locate and load all modules matching the pattern */
45 while (0 != (entry = readdir(library)))
47 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
49 if (!this->Load(entry->d_name))
55 /* Loadmodule will now return false if any one of the modules failed
56 * to load (but wont abort when it encounters a bad one) and when 1 or
57 * more modules were actually loaded.
59 return (n_match > 0 ? false : true);
63 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
64 std::string filename_str = filename;
66 if (!ServerConfig::FileExists(modfile))
68 LastModuleError = "Module file could not be found: " + filename_str;
69 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
73 if (Modules.find(filename_str) != Modules.end())
75 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
76 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
80 Module* newmod = NULL;
81 DLLManager* newhandle = new DLLManager(modfile);
85 newmod = newhandle->callInit();
89 newmod->ModuleSourceFile = filename_str;
90 newmod->ModuleDLLManager = newhandle;
91 Version v = newmod->GetVersion();
93 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
94 filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
96 Modules[filename_str] = newmod;
100 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
101 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
106 catch (CoreException& modexcept)
108 // failure in module constructor
111 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
112 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
117 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
119 /* We give every module a chance to re-prioritize when we introduce a new one,
120 * not just the one thats loading, as the new module could affect the preference
123 for(int tries = 0; tries < 20; tries++)
125 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
126 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
127 n->second->Prioritize();
129 if (prioritizationState == PRIO_STATE_LAST)
132 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
135 ServerInstance->BuildISupport();
139 bool ModuleManager::CanUnload(Module* mod)
141 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
143 if (modfind == Modules.end() || modfind->second != mod)
145 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
146 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
149 if (mod->GetVersion().Flags & VF_STATIC)
151 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
152 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
158 void ModuleManager::DoSafeUnload(Module* mod)
160 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
162 std::vector<reference<ExtensionItem> > items;
163 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
164 /* Give the module a chance to tidy out all its metadata */
165 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
167 mod->OnCleanup(TYPE_CHANNEL,c->second);
168 c->second->doUnhookExtensions(items);
169 const UserMembList* users = c->second->GetUsers();
170 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
171 mi->second->doUnhookExtensions(items);
173 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
175 mod->OnCleanup(TYPE_USER,u->second);
176 u->second->doUnhookExtensions(items);
178 for(char m='A'; m <= 'z'; m++)
181 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
182 if (mh && mh->creator == mod)
183 ServerInstance->Modes->DelMode(mh);
184 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
185 if (mh && mh->creator == mod)
186 ServerInstance->Modes->DelMode(mh);
188 for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
190 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
191 if (curr->second->creator == mod)
192 DataProviders.erase(curr);
194 for(unsigned int i = 0; i < ServerInstance->Modules->ActiveDynrefs.size(); i++)
195 ServerInstance->Modules->ActiveDynrefs[i]->ClearCache();
197 /* Tidy up any dangling resolvers */
198 ServerInstance->Res->CleanResolvers(mod);
200 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
204 Modules.erase(modfind);
205 ServerInstance->GlobalCulls.AddItem(mod);
207 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
209 ServerInstance->BuildISupport();
213 struct UnloadAction : public HandlerBase0<void>
216 UnloadAction(Module* m) : mod(m) {}
219 DLLManager* dll = mod->ModuleDLLManager;
220 ServerInstance->Modules->DoSafeUnload(mod);
221 ServerInstance->GlobalCulls.Apply();
223 ServerInstance->GlobalCulls.AddItem(this);
227 struct ReloadAction : public HandlerBase0<void>
230 HandlerBase1<void, bool>* const callback;
231 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
232 : mod(m), callback(c) {}
235 DLLManager* dll = mod->ModuleDLLManager;
236 std::string name = mod->ModuleSourceFile;
237 ServerInstance->Modules->DoSafeUnload(mod);
238 ServerInstance->GlobalCulls.Apply();
240 bool rv = ServerInstance->Modules->Load(name.c_str());
242 ServerInstance->GlobalCulls.AddItem(this);
247 bool ModuleManager::Unload(Module* mod)
251 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
255 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
258 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
260 callback->Call(false);
263 /* We must load the modules AFTER initializing the socket engine, now */
264 void ModuleManager::LoadAll()
268 printf("\nLoading core commands");
271 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
274 dirent* entry = NULL;
275 while (0 != (entry = readdir(library)))
277 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
282 if (!Load(entry->d_name))
284 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
285 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
286 ServerInstance->Exit(EXIT_STATUS_MODULE);
294 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
295 for(ConfigIter i = tags.first; i != tags.second; ++i)
297 ConfigTag* tag = i->second;
298 std::string name = tag->getString("name");
299 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
301 if (!this->Load(name.c_str()))
303 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
304 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
305 ServerInstance->Exit(EXIT_STATUS_MODULE);
310 void ModuleManager::UnloadAll()
312 /* We do this more than once, so that any service providers get a
313 * chance to be unhooked by the modules using them, but then get
314 * a chance to be removed themsleves.
316 * Note: this deliberately does NOT delete the DLLManager objects
318 for (int tries = 0; tries < 4; tries++)
320 std::map<std::string, Module*>::iterator i = Modules.begin();
321 while (i != Modules.end())
323 std::map<std::string, Module*>::iterator me = i++;
324 if (CanUnload(me->second))
326 DoSafeUnload(me->second);
329 ServerInstance->GlobalCulls.Apply();