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