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