]> jfr.im git - irc/rqf/shadowircd.git/blame - src/modules.c
Add privilegeset_extend().
[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"
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[] = {
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 71 "m_squit",
72 NULL
73};
74
75#define MODS_INCREMENT 10
76int num_mods = 0;
77int max_mods = MODS_INCREMENT;
78
af81d5a0 79static rb_dlink_list mod_paths;
212380e3 80
81static int mo_modload(struct Client *, struct Client *, int, const char **);
82static int mo_modlist(struct Client *, struct Client *, int, const char **);
83static int mo_modreload(struct Client *, struct Client *, int, const char **);
84static int mo_modunload(struct Client *, struct Client *, int, const char **);
85static int mo_modrestart(struct Client *, struct Client *, int, const char **);
86
87struct 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
92struct 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
97struct 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
102struct 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
107struct 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
112extern struct Message error_msgtab;
113
114void
115modules_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 */
134static struct module_path *
135mod_find_path(const char *path)
136{
af81d5a0 137 rb_dlink_node *ptr;
212380e3 138 struct module_path *mpath;
139
8e69bb4e 140 RB_DLINK_FOREACH(ptr, mod_paths.head)
212380e3 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 */
157void
158mod_add_path(const char *path)
159{
160 struct module_path *pathst;
161
162 if(mod_find_path(path))
163 return;
164
8e43b0b4 165 pathst = rb_malloc(sizeof(struct module_path));
212380e3 166
167 strcpy(pathst->path, path);
af81d5a0 168 rb_dlinkAddAlloc(pathst, &mod_paths);
212380e3 169}
170
171/* mod_clear_paths()
172 *
173 * input -
174 * output -
175 * side effects - clear the lists of paths
176 */
177void
178mod_clear_paths(void)
179{
90a3c35b 180 rb_dlink_node *ptr, *next_ptr;
212380e3 181
90a3c35b 182 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
212380e3 183 {
90a3c35b 184 rb_free(ptr->data);
3c8a7c39 185 rb_free_rb_dlink_node(ptr);
212380e3 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 */
198char *
199irc_basename(const char *path)
200{
8e43b0b4 201 char *mod_basename = rb_malloc(strlen(path) + 1);
212380e3 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
220int
221findmodule_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 */
239void
240load_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
8e43b0b4 249 modlist = (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT));
212380e3 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 {
38e6acdd 266 (void) rb_snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name);
212380e3 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 */
280void
281load_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 {
38e6acdd 289 rb_snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH,
212380e3 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 */
308int
309load_one_module(const char *path, int coremodule)
310{
311 char modpath[MAXPATHLEN];
af81d5a0 312 rb_dlink_node *pathst;
212380e3 313 struct module_path *mpath;
314
315 struct stat statbuf;
316
317 if (server_state_foreground == 1)
318 inotice("loading module %s ...", path);
319
8e69bb4e 320 RB_DLINK_FOREACH(pathst, mod_paths.head)
212380e3 321 {
322 mpath = pathst->data;
323
38e6acdd 324 rb_snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path);
212380e3 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 .. */
348static int
349mo_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);
90a3c35b 365 rb_free(m_bn);
212380e3 366 return 0;
367 }
368
369 load_one_module(parv[1], 0);
370
90a3c35b 371 rb_free(m_bn);
212380e3 372
373 return 0;
374}
375
376
377/* unload a module .. */
378static int
379mo_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);
90a3c35b 396 rb_free(m_bn);
212380e3 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);
90a3c35b 403 rb_free(m_bn);
212380e3 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
90a3c35b 412 rb_free(m_bn);
212380e3 413 return 0;
414}
415
416/* unload and load in one! */
417static int
418mo_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);
90a3c35b 436 rb_free(m_bn);
212380e3 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);
90a3c35b 445 rb_free(m_bn);
212380e3 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
90a3c35b 457 rb_free(m_bn);
212380e3 458 return 0;
459}
460
461/* list modules .. */
462static int
463mo_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 */
501static int
502mo_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 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
536#ifdef CHARYBDIS_PROFILE
537# ifndef RTLD_PROFILE
538# warning libdl may not support profiling, sucks. :(
539# define RTLD_PROFILE 0
540# endif
541#endif
542
543static void increase_modlist(void);
544
545#define MODS_INCREMENT 10
546
547static char unknown_ver[] = "<unknown>";
548
549/* This file contains the core functions to use dynamic libraries.
550 * -TimeMr14C
551 */
552
553
554#ifdef HAVE_MACH_O_DYLD_H
555/*
556** jmallett's dl*(3) shims for NSModule(3) systems.
557*/
558#include <mach-o/dyld.h>
559
560#ifndef HAVE_DLOPEN
561#ifndef RTLD_LAZY
562#define RTLD_LAZY 2185 /* built-in dl*(3) don't care */
563#endif
564
565void undefinedErrorHandler(const char *);
566NSModule multipleErrorHandler(NSSymbol, NSModule, NSModule);
567void linkEditErrorHandler(NSLinkEditErrors, int, const char *, const char *);
568char *dlerror(void);
569void *dlopen(char *, int);
570int dlclose(void *);
571void *dlsym(void *, char *);
572
573static int firstLoad = TRUE;
574static int myDlError;
575static char *myErrorTable[] = { "Loading file as object failed\n",
576 "Loading file as object succeeded\n",
577 "Not a valid shared object\n",
578 "Architecture of object invalid on this architecture\n",
579 "Invalid or corrupt image\n",
580 "Could not access object\n",
581 "NSCreateObjectFileImageFromFile failed\n",
582 NULL
583};
584
585void
586undefinedErrorHandler(const char *symbolName)
587{
588 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Undefined symbol: %s", symbolName);
589 ilog(L_MAIN, "Undefined symbol: %s", symbolName);
590 return;
591}
592
593NSModule
594multipleErrorHandler(NSSymbol s, NSModule old, NSModule new)
595{
596 /* XXX
597 ** This results in substantial leaking of memory... Should free one
598 ** module, maybe?
599 */
600 sendto_realops_snomask(SNO_GENERAL, L_ALL,
601 "Symbol `%s' found in `%s' and `%s'",
602 NSNameOfSymbol(s), NSNameOfModule(old), NSNameOfModule(new));
603 ilog(L_MAIN, "Symbol `%s' found in `%s' and `%s'",
604 NSNameOfSymbol(s), NSNameOfModule(old), NSNameOfModule(new));
605 /* We return which module should be considered valid, I believe */
606 return new;
607}
608
609void
610linkEditErrorHandler(NSLinkEditErrors errorClass, int errnum,
611 const char *fileName, const char *errorString)
612{
613 sendto_realops_snomask(SNO_GENERAL, L_ALL,
614 "Link editor error: %s for %s", errorString, fileName);
615 ilog(L_MAIN, "Link editor error: %s for %s", errorString, fileName);
616 return;
617}
618
619char *
620dlerror(void)
621{
622 return myDlError == NSObjectFileImageSuccess ? NULL : myErrorTable[myDlError % 7];
623}
624
625void *
626dlopen(char *filename, int unused)
627{
628 NSObjectFileImage myImage;
629 NSModule myModule;
630
631 if(firstLoad)
632 {
633 /*
634 ** If we are loading our first symbol (huzzah!) we should go ahead
635 ** and install link editor error handling!
636 */
637 NSLinkEditErrorHandlers linkEditorErrorHandlers;
638
639 linkEditorErrorHandlers.undefined = undefinedErrorHandler;
640 linkEditorErrorHandlers.multiple = multipleErrorHandler;
641 linkEditorErrorHandlers.linkEdit = linkEditErrorHandler;
642 NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers);
643 firstLoad = FALSE;
644 }
645 myDlError = NSCreateObjectFileImageFromFile(filename, &myImage);
646 if(myDlError != NSObjectFileImageSuccess)
647 {
648 return NULL;
649 }
650 myModule = NSLinkModule(myImage, filename, NSLINKMODULE_OPTION_PRIVATE);
651 return (void *) myModule;
652}
653
654int
655dlclose(void *myModule)
656{
657 NSUnLinkModule(myModule, FALSE);
658 return 0;
659}
660
661void *
662dlsym(void *myModule, char *mySymbolName)
663{
664 NSSymbol mySymbol;
665
666 mySymbol = NSLookupSymbolInModule((NSModule) myModule, mySymbolName);
667 return NSAddressOfSymbol(mySymbol);
668}
669#endif
670#endif
671
672
673/*
674 * HPUX dl compat functions
675 */
676#if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN)
677#define RTLD_LAZY BIND_DEFERRED
678#define RTLD_GLOBAL DYNAMIC_PATH
679#define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0)
680#define dlclose(handle) shl_unload((shl_t)(handle))
681#define dlsym(handle,name) hpux_dlsym(handle,name)
682#define dlerror() strerror(errno)
683
684static void *
685hpux_dlsym(void *handle, char *name)
686{
687 void *sym_addr;
688 if(!shl_findsym((shl_t *) & handle, name, TYPE_UNDEFINED, &sym_addr))
689 return sym_addr;
690 return NULL;
691}
692
693#endif
694
695/* unload_one_module()
696 *
697 * inputs - name of module to unload
698 * - 1 to say modules unloaded, 0 to not
699 * output - 0 if successful, -1 if error
700 * side effects - module is unloaded
701 */
702int
703unload_one_module(const char *name, int warn)
704{
705 int modindex;
706
707 if((modindex = findmodule_byname(name)) == -1)
708 return -1;
709
710 /*
711 ** XXX - The type system in C does not allow direct conversion between
712 ** data and function pointers, but as it happens, most C compilers will
713 ** safely do this, however it is a theoretical overlow to cast as we
714 ** must do here. I have library functions to take care of this, but
715 ** despite being more "correct" for the C language, this is more
716 ** practical. Removing the abuse of the ability to cast ANY pointer
717 ** to and from an integer value here will break some compilers.
718 ** -jmallett
719 */
720 /* Left the comment in but the code isn't here any more -larne */
721 switch (modlist[modindex]->mapi_version)
722 {
723 case 1:
724 {
725 struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header;
726 if(mheader->mapi_command_list)
727 {
728 struct Message **m;
729 for (m = mheader->mapi_command_list; *m; ++m)
730 mod_del_cmd(*m);
731 }
732
733 /* hook events are never removed, we simply lose the
734 * ability to call them --fl
735 */
736 if(mheader->mapi_hfn_list)
737 {
738 mapi_hfn_list_av1 *m;
739 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
740 remove_hook(m->hapi_name, m->fn);
741 }
742
743 if(mheader->mapi_unregister)
744 mheader->mapi_unregister();
745 break;
746 }
747 default:
748 sendto_realops_snomask(SNO_GENERAL, L_ALL,
749 "Unknown/unsupported MAPI version %d when unloading %s!",
750 modlist[modindex]->mapi_version, modlist[modindex]->name);
751 ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
752 modlist[modindex]->mapi_version, modlist[modindex]->name);
753 break;
754 }
755
756 dlclose(modlist[modindex]->address);
757
90a3c35b 758 rb_free(modlist[modindex]->name);
212380e3 759 memcpy(&modlist[modindex], &modlist[modindex + 1],
760 sizeof(struct module) * ((num_mods - 1) - modindex));
761
762 if(num_mods != 0)
763 num_mods--;
764
765 if(warn == 1)
766 {
767 ilog(L_MAIN, "Module %s unloaded", name);
768 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name);
769 }
770
771 return 0;
772}
773
774
775/*
776 * load_a_module()
777 *
778 * inputs - path name of module, int to notice, int of core
779 * output - -1 if error 0 if success
780 * side effects - loads a module if successful
781 */
782int
783load_a_module(const char *path, int warn, int core)
784{
785 void *tmpptr = NULL;
786
787 char *mod_basename;
788 const char *ver;
789
790 int *mapi_version;
791
792 mod_basename = irc_basename(path);
793
794#ifdef CHARYBDIS_PROFILE
795 tmpptr = dlopen(path, RTLD_NOW | RTLD_PROFILE);
796#else
797 tmpptr = dlopen(path, RTLD_NOW);
798#endif
799
800 if(tmpptr == NULL)
801 {
802 const char *err = dlerror();
803
804 sendto_realops_snomask(SNO_GENERAL, L_ALL,
805 "Error loading module %s: %s", mod_basename, err);
806 ilog(L_MAIN, "Error loading module %s: %s", mod_basename, err);
90a3c35b 807 rb_free(mod_basename);
212380e3 808 return -1;
809 }
810
811
812 /*
813 * _mheader is actually a struct mapi_mheader_*, but mapi_version
814 * is always the first member of this structure, so we treate it
815 * as a single int in order to determine the API version.
816 * -larne.
817 */
818 mapi_version = (int *) (uintptr_t) dlsym(tmpptr, "_mheader");
819 if((mapi_version == NULL
820 && (mapi_version = (int *) (uintptr_t) dlsym(tmpptr, "__mheader")) == NULL)
821 || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR)
822 {
823 sendto_realops_snomask(SNO_GENERAL, L_ALL,
824 "Data format error: module %s has no MAPI header.",
825 mod_basename);
826 ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_basename);
827 (void) dlclose(tmpptr);
90a3c35b 828 rb_free(mod_basename);
212380e3 829 return -1;
830 }
831
832 switch (MAPI_VERSION(*mapi_version))
833 {
834 case 1:
835 {
836 struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *) mapi_version; /* see above */
837 if(mheader->mapi_register && (mheader->mapi_register() == -1))
838 {
839 ilog(L_MAIN, "Module %s indicated failure during load.",
840 mod_basename);
841 sendto_realops_snomask(SNO_GENERAL, L_ALL,
842 "Module %s indicated failure during load.",
843 mod_basename);
844 dlclose(tmpptr);
90a3c35b 845 rb_free(mod_basename);
212380e3 846 return -1;
847 }
848 if(mheader->mapi_command_list)
849 {
850 struct Message **m;
851 for (m = mheader->mapi_command_list; *m; ++m)
852 mod_add_cmd(*m);
853 }
854
855 if(mheader->mapi_hook_list)
856 {
857 mapi_hlist_av1 *m;
858 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
859 *m->hapi_id = register_hook(m->hapi_name);
860 }
861
862 if(mheader->mapi_hfn_list)
863 {
864 mapi_hfn_list_av1 *m;
865 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
866 add_hook(m->hapi_name, m->fn);
867 }
868
869 ver = mheader->mapi_module_version;
870 break;
871 }
872
873 default:
874 ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.",
875 mod_basename, MAPI_VERSION(*mapi_version));
876 sendto_realops_snomask(SNO_GENERAL, L_ALL,
877 "Module %s has unknown/unsupported MAPI version %d.",
878 mod_basename, *mapi_version);
879 dlclose(tmpptr);
90a3c35b 880 rb_free(mod_basename);
212380e3 881 return -1;
882 }
883
884 if(ver == NULL)
885 ver = unknown_ver;
886
887 increase_modlist();
888
8e43b0b4 889 modlist[num_mods] = rb_malloc(sizeof(struct module));
212380e3 890 modlist[num_mods]->address = tmpptr;
891 modlist[num_mods]->version = ver;
892 modlist[num_mods]->core = core;
62d28946 893 modlist[num_mods]->name = rb_strdup(mod_basename);
212380e3 894 modlist[num_mods]->mapi_header = mapi_version;
895 modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version);
896 num_mods++;
897
898 if(warn == 1)
899 {
900 sendto_realops_snomask(SNO_GENERAL, L_ALL,
901 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
902 mod_basename, ver, MAPI_VERSION(*mapi_version),
903 (unsigned long) tmpptr);
904 ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
905 mod_basename, ver, MAPI_VERSION(*mapi_version), (unsigned long) tmpptr);
906 }
90a3c35b 907 rb_free(mod_basename);
212380e3 908 return 0;
909}
910
911/*
912 * increase_modlist
913 *
914 * inputs - NONE
915 * output - NONE
916 * side effects - expand the size of modlist if necessary
917 */
918static void
919increase_modlist(void)
920{
921 struct module **new_modlist = NULL;
922
923 if((num_mods + 1) < max_mods)
924 return;
925
8e43b0b4 926 new_modlist = (struct module **) rb_malloc(sizeof(struct module) *
212380e3 927 (max_mods + MODS_INCREMENT));
928 memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module) * num_mods);
929
90a3c35b 930 rb_free(modlist);
212380e3 931 modlist = new_modlist;
932 max_mods += MODS_INCREMENT;
933}
934
935#else /* STATIC_MODULES */
936
937/* load_all_modules()
938 *
939 * input -
940 * output -
941 * side effects - all the msgtabs are added for static modules
942 */
943void
944load_all_modules(int warn)
945{
946 load_static_modules();
947}
948
949#endif /* STATIC_MODULES */