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 (entry->d_name[0] == '.')
49 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
51 if (!this->Load(entry->d_name))
57 /* Loadmodule will now return false if any one of the modules failed
58 * to load (but wont abort when it encounters a bad one) and when 1 or
59 * more modules were actually loaded.
61 return (n_match > 0 ? false : true);
65 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
66 std::string filename_str = filename;
68 if (!ServerConfig::FileExists(modfile))
70 LastModuleError = "Module file could not be found: " + filename_str;
71 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
75 if (Modules.find(filename_str) != Modules.end())
77 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
78 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
82 Module* newmod = NULL;
83 DLLManager* newhandle = new DLLManager(modfile);
87 newmod = newhandle->CallInit();
91 newmod->ModuleSourceFile = filename_str;
92 newmod->ModuleDLLManager = newhandle;
93 Modules[filename_str] = newmod;
96 Version v = newmod->GetVersion();
97 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
98 filename, newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
102 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
103 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
108 catch (CoreException& modexcept)
110 // failure in module constructor
112 DoSafeUnload(newmod);
115 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
116 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
121 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
123 /* We give every module a chance to re-prioritize when we introduce a new one,
124 * not just the one thats loading, as the new module could affect the preference
127 for(int tries = 0; tries < 20; tries++)
129 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
130 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
131 n->second->Prioritize();
133 if (prioritizationState == PRIO_STATE_LAST)
136 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
139 ServerInstance->BuildISupport();
144 struct UnloadAction : public HandlerBase0<void>
147 UnloadAction(Module* m) : mod(m) {}
150 DLLManager* dll = mod->ModuleDLLManager;
151 ServerInstance->Modules->DoSafeUnload(mod);
152 ServerInstance->GlobalCulls.Apply();
154 ServerInstance->GlobalCulls.AddItem(this);
158 struct ReloadAction : public HandlerBase0<void>
161 HandlerBase1<void, bool>* const callback;
162 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
163 : mod(m), callback(c) {}
166 DLLManager* dll = mod->ModuleDLLManager;
167 std::string name = mod->ModuleSourceFile;
168 ServerInstance->Modules->DoSafeUnload(mod);
169 ServerInstance->GlobalCulls.Apply();
171 bool rv = ServerInstance->Modules->Load(name.c_str());
173 ServerInstance->GlobalCulls.AddItem(this);
178 bool ModuleManager::Unload(Module* mod)
182 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
186 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
189 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
191 callback->Call(false);
194 /* We must load the modules AFTER initializing the socket engine, now */
195 void ModuleManager::LoadAll()
199 printf("\nLoading core commands");
202 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
205 dirent* entry = NULL;
206 while (0 != (entry = readdir(library)))
208 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
213 if (!Load(entry->d_name))
215 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
216 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
217 ServerInstance->Exit(EXIT_STATUS_MODULE);
225 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
226 for(ConfigIter i = tags.first; i != tags.second; ++i)
228 ConfigTag* tag = i->second;
229 std::string name = tag->getString("name");
230 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
232 if (!this->Load(name.c_str()))
234 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
235 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
236 ServerInstance->Exit(EXIT_STATUS_MODULE);
241 void ModuleManager::UnloadAll()
243 /* We do this more than once, so that any service providers get a
244 * chance to be unhooked by the modules using them, but then get
245 * a chance to be removed themsleves.
247 * Note: this deliberately does NOT delete the DLLManager objects
249 for (int tries = 0; tries < 4; tries++)
251 std::map<std::string, Module*>::iterator i = Modules.begin();
252 while (i != Modules.end())
254 std::map<std::string, Module*>::iterator me = i++;
255 if (CanUnload(me->second))
257 DoSafeUnload(me->second);
260 ServerInstance->GlobalCulls.Apply();