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