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
|
/*
* atheme-services: A collection of minimalist IRC services
* module.c: Module management.
*
* Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "atheme.h"
#include "linker.h"
#include <dlfcn.h>
mowgli_heap_t *module_heap;
mowgli_list_t modules, modules_inprogress;
module_t *modtarget = NULL;
void modules_init(void)
{
module_heap = mowgli_heap_create(sizeof(module_t), 256, BH_NOW);
if (!module_heap)
{
slog(LG_ERROR, "modules_init(): block allocator failed.");
exit(EXIT_FAILURE);
}
}
/*
* module_load()
*
* inputs:
* a literal filename for a module to load.
*
* outputs:
* the respective module_t object of the module.
*
* side effects:
* a module is loaded and necessary initialization code is run.
*/
module_t *module_load(const char *filespec)
{
mowgli_node_t *n;
module_t *m, *old_modtarget;
v3_moduleheader_t *h;
void *handle = NULL;
#if defined(HAVE_DLINFO) && !defined(__UCLIBC__)
struct link_map *map;
#endif
if ((m = module_find(filespec)))
{
slog(LG_INFO, "module_load(): module \2%s\2 is already loaded [at 0x%lx]", filespec, (unsigned long)m->address);
return NULL;
}
handle = linker_open_ext(filespec);
if (!handle)
{
char *errp = sstrdup(dlerror());
slog(LG_ERROR, "module_load(): error: \2%s\2", errp);
free(errp);
return NULL;
}
h = (v3_moduleheader_t *) linker_getsym(handle, "_header");
if (h == NULL || h->atheme_mod != MAPI_ATHEME_MAGIC)
{
slog(LG_ERROR, "module_load(): \2%s\2: Attempted to load an incompatible module. Aborting.", filespec);
linker_close(handle);
return NULL;
}
if (h->abi_ver != MAPI_ATHEME_V3)
{
slog(LG_ERROR, "module_load(): \2%s\2: MAPI version mismatch (%u != %u), please recompile.", filespec, h->abi_ver, MAPI_ATHEME_V3);
linker_close(handle);
return NULL;
}
if (h->abi_rev != CURRENT_ABI_REVISION)
{
slog(LG_ERROR, "module_load(): \2%s\2: ABI revision mismatch (%u != %u), please recompile.", filespec, h->abi_rev, CURRENT_ABI_REVISION);
linker_close(handle);
return NULL;
}
if (module_find_published(h->name))
{
slog(LG_INFO, "module_load(): \2%s\2: Published name \2%s\2 already exists.", filespec, h->name);
linker_close(handle);
return NULL;
}
m = mowgli_heap_alloc(module_heap);
strlcpy(m->modpath, filespec, BUFSIZE);
m->handle = handle;
m->mflags = MODTYPE_STANDARD;
m->header = h;
#if defined(HAVE_DLINFO) && !defined(__UCLIBC__)
dlinfo(handle, RTLD_DI_LINKMAP, &map);
if (map != NULL)
m->address = (void *) map->l_addr;
else
m->address = handle;
#else
/* best we can do here without dlinfo() --nenolod */
m->address = handle;
#endif
n = mowgli_node_create();
mowgli_node_add(m, n, &modules_inprogress);
/* set the module target for module dependencies */
old_modtarget = modtarget;
modtarget = m;
if (h->modinit)
h->modinit(m);
/* we won't be loading symbols outside the init code */
modtarget = old_modtarget;
mowgli_node_delete(n, &modules_inprogress);
if (m->mflags & MODTYPE_FAIL)
{
slog(LG_ERROR, "module_load(): module \2%s\2 init failed", filespec);
mowgli_node_free(n);
module_unload(m);
return NULL;
}
mowgli_node_add(m, n, &modules);
slog(LG_DEBUG, "module_load(): loaded %s [at 0x%lx; MAPI version %d]", h->name, (unsigned long)m->address, h->abi_ver);
if (me.connected && !cold_start)
{
wallops(_("Module %s loaded [at 0x%lx; MAPI version %d]"), h->name, (unsigned long)m->address, h->abi_ver);
slog(LG_INFO, _("MODLOAD: \2%s\2 [at 0x%lx; MAPI version %d]"), h->name, (unsigned long)m->address, h->abi_ver);
}
return m;
}
/*
* module_load_dir()
*
* inputs:
* a directory containing modules to load.
*
* outputs:
* none
*
* side effects:
* qualifying modules are passed to module_load().
*/
void module_load_dir(const char *dirspec)
{
DIR *module_dir = NULL;
struct dirent *ldirent = NULL;
char module_filename[4096];
module_dir = opendir(dirspec);
if (!module_dir)
{
slog(LG_ERROR, "module_load_dir(): %s: %s", dirspec, strerror(errno));
return;
}
while ((ldirent = readdir(module_dir)) != NULL)
{
if (!strstr(ldirent->d_name, ".so"))
continue;
snprintf(module_filename, sizeof(module_filename), "%s/%s", dirspec, ldirent->d_name);
module_load(module_filename);
}
closedir(module_dir);
}
/*
* module_load_dir_match()
*
* inputs:
* a directory containing modules to load, and a pattern to match against
* to determine whether or not a module qualifies for loading.
*
* outputs:
* none
*
* side effects:
* qualifying modules are passed to module_load().
*/
void module_load_dir_match(const char *dirspec, const char *pattern)
{
DIR *module_dir = NULL;
struct dirent *ldirent = NULL;
char module_filename[4096];
module_dir = opendir(dirspec);
if (!module_dir)
{
slog(LG_ERROR, "module_load_dir(): %s: %s", dirspec, strerror(errno));
return;
}
while ((ldirent = readdir(module_dir)) != NULL)
{
if (!strstr(ldirent->d_name, ".so") && match(pattern, ldirent->d_name))
continue;
snprintf(module_filename, sizeof(module_filename), "%s/%s", dirspec, ldirent->d_name);
module_load(module_filename);
}
closedir(module_dir);
}
/*
* module_unload()
*
* inputs:
* a module object to unload.
*
* outputs:
* none
*
* side effects:
* a module is unloaded and neccessary deinitalization code is run.
*/
void module_unload(module_t * m)
{
mowgli_node_t *n, *tn;
if (!m)
return;
/* unload modules which depend on us */
while (m->dephost.head != NULL)
module_unload((module_t *) m->dephost.head->data);
/* let modules that we depend on know that we no longer exist */
MOWGLI_ITER_FOREACH_SAFE(n, tn, m->deplist.head)
{
module_t *hm = (module_t *) n->data;
mowgli_node_t *hn = mowgli_node_find(m, &hm->dephost);
mowgli_node_delete(hn, &hm->dephost);
mowgli_node_free(hn);
mowgli_node_delete(n, &m->deplist);
mowgli_node_free(n);
}
n = mowgli_node_find(m, &modules);
if (n != NULL)
{
slog(LG_INFO, "module_unload(): unloaded \2%s\2", m->header->name);
if (me.connected)
{
wallops(_("Module %s unloaded."), m->header->name);
}
if (m->header->deinit)
m->header->deinit();
mowgli_node_delete(n, &modules);
mowgli_node_free(n);
}
/* else unloaded in embryonic state */
linker_close(m->handle);
mowgli_heap_free(module_heap, m);
}
/*
* module_locate_symbol()
*
* inputs:
* a name of a module and a symbol to look for inside it.
*
* outputs:
* the pointer to the module symbol, else NULL if not found.
*
* side effects:
* none
*/
void *module_locate_symbol(const char *modname, const char *sym)
{
module_t *m;
void *symptr;
if (!(m = module_find_published(modname)))
{
slog(LG_ERROR, "module_locate_symbol(): %s is not loaded.", modname);
return NULL;
}
if (modtarget != NULL && !mowgli_node_find(m, &modtarget->deplist))
{
slog(LG_DEBUG, "module_locate_symbol(): %s added as a dependency for %s (symbol: %s)",
m->header->name, modtarget->header->name, sym);
mowgli_node_add(m, mowgli_node_create(), &modtarget->deplist);
mowgli_node_add(modtarget, mowgli_node_create(), &m->dephost);
}
symptr = linker_getsym(m->handle, sym);
if (symptr == NULL)
slog(LG_ERROR, "module_locate_symbol(): could not find symbol %s in module %s.", sym, modname);
return symptr;
}
/*
* module_find()
*
* inputs:
* a name of a module to locate the object for.
*
* outputs:
* the module object if the module is located, else NULL.
*
* side effects:
* none
*/
module_t *module_find(const char *name)
{
mowgli_node_t *n;
MOWGLI_ITER_FOREACH(n, modules.head)
{
module_t *m = n->data;
if (!strcasecmp(m->modpath, name))
return m;
}
return NULL;
}
/*
* module_find_published()
*
* inputs:
* a published (in _header) name of a module to locate the object for.
*
* outputs:
* the module object if the module is located, else NULL.
*
* side effects:
* none
*/
module_t *module_find_published(const char *name)
{
mowgli_node_t *n;
MOWGLI_ITER_FOREACH(n, modules.head)
{
module_t *m = n->data;
if (!strcasecmp(m->header->name, name))
return m;
}
return NULL;
}
/*
* module_request()
*
* inputs:
* a published name of a module to load.
*
* outputs:
* true: if the module was loaded, or is already present.
*
* side effects:
* a module might be loaded.
*/
bool module_request(const char *name)
{
module_t *m;
mowgli_node_t *n;
char path[BUFSIZE];
if (module_find_published(name))
return true;
MOWGLI_ITER_FOREACH(n, modules_inprogress.head)
{
m = n->data;
if (!strcasecmp(m->header->name, name))
{
slog(LG_ERROR, "module_request(): circular dependency between modules %s and %s",
modtarget != NULL ? modtarget->header->name : "?",
m->header->name);
return false;
}
}
snprintf(path, BUFSIZE, "%s/modules/%s", MODDIR, name);
if (module_load(path) == NULL)
return false;
return true;
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
|