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, newhandle->GetVersion().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();
140 struct UnloadAction : public HandlerBase0<void>
143 UnloadAction(Module* m) : mod(m) {}
146 DLLManager* dll = mod->ModuleDLLManager;
147 ServerInstance->Modules->DoSafeUnload(mod);
148 ServerInstance->GlobalCulls.Apply();
150 ServerInstance->GlobalCulls.AddItem(this);
154 struct ReloadAction : public HandlerBase0<void>
157 HandlerBase1<void, bool>* const callback;
158 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
159 : mod(m), callback(c) {}
162 DLLManager* dll = mod->ModuleDLLManager;
163 std::string name = mod->ModuleSourceFile;
164 ServerInstance->Modules->DoSafeUnload(mod);
165 ServerInstance->GlobalCulls.Apply();
167 bool rv = ServerInstance->Modules->Load(name.c_str());
169 ServerInstance->GlobalCulls.AddItem(this);
174 bool ModuleManager::Unload(Module* mod)
178 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
182 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
185 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
187 callback->Call(false);
190 /* We must load the modules AFTER initializing the socket engine, now */
191 void ModuleManager::LoadAll()
195 printf("\nLoading core commands");
198 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
201 dirent* entry = NULL;
202 while (0 != (entry = readdir(library)))
204 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
209 if (!Load(entry->d_name))
211 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
212 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
213 ServerInstance->Exit(EXIT_STATUS_MODULE);
221 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
222 for(ConfigIter i = tags.first; i != tags.second; ++i)
224 ConfigTag* tag = i->second;
225 std::string name = tag->getString("name");
226 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
228 if (!this->Load(name.c_str()))
230 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
231 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
232 ServerInstance->Exit(EXIT_STATUS_MODULE);
237 void ModuleManager::UnloadAll()
239 /* We do this more than once, so that any service providers get a
240 * chance to be unhooked by the modules using them, but then get
241 * a chance to be removed themsleves.
243 * Note: this deliberately does NOT delete the DLLManager objects
245 for (int tries = 0; tries < 4; tries++)
247 std::map<std::string, Module*>::iterator i = Modules.begin();
248 while (i != Modules.end())
250 std::map<std::string, Module*>::iterator me = i++;
251 if (CanUnload(me->second))
253 DoSafeUnload(me->second);
256 ServerInstance->GlobalCulls.Apply();