]> jfr.im git - solanum.git/blame - ircd/modules.c
Change all leftover libratbox stuff to librb.
[solanum.git] / ircd / 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
212380e3
AC
23 */
24
25#include "stdinc.h"
212380e3 26#include "modules.h"
4016731b 27#include "logger.h"
212380e3
AC
28#include "ircd.h"
29#include "client.h"
30#include "send.h"
31#include "s_conf.h"
32#include "s_newconf.h"
33#include "numeric.h"
34#include "parse.h"
35#include "ircd_defs.h"
4562c604 36#include "match.h"
15feac53 37#include "s_serv.h"
212380e3 38
f272e7ab
AC
39#include <ltdl.h>
40
212380e3
AC
41struct module **modlist = NULL;
42
43static const char *core_module_table[] = {
431a1a27 44 "m_ban",
212380e3
AC
45 "m_die",
46 "m_error",
47 "m_join",
48 "m_kick",
49 "m_kill",
50 "m_message",
51 "m_mode",
52 "m_nick",
53 "m_part",
54 "m_quit",
55 "m_server",
212380e3
AC
56 "m_squit",
57 NULL
58};
59
60#define MODS_INCREMENT 10
61int num_mods = 0;
62int max_mods = MODS_INCREMENT;
63
330fc5c1 64static rb_dlink_list mod_paths;
212380e3 65
4a84a763
AC
66static int mo_modload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
67static int mo_modlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
68static int mo_modreload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
69static int mo_modunload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
70static int mo_modrestart(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
71
72static int me_modload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
73static int me_modlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
74static int me_modreload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
75static int me_modunload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
76static int me_modrestart(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
15feac53
AC
77
78static int do_modload(struct Client *, const char *);
79static int do_modunload(struct Client *, const char *);
80static int do_modreload(struct Client *, const char *);
81static int do_modlist(struct Client *, const char *);
82static int do_modrestart(struct Client *);
83
212380e3 84struct Message modload_msgtab = {
7baa37a9 85 "MODLOAD", 0, 0, 0, 0,
15feac53 86 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modload, 2}, {mo_modload, 2}}
212380e3
AC
87};
88
89struct Message modunload_msgtab = {
7baa37a9 90 "MODUNLOAD", 0, 0, 0, 0,
15feac53 91 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modunload, 2}, {mo_modunload, 2}}
212380e3
AC
92};
93
94struct Message modreload_msgtab = {
7baa37a9 95 "MODRELOAD", 0, 0, 0, 0,
15feac53 96 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modreload, 2}, {mo_modreload, 2}}
212380e3
AC
97};
98
99struct Message modlist_msgtab = {
7baa37a9 100 "MODLIST", 0, 0, 0, 0,
15feac53 101 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modlist, 0}, {mo_modlist, 0}}
212380e3
AC
102};
103
104struct Message modrestart_msgtab = {
7baa37a9 105 "MODRESTART", 0, 0, 0, 0,
15feac53 106 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modrestart, 0}, {mo_modrestart, 0}}
212380e3
AC
107};
108
212380e3
AC
109void
110modules_init(void)
111{
f272e7ab
AC
112 if(lt_dlinit())
113 {
114 ilog(L_MAIN, "lt_dlinit failed");
115 exit(0);
116 }
117
212380e3
AC
118 mod_add_cmd(&modload_msgtab);
119 mod_add_cmd(&modunload_msgtab);
120 mod_add_cmd(&modreload_msgtab);
121 mod_add_cmd(&modlist_msgtab);
122 mod_add_cmd(&modrestart_msgtab);
123
124 /* Add the default paths we look in to the module system --nenolod */
125 mod_add_path(MODPATH);
126 mod_add_path(AUTOMODPATH);
127}
128
129/* mod_find_path()
130 *
131 * input - path
132 * output - none
133 * side effects - returns a module path from path
134 */
26c6ac3d 135static char *
212380e3
AC
136mod_find_path(const char *path)
137{
330fc5c1 138 rb_dlink_node *ptr;
26c6ac3d 139 char *mpath;
212380e3 140
5cefa1d6 141 RB_DLINK_FOREACH(ptr, mod_paths.head)
212380e3
AC
142 {
143 mpath = ptr->data;
144
26c6ac3d 145 if(!strcmp(path, mpath))
212380e3
AC
146 return mpath;
147 }
148
149 return NULL;
150}
151
152/* mod_add_path
153 *
154 * input - path
55abcbb2 155 * ouput -
212380e3
AC
156 * side effects - adds path to list
157 */
158void
159mod_add_path(const char *path)
160{
26c6ac3d 161 char *pathst;
212380e3
AC
162
163 if(mod_find_path(path))
164 return;
165
26c6ac3d 166 pathst = rb_strdup(path);
330fc5c1 167 rb_dlinkAddAlloc(pathst, &mod_paths);
212380e3
AC
168}
169
170/* mod_clear_paths()
171 *
172 * input -
173 * output -
174 * side effects - clear the lists of paths
175 */
176void
177mod_clear_paths(void)
178{
637c4932 179 rb_dlink_node *ptr, *next_ptr;
212380e3 180
637c4932 181 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
212380e3 182 {
637c4932 183 rb_free(ptr->data);
30106156 184 rb_free_rb_dlink_node(ptr);
212380e3
AC
185 }
186
187 mod_paths.head = mod_paths.tail = NULL;
188 mod_paths.length = 0;
189}
190
212380e3
AC
191/* findmodule_byname
192 *
193 * input -
194 * output -
195 * side effects -
196 */
197
198int
199findmodule_byname(const char *name)
200{
201 int i;
202
203 for (i = 0; i < num_mods; i++)
204 {
205 if(!irccmp(modlist[i]->name, name))
206 return i;
207 }
208 return -1;
209}
210
211/* load_all_modules()
212 *
213 * input -
214 * output -
215 * side effects -
216 */
217void
218load_all_modules(int warn)
219{
220 DIR *system_module_dir = NULL;
221 struct dirent *ldirent = NULL;
222 char module_fq_name[PATH_MAX + 1];
223 int len;
224
225 modules_init();
226
1e170010 227 modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT));
212380e3
AC
228
229 max_mods = MODS_INCREMENT;
230
231 system_module_dir = opendir(AUTOMODPATH);
232
233 if(system_module_dir == NULL)
234 {
235 ilog(L_MAIN, "Could not load modules from %s: %s", AUTOMODPATH, strerror(errno));
236 return;
237 }
238
239 while ((ldirent = readdir(system_module_dir)) != NULL)
240 {
5203cba5 241 len = strlen(ldirent->d_name);
2a19fc3f 242 if((len > 3) && !strcmp(ldirent->d_name+len-3, ".la"))
5203cba5
VI
243 {
244 (void) snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name);
245 (void) load_a_module(module_fq_name, warn, 0);
246 }
212380e3
AC
247
248 }
249 (void) closedir(system_module_dir);
250}
251
252/* load_core_modules()
253 *
254 * input -
255 * output -
256 * side effects - core modules are loaded, if any fail, kill ircd
257 */
258void
259load_core_modules(int warn)
260{
d74fa5b5 261 char module_name[PATH_MAX];
212380e3
AC
262 int i;
263
264
265 for (i = 0; core_module_table[i]; i++)
266 {
5203cba5 267 snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH,
2a19fc3f 268 core_module_table[i], ".la");
212380e3
AC
269
270 if(load_a_module(module_name, warn, 1) == -1)
271 {
272 ilog(L_MAIN,
273 "Error loading core module %s%s: terminating ircd",
2a19fc3f 274 core_module_table[i], ".la");
212380e3
AC
275 exit(0);
276 }
277 }
278}
279
280/* load_one_module()
281 *
282 * input -
283 * output -
284 * side effects -
285 */
286int
287load_one_module(const char *path, int coremodule)
288{
d74fa5b5 289 char modpath[PATH_MAX];
330fc5c1 290 rb_dlink_node *pathst;
26c6ac3d 291 const char *mpath;
212380e3
AC
292
293 struct stat statbuf;
294
295 if (server_state_foreground == 1)
55abcbb2 296 inotice("loading module %s ...", path);
212380e3 297
5cefa1d6 298 RB_DLINK_FOREACH(pathst, mod_paths.head)
212380e3
AC
299 {
300 mpath = pathst->data;
301
5203cba5 302 snprintf(modpath, sizeof(modpath), "%s/%s", mpath, path);
212380e3
AC
303 if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL))
304 {
305 if(stat(modpath, &statbuf) == 0)
306 {
307 if(S_ISREG(statbuf.st_mode))
308 {
309 /* Regular files only please */
310 if(coremodule)
311 return load_a_module(modpath, 1, 1);
312 else
313 return load_a_module(modpath, 1, 0);
314 }
315 }
316
317 }
318 }
319
320 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path);
321 return -1;
322}
323
324
325/* load a module .. */
326static int
4a84a763 327mo_modload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 328{
212380e3
AC
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
15feac53
AC
336 if(parc > 2)
337 {
338 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
339 "ENCAP %s MODLOAD %s", parv[2], parv[1]);
340 if (match(parv[2], me.name) == 0)
341 return 0;
342 }
343
344 return do_modload(source_p, parv[1]);
345}
346
347static int
4a84a763 348me_modload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
15feac53
AC
349{
350 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
351 {
352 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
353 "to load modules on this server.");
354 return 0;
355 }
356
357 return do_modload(source_p, parv[1]);
358}
359
360static int
361do_modload(struct Client *source_p, const char *module)
362{
363 char *m_bn = rb_basename(module);
212380e3
AC
364
365 if(findmodule_byname(m_bn) != -1)
366 {
5366977b 367 sendto_one_notice(source_p, ":Module %s is already loaded", m_bn);
637c4932 368 rb_free(m_bn);
212380e3
AC
369 return 0;
370 }
371
15feac53 372 load_one_module(module, 0);
212380e3 373
637c4932 374 rb_free(m_bn);
212380e3
AC
375
376 return 0;
377}
378
379
380/* unload a module .. */
381static int
4a84a763 382mo_modunload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 383{
212380e3
AC
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
15feac53
AC
391 if(parc > 2)
392 {
393 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
394 "ENCAP %s MODUNLOAD %s", parv[2], parv[1]);
395 if (match(parv[2], me.name) == 0)
396 return 0;
397 }
398
399 return do_modunload(source_p, parv[1]);
400}
401
402static int
4a84a763 403me_modunload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
15feac53
AC
404{
405 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
406 {
407 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
408 "to load modules on this server.");
409 return 0;
410 }
411
412 return do_modunload(source_p, parv[1]);
413}
414
415static int
416do_modunload(struct Client *source_p, const char *module)
417{
418 int modindex;
419 char *m_bn = rb_basename(module);
212380e3
AC
420
421 if((modindex = findmodule_byname(m_bn)) == -1)
422 {
5366977b 423 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
637c4932 424 rb_free(m_bn);
212380e3
AC
425 return 0;
426 }
427
428 if(modlist[modindex]->core == 1)
429 {
5366977b 430 sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn);
637c4932 431 rb_free(m_bn);
212380e3
AC
432 return 0;
433 }
434
435 if(unload_one_module(m_bn, 1) == -1)
436 {
5366977b 437 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
212380e3 438 }
5366977b 439
637c4932 440 rb_free(m_bn);
212380e3
AC
441 return 0;
442}
443
444/* unload and load in one! */
445static int
4a84a763 446mo_modreload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 447{
212380e3
AC
448 if(!IsOperAdmin(source_p))
449 {
450 sendto_one(source_p, form_str(ERR_NOPRIVS),
451 me.name, source_p->name, "admin");
452 return 0;
453 }
454
15feac53
AC
455 if(parc > 2)
456 {
457 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
458 "ENCAP %s MODRELOAD %s", parv[2], parv[1]);
459 if (match(parv[2], me.name) == 0)
460 return 0;
461 }
462
463 return do_modreload(source_p, parv[1]);
464}
465
466static int
4a84a763 467me_modreload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
15feac53
AC
468{
469 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
470 {
471 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
472 "to load modules on this server.");
473 return 0;
474 }
475
476 return do_modreload(source_p, parv[1]);
477}
478
479static int
480do_modreload(struct Client *source_p, const char *module)
481{
482 int modindex;
483 int check_core;
484 char *m_bn = rb_basename(module);
212380e3
AC
485
486 if((modindex = findmodule_byname(m_bn)) == -1)
487 {
5366977b 488 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
637c4932 489 rb_free(m_bn);
212380e3
AC
490 return 0;
491 }
492
493 check_core = modlist[modindex]->core;
494
495 if(unload_one_module(m_bn, 1) == -1)
496 {
5366977b 497 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
637c4932 498 rb_free(m_bn);
212380e3
AC
499 return 0;
500 }
501
7d778d51 502 if((load_one_module(m_bn, check_core) == -1) && check_core)
212380e3 503 {
15feac53 504 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
7d778d51
AC
505 "Error reloading core module: %s: terminating ircd", m_bn);
506 ilog(L_MAIN, "Error loading core module %s: terminating ircd", m_bn);
212380e3
AC
507 exit(0);
508 }
509
637c4932 510 rb_free(m_bn);
212380e3
AC
511 return 0;
512}
513
514/* list modules .. */
515static int
4a84a763 516mo_modlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 517{
212380e3
AC
518 if(!IsOperAdmin(source_p))
519 {
520 sendto_one(source_p, form_str(ERR_NOPRIVS),
521 me.name, source_p->name, "admin");
522 return 0;
523 }
524
15feac53
AC
525 if(parc > 2)
526 {
527 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
528 "ENCAP %s MODLIST %s", parv[2], parv[1]);
529 if (match(parv[2], me.name) == 0)
530 return 0;
531 }
532
533 return do_modlist(source_p, parc > 1 ? parv[1] : 0);
534}
535
536static int
4a84a763 537me_modlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
15feac53
AC
538{
539 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
540 {
541 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
542 "to load modules on this server.");
543 return 0;
544 }
545
546 return do_modlist(source_p, parv[1]);
547}
548
549static int
550do_modlist(struct Client *source_p, const char *pattern)
551{
552 int i;
553
212380e3
AC
554 for (i = 0; i < num_mods; i++)
555 {
15feac53 556 if(pattern)
212380e3 557 {
15feac53 558 if(match(pattern, modlist[i]->name))
212380e3
AC
559 {
560 sendto_one(source_p, form_str(RPL_MODLIST),
561 me.name, source_p->name,
562 modlist[i]->name,
a9f12814 563 (unsigned long)(uintptr_t)modlist[i]->address,
212380e3
AC
564 modlist[i]->version, modlist[i]->core ? "(core)" : "");
565 }
566 }
567 else
568 {
569 sendto_one(source_p, form_str(RPL_MODLIST),
570 me.name, source_p->name, modlist[i]->name,
15feac53 571 (unsigned long)(uintptr_t)modlist[i]->address, modlist[i]->version,
212380e3
AC
572 modlist[i]->core ? "(core)" : "");
573 }
574 }
575
576 sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, source_p->name);
577 return 0;
578}
579
580/* unload and reload all modules */
581static int
4a84a763 582mo_modrestart(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 583{
212380e3
AC
584 if(!IsOperAdmin(source_p))
585 {
586 sendto_one(source_p, form_str(ERR_NOPRIVS),
587 me.name, source_p->name, "admin");
588 return 0;
589 }
590
15feac53
AC
591 if(parc > 1)
592 {
593 sendto_match_servs(source_p, parv[1], CAP_ENCAP, NOCAPS,
594 "ENCAP %s MODRESTART", parv[1]);
595 if (match(parv[1], me.name) == 0)
596 return 0;
597 }
598
599 return do_modrestart(source_p);
600}
601
602static int
4a84a763 603me_modrestart(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
15feac53
AC
604{
605 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
606 {
607 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
608 "to load modules on this server.");
609 return 0;
610 }
611
612 return do_modrestart(source_p);
613}
614
615static int
616do_modrestart(struct Client *source_p)
617{
618 int modnum;
619
5366977b 620 sendto_one_notice(source_p, ":Reloading all modules");
212380e3
AC
621
622 modnum = num_mods;
623 while (num_mods)
624 unload_one_module(modlist[0]->name, 0);
625
626 load_all_modules(0);
627 load_core_modules(0);
628 rehash(0);
629
15feac53 630 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
212380e3
AC
631 "Module Restart: %d modules unloaded, %d modules loaded",
632 modnum, num_mods);
633 ilog(L_MAIN, "Module Restart: %d modules unloaded, %d modules loaded", modnum, num_mods);
634 return 0;
635}
636
637
212380e3
AC
638static void increase_modlist(void);
639
640#define MODS_INCREMENT 10
641
642static char unknown_ver[] = "<unknown>";
643
212380e3
AC
644/* unload_one_module()
645 *
646 * inputs - name of module to unload
647 * - 1 to say modules unloaded, 0 to not
648 * output - 0 if successful, -1 if error
649 * side effects - module is unloaded
650 */
651int
652unload_one_module(const char *name, int warn)
653{
654 int modindex;
655
656 if((modindex = findmodule_byname(name)) == -1)
657 return -1;
658
659 /*
660 ** XXX - The type system in C does not allow direct conversion between
661 ** data and function pointers, but as it happens, most C compilers will
55abcbb2
KB
662 ** safely do this, however it is a theoretical overlow to cast as we
663 ** must do here. I have library functions to take care of this, but
664 ** despite being more "correct" for the C language, this is more
212380e3
AC
665 ** practical. Removing the abuse of the ability to cast ANY pointer
666 ** to and from an integer value here will break some compilers.
667 ** -jmallett
668 */
669 /* Left the comment in but the code isn't here any more -larne */
670 switch (modlist[modindex]->mapi_version)
671 {
672 case 1:
673 {
674 struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header;
675 if(mheader->mapi_command_list)
676 {
677 struct Message **m;
678 for (m = mheader->mapi_command_list; *m; ++m)
679 mod_del_cmd(*m);
680 }
681
682 /* hook events are never removed, we simply lose the
683 * ability to call them --fl
684 */
685 if(mheader->mapi_hfn_list)
686 {
687 mapi_hfn_list_av1 *m;
688 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
689 remove_hook(m->hapi_name, m->fn);
690 }
691
692 if(mheader->mapi_unregister)
693 mheader->mapi_unregister();
694 break;
695 }
696 default:
697 sendto_realops_snomask(SNO_GENERAL, L_ALL,
698 "Unknown/unsupported MAPI version %d when unloading %s!",
699 modlist[modindex]->mapi_version, modlist[modindex]->name);
700 ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
701 modlist[modindex]->mapi_version, modlist[modindex]->name);
702 break;
703 }
704
f272e7ab 705 lt_dlclose(modlist[modindex]->address);
212380e3 706
637c4932 707 rb_free(modlist[modindex]->name);
55d5f797 708 rb_free(modlist[modindex]);
f54e1a8f 709 memmove(&modlist[modindex], &modlist[modindex + 1],
1e170010 710 sizeof(struct module *) * ((num_mods - 1) - modindex));
212380e3
AC
711
712 if(num_mods != 0)
713 num_mods--;
714
715 if(warn == 1)
716 {
717 ilog(L_MAIN, "Module %s unloaded", name);
718 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name);
719 }
720
721 return 0;
722}
723
724
725/*
726 * load_a_module()
727 *
728 * inputs - path name of module, int to notice, int of core
729 * output - -1 if error 0 if success
730 * side effects - loads a module if successful
731 */
732int
733load_a_module(const char *path, int warn, int core)
734{
f272e7ab 735 lt_dlhandle tmpptr;
212380e3
AC
736 char *mod_basename;
737 const char *ver;
738
739 int *mapi_version;
740
b7a689d1 741 mod_basename = rb_basename(path);
212380e3 742
d76258f5 743 tmpptr = lt_dlopen(path);
212380e3
AC
744
745 if(tmpptr == NULL)
746 {
f272e7ab 747 const char *err = lt_dlerror();
212380e3
AC
748
749 sendto_realops_snomask(SNO_GENERAL, L_ALL,
750 "Error loading module %s: %s", mod_basename, err);
751 ilog(L_MAIN, "Error loading module %s: %s", mod_basename, err);
637c4932 752 rb_free(mod_basename);
212380e3
AC
753 return -1;
754 }
755
756
757 /*
758 * _mheader is actually a struct mapi_mheader_*, but mapi_version
759 * is always the first member of this structure, so we treate it
760 * as a single int in order to determine the API version.
761 * -larne.
762 */
f272e7ab 763 mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader");
212380e3 764 if((mapi_version == NULL
f272e7ab 765 && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL)
212380e3
AC
766 || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR)
767 {
768 sendto_realops_snomask(SNO_GENERAL, L_ALL,
769 "Data format error: module %s has no MAPI header.",
770 mod_basename);
771 ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_basename);
f272e7ab 772 (void) lt_dlclose(tmpptr);
637c4932 773 rb_free(mod_basename);
212380e3
AC
774 return -1;
775 }
776
777 switch (MAPI_VERSION(*mapi_version))
778 {
779 case 1:
780 {
29c92cf9 781 struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */
212380e3
AC
782 if(mheader->mapi_register && (mheader->mapi_register() == -1))
783 {
784 ilog(L_MAIN, "Module %s indicated failure during load.",
785 mod_basename);
786 sendto_realops_snomask(SNO_GENERAL, L_ALL,
787 "Module %s indicated failure during load.",
788 mod_basename);
f272e7ab 789 lt_dlclose(tmpptr);
637c4932 790 rb_free(mod_basename);
212380e3
AC
791 return -1;
792 }
793 if(mheader->mapi_command_list)
794 {
795 struct Message **m;
796 for (m = mheader->mapi_command_list; *m; ++m)
797 mod_add_cmd(*m);
798 }
799
800 if(mheader->mapi_hook_list)
801 {
802 mapi_hlist_av1 *m;
803 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
804 *m->hapi_id = register_hook(m->hapi_name);
805 }
806
807 if(mheader->mapi_hfn_list)
808 {
809 mapi_hfn_list_av1 *m;
810 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
811 add_hook(m->hapi_name, m->fn);
812 }
813
814 ver = mheader->mapi_module_version;
815 break;
816 }
817
818 default:
819 ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.",
820 mod_basename, MAPI_VERSION(*mapi_version));
821 sendto_realops_snomask(SNO_GENERAL, L_ALL,
822 "Module %s has unknown/unsupported MAPI version %d.",
823 mod_basename, *mapi_version);
f272e7ab 824 lt_dlclose(tmpptr);
637c4932 825 rb_free(mod_basename);
212380e3
AC
826 return -1;
827 }
828
829 if(ver == NULL)
830 ver = unknown_ver;
831
832 increase_modlist();
833
eddc2ab6 834 modlist[num_mods] = rb_malloc(sizeof(struct module));
212380e3
AC
835 modlist[num_mods]->address = tmpptr;
836 modlist[num_mods]->version = ver;
837 modlist[num_mods]->core = core;
47a03750 838 modlist[num_mods]->name = rb_strdup(mod_basename);
212380e3
AC
839 modlist[num_mods]->mapi_header = mapi_version;
840 modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version);
841 num_mods++;
842
843 if(warn == 1)
844 {
845 sendto_realops_snomask(SNO_GENERAL, L_ALL,
846 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
847 mod_basename, ver, MAPI_VERSION(*mapi_version),
848 (unsigned long) tmpptr);
849 ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
850 mod_basename, ver, MAPI_VERSION(*mapi_version), (unsigned long) tmpptr);
851 }
637c4932 852 rb_free(mod_basename);
212380e3
AC
853 return 0;
854}
855
856/*
857 * increase_modlist
858 *
859 * inputs - NONE
860 * output - NONE
861 * side effects - expand the size of modlist if necessary
862 */
863static void
864increase_modlist(void)
865{
866 struct module **new_modlist = NULL;
867
868 if((num_mods + 1) < max_mods)
869 return;
870
1e170010 871 new_modlist = (struct module **) rb_malloc(sizeof(struct module *) *
212380e3 872 (max_mods + MODS_INCREMENT));
1e170010 873 memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods);
212380e3 874
637c4932 875 rb_free(modlist);
212380e3
AC
876 modlist = new_modlist;
877 max_mods += MODS_INCREMENT;
878}