]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_dynamic.cpp
m_spanningtree Remove duplicate code for sending channel messages from RouteCommand()
[user/henk/code/inspircd.git] / src / modmanager_dynamic.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "xline.h"
22 #include "socket.h"
23 #include "socketengine.h"
24 #include "command_parse.h"
25 #include "exitcodes.h"
26 #include <iostream>
27
28 #ifndef _WIN32
29 #include <dirent.h>
30 #endif
31
32 #ifndef PURE_STATIC
33
34 bool ModuleManager::Load(const std::string& filename, bool defer)
35 {
36         /* Don't allow people to specify paths for modules, it doesn't work as expected */
37         if (filename.find('/') != std::string::npos)
38                 return false;
39
40         const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);
41
42         if (!ServerConfig::FileExists(moduleFile.c_str()))
43         {
44                 LastModuleError = "Module file could not be found: " + filename;
45                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
46                 return false;
47         }
48
49         if (Modules.find(filename) != Modules.end())
50         {
51                 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
52                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
53                 return false;
54         }
55
56         Module* newmod = NULL;
57         DLLManager* newhandle = new DLLManager(moduleFile.c_str());
58
59         try
60         {
61                 newmod = newhandle->CallInit();
62
63                 if (newmod)
64                 {
65                         newmod->ModuleSourceFile = filename;
66                         newmod->ModuleDLLManager = newhandle;
67                         newmod->dying = false;
68                         Modules[filename] = newmod;
69                         std::string version = newhandle->GetVersion();
70                         if (defer)
71                         {
72                                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
73                                         filename.c_str(), version.c_str());
74                         }
75                         else
76                         {
77                                 AttachAll(newmod);
78                                 newmod->init();
79
80                                 Version v = newmod->GetVersion();
81                                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
82                                         filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
83                         }
84                 }
85                 else
86                 {
87                         LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
88                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
89                         delete newhandle;
90                         return false;
91                 }
92         }
93         catch (CoreException& modexcept)
94         {
95                 // failure in module constructor
96                 if (newmod)
97                 {
98                         DoSafeUnload(newmod);
99                         ServerInstance->GlobalCulls.AddItem(newhandle);
100                 }
101                 else
102                         delete newhandle;
103                 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
104                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
105                 return false;
106         }
107
108         this->ModCount++;
109         if (defer)
110                 return true;
111
112         FOREACH_MOD(OnLoadModule, (newmod));
113         /* We give every module a chance to re-prioritize when we introduce a new one,
114          * not just the one thats loading, as the new module could affect the preference
115          * of others
116          */
117         for(int tries = 0; tries < 20; tries++)
118         {
119                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
120                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
121                         n->second->Prioritize();
122
123                 if (prioritizationState == PRIO_STATE_LAST)
124                         break;
125                 if (tries == 19)
126                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected while loading " + filename);
127         }
128
129         ServerInstance->ISupport.Build();
130         return true;
131 }
132
133 namespace {
134         struct UnloadAction : public HandlerBase0<void>
135         {
136                 Module* const mod;
137                 UnloadAction(Module* m) : mod(m) {}
138                 void Call()
139                 {
140                         DLLManager* dll = mod->ModuleDLLManager;
141                         ServerInstance->Modules->DoSafeUnload(mod);
142                         ServerInstance->GlobalCulls.Apply();
143                         delete dll;
144                         ServerInstance->GlobalCulls.AddItem(this);
145                 }
146         };
147
148         struct ReloadAction : public HandlerBase0<void>
149         {
150                 Module* const mod;
151                 HandlerBase1<void, bool>* const callback;
152                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
153                         : mod(m), callback(c) {}
154                 void Call()
155                 {
156                         DLLManager* dll = mod->ModuleDLLManager;
157                         std::string name = mod->ModuleSourceFile;
158                         ServerInstance->Modules->DoSafeUnload(mod);
159                         ServerInstance->GlobalCulls.Apply();
160                         delete dll;
161                         bool rv = ServerInstance->Modules->Load(name.c_str());
162                         if (callback)
163                                 callback->Call(rv);
164                         ServerInstance->GlobalCulls.AddItem(this);
165                 }
166         };
167 }
168
169 bool ModuleManager::Unload(Module* mod)
170 {
171         if (!CanUnload(mod))
172                 return false;
173         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
174         return true;
175 }
176
177 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
178 {
179         if (CanUnload(mod))
180                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
181         else
182                 callback->Call(false);
183 }
184
185 /* We must load the modules AFTER initializing the socket engine, now */
186 void ModuleManager::LoadAll()
187 {
188         ModCount = 0;
189
190         std::cout << std::endl << "Loading core commands";
191         fflush(stdout);
192
193         DIR* library = opendir(ServerInstance->Config->Paths.Module.c_str());
194         if (library)
195         {
196                 dirent* entry = NULL;
197                 while (0 != (entry = readdir(library)))
198                 {
199                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
200                         {
201                                 std::cout << ".";
202                                 fflush(stdout);
203
204                                 if (!Load(entry->d_name, true))
205                                 {
206                                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
207                                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
208                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
209                                 }
210                         }
211                 }
212                 closedir(library);
213                 std::cout << std::endl;
214         }
215
216         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
217         for(ConfigIter i = tags.first; i != tags.second; ++i)
218         {
219                 ConfigTag* tag = i->second;
220                 std::string name = tag->getString("name");
221                 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
222
223                 if (!this->Load(name, true))
224                 {
225                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
226                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
227                         ServerInstance->Exit(EXIT_STATUS_MODULE);
228                 }
229         }
230
231         for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
232         {
233                 Module* mod = i->second;
234                 try
235                 {
236                         ServerInstance->Logs->Log("MODULE", LOG_DEBUG, "Initializing %s", i->first.c_str());
237                         AttachAll(mod);
238                         mod->init();
239                 }
240                 catch (CoreException& modexcept)
241                 {
242                         LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
243                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
244                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
245                         ServerInstance->Exit(EXIT_STATUS_MODULE);
246                 }
247         }
248
249         /* We give every module a chance to re-prioritize when we introduce a new one,
250          * not just the one thats loading, as the new module could affect the preference
251          * of others
252          */
253         for(int tries = 0; tries < 20; tries++)
254         {
255                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
256                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
257                         n->second->Prioritize();
258
259                 if (prioritizationState == PRIO_STATE_LAST)
260                         break;
261                 if (tries == 19)
262                 {
263                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected");
264                         ServerInstance->Exit(EXIT_STATUS_MODULE);
265                 }
266         }
267 }
268
269 #endif