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