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