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());
174 ServerInstance->GlobalCulls.AddItem(this);
179 bool ModuleManager::Unload(Module* mod)
183 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
187 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
190 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
192 callback->Call(false);
195 /* We must load the modules AFTER initializing the socket engine, now */
196 void ModuleManager::LoadAll()
200 printf("\nLoading core commands");
203 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
206 dirent* entry = NULL;
207 while (0 != (entry = readdir(library)))
209 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
214 if (!Load(entry->d_name))
216 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
217 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
218 ServerInstance->Exit(EXIT_STATUS_MODULE);
226 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
227 for(ConfigIter i = tags.first; i != tags.second; ++i)
229 ConfigTag* tag = i->second;
230 std::string name = tag->getString("name");
231 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
233 if (!this->Load(name.c_str()))
235 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
236 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
237 ServerInstance->Exit(EXIT_STATUS_MODULE);
242 void ModuleManager::UnloadAll()
244 /* We do this more than once, so that any service providers get a
245 * chance to be unhooked by the modules using them, but then get
246 * a chance to be removed themsleves.
248 * Note: this deliberately does NOT delete the DLLManager objects
250 for (int tries = 0; tries < 4; tries++)
252 std::map<std::string, Module*>::iterator i = Modules.begin();
253 while (i != Modules.end())
255 std::map<std::string, Module*>::iterator me = i++;
256 if (CanUnload(me->second))
258 DoSafeUnload(me->second);
261 ServerInstance->GlobalCulls.Apply();