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