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