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