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