]>
Commit | Line | Data |
---|---|---|
212380e3 | 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 | 25 | */ |
26 | ||
27 | #include "stdinc.h" | |
28 | ||
29 | ||
30 | #include "modules.h" | |
d3455e2c | 31 | #include "logger.h" |
212380e3 | 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" | |
13ae2f4b | 40 | #include "match.h" |
212380e3 | 41 | |
42 | ||
43 | ||
44 | /* -TimeMr14C: | |
45 | * I have moved the dl* function definitions and | |
46 | * the two functions (load_a_module / unload_a_module) to the | |
47 | * file dynlink.c | |
48 | * And also made the necessary changes to those functions | |
49 | * to comply with shl_load and friends. | |
50 | * In this file, to keep consistency with the makefile, | |
51 | * I added the ability to load *.sl files, too. | |
52 | * 27/02/2002 | |
53 | */ | |
54 | ||
55 | #ifndef STATIC_MODULES | |
56 | ||
57 | struct module **modlist = NULL; | |
58 | ||
59 | static const char *core_module_table[] = { | |
60 | "m_die", | |
61 | "m_error", | |
62 | "m_join", | |
63 | "m_kick", | |
64 | "m_kill", | |
65 | "m_message", | |
66 | "m_mode", | |
67 | "m_nick", | |
68 | "m_part", | |
69 | "m_quit", | |
70 | "m_server", | |
212380e3 | 71 | "m_squit", |
72 | NULL | |
73 | }; | |
74 | ||
75 | #define MODS_INCREMENT 10 | |
76 | int num_mods = 0; | |
77 | int max_mods = MODS_INCREMENT; | |
78 | ||
af81d5a0 | 79 | static rb_dlink_list mod_paths; |
212380e3 | 80 | |
81 | static int mo_modload(struct Client *, struct Client *, int, const char **); | |
82 | static int mo_modlist(struct Client *, struct Client *, int, const char **); | |
83 | static int mo_modreload(struct Client *, struct Client *, int, const char **); | |
84 | static int mo_modunload(struct Client *, struct Client *, int, const char **); | |
85 | static int mo_modrestart(struct Client *, struct Client *, int, const char **); | |
86 | ||
87 | struct Message modload_msgtab = { | |
88 | "MODLOAD", 0, 0, 0, MFLG_SLOW, | |
89 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modload, 2}} | |
90 | }; | |
91 | ||
92 | struct Message modunload_msgtab = { | |
93 | "MODUNLOAD", 0, 0, 0, MFLG_SLOW, | |
94 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modunload, 2}} | |
95 | }; | |
96 | ||
97 | struct Message modreload_msgtab = { | |
98 | "MODRELOAD", 0, 0, 0, MFLG_SLOW, | |
99 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modreload, 2}} | |
100 | }; | |
101 | ||
102 | struct Message modlist_msgtab = { | |
103 | "MODLIST", 0, 0, 0, MFLG_SLOW, | |
104 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modlist, 0}} | |
105 | }; | |
106 | ||
107 | struct Message modrestart_msgtab = { | |
108 | "MODRESTART", 0, 0, 0, MFLG_SLOW, | |
109 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modrestart, 0}} | |
110 | }; | |
111 | ||
112 | extern struct Message error_msgtab; | |
113 | ||
114 | void | |
115 | modules_init(void) | |
116 | { | |
117 | mod_add_cmd(&modload_msgtab); | |
118 | mod_add_cmd(&modunload_msgtab); | |
119 | mod_add_cmd(&modreload_msgtab); | |
120 | mod_add_cmd(&modlist_msgtab); | |
121 | mod_add_cmd(&modrestart_msgtab); | |
122 | ||
123 | /* Add the default paths we look in to the module system --nenolod */ | |
124 | mod_add_path(MODPATH); | |
125 | mod_add_path(AUTOMODPATH); | |
126 | } | |
127 | ||
128 | /* mod_find_path() | |
129 | * | |
130 | * input - path | |
131 | * output - none | |
132 | * side effects - returns a module path from path | |
133 | */ | |
134 | static struct module_path * | |
135 | mod_find_path(const char *path) | |
136 | { | |
af81d5a0 | 137 | rb_dlink_node *ptr; |
212380e3 | 138 | struct module_path *mpath; |
139 | ||
8e69bb4e | 140 | RB_DLINK_FOREACH(ptr, mod_paths.head) |
212380e3 | 141 | { |
142 | mpath = ptr->data; | |
143 | ||
144 | if(!strcmp(path, mpath->path)) | |
145 | return mpath; | |
146 | } | |
147 | ||
148 | return NULL; | |
149 | } | |
150 | ||
151 | /* mod_add_path | |
152 | * | |
153 | * input - path | |
154 | * ouput - | |
155 | * side effects - adds path to list | |
156 | */ | |
157 | void | |
158 | mod_add_path(const char *path) | |
159 | { | |
160 | struct module_path *pathst; | |
161 | ||
162 | if(mod_find_path(path)) | |
163 | return; | |
164 | ||
8e43b0b4 | 165 | pathst = rb_malloc(sizeof(struct module_path)); |
212380e3 | 166 | |
167 | strcpy(pathst->path, path); | |
af81d5a0 | 168 | rb_dlinkAddAlloc(pathst, &mod_paths); |
212380e3 | 169 | } |
170 | ||
171 | /* mod_clear_paths() | |
172 | * | |
173 | * input - | |
174 | * output - | |
175 | * side effects - clear the lists of paths | |
176 | */ | |
177 | void | |
178 | mod_clear_paths(void) | |
179 | { | |
90a3c35b | 180 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 181 | |
90a3c35b | 182 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
212380e3 | 183 | { |
90a3c35b | 184 | rb_free(ptr->data); |
3c8a7c39 | 185 | rb_free_rb_dlink_node(ptr); |
212380e3 | 186 | } |
187 | ||
188 | mod_paths.head = mod_paths.tail = NULL; | |
189 | mod_paths.length = 0; | |
190 | } | |
191 | ||
212380e3 | 192 | /* findmodule_byname |
193 | * | |
194 | * input - | |
195 | * output - | |
196 | * side effects - | |
197 | */ | |
198 | ||
199 | int | |
200 | findmodule_byname(const char *name) | |
201 | { | |
202 | int i; | |
203 | ||
204 | for (i = 0; i < num_mods; i++) | |
205 | { | |
206 | if(!irccmp(modlist[i]->name, name)) | |
207 | return i; | |
208 | } | |
209 | return -1; | |
210 | } | |
211 | ||
212 | /* load_all_modules() | |
213 | * | |
214 | * input - | |
215 | * output - | |
216 | * side effects - | |
217 | */ | |
218 | void | |
219 | load_all_modules(int warn) | |
220 | { | |
221 | DIR *system_module_dir = NULL; | |
222 | struct dirent *ldirent = NULL; | |
223 | char module_fq_name[PATH_MAX + 1]; | |
224 | int len; | |
225 | ||
226 | modules_init(); | |
227 | ||
8e43b0b4 | 228 | modlist = (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT)); |
212380e3 | 229 | |
230 | max_mods = MODS_INCREMENT; | |
231 | ||
232 | system_module_dir = opendir(AUTOMODPATH); | |
233 | ||
234 | if(system_module_dir == NULL) | |
235 | { | |
236 | ilog(L_MAIN, "Could not load modules from %s: %s", AUTOMODPATH, strerror(errno)); | |
237 | return; | |
238 | } | |
239 | ||
240 | while ((ldirent = readdir(system_module_dir)) != NULL) | |
241 | { | |
242 | len = strlen(ldirent->d_name); | |
243 | if((len > 3) && !strcmp(ldirent->d_name+len-3, SHARED_SUFFIX)) | |
244 | { | |
38e6acdd | 245 | (void) rb_snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name); |
212380e3 | 246 | (void) load_a_module(module_fq_name, warn, 0); |
247 | } | |
248 | ||
249 | } | |
250 | (void) closedir(system_module_dir); | |
251 | } | |
252 | ||
253 | /* load_core_modules() | |
254 | * | |
255 | * input - | |
256 | * output - | |
257 | * side effects - core modules are loaded, if any fail, kill ircd | |
258 | */ | |
259 | void | |
260 | load_core_modules(int warn) | |
261 | { | |
262 | char module_name[MAXPATHLEN]; | |
263 | int i; | |
264 | ||
265 | ||
266 | for (i = 0; core_module_table[i]; i++) | |
267 | { | |
38e6acdd | 268 | rb_snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH, |
212380e3 | 269 | core_module_table[i], SHARED_SUFFIX); |
270 | ||
271 | if(load_a_module(module_name, warn, 1) == -1) | |
272 | { | |
273 | ilog(L_MAIN, | |
274 | "Error loading core module %s%s: terminating ircd", | |
275 | core_module_table[i], SHARED_SUFFIX); | |
276 | exit(0); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | /* load_one_module() | |
282 | * | |
283 | * input - | |
284 | * output - | |
285 | * side effects - | |
286 | */ | |
287 | int | |
288 | load_one_module(const char *path, int coremodule) | |
289 | { | |
290 | char modpath[MAXPATHLEN]; | |
af81d5a0 | 291 | rb_dlink_node *pathst; |
212380e3 | 292 | struct module_path *mpath; |
293 | ||
294 | struct stat statbuf; | |
295 | ||
296 | if (server_state_foreground == 1) | |
297 | inotice("loading module %s ...", path); | |
298 | ||
8e69bb4e | 299 | RB_DLINK_FOREACH(pathst, mod_paths.head) |
212380e3 | 300 | { |
301 | mpath = pathst->data; | |
302 | ||
38e6acdd | 303 | rb_snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path); |
212380e3 | 304 | if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) |
305 | { | |
306 | if(stat(modpath, &statbuf) == 0) | |
307 | { | |
308 | if(S_ISREG(statbuf.st_mode)) | |
309 | { | |
310 | /* Regular files only please */ | |
311 | if(coremodule) | |
312 | return load_a_module(modpath, 1, 1); | |
313 | else | |
314 | return load_a_module(modpath, 1, 0); | |
315 | } | |
316 | } | |
317 | ||
318 | } | |
319 | } | |
320 | ||
321 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path); | |
322 | return -1; | |
323 | } | |
324 | ||
325 | ||
326 | /* load a module .. */ | |
327 | static int | |
328 | mo_modload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
329 | { | |
330 | char *m_bn; | |
331 | ||
332 | if(!IsOperAdmin(source_p)) | |
333 | { | |
334 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
335 | me.name, source_p->name, "admin"); | |
336 | return 0; | |
337 | } | |
338 | ||
3b0f3294 | 339 | m_bn = rb_basename(parv[1]); |
212380e3 | 340 | |
341 | if(findmodule_byname(m_bn) != -1) | |
342 | { | |
5366977b | 343 | sendto_one_notice(source_p, ":Module %s is already loaded", m_bn); |
90a3c35b | 344 | rb_free(m_bn); |
212380e3 | 345 | return 0; |
346 | } | |
347 | ||
348 | load_one_module(parv[1], 0); | |
349 | ||
90a3c35b | 350 | rb_free(m_bn); |
212380e3 | 351 | |
352 | return 0; | |
353 | } | |
354 | ||
355 | ||
356 | /* unload a module .. */ | |
357 | static int | |
358 | mo_modunload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
359 | { | |
360 | char *m_bn; | |
361 | int modindex; | |
362 | ||
363 | if(!IsOperAdmin(source_p)) | |
364 | { | |
365 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
366 | me.name, source_p->name, "admin"); | |
367 | return 0; | |
368 | } | |
369 | ||
3b0f3294 | 370 | m_bn = rb_basename(parv[1]); |
212380e3 | 371 | |
372 | if((modindex = findmodule_byname(m_bn)) == -1) | |
373 | { | |
5366977b | 374 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
90a3c35b | 375 | rb_free(m_bn); |
212380e3 | 376 | return 0; |
377 | } | |
378 | ||
379 | if(modlist[modindex]->core == 1) | |
380 | { | |
5366977b | 381 | sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn); |
90a3c35b | 382 | rb_free(m_bn); |
212380e3 | 383 | return 0; |
384 | } | |
385 | ||
386 | if(unload_one_module(m_bn, 1) == -1) | |
387 | { | |
5366977b | 388 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 | 389 | } |
5366977b | 390 | |
90a3c35b | 391 | rb_free(m_bn); |
212380e3 | 392 | return 0; |
393 | } | |
394 | ||
395 | /* unload and load in one! */ | |
396 | static int | |
397 | mo_modreload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
398 | { | |
399 | char *m_bn; | |
400 | int modindex; | |
401 | int check_core; | |
402 | ||
403 | if(!IsOperAdmin(source_p)) | |
404 | { | |
405 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
406 | me.name, source_p->name, "admin"); | |
407 | return 0; | |
408 | } | |
409 | ||
3b0f3294 | 410 | m_bn = rb_basename(parv[1]); |
212380e3 | 411 | |
412 | if((modindex = findmodule_byname(m_bn)) == -1) | |
413 | { | |
5366977b | 414 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
90a3c35b | 415 | rb_free(m_bn); |
212380e3 | 416 | return 0; |
417 | } | |
418 | ||
419 | check_core = modlist[modindex]->core; | |
420 | ||
421 | if(unload_one_module(m_bn, 1) == -1) | |
422 | { | |
5366977b | 423 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
90a3c35b | 424 | rb_free(m_bn); |
212380e3 | 425 | return 0; |
426 | } | |
427 | ||
428 | if((load_one_module(parv[1], check_core) == -1) && check_core) | |
429 | { | |
430 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
431 | "Error reloading core module: %s: terminating ircd", parv[1]); | |
432 | ilog(L_MAIN, "Error loading core module %s: terminating ircd", parv[1]); | |
433 | exit(0); | |
434 | } | |
435 | ||
90a3c35b | 436 | rb_free(m_bn); |
212380e3 | 437 | return 0; |
438 | } | |
439 | ||
440 | /* list modules .. */ | |
441 | static int | |
442 | mo_modlist(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
443 | { | |
444 | int i; | |
445 | ||
446 | if(!IsOperAdmin(source_p)) | |
447 | { | |
448 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
449 | me.name, source_p->name, "admin"); | |
450 | return 0; | |
451 | } | |
452 | ||
453 | for (i = 0; i < num_mods; i++) | |
454 | { | |
455 | if(parc > 1) | |
456 | { | |
457 | if(match(parv[1], modlist[i]->name)) | |
458 | { | |
459 | sendto_one(source_p, form_str(RPL_MODLIST), | |
460 | me.name, source_p->name, | |
461 | modlist[i]->name, | |
462 | modlist[i]->address, | |
463 | modlist[i]->version, modlist[i]->core ? "(core)" : ""); | |
464 | } | |
465 | } | |
466 | else | |
467 | { | |
468 | sendto_one(source_p, form_str(RPL_MODLIST), | |
469 | me.name, source_p->name, modlist[i]->name, | |
470 | modlist[i]->address, modlist[i]->version, | |
471 | modlist[i]->core ? "(core)" : ""); | |
472 | } | |
473 | } | |
474 | ||
475 | sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, source_p->name); | |
476 | return 0; | |
477 | } | |
478 | ||
479 | /* unload and reload all modules */ | |
480 | static int | |
481 | mo_modrestart(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
482 | { | |
483 | int modnum; | |
484 | ||
485 | if(!IsOperAdmin(source_p)) | |
486 | { | |
487 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
488 | me.name, source_p->name, "admin"); | |
489 | return 0; | |
490 | } | |
491 | ||
5366977b | 492 | sendto_one_notice(source_p, ":Reloading all modules"); |
212380e3 | 493 | |
494 | modnum = num_mods; | |
495 | while (num_mods) | |
496 | unload_one_module(modlist[0]->name, 0); | |
497 | ||
498 | load_all_modules(0); | |
499 | load_core_modules(0); | |
500 | rehash(0); | |
501 | ||
502 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
503 | "Module Restart: %d modules unloaded, %d modules loaded", | |
504 | modnum, num_mods); | |
505 | ilog(L_MAIN, "Module Restart: %d modules unloaded, %d modules loaded", modnum, num_mods); | |
506 | return 0; | |
507 | } | |
508 | ||
509 | ||
510 | ||
511 | #ifndef RTLD_NOW | |
512 | #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */ | |
513 | #endif | |
514 | ||
1386c9a7 JT |
515 | #ifndef RTLD_LOCAL |
516 | #define RTLD_LOCAL 0 | |
517 | #endif | |
518 | ||
212380e3 | 519 | #ifdef CHARYBDIS_PROFILE |
520 | # ifndef RTLD_PROFILE | |
521 | # warning libdl may not support profiling, sucks. :( | |
522 | # define RTLD_PROFILE 0 | |
523 | # endif | |
524 | #endif | |
525 | ||
526 | static void increase_modlist(void); | |
527 | ||
528 | #define MODS_INCREMENT 10 | |
529 | ||
530 | static char unknown_ver[] = "<unknown>"; | |
531 | ||
532 | /* This file contains the core functions to use dynamic libraries. | |
533 | * -TimeMr14C | |
534 | */ | |
535 | ||
536 | ||
537 | #ifdef HAVE_MACH_O_DYLD_H | |
538 | /* | |
539 | ** jmallett's dl*(3) shims for NSModule(3) systems. | |
540 | */ | |
541 | #include <mach-o/dyld.h> | |
542 | ||
543 | #ifndef HAVE_DLOPEN | |
544 | #ifndef RTLD_LAZY | |
545 | #define RTLD_LAZY 2185 /* built-in dl*(3) don't care */ | |
546 | #endif | |
547 | ||
548 | void undefinedErrorHandler(const char *); | |
549 | NSModule multipleErrorHandler(NSSymbol, NSModule, NSModule); | |
550 | void linkEditErrorHandler(NSLinkEditErrors, int, const char *, const char *); | |
551 | char *dlerror(void); | |
552 | void *dlopen(char *, int); | |
553 | int dlclose(void *); | |
554 | void *dlsym(void *, char *); | |
555 | ||
556 | static int firstLoad = TRUE; | |
557 | static int myDlError; | |
558 | static char *myErrorTable[] = { "Loading file as object failed\n", | |
559 | "Loading file as object succeeded\n", | |
560 | "Not a valid shared object\n", | |
561 | "Architecture of object invalid on this architecture\n", | |
562 | "Invalid or corrupt image\n", | |
563 | "Could not access object\n", | |
564 | "NSCreateObjectFileImageFromFile failed\n", | |
565 | NULL | |
566 | }; | |
567 | ||
568 | void | |
569 | undefinedErrorHandler(const char *symbolName) | |
570 | { | |
571 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Undefined symbol: %s", symbolName); | |
572 | ilog(L_MAIN, "Undefined symbol: %s", symbolName); | |
573 | return; | |
574 | } | |
575 | ||
576 | NSModule | |
577 | multipleErrorHandler(NSSymbol s, NSModule old, NSModule new) | |
578 | { | |
579 | /* XXX | |
580 | ** This results in substantial leaking of memory... Should free one | |
581 | ** module, maybe? | |
582 | */ | |
583 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
584 | "Symbol `%s' found in `%s' and `%s'", | |
585 | NSNameOfSymbol(s), NSNameOfModule(old), NSNameOfModule(new)); | |
586 | ilog(L_MAIN, "Symbol `%s' found in `%s' and `%s'", | |
587 | NSNameOfSymbol(s), NSNameOfModule(old), NSNameOfModule(new)); | |
588 | /* We return which module should be considered valid, I believe */ | |
589 | return new; | |
590 | } | |
591 | ||
592 | void | |
593 | linkEditErrorHandler(NSLinkEditErrors errorClass, int errnum, | |
594 | const char *fileName, const char *errorString) | |
595 | { | |
596 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
597 | "Link editor error: %s for %s", errorString, fileName); | |
598 | ilog(L_MAIN, "Link editor error: %s for %s", errorString, fileName); | |
599 | return; | |
600 | } | |
601 | ||
602 | char * | |
603 | dlerror(void) | |
604 | { | |
605 | return myDlError == NSObjectFileImageSuccess ? NULL : myErrorTable[myDlError % 7]; | |
606 | } | |
607 | ||
608 | void * | |
609 | dlopen(char *filename, int unused) | |
610 | { | |
611 | NSObjectFileImage myImage; | |
612 | NSModule myModule; | |
613 | ||
614 | if(firstLoad) | |
615 | { | |
616 | /* | |
617 | ** If we are loading our first symbol (huzzah!) we should go ahead | |
618 | ** and install link editor error handling! | |
619 | */ | |
620 | NSLinkEditErrorHandlers linkEditorErrorHandlers; | |
621 | ||
622 | linkEditorErrorHandlers.undefined = undefinedErrorHandler; | |
623 | linkEditorErrorHandlers.multiple = multipleErrorHandler; | |
624 | linkEditorErrorHandlers.linkEdit = linkEditErrorHandler; | |
625 | NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers); | |
626 | firstLoad = FALSE; | |
627 | } | |
628 | myDlError = NSCreateObjectFileImageFromFile(filename, &myImage); | |
629 | if(myDlError != NSObjectFileImageSuccess) | |
630 | { | |
631 | return NULL; | |
632 | } | |
633 | myModule = NSLinkModule(myImage, filename, NSLINKMODULE_OPTION_PRIVATE); | |
634 | return (void *) myModule; | |
635 | } | |
636 | ||
637 | int | |
638 | dlclose(void *myModule) | |
639 | { | |
640 | NSUnLinkModule(myModule, FALSE); | |
641 | return 0; | |
642 | } | |
643 | ||
644 | void * | |
645 | dlsym(void *myModule, char *mySymbolName) | |
646 | { | |
647 | NSSymbol mySymbol; | |
648 | ||
649 | mySymbol = NSLookupSymbolInModule((NSModule) myModule, mySymbolName); | |
650 | return NSAddressOfSymbol(mySymbol); | |
651 | } | |
652 | #endif | |
653 | #endif | |
654 | ||
655 | ||
656 | /* | |
657 | * HPUX dl compat functions | |
658 | */ | |
659 | #if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN) | |
660 | #define RTLD_LAZY BIND_DEFERRED | |
661 | #define RTLD_GLOBAL DYNAMIC_PATH | |
662 | #define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0) | |
663 | #define dlclose(handle) shl_unload((shl_t)(handle)) | |
664 | #define dlsym(handle,name) hpux_dlsym(handle,name) | |
665 | #define dlerror() strerror(errno) | |
666 | ||
667 | static void * | |
668 | hpux_dlsym(void *handle, char *name) | |
669 | { | |
670 | void *sym_addr; | |
671 | if(!shl_findsym((shl_t *) & handle, name, TYPE_UNDEFINED, &sym_addr)) | |
672 | return sym_addr; | |
673 | return NULL; | |
674 | } | |
675 | ||
676 | #endif | |
677 | ||
678 | /* unload_one_module() | |
679 | * | |
680 | * inputs - name of module to unload | |
681 | * - 1 to say modules unloaded, 0 to not | |
682 | * output - 0 if successful, -1 if error | |
683 | * side effects - module is unloaded | |
684 | */ | |
685 | int | |
686 | unload_one_module(const char *name, int warn) | |
687 | { | |
688 | int modindex; | |
689 | ||
690 | if((modindex = findmodule_byname(name)) == -1) | |
691 | return -1; | |
692 | ||
693 | /* | |
694 | ** XXX - The type system in C does not allow direct conversion between | |
695 | ** data and function pointers, but as it happens, most C compilers will | |
696 | ** safely do this, however it is a theoretical overlow to cast as we | |
697 | ** must do here. I have library functions to take care of this, but | |
698 | ** despite being more "correct" for the C language, this is more | |
699 | ** practical. Removing the abuse of the ability to cast ANY pointer | |
700 | ** to and from an integer value here will break some compilers. | |
701 | ** -jmallett | |
702 | */ | |
703 | /* Left the comment in but the code isn't here any more -larne */ | |
704 | switch (modlist[modindex]->mapi_version) | |
705 | { | |
706 | case 1: | |
707 | { | |
708 | struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header; | |
709 | if(mheader->mapi_command_list) | |
710 | { | |
711 | struct Message **m; | |
712 | for (m = mheader->mapi_command_list; *m; ++m) | |
713 | mod_del_cmd(*m); | |
714 | } | |
715 | ||
716 | /* hook events are never removed, we simply lose the | |
717 | * ability to call them --fl | |
718 | */ | |
719 | if(mheader->mapi_hfn_list) | |
720 | { | |
721 | mapi_hfn_list_av1 *m; | |
722 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
723 | remove_hook(m->hapi_name, m->fn); | |
724 | } | |
725 | ||
726 | if(mheader->mapi_unregister) | |
727 | mheader->mapi_unregister(); | |
728 | break; | |
729 | } | |
730 | default: | |
731 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
732 | "Unknown/unsupported MAPI version %d when unloading %s!", | |
733 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
734 | ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!", | |
735 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
736 | break; | |
737 | } | |
738 | ||
739 | dlclose(modlist[modindex]->address); | |
740 | ||
90a3c35b | 741 | rb_free(modlist[modindex]->name); |
212380e3 | 742 | memcpy(&modlist[modindex], &modlist[modindex + 1], |
743 | sizeof(struct module) * ((num_mods - 1) - modindex)); | |
744 | ||
745 | if(num_mods != 0) | |
746 | num_mods--; | |
747 | ||
748 | if(warn == 1) | |
749 | { | |
750 | ilog(L_MAIN, "Module %s unloaded", name); | |
751 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name); | |
752 | } | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
757 | ||
758 | /* | |
759 | * load_a_module() | |
760 | * | |
761 | * inputs - path name of module, int to notice, int of core | |
762 | * output - -1 if error 0 if success | |
763 | * side effects - loads a module if successful | |
764 | */ | |
765 | int | |
766 | load_a_module(const char *path, int warn, int core) | |
767 | { | |
768 | void *tmpptr = NULL; | |
769 | ||
770 | char *mod_basename; | |
771 | const char *ver; | |
772 | ||
773 | int *mapi_version; | |
774 | ||
3b0f3294 | 775 | mod_basename = rb_basename(path); |
212380e3 | 776 | |
777 | #ifdef CHARYBDIS_PROFILE | |
1386c9a7 | 778 | tmpptr = dlopen(path, RTLD_NOW | RTLD_LOCAL | RTLD_PROFILE); |
212380e3 | 779 | #else |
1386c9a7 | 780 | tmpptr = dlopen(path, RTLD_NOW | RTLD_LOCAL); |
212380e3 | 781 | #endif |
782 | ||
783 | if(tmpptr == NULL) | |
784 | { | |
785 | const char *err = dlerror(); | |
786 | ||
787 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
788 | "Error loading module %s: %s", mod_basename, err); | |
789 | ilog(L_MAIN, "Error loading module %s: %s", mod_basename, err); | |
90a3c35b | 790 | rb_free(mod_basename); |
212380e3 | 791 | return -1; |
792 | } | |
793 | ||
794 | ||
795 | /* | |
796 | * _mheader is actually a struct mapi_mheader_*, but mapi_version | |
797 | * is always the first member of this structure, so we treate it | |
798 | * as a single int in order to determine the API version. | |
799 | * -larne. | |
800 | */ | |
801 | mapi_version = (int *) (uintptr_t) dlsym(tmpptr, "_mheader"); | |
802 | if((mapi_version == NULL | |
803 | && (mapi_version = (int *) (uintptr_t) dlsym(tmpptr, "__mheader")) == NULL) | |
804 | || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR) | |
805 | { | |
806 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
807 | "Data format error: module %s has no MAPI header.", | |
808 | mod_basename); | |
809 | ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_basename); | |
810 | (void) dlclose(tmpptr); | |
90a3c35b | 811 | rb_free(mod_basename); |
212380e3 | 812 | return -1; |
813 | } | |
814 | ||
815 | switch (MAPI_VERSION(*mapi_version)) | |
816 | { | |
817 | case 1: | |
818 | { | |
819 | struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *) mapi_version; /* see above */ | |
820 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) | |
821 | { | |
822 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
823 | mod_basename); | |
824 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
825 | "Module %s indicated failure during load.", | |
826 | mod_basename); | |
827 | dlclose(tmpptr); | |
90a3c35b | 828 | rb_free(mod_basename); |
212380e3 | 829 | return -1; |
830 | } | |
831 | if(mheader->mapi_command_list) | |
832 | { | |
833 | struct Message **m; | |
834 | for (m = mheader->mapi_command_list; *m; ++m) | |
835 | mod_add_cmd(*m); | |
836 | } | |
837 | ||
838 | if(mheader->mapi_hook_list) | |
839 | { | |
840 | mapi_hlist_av1 *m; | |
841 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
842 | *m->hapi_id = register_hook(m->hapi_name); | |
843 | } | |
844 | ||
845 | if(mheader->mapi_hfn_list) | |
846 | { | |
847 | mapi_hfn_list_av1 *m; | |
848 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
849 | add_hook(m->hapi_name, m->fn); | |
850 | } | |
851 | ||
852 | ver = mheader->mapi_module_version; | |
853 | break; | |
854 | } | |
855 | ||
856 | default: | |
857 | ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.", | |
858 | mod_basename, MAPI_VERSION(*mapi_version)); | |
859 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
860 | "Module %s has unknown/unsupported MAPI version %d.", | |
861 | mod_basename, *mapi_version); | |
862 | dlclose(tmpptr); | |
90a3c35b | 863 | rb_free(mod_basename); |
212380e3 | 864 | return -1; |
865 | } | |
866 | ||
867 | if(ver == NULL) | |
868 | ver = unknown_ver; | |
869 | ||
870 | increase_modlist(); | |
871 | ||
8e43b0b4 | 872 | modlist[num_mods] = rb_malloc(sizeof(struct module)); |
212380e3 | 873 | modlist[num_mods]->address = tmpptr; |
874 | modlist[num_mods]->version = ver; | |
875 | modlist[num_mods]->core = core; | |
62d28946 | 876 | modlist[num_mods]->name = rb_strdup(mod_basename); |
212380e3 | 877 | modlist[num_mods]->mapi_header = mapi_version; |
878 | modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version); | |
879 | num_mods++; | |
880 | ||
881 | if(warn == 1) | |
882 | { | |
883 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
884 | "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx", | |
885 | mod_basename, ver, MAPI_VERSION(*mapi_version), | |
886 | (unsigned long) tmpptr); | |
887 | ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx", | |
888 | mod_basename, ver, MAPI_VERSION(*mapi_version), (unsigned long) tmpptr); | |
889 | } | |
90a3c35b | 890 | rb_free(mod_basename); |
212380e3 | 891 | return 0; |
892 | } | |
893 | ||
894 | /* | |
895 | * increase_modlist | |
896 | * | |
897 | * inputs - NONE | |
898 | * output - NONE | |
899 | * side effects - expand the size of modlist if necessary | |
900 | */ | |
901 | static void | |
902 | increase_modlist(void) | |
903 | { | |
904 | struct module **new_modlist = NULL; | |
905 | ||
906 | if((num_mods + 1) < max_mods) | |
907 | return; | |
908 | ||
8e43b0b4 | 909 | new_modlist = (struct module **) rb_malloc(sizeof(struct module) * |
212380e3 | 910 | (max_mods + MODS_INCREMENT)); |
911 | memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module) * num_mods); | |
912 | ||
90a3c35b | 913 | rb_free(modlist); |
212380e3 | 914 | modlist = new_modlist; |
915 | max_mods += MODS_INCREMENT; | |
916 | } | |
917 | ||
918 | #else /* STATIC_MODULES */ | |
919 | ||
920 | /* load_all_modules() | |
921 | * | |
922 | * input - | |
923 | * output - | |
924 | * side effects - all the msgtabs are added for static modules | |
925 | */ | |
926 | void | |
927 | load_all_modules(int warn) | |
928 | { | |
929 | load_static_modules(); | |
930 | } | |
931 | ||
932 | #endif /* STATIC_MODULES */ |