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