]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_dynamic.cpp
Define comparator for irc::sockets::sockaddrs
[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", filename.c_str());
96                         }
97                         else
98                         {
99                                 newmod->init();
100
101                                 Version v = newmod->GetVersion();
102                                 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
103                                         filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
104                         }
105                 }
106                 else
107                 {
108                         LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
109                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
110                         delete newhandle;
111                         return false;
112                 }
113         }
114         catch (CoreException& modexcept)
115         {
116                 // failure in module constructor
117                 if (newmod)
118                         DoSafeUnload(newmod);
119                 else
120                         delete newhandle;
121                 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
122                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
123                 return false;
124         }
125
126         this->ModCount++;
127         if (defer)
128                 return true;
129
130         FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
131         /* We give every module a chance to re-prioritize when we introduce a new one,
132          * not just the one thats loading, as the new module could affect the preference
133          * of others
134          */
135         for(int tries = 0; tries < 20; tries++)
136         {
137                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
138                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
139                         n->second->Prioritize();
140
141                 if (prioritizationState == PRIO_STATE_LAST)
142                         break;
143                 if (tries == 19)
144                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
145         }
146
147         ServerInstance->BuildISupport();
148         return true;
149 }
150
151 namespace {
152         struct UnloadAction : public HandlerBase0<void>
153         {
154                 Module* const mod;
155                 UnloadAction(Module* m) : mod(m) {}
156                 void Call()
157                 {
158                         DLLManager* dll = mod->ModuleDLLManager;
159                         ServerInstance->Modules->DoSafeUnload(mod);
160                         ServerInstance->GlobalCulls.Apply();
161                         delete dll;
162                         ServerInstance->GlobalCulls.AddItem(this);
163                 }
164         };
165
166         struct ReloadAction : public HandlerBase0<void>
167         {
168                 Module* const mod;
169                 HandlerBase1<void, bool>* const callback;
170                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
171                         : mod(m), callback(c) {}
172                 void Call()
173                 {
174                         DLLManager* dll = mod->ModuleDLLManager;
175                         std::string name = mod->ModuleSourceFile;
176                         ServerInstance->Modules->DoSafeUnload(mod);
177                         ServerInstance->GlobalCulls.Apply();
178                         delete dll;
179                         bool rv = ServerInstance->Modules->Load(name.c_str());
180                         if (callback)
181                                 callback->Call(rv);
182                         ServerInstance->GlobalCulls.AddItem(this);
183                 }
184         };
185 }
186
187 bool ModuleManager::Unload(Module* mod)
188 {
189         if (!CanUnload(mod))
190                 return false;
191         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
192         return true;
193 }
194
195 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
196 {
197         if (CanUnload(mod))
198                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
199         else
200                 callback->Call(false);
201 }
202
203 /* We must load the modules AFTER initializing the socket engine, now */
204 void ModuleManager::LoadAll()
205 {
206         ModCount = 0;
207
208         printf("\nLoading core commands");
209         fflush(stdout);
210
211         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
212         if (library)
213         {
214                 dirent* entry = NULL;
215                 while (0 != (entry = readdir(library)))
216                 {
217                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
218                         {
219                                 printf(".");
220                                 fflush(stdout);
221
222                                 if (!Load(entry->d_name, true))
223                                 {
224                                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
225                                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
226                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
227                                 }
228                         }
229                 }
230                 closedir(library);
231                 printf("\n");
232         }
233
234         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
235         for(ConfigIter i = tags.first; i != tags.second; ++i)
236         {
237                 ConfigTag* tag = i->second;
238                 std::string name = tag->getString("name");
239                 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
240
241                 if (!this->Load(name, true))
242                 {
243                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
244                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
245                         ServerInstance->Exit(EXIT_STATUS_MODULE);
246                 }
247         }
248
249         for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
250         {
251                 Module* mod = i->second;
252                 try 
253                 {
254                         mod->init();
255                 }
256                 catch (CoreException& modexcept)
257                 {
258                         LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
259                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
260                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
261                         ServerInstance->Exit(EXIT_STATUS_MODULE);
262                 }
263         }
264
265         /* We give every module a chance to re-prioritize when we introduce a new one,
266          * not just the one thats loading, as the new module could affect the preference
267          * of others
268          */
269         for(int tries = 0; tries < 20; tries++)
270         {
271                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
272                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
273                         n->second->Prioritize();
274
275                 if (prioritizationState == PRIO_STATE_LAST)
276                         break;
277                 if (tries == 19)
278                 {
279                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
280                         ServerInstance->Exit(EXIT_STATUS_MODULE);
281                 }
282         }
283 }
284
285 #endif