]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_dynamic.cpp
Fall back to copying bind IP if getsockname() fails, as it apparently can on Windows
[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 (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
48                                 {
49                                         if (!this->Load(entry->d_name))
50                                                 n_match++;
51                                 }
52                         }
53                         closedir(library);
54                 }
55                 /* Loadmodule will now return false if any one of the modules failed
56                  * to load (but wont abort when it encounters a bad one) and when 1 or
57                  * more modules were actually loaded.
58                  */
59                 return (n_match > 0 ? false : true);
60         }
61
62         char modfile[MAXBUF];
63         snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
64         std::string filename_str = filename;
65
66         if (!ServerConfig::FileExists(modfile))
67         {
68                 LastModuleError = "Module file could not be found: " + filename_str;
69                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
70                 return false;
71         }
72
73         if (Modules.find(filename_str) != Modules.end())
74         {
75                 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
76                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
77                 return false;
78         }
79
80         Module* newmod = NULL;
81         DLLManager* newhandle = new DLLManager(modfile);
82
83         try
84         {
85                 newmod = newhandle->CallInit();
86
87                 if (newmod)
88                 {
89                         newmod->ModuleSourceFile = filename_str;
90                         newmod->ModuleDLLManager = newhandle;
91                         Version v = newmod->GetVersion();
92                         Modules[filename_str] = newmod;
93
94                         newmod->init();
95
96                         ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
97                                 filename, newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
98                 }
99                 else
100                 {
101                         LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
102                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
103                         delete newhandle;
104                         return false;
105                 }
106         }
107         catch (CoreException& modexcept)
108         {
109                 // failure in module constructor
110                 if (newmod)
111                         DoSafeUnload(newmod);
112                 delete newmod;
113                 delete newhandle;
114                 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
115                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
116                 return false;
117         }
118
119         this->ModCount++;
120         FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
121
122         /* We give every module a chance to re-prioritize when we introduce a new one,
123          * not just the one thats loading, as the new module could affect the preference
124          * of others
125          */
126         for(int tries = 0; tries < 20; tries++)
127         {
128                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
129                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
130                         n->second->Prioritize();
131
132                 if (prioritizationState == PRIO_STATE_LAST)
133                         break;
134                 if (tries == 19)
135                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
136         }
137
138         ServerInstance->BuildISupport();
139         return true;
140 }
141
142 namespace {
143         struct UnloadAction : public HandlerBase0<void>
144         {
145                 Module* const mod;
146                 UnloadAction(Module* m) : mod(m) {}
147                 void Call()
148                 {
149                         DLLManager* dll = mod->ModuleDLLManager;
150                         ServerInstance->Modules->DoSafeUnload(mod);
151                         ServerInstance->GlobalCulls.Apply();
152                         delete dll;
153                         ServerInstance->GlobalCulls.AddItem(this);
154                 }
155         };
156
157         struct ReloadAction : public HandlerBase0<void>
158         {
159                 Module* const mod;
160                 HandlerBase1<void, bool>* const callback;
161                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
162                         : mod(m), callback(c) {}
163                 void Call()
164                 {
165                         DLLManager* dll = mod->ModuleDLLManager;
166                         std::string name = mod->ModuleSourceFile;
167                         ServerInstance->Modules->DoSafeUnload(mod);
168                         ServerInstance->GlobalCulls.Apply();
169                         delete dll;
170                         bool rv = ServerInstance->Modules->Load(name.c_str());
171                         callback->Call(rv);
172                         ServerInstance->GlobalCulls.AddItem(this);
173                 }
174         };
175 }
176
177 bool ModuleManager::Unload(Module* mod)
178 {
179         if (!CanUnload(mod))
180                 return false;
181         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
182         return true;
183 }
184
185 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
186 {
187         if (CanUnload(mod))
188                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
189         else
190                 callback->Call(false);
191 }
192
193 /* We must load the modules AFTER initializing the socket engine, now */
194 void ModuleManager::LoadAll()
195 {
196         ModCount = 0;
197
198         printf("\nLoading core commands");
199         fflush(stdout);
200
201         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
202         if (library)
203         {
204                 dirent* entry = NULL;
205                 while (0 != (entry = readdir(library)))
206                 {
207                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
208                         {
209                                 printf(".");
210                                 fflush(stdout);
211
212                                 if (!Load(entry->d_name))
213                                 {
214                                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
215                                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
216                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
217                                 }
218                         }
219                 }
220                 closedir(library);
221                 printf("\n");
222         }
223
224         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
225         for(ConfigIter i = tags.first; i != tags.second; ++i)
226         {
227                 ConfigTag* tag = i->second;
228                 std::string name = tag->getString("name");
229                 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
230
231                 if (!this->Load(name.c_str()))
232                 {
233                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
234                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
235                         ServerInstance->Exit(EXIT_STATUS_MODULE);
236                 }
237         }
238 }
239
240 void ModuleManager::UnloadAll()
241 {
242         /* We do this more than once, so that any service providers get a
243          * chance to be unhooked by the modules using them, but then get
244          * a chance to be removed themsleves.
245          *
246          * Note: this deliberately does NOT delete the DLLManager objects
247          */
248         for (int tries = 0; tries < 4; tries++)
249         {
250                 std::map<std::string, Module*>::iterator i = Modules.begin();
251                 while (i != Modules.end())
252                 {
253                         std::map<std::string, Module*>::iterator me = i++;
254                         if (CanUnload(me->second))
255                         {
256                                 DoSafeUnload(me->second);
257                         }
258                 }
259                 ServerInstance->GlobalCulls.Apply();
260         }
261 }
262
263 #endif