]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_dynamic.cpp
3845fd6da45e50b9679f73c4a743f461cce8792f
[user/henk/code/inspircd.git] / src / modmanager_dynamic.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16 #include "socket.h"
17 #include "socketengine.h"
18 #include "command_parse.h"
19 #include "dns.h"
20 #include "exitcodes.h"
21
22 #ifndef WIN32
23 #include <dirent.h>
24 #endif
25
26 #ifndef PURE_STATIC
27
28 bool ModuleManager::Load(const std::string& filename, bool defer)
29 {
30         /* Don't allow people to specify paths for modules, it doesn't work as expected */
31         if (filename.find('/') != std::string::npos)
32                 return false;
33         /* Do we have a glob pattern in the filename?
34          * The user wants to load multiple modules which
35          * match the pattern.
36          */
37         if (strchr(filename.c_str(),'*') || (strchr(filename.c_str(),'?')))
38         {
39                 int n_match = 0;
40                 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
41                 if (library)
42                 {
43                         /* Try and locate and load all modules matching the pattern */
44                         dirent* entry = NULL;
45                         while (0 != (entry = readdir(library)))
46                         {
47                                 if (entry->d_name[0] == '.')
48                                         continue;
49                                 if (InspIRCd::Match(entry->d_name, filename.c_str(), ascii_case_insensitive_map))
50                                 {
51                                         if (!this->Load(entry->d_name, defer))
52                                                 n_match++;
53                                 }
54                         }
55                         closedir(library);
56                 }
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.
60                  */
61                 return (n_match > 0 ? false : true);
62         }
63
64         char modfile[MAXBUF];
65         snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
66
67         if (!ServerConfig::FileExists(modfile))
68         {
69                 LastModuleError = "Module file could not be found: " + filename;
70                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
71                 return false;
72         }
73
74         if (Modules.find(filename) != Modules.end())
75         {
76                 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
77                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
78                 return false;
79         }
80
81         Module* newmod = NULL;
82         DLLManager* newhandle = new DLLManager(modfile);
83
84         try
85         {
86                 newmod = newhandle->CallInit();
87
88                 if (newmod)
89                 {
90                         newmod->ModuleSourceFile = filename;
91                         newmod->ModuleDLLManager = newhandle;
92                         Modules[filename] = newmod;
93                         if (defer)
94                         {
95                                 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
96                                         filename.c_str(), newhandle->GetVersion().c_str());
97                         }
98                         else
99                         {
100                                 newmod->init();
101
102                                 Version v = newmod->GetVersion();
103                                 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
104                                         filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
105                         }
106                 }
107                 else
108                 {
109                         LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
110                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
111                         delete newhandle;
112                         return false;
113                 }
114         }
115         catch (CoreException& modexcept)
116         {
117                 // failure in module constructor
118                 if (newmod)
119                         DoSafeUnload(newmod);
120                 else
121                         delete newhandle;
122                 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
123                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
124                 return false;
125         }
126
127         this->ModCount++;
128         if (defer)
129                 return true;
130
131         FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
132         /* We give every module a chance to re-prioritize when we introduce a new one,
133          * not just the one thats loading, as the new module could affect the preference
134          * of others
135          */
136         for(int tries = 0; tries < 20; tries++)
137         {
138                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
139                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
140                         n->second->Prioritize();
141
142                 if (prioritizationState == PRIO_STATE_LAST)
143                         break;
144                 if (tries == 19)
145                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
146         }
147
148         ServerInstance->BuildISupport();
149         return true;
150 }
151
152 namespace {
153         struct UnloadAction : public HandlerBase0<void>
154         {
155                 Module* const mod;
156                 UnloadAction(Module* m) : mod(m) {}
157                 void Call()
158                 {
159                         DLLManager* dll = mod->ModuleDLLManager;
160                         ServerInstance->Modules->DoSafeUnload(mod);
161                         ServerInstance->GlobalCulls.Apply();
162                         delete dll;
163                         ServerInstance->GlobalCulls.AddItem(this);
164                 }
165         };
166
167         struct ReloadAction : public HandlerBase0<void>
168         {
169                 Module* const mod;
170                 HandlerBase1<void, bool>* const callback;
171                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
172                         : mod(m), callback(c) {}
173                 void Call()
174                 {
175                         DLLManager* dll = mod->ModuleDLLManager;
176                         std::string name = mod->ModuleSourceFile;
177                         ServerInstance->Modules->DoSafeUnload(mod);
178                         ServerInstance->GlobalCulls.Apply();
179                         delete dll;
180                         bool rv = ServerInstance->Modules->Load(name.c_str());
181                         if (callback)
182                                 callback->Call(rv);
183                         ServerInstance->GlobalCulls.AddItem(this);
184                 }
185         };
186 }
187
188 bool ModuleManager::Unload(Module* mod)
189 {
190         if (!CanUnload(mod))
191                 return false;
192         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
193         return true;
194 }
195
196 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
197 {
198         if (CanUnload(mod))
199                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
200         else
201                 callback->Call(false);
202 }
203
204 /* We must load the modules AFTER initializing the socket engine, now */
205 void ModuleManager::LoadAll()
206 {
207         ModCount = 0;
208
209         printf("\nLoading core commands");
210         fflush(stdout);
211
212         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
213         if (library)
214         {
215                 dirent* entry = NULL;
216                 while (0 != (entry = readdir(library)))
217                 {
218                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
219                         {
220                                 printf(".");
221                                 fflush(stdout);
222
223                                 if (!Load(entry->d_name, true))
224                                 {
225                                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
226                                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
227                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
228                                 }
229                         }
230                 }
231                 closedir(library);
232                 printf("\n");
233         }
234
235         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
236         for(ConfigIter i = tags.first; i != tags.second; ++i)
237         {
238                 ConfigTag* tag = i->second;
239                 std::string name = tag->getString("name");
240                 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
241
242                 if (!this->Load(name, true))
243                 {
244                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
245                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
246                         ServerInstance->Exit(EXIT_STATUS_MODULE);
247                 }
248         }
249
250         for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
251         {
252                 Module* mod = i->second;
253                 try 
254                 {
255                         ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
256                         mod->init();
257                 }
258                 catch (CoreException& modexcept)
259                 {
260                         LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
261                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
262                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
263                         ServerInstance->Exit(EXIT_STATUS_MODULE);
264                 }
265         }
266
267         /* We give every module a chance to re-prioritize when we introduce a new one,
268          * not just the one thats loading, as the new module could affect the preference
269          * of others
270          */
271         for(int tries = 0; tries < 20; tries++)
272         {
273                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
274                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
275                         n->second->Prioritize();
276
277                 if (prioritizationState == PRIO_STATE_LAST)
278                         break;
279                 if (tries == 19)
280                 {
281                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
282                         ServerInstance->Exit(EXIT_STATUS_MODULE);
283                 }
284         }
285 }
286
287 #endif