]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_dynamic.cpp
d30f9f5954ae4911d24c16e55a6cc44189e82704
[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 char* filename)
29 {
30         /* Don't allow people to specify paths for modules, it doesn't work as expected */
31         if (strchr(filename, '/'))
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,'*') || (strchr(filename,'?')))
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, ascii_case_insensitive_map))
50                                 {
51                                         if (!this->Load(entry->d_name))
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);
66         std::string filename_str = filename;
67
68         if (!ServerConfig::FileExists(modfile))
69         {
70                 LastModuleError = "Module file could not be found: " + filename_str;
71                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
72                 return false;
73         }
74
75         if (Modules.find(filename_str) != Modules.end())
76         {
77                 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
78                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
79                 return false;
80         }
81
82         Module* newmod = NULL;
83         DLLManager* newhandle = new DLLManager(modfile);
84
85         try
86         {
87                 newmod = newhandle->CallInit();
88
89                 if (newmod)
90                 {
91                         newmod->ModuleSourceFile = filename_str;
92                         newmod->ModuleDLLManager = newhandle;
93                         Modules[filename_str] = newmod;
94                         newmod->init();
95
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]"));
99                 }
100                 else
101                 {
102                         LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
103                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
104                         delete newhandle;
105                         return false;
106                 }
107         }
108         catch (CoreException& modexcept)
109         {
110                 // failure in module constructor
111                 if (newmod)
112                         DoSafeUnload(newmod);
113                 delete newmod;
114                 delete newhandle;
115                 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
116                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
117                 return false;
118         }
119
120         this->ModCount++;
121         FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
122
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
125          * of others
126          */
127         for(int tries = 0; tries < 20; tries++)
128         {
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();
132
133                 if (prioritizationState == PRIO_STATE_LAST)
134                         break;
135                 if (tries == 19)
136                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
137         }
138
139         ServerInstance->BuildISupport();
140         return true;
141 }
142
143 namespace {
144         struct UnloadAction : public HandlerBase0<void>
145         {
146                 Module* const mod;
147                 UnloadAction(Module* m) : mod(m) {}
148                 void Call()
149                 {
150                         DLLManager* dll = mod->ModuleDLLManager;
151                         ServerInstance->Modules->DoSafeUnload(mod);
152                         ServerInstance->GlobalCulls.Apply();
153                         delete dll;
154                         ServerInstance->GlobalCulls.AddItem(this);
155                 }
156         };
157
158         struct ReloadAction : public HandlerBase0<void>
159         {
160                 Module* const mod;
161                 HandlerBase1<void, bool>* const callback;
162                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
163                         : mod(m), callback(c) {}
164                 void Call()
165                 {
166                         DLLManager* dll = mod->ModuleDLLManager;
167                         std::string name = mod->ModuleSourceFile;
168                         ServerInstance->Modules->DoSafeUnload(mod);
169                         ServerInstance->GlobalCulls.Apply();
170                         delete dll;
171                         bool rv = ServerInstance->Modules->Load(name.c_str());
172                         callback->Call(rv);
173                         ServerInstance->GlobalCulls.AddItem(this);
174                 }
175         };
176 }
177
178 bool ModuleManager::Unload(Module* mod)
179 {
180         if (!CanUnload(mod))
181                 return false;
182         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
183         return true;
184 }
185
186 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
187 {
188         if (CanUnload(mod))
189                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
190         else
191                 callback->Call(false);
192 }
193
194 /* We must load the modules AFTER initializing the socket engine, now */
195 void ModuleManager::LoadAll()
196 {
197         ModCount = 0;
198
199         printf("\nLoading core commands");
200         fflush(stdout);
201
202         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
203         if (library)
204         {
205                 dirent* entry = NULL;
206                 while (0 != (entry = readdir(library)))
207                 {
208                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
209                         {
210                                 printf(".");
211                                 fflush(stdout);
212
213                                 if (!Load(entry->d_name))
214                                 {
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);
218                                 }
219                         }
220                 }
221                 closedir(library);
222                 printf("\n");
223         }
224
225         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
226         for(ConfigIter i = tags.first; i != tags.second; ++i)
227         {
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());
231
232                 if (!this->Load(name.c_str()))
233                 {
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);
237                 }
238         }
239 }
240
241 void ModuleManager::UnloadAll()
242 {
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.
246          *
247          * Note: this deliberately does NOT delete the DLLManager objects
248          */
249         for (int tries = 0; tries < 4; tries++)
250         {
251                 std::map<std::string, Module*>::iterator i = Modules.begin();
252                 while (i != Modules.end())
253                 {
254                         std::map<std::string, Module*>::iterator me = i++;
255                         if (CanUnload(me->second))
256                         {
257                                 DoSafeUnload(me->second);
258                         }
259                 }
260                 ServerInstance->GlobalCulls.Apply();
261         }
262 }
263
264 #endif