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