]>
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" | |
31 | #include "s_log.h" | |
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" | |
40 | #include "irc_string.h" | |
41 | #include "memory.h" | |
42 | #include "tools.h" | |
43 | #include "sprintf_irc.h" | |
44 | ||
45 | ||
46 | ||
47 | /* -TimeMr14C: | |
48 | * I have moved the dl* function definitions and | |
49 | * the two functions (load_a_module / unload_a_module) to the | |
50 | * file dynlink.c | |
51 | * And also made the necessary changes to those functions | |
52 | * to comply with shl_load and friends. | |
53 | * In this file, to keep consistency with the makefile, | |
54 | * I added the ability to load *.sl files, too. | |
55 | * 27/02/2002 | |
56 | */ | |
57 | ||
58 | #ifndef STATIC_MODULES | |
59 | ||
60 | struct module **modlist = NULL; | |
61 | ||
62 | static const char *core_module_table[] = { | |
63 | "m_die", | |
64 | "m_error", | |
65 | "m_join", | |
66 | "m_kick", | |
67 | "m_kill", | |
68 | "m_message", | |
69 | "m_mode", | |
70 | "m_nick", | |
71 | "m_part", | |
72 | "m_quit", | |
73 | "m_server", | |
74 | "m_sjoin", | |
75 | "m_squit", | |
76 | NULL | |
77 | }; | |
78 | ||
79 | #define MODS_INCREMENT 10 | |
80 | int num_mods = 0; | |
81 | int max_mods = MODS_INCREMENT; | |
82 | ||
83 | static dlink_list mod_paths; | |
84 | ||
85 | static int mo_modload(struct Client *, struct Client *, int, const char **); | |
86 | static int mo_modlist(struct Client *, struct Client *, int, const char **); | |
87 | static int mo_modreload(struct Client *, struct Client *, int, const char **); | |
88 | static int mo_modunload(struct Client *, struct Client *, int, const char **); | |
89 | static int mo_modrestart(struct Client *, struct Client *, int, const char **); | |
90 | ||
91 | struct Message modload_msgtab = { | |
92 | "MODLOAD", 0, 0, 0, MFLG_SLOW, | |
93 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modload, 2}} | |
94 | }; | |
95 | ||
96 | struct Message modunload_msgtab = { | |
97 | "MODUNLOAD", 0, 0, 0, MFLG_SLOW, | |
98 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modunload, 2}} | |
99 | }; | |
100 | ||
101 | struct Message modreload_msgtab = { | |
102 | "MODRELOAD", 0, 0, 0, MFLG_SLOW, | |
103 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modreload, 2}} | |
104 | }; | |
105 | ||
106 | struct Message modlist_msgtab = { | |
107 | "MODLIST", 0, 0, 0, MFLG_SLOW, | |
108 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modlist, 0}} | |
109 | }; | |
110 | ||
111 | struct Message modrestart_msgtab = { | |
112 | "MODRESTART", 0, 0, 0, MFLG_SLOW, | |
113 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_modrestart, 0}} | |
114 | }; | |
115 | ||
116 | extern struct Message error_msgtab; | |
117 | ||
118 | void | |
119 | modules_init(void) | |
120 | { | |
121 | mod_add_cmd(&modload_msgtab); | |
122 | mod_add_cmd(&modunload_msgtab); | |
123 | mod_add_cmd(&modreload_msgtab); | |
124 | mod_add_cmd(&modlist_msgtab); | |
125 | mod_add_cmd(&modrestart_msgtab); | |
126 | ||
127 | /* Add the default paths we look in to the module system --nenolod */ | |
128 | mod_add_path(MODPATH); | |
129 | mod_add_path(AUTOMODPATH); | |
130 | } | |
131 | ||
132 | /* mod_find_path() | |
133 | * | |
134 | * input - path | |
135 | * output - none | |
136 | * side effects - returns a module path from path | |
137 | */ | |
138 | static struct module_path * | |
139 | mod_find_path(const char *path) | |
140 | { | |
141 | dlink_node *ptr; | |
142 | struct module_path *mpath; | |
143 | ||
144 | DLINK_FOREACH(ptr, mod_paths.head) | |
145 | { | |
146 | mpath = ptr->data; | |
147 | ||
148 | if(!strcmp(path, mpath->path)) | |
149 | return mpath; | |
150 | } | |
151 | ||
152 | return NULL; | |
153 | } | |
154 | ||
155 | /* mod_add_path | |
156 | * | |
157 | * input - path | |
158 | * ouput - | |
159 | * side effects - adds path to list | |
160 | */ | |
161 | void | |
162 | mod_add_path(const char *path) | |
163 | { | |
164 | struct module_path *pathst; | |
165 | ||
166 | if(mod_find_path(path)) | |
167 | return; | |
168 | ||
169 | pathst = MyMalloc(sizeof(struct module_path)); | |
170 | ||
171 | strcpy(pathst->path, path); | |
172 | dlinkAddAlloc(pathst, &mod_paths); | |
173 | } | |
174 | ||
175 | /* mod_clear_paths() | |
176 | * | |
177 | * input - | |
178 | * output - | |
179 | * side effects - clear the lists of paths | |
180 | */ | |
181 | void | |
182 | mod_clear_paths(void) | |
183 | { | |
184 | dlink_node *ptr, *next_ptr; | |
185 | ||
186 | DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) | |
187 | { | |
188 | MyFree(ptr->data); | |
189 | free_dlink_node(ptr); | |
190 | } | |
191 | ||
192 | mod_paths.head = mod_paths.tail = NULL; | |
193 | mod_paths.length = 0; | |
194 | } | |
195 | ||
196 | /* irc_basename | |
197 | * | |
198 | * input - | |
199 | * output - | |
200 | * side effects - | |
201 | */ | |
202 | char * | |
203 | irc_basename(const char *path) | |
204 | { | |
205 | char *mod_basename = MyMalloc(strlen(path) + 1); | |
206 | const char *s; | |
207 | ||
208 | if(!(s = strrchr(path, '/'))) | |
209 | s = path; | |
210 | else | |
211 | s++; | |
212 | ||
213 | (void) strcpy(mod_basename, s); | |
214 | return mod_basename; | |
215 | } | |
216 | ||
217 | /* findmodule_byname | |
218 | * | |
219 | * input - | |
220 | * output - | |
221 | * side effects - | |
222 | */ | |
223 | ||
224 | int | |
225 | findmodule_byname(const char *name) | |
226 | { | |
227 | int i; | |
228 | ||
229 | for (i = 0; i < num_mods; i++) | |
230 | { | |
231 | if(!irccmp(modlist[i]->name, name)) | |
232 | return i; | |
233 | } | |
234 | return -1; | |
235 | } | |
236 | ||
237 | /* load_all_modules() | |
238 | * | |
239 | * input - | |
240 | * output - | |
241 | * side effects - | |
242 | */ | |
243 | void | |
244 | load_all_modules(int warn) | |
245 | { | |
246 | DIR *system_module_dir = NULL; | |
247 | struct dirent *ldirent = NULL; | |
248 | char module_fq_name[PATH_MAX + 1]; | |
249 | int len; | |
250 | ||
251 | modules_init(); | |
252 | ||
253 | modlist = (struct module **) MyMalloc(sizeof(struct module) * (MODS_INCREMENT)); | |
254 | ||
255 | max_mods = MODS_INCREMENT; | |
256 | ||
257 | system_module_dir = opendir(AUTOMODPATH); | |
258 | ||
259 | if(system_module_dir == NULL) | |
260 | { | |
261 | ilog(L_MAIN, "Could not load modules from %s: %s", AUTOMODPATH, strerror(errno)); | |
262 | return; | |
263 | } | |
264 | ||
265 | while ((ldirent = readdir(system_module_dir)) != NULL) | |
266 | { | |
267 | len = strlen(ldirent->d_name); | |
268 | if((len > 3) && !strcmp(ldirent->d_name+len-3, SHARED_SUFFIX)) | |
269 | { | |
270 | (void) ircsnprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name); | |
271 | (void) load_a_module(module_fq_name, warn, 0); | |
272 | } | |
273 | ||
274 | } | |
275 | (void) closedir(system_module_dir); | |
276 | } | |
277 | ||
278 | /* load_core_modules() | |
279 | * | |
280 | * input - | |
281 | * output - | |
282 | * side effects - core modules are loaded, if any fail, kill ircd | |
283 | */ | |
284 | void | |
285 | load_core_modules(int warn) | |
286 | { | |
287 | char module_name[MAXPATHLEN]; | |
288 | int i; | |
289 | ||
290 | ||
291 | for (i = 0; core_module_table[i]; i++) | |
292 | { | |
293 | ircsnprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH, | |
294 | core_module_table[i], SHARED_SUFFIX); | |
295 | ||
296 | if(load_a_module(module_name, warn, 1) == -1) | |
297 | { | |
298 | ilog(L_MAIN, | |
299 | "Error loading core module %s%s: terminating ircd", | |
300 | core_module_table[i], SHARED_SUFFIX); | |
301 | exit(0); | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
306 | /* load_one_module() | |
307 | * | |
308 | * input - | |
309 | * output - | |
310 | * side effects - | |
311 | */ | |
312 | int | |
313 | load_one_module(const char *path, int coremodule) | |
314 | { | |
315 | char modpath[MAXPATHLEN]; | |
316 | dlink_node *pathst; | |
317 | struct module_path *mpath; | |
318 | ||
319 | struct stat statbuf; | |
320 | ||
321 | if (server_state_foreground == 1) | |
322 | inotice("loading module %s ...", path); | |
323 | ||
324 | DLINK_FOREACH(pathst, mod_paths.head) | |
325 | { | |
326 | mpath = pathst->data; | |
327 | ||
328 | ircsnprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path); | |
329 | if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) | |
330 | { | |
331 | if(stat(modpath, &statbuf) == 0) | |
332 | { | |
333 | if(S_ISREG(statbuf.st_mode)) | |
334 | { | |
335 | /* Regular files only please */ | |
336 | if(coremodule) | |
337 | return load_a_module(modpath, 1, 1); | |
338 | else | |
339 | return load_a_module(modpath, 1, 0); | |
340 | } | |
341 | } | |
342 | ||
343 | } | |
344 | } | |
345 | ||
346 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path); | |
347 | return -1; | |
348 | } | |
349 | ||
350 | ||
351 | /* load a module .. */ | |
352 | static int | |
353 | mo_modload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
354 | { | |
355 | char *m_bn; | |
356 | ||
357 | if(!IsOperAdmin(source_p)) | |
358 | { | |
359 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
360 | me.name, source_p->name, "admin"); | |
361 | return 0; | |
362 | } | |
363 | ||
364 | m_bn = irc_basename(parv[1]); | |
365 | ||
366 | if(findmodule_byname(m_bn) != -1) | |
367 | { | |
5366977b | 368 | sendto_one_notice(source_p, ":Module %s is already loaded", m_bn); |
212380e3 AC |
369 | MyFree(m_bn); |
370 | return 0; | |
371 | } | |
372 | ||
373 | load_one_module(parv[1], 0); | |
374 | ||
375 | MyFree(m_bn); | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | ||
381 | /* unload a module .. */ | |
382 | static int | |
383 | mo_modunload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
384 | { | |
385 | char *m_bn; | |
386 | int modindex; | |
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 | ||
395 | m_bn = irc_basename(parv[1]); | |
396 | ||
397 | if((modindex = findmodule_byname(m_bn)) == -1) | |
398 | { | |
5366977b | 399 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 AC |
400 | MyFree(m_bn); |
401 | return 0; | |
402 | } | |
403 | ||
404 | if(modlist[modindex]->core == 1) | |
405 | { | |
5366977b | 406 | sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn); |
212380e3 AC |
407 | MyFree(m_bn); |
408 | return 0; | |
409 | } | |
410 | ||
411 | if(unload_one_module(m_bn, 1) == -1) | |
412 | { | |
5366977b | 413 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 | 414 | } |
5366977b | 415 | |
212380e3 AC |
416 | MyFree(m_bn); |
417 | return 0; | |
418 | } | |
419 | ||
420 | /* unload and load in one! */ | |
421 | static int | |
422 | mo_modreload(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
423 | { | |
424 | char *m_bn; | |
425 | int modindex; | |
426 | int check_core; | |
427 | ||
428 | if(!IsOperAdmin(source_p)) | |
429 | { | |
430 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
431 | me.name, source_p->name, "admin"); | |
432 | return 0; | |
433 | } | |
434 | ||
435 | m_bn = irc_basename(parv[1]); | |
436 | ||
437 | if((modindex = findmodule_byname(m_bn)) == -1) | |
438 | { | |
5366977b | 439 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 AC |
440 | MyFree(m_bn); |
441 | return 0; | |
442 | } | |
443 | ||
444 | check_core = modlist[modindex]->core; | |
445 | ||
446 | if(unload_one_module(m_bn, 1) == -1) | |
447 | { | |
5366977b | 448 | sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); |
212380e3 AC |
449 | MyFree(m_bn); |
450 | return 0; | |
451 | } | |
452 | ||
453 | if((load_one_module(parv[1], check_core) == -1) && check_core) | |
454 | { | |
455 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
456 | "Error reloading core module: %s: terminating ircd", parv[1]); | |
457 | ilog(L_MAIN, "Error loading core module %s: terminating ircd", parv[1]); | |
458 | exit(0); | |
459 | } | |
460 | ||
461 | MyFree(m_bn); | |
462 | return 0; | |
463 | } | |
464 | ||
465 | /* list modules .. */ | |
466 | static int | |
467 | mo_modlist(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
468 | { | |
469 | int i; | |
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 | ||
478 | for (i = 0; i < num_mods; i++) | |
479 | { | |
480 | if(parc > 1) | |
481 | { | |
482 | if(match(parv[1], modlist[i]->name)) | |
483 | { | |
484 | sendto_one(source_p, form_str(RPL_MODLIST), | |
485 | me.name, source_p->name, | |
486 | modlist[i]->name, | |
487 | modlist[i]->address, | |
488 | modlist[i]->version, modlist[i]->core ? "(core)" : ""); | |
489 | } | |
490 | } | |
491 | else | |
492 | { | |
493 | sendto_one(source_p, form_str(RPL_MODLIST), | |
494 | me.name, source_p->name, modlist[i]->name, | |
495 | modlist[i]->address, modlist[i]->version, | |
496 | modlist[i]->core ? "(core)" : ""); | |
497 | } | |
498 | } | |
499 | ||
500 | sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, source_p->name); | |
501 | return 0; | |
502 | } | |
503 | ||
504 | /* unload and reload all modules */ | |
505 | static int | |
506 | mo_modrestart(struct Client *client_p, struct Client *source_p, int parc, const char **parv) | |
507 | { | |
508 | int modnum; | |
509 | ||
510 | if(!IsOperAdmin(source_p)) | |
511 | { | |
512 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
513 | me.name, source_p->name, "admin"); | |
514 | return 0; | |
515 | } | |
516 | ||
5366977b | 517 | sendto_one_notice(source_p, ":Reloading all modules"); |
212380e3 AC |
518 | |
519 | modnum = num_mods; | |
520 | while (num_mods) | |
521 | unload_one_module(modlist[0]->name, 0); | |
522 | ||
523 | load_all_modules(0); | |
524 | load_core_modules(0); | |
525 | rehash(0); | |
526 | ||
527 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
528 | "Module Restart: %d modules unloaded, %d modules loaded", | |
529 | modnum, num_mods); | |
530 | ilog(L_MAIN, "Module Restart: %d modules unloaded, %d modules loaded", modnum, num_mods); | |
531 | return 0; | |
532 | } | |
533 | ||
534 | ||
535 | ||
536 | #ifndef RTLD_NOW | |
537 | #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */ | |
538 | #endif | |
539 | ||
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 | ||
762 | MyFree(modlist[modindex]->name); | |
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 | |
799 | tmpptr = dlopen(path, RTLD_NOW | RTLD_PROFILE); | |
800 | #else | |
801 | tmpptr = dlopen(path, RTLD_NOW); | |
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); | |
811 | MyFree(mod_basename); | |
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); | |
832 | MyFree(mod_basename); | |
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); | |
849 | MyFree(mod_basename); | |
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); | |
884 | MyFree(mod_basename); | |
885 | return -1; | |
886 | } | |
887 | ||
888 | if(ver == NULL) | |
889 | ver = unknown_ver; | |
890 | ||
891 | increase_modlist(); | |
892 | ||
893 | modlist[num_mods] = MyMalloc(sizeof(struct module)); | |
894 | modlist[num_mods]->address = tmpptr; | |
895 | modlist[num_mods]->version = ver; | |
896 | modlist[num_mods]->core = core; | |
897 | DupString(modlist[num_mods]->name, mod_basename); | |
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 | } | |
911 | MyFree(mod_basename); | |
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 | ||
930 | new_modlist = (struct module **) MyMalloc(sizeof(struct module) * | |
931 | (max_mods + MODS_INCREMENT)); | |
932 | memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module) * num_mods); | |
933 | ||
934 | MyFree(modlist); | |
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 */ |