]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * modules.c: A module loader. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
5366977b | 24 | * $Id: modules.c 3161 2007-01-25 07:23:01Z nenolod $ |
212380e3 AC |
25 | */ |
26 | ||
27 | #include "stdinc.h" | |
28 | ||
29 | ||
30 | #include "modules.h" | |
4016731b | 31 | #include "logger.h" |
212380e3 AC |
32 | #include "ircd.h" |
33 | #include "client.h" | |
34 | #include "send.h" | |
35 | #include "s_conf.h" | |
36 | #include "s_newconf.h" | |
37 | #include "numeric.h" | |
38 | #include "parse.h" | |
39 | #include "ircd_defs.h" | |
4562c604 | 40 | #include "match.h" |
212380e3 AC |
41 | |
42 | ||
f272e7ab AC |
43 | #include <ltdl.h> |
44 | ||
212380e3 AC |
45 | struct module **modlist = NULL; |
46 | ||
47 | static const char *core_module_table[] = { | |
431a1a27 | 48 | "m_ban", |
212380e3 AC |
49 | "m_die", |
50 | "m_error", | |
51 | "m_join", | |
52 | "m_kick", | |
53 | "m_kill", | |
54 | "m_message", | |
55 | "m_mode", | |
56 | "m_nick", | |
57 | "m_part", | |
58 | "m_quit", | |
59 | "m_server", | |
212380e3 AC |
60 | "m_squit", |
61 | NULL | |
62 | }; | |
63 | ||
64 | #define MODS_INCREMENT 10 | |
65 | int num_mods = 0; | |
66 | int max_mods = MODS_INCREMENT; | |
67 | ||
330fc5c1 | 68 | static rb_dlink_list mod_paths; |
212380e3 AC |
69 | |
70 | static int mo_modload(struct Client *, struct Client *, int, const char **); | |
71 | static int mo_modlist(struct Client *, struct Client *, int, const char **); | |
72 | static int mo_modreload(struct Client *, struct Client *, int, const char **); | |
73 | static int mo_modunload(struct Client *, struct Client *, int, const char **); | |
74 | static int mo_modrestart(struct Client *, struct Client *, int, const char **); | |
75 | ||
76 | struct Message modload_msgtab = { | |
77 | "MODLOAD", 0, 0, 0, MFLG_SLOW, | |
78 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modload, 2}} | |
79 | }; | |
80 | ||
81 | struct Message modunload_msgtab = { | |
82 | "MODUNLOAD", 0, 0, 0, MFLG_SLOW, | |
83 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modunload, 2}} | |
84 | }; | |
85 | ||
86 | struct Message modreload_msgtab = { | |
87 | "MODRELOAD", 0, 0, 0, MFLG_SLOW, | |
88 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modreload, 2}} | |
89 | }; | |
90 | ||
91 | struct Message modlist_msgtab = { | |
92 | "MODLIST", 0, 0, 0, MFLG_SLOW, | |
93 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modlist, 0}} | |
94 | }; | |
95 | ||
96 | struct Message modrestart_msgtab = { | |
97 | "MODRESTART", 0, 0, 0, MFLG_SLOW, | |
98 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modrestart, 0}} | |
99 | }; | |
100 | ||
212380e3 AC |
101 | void |
102 | modules_init(void) | |
103 | { | |
f272e7ab AC |
104 | if(lt_dlinit()) |
105 | { | |
106 | ilog(L_MAIN, "lt_dlinit failed"); | |
107 | exit(0); | |
108 | } | |
109 | ||
212380e3 AC |
110 | mod_add_cmd(&modload_msgtab); |
111 | mod_add_cmd(&modunload_msgtab); | |
112 | mod_add_cmd(&modreload_msgtab); | |
113 | mod_add_cmd(&modlist_msgtab); | |
114 | mod_add_cmd(&modrestart_msgtab); | |
115 | ||
116 | /* Add the default paths we look in to the module system --nenolod */ | |
117 | mod_add_path(MODPATH); | |
118 | mod_add_path(AUTOMODPATH); | |
119 | } | |
120 | ||
121 | /* mod_find_path() | |
122 | * | |
123 | * input - path | |
124 | * output - none | |
125 | * side effects - returns a module path from path | |
126 | */ | |
26c6ac3d | 127 | static char * |
212380e3 AC |
128 | mod_find_path(const char *path) |
129 | { | |
330fc5c1 | 130 | rb_dlink_node *ptr; |
26c6ac3d | 131 | char *mpath; |
212380e3 | 132 | |
5cefa1d6 | 133 | RB_DLINK_FOREACH(ptr, mod_paths.head) |
212380e3 AC |
134 | { |
135 | mpath = ptr->data; | |
136 | ||
26c6ac3d | 137 | if(!strcmp(path, mpath)) |
212380e3 AC |
138 | return mpath; |
139 | } | |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
144 | /* mod_add_path | |
145 | * | |
146 | * input - path | |
55abcbb2 | 147 | * ouput - |
212380e3 AC |
148 | * side effects - adds path to list |
149 | */ | |
150 | void | |
151 | mod_add_path(const char *path) | |
152 | { | |
26c6ac3d | 153 | char *pathst; |
212380e3 AC |
154 | |
155 | if(mod_find_path(path)) | |
156 | return; | |
157 | ||
26c6ac3d | 158 | pathst = rb_strdup(path); |
330fc5c1 | 159 | rb_dlinkAddAlloc(pathst, &mod_paths); |
212380e3 AC |
160 | } |
161 | ||
162 | /* mod_clear_paths() | |
163 | * | |
164 | * input - | |
165 | * output - | |
166 | * side effects - clear the lists of paths | |
167 | */ | |
168 | void | |
169 | mod_clear_paths(void) | |
170 | { | |
637c4932 | 171 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 172 | |
637c4932 | 173 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
212380e3 | 174 | { |
637c4932 | 175 | rb_free(ptr->data); |
30106156 | 176 | rb_free_rb_dlink_node(ptr); |
212380e3 AC |
177 | } |
178 | ||
179 | mod_paths.head = mod_paths.tail = NULL; | |
180 | mod_paths.length = 0; | |
181 | } | |
182 | ||
212380e3 AC |
183 | /* findmodule_byname |
184 | * | |
185 | * input - | |
186 | * output - | |
187 | * side effects - | |
188 | */ | |
189 | ||
190 | int | |
191 | findmodule_byname(const char *name) | |
192 | { | |
193 | int i; | |
194 | ||
195 | for (i = 0; i < num_mods; i++) | |
196 | { | |
197 | if(!irccmp(modlist[i]->name, name)) | |
198 | return i; | |
199 | } | |
200 | return -1; | |
201 | } | |
202 | ||
203 | /* load_all_modules() | |
204 | * | |
205 | * input - | |
206 | * output - | |
207 | * side effects - | |
208 | */ | |
209 | void | |
210 | load_all_modules(int warn) | |
211 | { | |
212 | DIR *system_module_dir = NULL; | |
213 | struct dirent *ldirent = NULL; | |
214 | char module_fq_name[PATH_MAX + 1]; | |
215 | int len; | |
216 | ||
217 | modules_init(); | |
218 | ||
1e170010 | 219 | modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT)); |
212380e3 AC |
220 | |
221 | max_mods = MODS_INCREMENT; | |
222 | ||
223 | system_module_dir = opendir(AUTOMODPATH); | |
224 | ||
225 | if(system_module_dir == NULL) | |
226 | { | |
227 | ilog(L_MAIN, "Could not load modules from %s: %s", AUTOMODPATH, strerror(errno)); | |
228 | return; | |
229 | } | |
230 | ||
231 | while ((ldirent = readdir(system_module_dir)) != NULL) | |
232 | { | |
233 | len = strlen(ldirent->d_name); | |
2a19fc3f | 234 | if((len > 3) && !strcmp(ldirent->d_name+len-3, ".la")) |
212380e3 | 235 | { |
b2f0da88 | 236 | (void) rb_snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name); |
212380e3 AC |
237 | (void) load_a_module(module_fq_name, warn, 0); |
238 | } | |
239 | ||
240 | } | |
241 | (void) closedir(system_module_dir); | |
242 | } | |
243 | ||
244 | /* load_core_modules() | |
245 | * | |
246 | * input - | |
247 | * output - | |
248 | * side effects - core modules are loaded, if any fail, kill ircd | |
249 | */ | |
250 | void | |
251 | load_core_modules(int warn) | |
252 | { | |
d74fa5b5 | 253 | char module_name[PATH_MAX]; |
212380e3 AC |
254 | int i; |
255 | ||
256 | ||
257 | for (i = 0; core_module_table[i]; i++) | |
258 | { | |
b2f0da88 | 259 | rb_snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH, |
2a19fc3f | 260 | core_module_table[i], ".la"); |
212380e3 AC |
261 | |
262 | if(load_a_module(module_name, warn, 1) == -1) | |
263 | { | |
264 | ilog(L_MAIN, | |
265 | "Error loading core module %s%s: terminating ircd", | |
2a19fc3f | 266 | core_module_table[i], ".la"); |
212380e3 AC |
267 | exit(0); |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | /* load_one_module() | |
273 | * | |
274 | * input - | |
275 | * output - | |
276 | * side effects - | |
277 | */ | |
278 | int | |
279 | load_one_module(const char *path, int coremodule) | |
280 | { | |
d74fa5b5 | 281 | char modpath[PATH_MAX]; |
330fc5c1 | 282 | rb_dlink_node *pathst; |
26c6ac3d | 283 | const char *mpath; |
212380e3 AC |
284 | |
285 | struct stat statbuf; | |
286 | ||
287 | if (server_state_foreground == 1) | |
55abcbb2 | 288 | inotice("loading module %s ...", path); |
212380e3 | 289 | |
5cefa1d6 | 290 | RB_DLINK_FOREACH(pathst, mod_paths.head) |
212380e3 AC |
291 | { |
292 | mpath = pathst->data; | |
293 | ||
26c6ac3d | 294 | rb_snprintf(modpath, sizeof(modpath), "%s/%s", mpath, path); |
212380e3 AC |
295 | if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) |
296 | { | |
297 | if(stat(modpath, &statbuf) == 0) | |
298 | { | |
299 | if(S_ISREG(statbuf.st_mode)) | |
300 | { | |
301 | /* Regular files only please */ | |
302 | if(coremodule) | |
303 | return load_a_module(modpath, 1, 1); | |
304 | else | |
305 | return load_a_module(modpath, 1, 0); | |
306 | } | |
307 | } | |
308 | ||
309 | } | |
310 | } | |
311 | ||
312 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path); | |
313 | return -1; | |
314 | } | |
315 | ||
316 | ||
317 | /* load a module .. */ | |
318 | static int | |
319 | mo_modload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
320 | { | |
321 | char *m_bn; | |
322 | ||
323 | if(!IsOperAdmin(source_p)) | |
324 | { | |
325 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
326 | me.name, source_p->name, "admin"); | |
327 | return 0; | |
328 | } | |
329 | ||
b7a689d1 | 330 | m_bn = rb_basename(parv[1]); |
212380e3 AC |
331 | |
332 | if(findmodule_byname(m_bn) != -1) | |
333 | { | |
5366977b | 334 | sendto_one_notice(source_p, ":Module %s is already loaded", m_bn); |
637c4932 | 335 | rb_free(m_bn); |
212380e3 AC |
336 | return 0; |
337 | } | |
338 | ||
339 | load_one_module(parv[1], 0); | |
340 | ||
637c4932 | 341 | rb_free(m_bn); |
212380e3 AC |
342 | |
343 | return 0; | |
344 | } | |
345 | ||
346 | ||
347 | /* unload a module .. */ | |
348 | static int | |
349 | mo_modunload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
350 | { | |
351 | char *m_bn; | |
352 | int modindex; | |
353 | ||
354 | if(!IsOperAdmin(source_p)) | |
355 | { | |
356 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
357 | me.name, source_p->name, "admin"); | |
358 | return 0; | |
359 | } | |
360 | ||
b7a689d1 | 361 | m_bn = rb_basename(parv[1]); |
212380e3 AC |
362 | |
363 | if((modindex = findmodule_byname(m_bn)) == -1) | |
364 | { | |
5366977b | 365 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
637c4932 | 366 | rb_free(m_bn); |
212380e3 AC |
367 | return 0; |
368 | } | |
369 | ||
370 | if(modlist[modindex]->core == 1) | |
371 | { | |
5366977b | 372 | sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn); |
637c4932 | 373 | rb_free(m_bn); |
212380e3 AC |
374 | return 0; |
375 | } | |
376 | ||
377 | if(unload_one_module(m_bn, 1) == -1) | |
378 | { | |
5366977b | 379 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 | 380 | } |
5366977b | 381 | |
637c4932 | 382 | rb_free(m_bn); |
212380e3 AC |
383 | return 0; |
384 | } | |
385 | ||
386 | /* unload and load in one! */ | |
387 | static int | |
388 | mo_modreload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
389 | { | |
390 | char *m_bn; | |
391 | int modindex; | |
392 | int check_core; | |
393 | ||
394 | if(!IsOperAdmin(source_p)) | |
395 | { | |
396 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
397 | me.name, source_p->name, "admin"); | |
398 | return 0; | |
399 | } | |
400 | ||
b7a689d1 | 401 | m_bn = rb_basename(parv[1]); |
212380e3 AC |
402 | |
403 | if((modindex = findmodule_byname(m_bn)) == -1) | |
404 | { | |
5366977b | 405 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
637c4932 | 406 | rb_free(m_bn); |
212380e3 AC |
407 | return 0; |
408 | } | |
409 | ||
410 | check_core = modlist[modindex]->core; | |
411 | ||
412 | if(unload_one_module(m_bn, 1) == -1) | |
413 | { | |
5366977b | 414 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
637c4932 | 415 | rb_free(m_bn); |
212380e3 AC |
416 | return 0; |
417 | } | |
418 | ||
7d778d51 | 419 | if((load_one_module(m_bn, check_core) == -1) && check_core) |
212380e3 AC |
420 | { |
421 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
7d778d51 AC |
422 | "Error reloading core module: %s: terminating ircd", m_bn); |
423 | ilog(L_MAIN, "Error loading core module %s: terminating ircd", m_bn); | |
212380e3 AC |
424 | exit(0); |
425 | } | |
426 | ||
637c4932 | 427 | rb_free(m_bn); |
212380e3 AC |
428 | return 0; |
429 | } | |
430 | ||
431 | /* list modules .. */ | |
432 | static int | |
433 | mo_modlist(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
434 | { | |
435 | int i; | |
436 | ||
437 | if(!IsOperAdmin(source_p)) | |
438 | { | |
439 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
440 | me.name, source_p->name, "admin"); | |
441 | return 0; | |
442 | } | |
443 | ||
444 | for (i = 0; i < num_mods; i++) | |
445 | { | |
446 | if(parc > 1) | |
447 | { | |
448 | if(match(parv[1], modlist[i]->name)) | |
449 | { | |
450 | sendto_one(source_p, form_str(RPL_MODLIST), | |
451 | me.name, source_p->name, | |
452 | modlist[i]->name, | |
a9f12814 | 453 | (unsigned long)(uintptr_t)modlist[i]->address, |
212380e3 AC |
454 | modlist[i]->version, modlist[i]->core ? "(core)" : ""); |
455 | } | |
456 | } | |
457 | else | |
458 | { | |
459 | sendto_one(source_p, form_str(RPL_MODLIST), | |
460 | me.name, source_p->name, modlist[i]->name, | |
a9f12814 JT |
461 | (unsigned long)(uintptr_t)modlist[i]->address, |
462 | modlist[i]->version, | |
212380e3 AC |
463 | modlist[i]->core ? "(core)" : ""); |
464 | } | |
465 | } | |
466 | ||
467 | sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, source_p->name); | |
468 | return 0; | |
469 | } | |
470 | ||
471 | /* unload and reload all modules */ | |
472 | static int | |
473 | mo_modrestart(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
474 | { | |
475 | int modnum; | |
476 | ||
477 | if(!IsOperAdmin(source_p)) | |
478 | { | |
479 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
480 | me.name, source_p->name, "admin"); | |
481 | return 0; | |
482 | } | |
483 | ||
5366977b | 484 | sendto_one_notice(source_p, ":Reloading all modules"); |
212380e3 AC |
485 | |
486 | modnum = num_mods; | |
487 | while (num_mods) | |
488 | unload_one_module(modlist[0]->name, 0); | |
489 | ||
490 | load_all_modules(0); | |
491 | load_core_modules(0); | |
492 | rehash(0); | |
493 | ||
494 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
495 | "Module Restart: %d modules unloaded, %d modules loaded", | |
496 | modnum, num_mods); | |
497 | ilog(L_MAIN, "Module Restart: %d modules unloaded, %d modules loaded", modnum, num_mods); | |
498 | return 0; | |
499 | } | |
500 | ||
501 | ||
502 | ||
503 | #ifndef RTLD_NOW | |
504 | #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */ | |
505 | #endif | |
506 | ||
d4db3063 JT |
507 | #ifndef RTLD_LOCAL |
508 | #define RTLD_LOCAL 0 | |
509 | #endif | |
510 | ||
212380e3 AC |
511 | #ifdef CHARYBDIS_PROFILE |
512 | # ifndef RTLD_PROFILE | |
513 | # warning libdl may not support profiling, sucks. :( | |
514 | # define RTLD_PROFILE 0 | |
515 | # endif | |
516 | #endif | |
517 | ||
518 | static void increase_modlist(void); | |
519 | ||
520 | #define MODS_INCREMENT 10 | |
521 | ||
522 | static char unknown_ver[] = "<unknown>"; | |
523 | ||
212380e3 AC |
524 | /* unload_one_module() |
525 | * | |
526 | * inputs - name of module to unload | |
527 | * - 1 to say modules unloaded, 0 to not | |
528 | * output - 0 if successful, -1 if error | |
529 | * side effects - module is unloaded | |
530 | */ | |
531 | int | |
532 | unload_one_module(const char *name, int warn) | |
533 | { | |
534 | int modindex; | |
535 | ||
536 | if((modindex = findmodule_byname(name)) == -1) | |
537 | return -1; | |
538 | ||
539 | /* | |
540 | ** XXX - The type system in C does not allow direct conversion between | |
541 | ** data and function pointers, but as it happens, most C compilers will | |
55abcbb2 KB |
542 | ** safely do this, however it is a theoretical overlow to cast as we |
543 | ** must do here. I have library functions to take care of this, but | |
544 | ** despite being more "correct" for the C language, this is more | |
212380e3 AC |
545 | ** practical. Removing the abuse of the ability to cast ANY pointer |
546 | ** to and from an integer value here will break some compilers. | |
547 | ** -jmallett | |
548 | */ | |
549 | /* Left the comment in but the code isn't here any more -larne */ | |
550 | switch (modlist[modindex]->mapi_version) | |
551 | { | |
552 | case 1: | |
553 | { | |
554 | struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header; | |
555 | if(mheader->mapi_command_list) | |
556 | { | |
557 | struct Message **m; | |
558 | for (m = mheader->mapi_command_list; *m; ++m) | |
559 | mod_del_cmd(*m); | |
560 | } | |
561 | ||
562 | /* hook events are never removed, we simply lose the | |
563 | * ability to call them --fl | |
564 | */ | |
565 | if(mheader->mapi_hfn_list) | |
566 | { | |
567 | mapi_hfn_list_av1 *m; | |
568 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
569 | remove_hook(m->hapi_name, m->fn); | |
570 | } | |
571 | ||
572 | if(mheader->mapi_unregister) | |
573 | mheader->mapi_unregister(); | |
574 | break; | |
575 | } | |
576 | default: | |
577 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
578 | "Unknown/unsupported MAPI version %d when unloading %s!", | |
579 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
580 | ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!", | |
581 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
582 | break; | |
583 | } | |
584 | ||
f272e7ab | 585 | lt_dlclose(modlist[modindex]->address); |
212380e3 | 586 | |
637c4932 | 587 | rb_free(modlist[modindex]->name); |
f54e1a8f | 588 | memmove(&modlist[modindex], &modlist[modindex + 1], |
1e170010 | 589 | sizeof(struct module *) * ((num_mods - 1) - modindex)); |
212380e3 AC |
590 | |
591 | if(num_mods != 0) | |
592 | num_mods--; | |
593 | ||
594 | if(warn == 1) | |
595 | { | |
596 | ilog(L_MAIN, "Module %s unloaded", name); | |
597 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name); | |
598 | } | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
603 | ||
604 | /* | |
605 | * load_a_module() | |
606 | * | |
607 | * inputs - path name of module, int to notice, int of core | |
608 | * output - -1 if error 0 if success | |
609 | * side effects - loads a module if successful | |
610 | */ | |
611 | int | |
612 | load_a_module(const char *path, int warn, int core) | |
613 | { | |
f272e7ab | 614 | lt_dlhandle tmpptr; |
212380e3 AC |
615 | char *mod_basename; |
616 | const char *ver; | |
617 | ||
618 | int *mapi_version; | |
619 | ||
b7a689d1 | 620 | mod_basename = rb_basename(path); |
212380e3 | 621 | |
f272e7ab | 622 | tmpptr = lt_dlopen(path); |
212380e3 AC |
623 | |
624 | if(tmpptr == NULL) | |
625 | { | |
f272e7ab | 626 | const char *err = lt_dlerror(); |
212380e3 AC |
627 | |
628 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
629 | "Error loading module %s: %s", mod_basename, err); | |
630 | ilog(L_MAIN, "Error loading module %s: %s", mod_basename, err); | |
637c4932 | 631 | rb_free(mod_basename); |
212380e3 AC |
632 | return -1; |
633 | } | |
634 | ||
635 | ||
636 | /* | |
637 | * _mheader is actually a struct mapi_mheader_*, but mapi_version | |
638 | * is always the first member of this structure, so we treate it | |
639 | * as a single int in order to determine the API version. | |
640 | * -larne. | |
641 | */ | |
f272e7ab | 642 | mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader"); |
212380e3 | 643 | if((mapi_version == NULL |
f272e7ab | 644 | && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL) |
212380e3 AC |
645 | || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR) |
646 | { | |
647 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
648 | "Data format error: module %s has no MAPI header.", | |
649 | mod_basename); | |
650 | ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_basename); | |
f272e7ab | 651 | (void) lt_dlclose(tmpptr); |
637c4932 | 652 | rb_free(mod_basename); |
212380e3 AC |
653 | return -1; |
654 | } | |
655 | ||
656 | switch (MAPI_VERSION(*mapi_version)) | |
657 | { | |
658 | case 1: | |
659 | { | |
29c92cf9 | 660 | struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */ |
212380e3 AC |
661 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) |
662 | { | |
663 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
664 | mod_basename); | |
665 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
666 | "Module %s indicated failure during load.", | |
667 | mod_basename); | |
f272e7ab | 668 | lt_dlclose(tmpptr); |
637c4932 | 669 | rb_free(mod_basename); |
212380e3 AC |
670 | return -1; |
671 | } | |
672 | if(mheader->mapi_command_list) | |
673 | { | |
674 | struct Message **m; | |
675 | for (m = mheader->mapi_command_list; *m; ++m) | |
676 | mod_add_cmd(*m); | |
677 | } | |
678 | ||
679 | if(mheader->mapi_hook_list) | |
680 | { | |
681 | mapi_hlist_av1 *m; | |
682 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
683 | *m->hapi_id = register_hook(m->hapi_name); | |
684 | } | |
685 | ||
686 | if(mheader->mapi_hfn_list) | |
687 | { | |
688 | mapi_hfn_list_av1 *m; | |
689 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
690 | add_hook(m->hapi_name, m->fn); | |
691 | } | |
692 | ||
693 | ver = mheader->mapi_module_version; | |
694 | break; | |
695 | } | |
696 | ||
697 | default: | |
698 | ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.", | |
699 | mod_basename, MAPI_VERSION(*mapi_version)); | |
700 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
701 | "Module %s has unknown/unsupported MAPI version %d.", | |
702 | mod_basename, *mapi_version); | |
f272e7ab | 703 | lt_dlclose(tmpptr); |
637c4932 | 704 | rb_free(mod_basename); |
212380e3 AC |
705 | return -1; |
706 | } | |
707 | ||
708 | if(ver == NULL) | |
709 | ver = unknown_ver; | |
710 | ||
711 | increase_modlist(); | |
712 | ||
eddc2ab6 | 713 | modlist[num_mods] = rb_malloc(sizeof(struct module)); |
212380e3 AC |
714 | modlist[num_mods]->address = tmpptr; |
715 | modlist[num_mods]->version = ver; | |
716 | modlist[num_mods]->core = core; | |
47a03750 | 717 | modlist[num_mods]->name = rb_strdup(mod_basename); |
212380e3 AC |
718 | modlist[num_mods]->mapi_header = mapi_version; |
719 | modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version); | |
720 | num_mods++; | |
721 | ||
722 | if(warn == 1) | |
723 | { | |
724 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
725 | "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx", | |
726 | mod_basename, ver, MAPI_VERSION(*mapi_version), | |
727 | (unsigned long) tmpptr); | |
728 | ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx", | |
729 | mod_basename, ver, MAPI_VERSION(*mapi_version), (unsigned long) tmpptr); | |
730 | } | |
637c4932 | 731 | rb_free(mod_basename); |
212380e3 AC |
732 | return 0; |
733 | } | |
734 | ||
735 | /* | |
736 | * increase_modlist | |
737 | * | |
738 | * inputs - NONE | |
739 | * output - NONE | |
740 | * side effects - expand the size of modlist if necessary | |
741 | */ | |
742 | static void | |
743 | increase_modlist(void) | |
744 | { | |
745 | struct module **new_modlist = NULL; | |
746 | ||
747 | if((num_mods + 1) < max_mods) | |
748 | return; | |
749 | ||
1e170010 | 750 | new_modlist = (struct module **) rb_malloc(sizeof(struct module *) * |
212380e3 | 751 | (max_mods + MODS_INCREMENT)); |
1e170010 | 752 | memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods); |
212380e3 | 753 | |
637c4932 | 754 | rb_free(modlist); |
212380e3 AC |
755 | modlist = new_modlist; |
756 | max_mods += MODS_INCREMENT; | |
757 | } |