1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/* Modular support
*
* (C) 2003-2013 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
*/
#include "services.h"
#include "modules.h"
#include "users.h"
#include "regchannel.h"
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <dirent.h>
#include <sys/types.h>
#include <dlfcn.h>
#endif
std::list<Module *> ModuleManager::Modules;
std::map<Anope::string, std::vector<Module *> > ModuleManager::EventHandlers;
#ifdef _WIN32
void ModuleManager::CleanupRuntimeDirectory()
{
Anope::string dirbuf = Anope::DataDir + "/runtime";
Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment please wait";
DIR *dirp = opendir(dirbuf.c_str());
if (!dirp)
{
Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << ")";
return;
}
for (dirent *dp; (dp = readdir(dirp));)
{
if (!dp->d_ino)
continue;
if (Anope::string(dp->d_name).equals_cs(".") || Anope::string(dp->d_name).equals_cs(".."))
continue;
Anope::string filebuf = dirbuf + "/" + dp->d_name;
unlink(filebuf.c_str());
}
closedir(dirp);
}
/**
* Copy the module from the modules folder to the runtime folder.
* This will prevent module updates while the modules is loaded from
* triggering a segfault, as the actaul file in use will be in the
* runtime folder.
* @param name the name of the module to copy
* @param output the destination to copy the module to
* @return MOD_ERR_OK on success
*/
static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &output)
{
Anope::string input = Anope::ModuleDir + "/modules/" + name + ".so";
struct stat s;
if (stat(input.c_str(), &s) == -1)
return MOD_ERR_NOEXIST;
else if (!S_ISREG(s.st_mode))
return MOD_ERR_NOEXIST;
std::ifstream source(input.c_str(), std::ios_base::in | std::ios_base::binary);
if (!source.is_open())
return MOD_ERR_NOEXIST;
char *tmp_output = strdup(output.c_str());
int target_fd = mkstemp(tmp_output);
if (target_fd == -1 || close(target_fd) == -1)
{
free(tmp_output);
source.close();
return MOD_ERR_FILE_IO;
}
output = tmp_output;
free(tmp_output);
Log(LOG_DEBUG_2) << "Runtime module location: " << output;
std::ofstream target(output.c_str(), std::ios_base::in | std::ios_base::binary);
if (!target.is_open())
{
source.close();
return MOD_ERR_FILE_IO;
}
int want = s.st_size;
char buffer[1024];
while (want > 0 && !source.fail() && !target.fail())
{
source.read(buffer, std::min(want, static_cast<int>(sizeof(buffer))));
int read_len = source.gcount();
target.write(buffer, read_len);
want -= read_len;
}
source.close();
target.close();
return !source.fail() && !target.fail() ? MOD_ERR_OK : MOD_ERR_FILE_IO;
}
#endif
std::vector<Module *> &ModuleManager::GetEventHandlers(const Anope::string &name)
{
std::map<Anope::string, std::vector<Module *> >::iterator it = EventHandlers.find(name);
if (it != EventHandlers.end())
return it->second;
std::vector<Module *> &modules = EventHandlers[name];
/* Populate initial vector */
std::copy(Modules.begin(), Modules.end(), std::back_inserter(modules));
return modules;
}
/* This code was found online at http://www.linuxjournal.com/article/3687#comment-26593
*
* This function will take a pointer from either dlsym or GetProcAddress and cast it in
* a way that won't cause C++ warnings/errors to come up.
*/
template <class TYPE> static TYPE function_cast(void *symbol)
{
union
{
void *symbol;
TYPE function;
} cast;
cast.symbol = symbol;
return cast.function;
}
ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
{
if (modname.empty())
return MOD_ERR_PARAMS;
if (FindModule(modname))
return MOD_ERR_EXISTS;
Log(LOG_DEBUG) << "Trying to load module: " << modname;
#ifdef _WIN32
/* Generate the filename for the temporary copy of the module */
Anope::string pbuf = Anope::DataDir + "/runtime/" + modname + ".so.XXXXXX";
/* Don't skip return value checking! -GD */
ModuleReturn ret = moduleCopyFile(modname, pbuf);
if (ret != MOD_ERR_OK)
{
if (ret == MOD_ERR_NOEXIST)
Log(LOG_TERMINAL) << "Error while loading " << modname << " (file does not exist)";
else if (ret == MOD_ERR_FILE_IO)
Log(LOG_TERMINAL) << "Error while loading " << modname << " (file IO error, check file permissions and diskspace)";
return ret;
}
#else
Anope::string pbuf = Anope::ModuleDir + "/modules/" + modname + ".so";
#endif
dlerror();
void *handle = dlopen(pbuf.c_str(), RTLD_NOW);
const char *err = dlerror();
if (!handle && err && *err)
{
Log() << err;
return MOD_ERR_NOLOAD;
}
dlerror();
Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit"));
err = dlerror();
if (!func && err && *err)
{
Log() << "No init function found, not an Anope module";
dlclose(handle);
return MOD_ERR_NOLOAD;
}
if (!func)
throw CoreException("Couldn't find constructor, yet moderror wasn't set?");
/* Create module. */
Anope::string nick;
if (u)
nick = u->nick;
Module *m;
try
{
m = func(modname, nick);
}
catch (const ModuleException &ex)
{
Log() << "Error while loading " << modname << ": " << ex.GetReason();
return MOD_ERR_EXCEPTION;
}
m->filename = pbuf;
m->handle = handle;
ModuleVersion v = m->GetVersion();
if (v.GetMajor() < Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() < Anope::VersionMinor()))
{
Log() << "Module " << modname << " is compiled against an older version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionShort();
DeleteModule(m);
return MOD_ERR_VERSION;
}
else if (v.GetMajor() > Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() > Anope::VersionMinor()))
{
Log() << "Module " << modname << " is compiled against a newer version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionShort();
DeleteModule(m);
return MOD_ERR_VERSION;
}
else if (v.GetPatch() < Anope::VersionPatch())
{
Log() << "Module " << modname << " is compiled against an older version of Anope, " << v.GetMajor() << "." << v.GetMinor() << "." << v.GetPatch() << ", this is " << Anope::VersionShort();
DeleteModule(m);
return MOD_ERR_VERSION;
}
else if (v.GetPatch() > Anope::VersionPatch())
{
Log() << "Module " << modname << " is compiled against a newer version of Anope, " << v.GetMajor() << "." << v.GetMinor() << "." << v.GetPatch() << ", this is " << Anope::VersionShort();
DeleteModule(m);
return MOD_ERR_VERSION;
}
else
Log(LOG_DEBUG_2) << "Module " << modname << " is compiled against current version of Anope " << Anope::VersionShort();
/* Initialize config */
try
{
m->OnReload(Config);
}
catch (const ModuleException &ex)
{
Log() << "Module " << modname << " couldn't load:" << ex.GetReason();
DeleteModule(m);
return MOD_ERR_EXCEPTION;
}
catch (const ConfigException &ex)
{
Log() << "Module " << modname << " couldn't load due to configuration problems: " << ex.GetReason();
DeleteModule(m);
return MOD_ERR_EXCEPTION;
}
catch (const NotImplementedException &ex)
{
}
Log(LOG_DEBUG) << "Module " << modname << " loaded.";
/* Attach module to all events */
for (std::map<Anope::string, std::vector<Module *> >::iterator it = EventHandlers.begin(); it != EventHandlers.end(); ++it)
it->second.push_back(m);
FOREACH_MOD(OnModuleLoad, (u, m));
return MOD_ERR_OK;
}
ModuleReturn ModuleManager::UnloadModule(Module *m, User *u)
{
if (!m)
return MOD_ERR_PARAMS;
FOREACH_MOD(OnModuleUnload, (u, m));
return DeleteModule(m);
}
Module *ModuleManager::FindModule(const Anope::string &name)
{
for (std::list<Module *>::const_iterator it = Modules.begin(), it_end = Modules.end(); it != it_end; ++it)
{
Module *m = *it;
if (m->name.equals_ci(name))
return m;
}
return NULL;
}
Module *ModuleManager::FindFirstOf(ModType type)
{
for (std::list<Module *>::const_iterator it = Modules.begin(), it_end = Modules.end(); it != it_end; ++it)
{
Module *m = *it;
if (m->type & type)
return m;
}
return NULL;
}
void ModuleManager::RequireVersion(int major, int minor, int patch)
{
if (Anope::VersionMajor() > major)
return;
else if (Anope::VersionMajor() == major)
{
if (minor == -1)
return;
else if (Anope::VersionMinor() > minor)
return;
else if (Anope::VersionMinor() == minor)
{
if (patch == -1)
return;
else if (Anope::VersionPatch() > patch)
return;
else if (Anope::VersionPatch() == patch)
return;
}
}
throw ModuleException("This module requires version " + stringify(major) + "." + stringify(minor) + "." + stringify(patch) + " - this is " + Anope::VersionShort());
}
ModuleReturn ModuleManager::DeleteModule(Module *m)
{
if (!m || !m->handle)
return MOD_ERR_PARAMS;
void *handle = m->handle;
Anope::string filename = m->filename;
Log(LOG_DEBUG) << "Unloading module " << m->name;
dlerror();
void (*destroy_func)(Module *m) = function_cast<void (*)(Module *)>(dlsym(m->handle, "AnopeFini"));
const char *err = dlerror();
if (!destroy_func || (err && *err))
{
Log() << "No destroy function found for " << m->name << ", chancing delete...";
delete m; /* we just have to chance they haven't overwrote the delete operator then... */
}
else
destroy_func(m); /* Let the module delete it self, just in case */
if (dlclose(handle))
Log() << dlerror();
#ifdef _WIN32
if (!filename.empty())
unlink(filename.c_str());
#endif
return MOD_ERR_OK;
}
void ModuleManager::DetachAll(Module *mod)
{
for (std::map<Anope::string, std::vector<Module *> >::iterator it = EventHandlers.begin(); it != EventHandlers.end(); ++it)
{
std::vector<Module *> &mods = it->second;
std::vector<Module *>::iterator it2 = std::find(mods.begin(), mods.end(), mod);
if (it2 != mods.end())
mods.erase(it2);
}
}
bool ModuleManager::SetPriority(Module *mod, Priority s)
{
for (std::map<Anope::string, std::vector<Module *> >::iterator it = EventHandlers.begin(); it != EventHandlers.end(); ++it)
SetPriority(mod, it->first, s);
return true;
}
bool ModuleManager::SetPriority(Module *mod, const Anope::string &i, Priority s, Module **modules, size_t sz)
{
/** To change the priority of a module, we first find its position in the vector,
* then we find the position of the other modules in the vector that this module
* wants to be before/after. We pick off either the first or last of these depending
* on which they want, and we make sure our module is *at least* before or after
* the first or last of this subset, depending again on the type of priority.
*/
/* Locate our module. This is O(n) but it only occurs on module load so we're
* not too bothered about it
*/
size_t source = 0;
bool found = false;
for (size_t x = 0, end = EventHandlers[i].size(); x != end; ++x)
if (EventHandlers[i][x] == mod)
{
source = x;
found = true;
break;
}
/* Eh? this module doesnt exist, probably trying to set priority on an event
* theyre not attached to.
*/
if (!found)
return false;
size_t swap_pos = 0;
bool swap = true;
switch (s)
{
/* Dummy value */
case PRIORITY_DONTCARE:
swap = false;
break;
/* Module wants to be first, sod everything else */
case PRIORITY_FIRST:
swap_pos = 0;
break;
/* Module is submissive and wants to be last... awww. */
case PRIORITY_LAST:
if (EventHandlers[i].empty())
swap_pos = 0;
else
swap_pos = EventHandlers[i].size() - 1;
break;
/* Place this module after a set of other modules */
case PRIORITY_AFTER:
/* Find the latest possible position */
swap_pos = 0;
swap = false;
for (size_t x = 0, end = EventHandlers[i].size(); x != end; ++x)
for (size_t n = 0; n < sz; ++n)
if (modules[n] && EventHandlers[i][x] == modules[n] && x >= swap_pos && source <= swap_pos)
{
swap_pos = x;
swap = true;
}
break;
/* Place this module before a set of other modules */
case PRIORITY_BEFORE:
swap_pos = EventHandlers[i].size() - 1;
swap = false;
for (size_t x = 0, end = EventHandlers[i].size(); x != end; ++x)
for (size_t n = 0; n < sz; ++n)
if (modules[n] && EventHandlers[i][x] == modules[n] && x <= swap_pos && source >= swap_pos)
{
swap = true;
swap_pos = x;
}
}
/* Do we need to swap? */
if (swap && swap_pos != source)
{
/* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
int incrmnt = 1;
if (source > swap_pos)
incrmnt = -1;
for (unsigned j = source; j != swap_pos; j += incrmnt)
{
if (j + incrmnt > EventHandlers[i].size() - 1 || (!j && incrmnt == -1))
continue;
std::swap(EventHandlers[i][j], EventHandlers[i][j + incrmnt]);
}
}
return true;
}
void ModuleManager::UnloadAll()
{
std::vector<Anope::string> modules;
for (size_t i = 1, j = 0; i != MT_END; j |= i, i <<= 1)
for (std::list<Module *>::iterator it = Modules.begin(), it_end = Modules.end(); it != it_end; ++it)
{
Module *m = *it;
if ((m->type & j) == m->type)
modules.push_back(m->name);
}
for (unsigned i = 0; i < modules.size(); ++i)
{
Module *m = FindModule(modules[i]);
if (m != NULL)
UnloadModule(m, NULL);
}
}
|