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