]> jfr.im git - irc/rqf/shadowircd.git/blob - src/modules.c
Remove check that cannot happen due to minparc in Message struct.
[irc/rqf/shadowircd.git] / src / modules.c
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 *
24 * $Id: modules.c 3161 2007-01-25 07:23:01Z nenolod $
25 */
26
27 #include "stdinc.h"
28
29
30 #include "modules.h"
31 #include "logger.h"
32 #include "ircd.h"
33 #include "client.h"
34 #include "send.h"
35 #include "s_conf.h"
36 #include "s_newconf.h"
37 #include "numeric.h"
38 #include "parse.h"
39 #include "ircd_defs.h"
40 #include "match.h"
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
57 struct module **modlist = NULL;
58
59 static 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",
71 "m_squit",
72 NULL
73 };
74
75 #define MODS_INCREMENT 10
76 int num_mods = 0;
77 int max_mods = MODS_INCREMENT;
78
79 static rb_dlink_list mod_paths;
80
81 static int mo_modload(struct Client *, struct Client *, int, const char **);
82 static int mo_modlist(struct Client *, struct Client *, int, const char **);
83 static int mo_modreload(struct Client *, struct Client *, int, const char **);
84 static int mo_modunload(struct Client *, struct Client *, int, const char **);
85 static int mo_modrestart(struct Client *, struct Client *, int, const char **);
86
87 struct 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
92 struct 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
97 struct 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
102 struct 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
107 struct 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
112 extern struct Message error_msgtab;
113
114 void
115 modules_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 */
134 static struct module_path *
135 mod_find_path(const char *path)
136 {
137 rb_dlink_node *ptr;
138 struct module_path *mpath;
139
140 RB_DLINK_FOREACH(ptr, mod_paths.head)
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 */
157 void
158 mod_add_path(const char *path)
159 {
160 struct module_path *pathst;
161
162 if(mod_find_path(path))
163 return;
164
165 pathst = rb_malloc(sizeof(struct module_path));
166
167 strcpy(pathst->path, path);
168 rb_dlinkAddAlloc(pathst, &mod_paths);
169 }
170
171 /* mod_clear_paths()
172 *
173 * input -
174 * output -
175 * side effects - clear the lists of paths
176 */
177 void
178 mod_clear_paths(void)
179 {
180 rb_dlink_node *ptr, *next_ptr;
181
182 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
183 {
184 rb_free(ptr->data);
185 rb_free_rb_dlink_node(ptr);
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 */
198 char *
199 irc_basename(const char *path)
200 {
201 char *mod_basename = rb_malloc(strlen(path) + 1);
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
220 int
221 findmodule_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 */
239 void
240 load_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
249 modlist = (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT));
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 {
266 (void) rb_snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name);
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 */
280 void
281 load_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 {
289 rb_snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH,
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 */
308 int
309 load_one_module(const char *path, int coremodule)
310 {
311 char modpath[MAXPATHLEN];
312 rb_dlink_node *pathst;
313 struct module_path *mpath;
314
315 struct stat statbuf;
316
317 if (server_state_foreground == 1)
318 inotice("loading module %s ...", path);
319
320 RB_DLINK_FOREACH(pathst, mod_paths.head)
321 {
322 mpath = pathst->data;
323
324 rb_snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path);
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 .. */
348 static int
349 mo_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 {
364 sendto_one_notice(source_p, ":Module %s is already loaded", m_bn);
365 rb_free(m_bn);
366 return 0;
367 }
368
369 load_one_module(parv[1], 0);
370
371 rb_free(m_bn);
372
373 return 0;
374 }
375
376
377 /* unload a module .. */
378 static int
379 mo_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 {
395 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
396 rb_free(m_bn);
397 return 0;
398 }
399
400 if(modlist[modindex]->core == 1)
401 {
402 sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn);
403 rb_free(m_bn);
404 return 0;
405 }
406
407 if(unload_one_module(m_bn, 1) == -1)
408 {
409 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
410 }
411
412 rb_free(m_bn);
413 return 0;
414 }
415
416 /* unload and load in one! */
417 static int
418 mo_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 {
435 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
436 rb_free(m_bn);
437 return 0;
438 }
439
440 check_core = modlist[modindex]->core;
441
442 if(unload_one_module(m_bn, 1) == -1)
443 {
444 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
445 rb_free(m_bn);
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
457 rb_free(m_bn);
458 return 0;
459 }
460
461 /* list modules .. */
462 static int
463 mo_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 */
501 static int
502 mo_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
513 sendto_one_notice(source_p, ":Reloading all modules");
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
543 static void increase_modlist(void);
544
545 #define MODS_INCREMENT 10
546
547 static 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
565 void undefinedErrorHandler(const char *);
566 NSModule multipleErrorHandler(NSSymbol, NSModule, NSModule);
567 void linkEditErrorHandler(NSLinkEditErrors, int, const char *, const char *);
568 char *dlerror(void);
569 void *dlopen(char *, int);
570 int dlclose(void *);
571 void *dlsym(void *, char *);
572
573 static int firstLoad = TRUE;
574 static int myDlError;
575 static 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
585 void
586 undefinedErrorHandler(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
593 NSModule
594 multipleErrorHandler(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
609 void
610 linkEditErrorHandler(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
619 char *
620 dlerror(void)
621 {
622 return myDlError == NSObjectFileImageSuccess ? NULL : myErrorTable[myDlError % 7];
623 }
624
625 void *
626 dlopen(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
654 int
655 dlclose(void *myModule)
656 {
657 NSUnLinkModule(myModule, FALSE);
658 return 0;
659 }
660
661 void *
662 dlsym(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
684 static void *
685 hpux_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 */
702 int
703 unload_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
758 rb_free(modlist[modindex]->name);
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 */
782 int
783 load_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);
807 rb_free(mod_basename);
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);
828 rb_free(mod_basename);
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);
845 rb_free(mod_basename);
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);
880 rb_free(mod_basename);
881 return -1;
882 }
883
884 if(ver == NULL)
885 ver = unknown_ver;
886
887 increase_modlist();
888
889 modlist[num_mods] = rb_malloc(sizeof(struct module));
890 modlist[num_mods]->address = tmpptr;
891 modlist[num_mods]->version = ver;
892 modlist[num_mods]->core = core;
893 modlist[num_mods]->name = rb_strdup(mod_basename);
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 }
907 rb_free(mod_basename);
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 */
918 static void
919 increase_modlist(void)
920 {
921 struct module **new_modlist = NULL;
922
923 if((num_mods + 1) < max_mods)
924 return;
925
926 new_modlist = (struct module **) rb_malloc(sizeof(struct module) *
927 (max_mods + MODS_INCREMENT));
928 memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module) * num_mods);
929
930 rb_free(modlist);
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 */
943 void
944 load_all_modules(int warn)
945 {
946 load_static_modules();
947 }
948
949 #endif /* STATIC_MODULES */