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