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