2 * ircd-ratbox: A slightly useful ircd.
3 * modules.c: A module loader.
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
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.
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.
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
32 #include "s_newconf.h"
35 #include "ircd_defs.h"
38 #include "capability.h"
43 # error "Charybdis requires loadable module support."
46 rb_dlink_list module_list
;
47 rb_dlink_list mod_paths
;
49 static const char *core_module_table
[] = {
67 #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */
74 ilog(L_MAIN
, "lt_dlinit failed");
78 /* Add the default paths we look in to the module system --nenolod */
79 mod_add_path(ircd_paths
[IRCD_PATH_MODULES
]);
80 mod_add_path(ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
]);
83 static unsigned int prev_caps
;
86 mod_remember_clicaps(void)
88 prev_caps
= capability_index_mask(cli_capindex
);
92 mod_notify_clicaps(void)
94 unsigned int cur_caps
= capability_index_mask(cli_capindex
);
95 unsigned int del
= prev_caps
& ~cur_caps
;
96 unsigned int new = cur_caps
& ~prev_caps
;
99 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * DEL :%s",
100 me
.name
, capability_index_list(cli_capindex
, del
));
102 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * NEW :%s",
103 me
.name
, capability_index_list(cli_capindex
, new));
110 * side effects - returns a module path from path
113 mod_find_path(const char *path
)
118 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
122 if(!strcmp(path
, mpath
))
133 * side effects - adds path to list
136 mod_add_path(const char *path
)
140 if(mod_find_path(path
))
143 pathst
= rb_strdup(path
);
144 rb_dlinkAddAlloc(pathst
, &mod_paths
);
151 * side effects - clear the lists of paths
154 mod_clear_paths(void)
156 rb_dlink_node
*ptr
, *next_ptr
;
158 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
161 rb_free_rb_dlink_node(ptr
);
164 mod_paths
.head
= mod_paths
.tail
= NULL
;
165 mod_paths
.length
= 0;
170 * input - module to load
171 * output - index of module on success, -1 on failure
172 * side effects - none
175 findmodule_byname(const char *name
)
178 char name_ext
[PATH_MAX
+ 1];
180 rb_strlcpy(name_ext
, name
, sizeof name_ext
);
181 rb_strlcat(name_ext
, LT_MODULE_EXT
, sizeof name_ext
);
183 RB_DLINK_FOREACH(ptr
, module_list
.head
)
185 struct module *mod
= ptr
->data
;
187 if(!irccmp(mod
->name
, name
))
190 if(!irccmp(mod
->name
, name_ext
))
197 /* load_all_modules()
204 load_all_modules(bool warn
)
206 DIR *system_module_dir
= NULL
;
207 struct dirent
*ldirent
= NULL
;
208 char module_fq_name
[PATH_MAX
+ 1];
209 size_t module_ext_len
= strlen(LT_MODULE_EXT
);
211 system_module_dir
= opendir(ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
]);
213 if(system_module_dir
== NULL
)
215 ilog(L_MAIN
, "Could not load modules from %s: %s", ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], strerror(errno
));
219 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
221 size_t len
= strlen(ldirent
->d_name
);
223 if(len
> module_ext_len
&&
224 rb_strncasecmp(ldirent
->d_name
+ (len
- module_ext_len
), LT_MODULE_EXT
, module_ext_len
) == 0)
226 (void) snprintf(module_fq_name
, sizeof(module_fq_name
), "%s%c%s",
227 ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], RB_PATH_SEPARATOR
, ldirent
->d_name
);
228 (void) load_a_module(module_fq_name
, warn
, MAPI_ORIGIN_CORE
, false);
232 (void) closedir(system_module_dir
);
235 /* load_core_modules()
239 * side effects - core modules are loaded, if any fail, kill ircd
242 load_core_modules(bool warn
)
244 char module_name
[PATH_MAX
];
248 for (i
= 0; core_module_table
[i
]; i
++)
250 snprintf(module_name
, sizeof(module_name
), "%s%c%s", ircd_paths
[IRCD_PATH_MODULES
], RB_PATH_SEPARATOR
,
251 core_module_table
[i
]);
253 if(load_a_module(module_name
, warn
, MAPI_ORIGIN_CORE
, true) == false)
256 "Error loading core module %s: terminating ircd",
257 core_module_table
[i
]);
270 load_one_module(const char *path
, int origin
, bool coremodule
)
272 char modpath
[PATH_MAX
];
273 rb_dlink_node
*pathst
;
275 if (server_state_foreground
)
276 inotice("loading module %s ...", path
);
279 origin
= MAPI_ORIGIN_CORE
;
281 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
284 const char *mpath
= pathst
->data
;
286 snprintf(modpath
, sizeof(modpath
), "%s%c%s%s", mpath
, RB_PATH_SEPARATOR
, path
, LT_MODULE_EXT
);
287 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
289 if(stat(modpath
, &statbuf
) == 0 && S_ISREG(statbuf
.st_mode
))
291 /* Regular files only please */
292 return load_a_module(modpath
, true, origin
, coremodule
);
298 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
302 static char unknown_ver
[] = "<unknown>";
303 static char unknown_description
[] = "<none>";
305 /* unload_one_module()
307 * inputs - name of module to unload
308 * - true to say modules unloaded, false to not
309 * output - true if successful, false if error
310 * side effects - module is unloaded
313 unload_one_module(const char *name
, bool warn
)
317 if((mod
= findmodule_byname(name
)) == NULL
)
321 ** XXX - The type system in C does not allow direct conversion between
322 ** data and function pointers, but as it happens, most C compilers will
323 ** safely do this, however it is a theoretical overlow to cast as we
324 ** must do here. I have library functions to take care of this, but
325 ** despite being more "correct" for the C language, this is more
326 ** practical. Removing the abuse of the ability to cast ANY pointer
327 ** to and from an integer value here will break some compilers.
330 /* Left the comment in but the code isn't here any more -larne */
331 switch (mod
->mapi_version
)
335 struct mapi_mheader_av1
*mheader
= mod
->mapi_header
;
336 if(mheader
->mapi_command_list
)
339 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
343 /* hook events are never removed, we simply lose the
344 * ability to call them --fl
346 if(mheader
->mapi_hfn_list
)
348 mapi_hfn_list_av1
*m
;
349 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
350 remove_hook(m
->hapi_name
, m
->fn
);
353 if(mheader
->mapi_unregister
)
354 mheader
->mapi_unregister();
359 struct mapi_mheader_av2
*mheader
= mod
->mapi_header
;
361 /* XXX duplicate code :( */
362 if(mheader
->mapi_command_list
)
365 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
369 /* hook events are never removed, we simply lose the
370 * ability to call them --fl
372 if(mheader
->mapi_hfn_list
)
374 mapi_hfn_list_av1
*m
;
375 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
376 remove_hook(m
->hapi_name
, m
->fn
);
379 if(mheader
->mapi_unregister
)
380 mheader
->mapi_unregister();
382 if(mheader
->mapi_cap_list
)
384 mapi_cap_list_av2
*m
;
385 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
387 struct CapabilityIndex
*idx
;
389 switch (m
->cap_index
)
391 case MAPI_CAP_CLIENT
:
394 case MAPI_CAP_SERVER
:
398 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
399 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
400 m
->cap_index
, m
->cap_name
, mod
->name
);
402 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
403 m
->cap_index
, m
->cap_name
, mod
->name
);
407 if (m
->cap_id
!= NULL
)
408 capability_orphan(idx
, m
->cap_name
);
414 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
415 "Unknown/unsupported MAPI version %d when unloading %s!",
416 mod
->mapi_version
, mod
->name
);
417 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
418 mod
->mapi_version
, mod
->name
);
422 lt_dlclose(mod
->address
);
424 rb_dlinkDelete(&mod
->node
, &module_list
);
430 ilog(L_MAIN
, "Module %s unloaded", name
);
431 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
440 * inputs - path name of module, bool to notice, int of origin, bool if core
441 * output - false if error true if success
442 * side effects - loads a module if successful
445 load_a_module(const char *path
, bool warn
, int origin
, bool core
)
449 char *mod_displayname
, *c
;
450 const char *ver
, *description
= NULL
;
454 mod_displayname
= rb_basename(path
);
456 /* Trim off the ending for the display name if we have to */
457 if((c
= rb_strcasestr(mod_displayname
, LT_MODULE_EXT
)) != NULL
)
460 tmpptr
= lt_dlopenext(path
);
464 const char *err
= lt_dlerror();
466 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
467 "Error loading module %s: %s", mod_displayname
, err
);
468 ilog(L_MAIN
, "Error loading module %s: %s", mod_displayname
, err
);
469 rb_free(mod_displayname
);
474 * _mheader is actually a struct mapi_mheader_*, but mapi_version
475 * is always the first member of this structure, so we treate it
476 * as a single int in order to determine the API version.
479 mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "_mheader");
480 if((mapi_version
== NULL
481 && (mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "__mheader")) == NULL
)
482 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
484 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
485 "Data format error: module %s has no MAPI header.",
487 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_displayname
);
488 (void) lt_dlclose(tmpptr
);
489 rb_free(mod_displayname
);
493 switch (MAPI_VERSION(*mapi_version
))
497 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*)(void *)mapi_version
; /* see above */
498 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
500 ilog(L_MAIN
, "Module %s indicated failure during load.",
502 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
503 "Module %s indicated failure during load.",
506 rb_free(mod_displayname
);
509 if(mheader
->mapi_command_list
)
512 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
516 if(mheader
->mapi_hook_list
)
519 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
520 *m
->hapi_id
= register_hook(m
->hapi_name
);
523 if(mheader
->mapi_hfn_list
)
525 mapi_hfn_list_av1
*m
;
526 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
527 add_hook(m
->hapi_name
, m
->fn
);
530 ver
= mheader
->mapi_module_version
;
535 struct mapi_mheader_av2
*mheader
= (struct mapi_mheader_av2
*)(void *)mapi_version
; /* see above */
537 /* XXX duplicated code :( */
538 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
540 ilog(L_MAIN
, "Module %s indicated failure during load.",
542 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
543 "Module %s indicated failure during load.",
546 rb_free(mod_displayname
);
550 /* Basic date code checks
552 * Don't make them fatal, but do complain about differences within a certain time frame.
553 * Later on if there are major API changes we can add fatal checks.
556 if(mheader
->mapi_datecode
!= datecode
&& mheader
->mapi_datecode
> 0)
558 long int delta
= datecode
- mheader
->mapi_datecode
;
559 if (delta
> MOD_WARN_DELTA
)
562 iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
563 mod_displayname
, delta
);
564 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
565 "Module %s build date is out of sync with ircd build date by %ld days, expect problems",
566 mod_displayname
, delta
);
570 if(mheader
->mapi_command_list
)
573 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
577 if(mheader
->mapi_hook_list
)
580 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
581 *m
->hapi_id
= register_hook(m
->hapi_name
);
584 if(mheader
->mapi_hfn_list
)
586 mapi_hfn_list_av1
*m
;
587 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
588 add_hook(m
->hapi_name
, m
->fn
);
591 /* New in MAPI v2 - version replacement */
592 ver
= mheader
->mapi_module_version
? mheader
->mapi_module_version
: ircd_version
;
593 description
= mheader
->mapi_module_description
;
595 if(mheader
->mapi_cap_list
)
597 mapi_cap_list_av2
*m
;
598 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
600 struct CapabilityIndex
*idx
;
603 switch (m
->cap_index
)
605 case MAPI_CAP_CLIENT
:
608 case MAPI_CAP_SERVER
:
612 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
613 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
614 m
->cap_index
, m
->cap_name
, mod_displayname
);
616 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
617 m
->cap_index
, m
->cap_name
, mod_displayname
);
621 result
= capability_put(idx
, m
->cap_name
, m
->cap_ownerdata
);
622 if (m
->cap_id
!= NULL
)
623 *(m
->cap_id
) = result
;
630 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
631 mod_displayname
, MAPI_VERSION(*mapi_version
));
632 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
633 "Module %s has unknown/unsupported MAPI version %d.",
634 mod_displayname
, *mapi_version
);
636 rb_free(mod_displayname
);
643 if(description
== NULL
)
644 description
= unknown_description
;
646 mod
= rb_malloc(sizeof(struct module));
647 mod
->address
= tmpptr
;
649 mod
->description
= description
;
651 mod
->name
= rb_strdup(mod_displayname
);
652 mod
->mapi_header
= mapi_version
;
653 mod
->mapi_version
= MAPI_VERSION(*mapi_version
);
654 mod
->origin
= origin
;
655 rb_dlinkAdd(mod
, &mod
->node
, &module_list
);
663 case MAPI_ORIGIN_EXTENSION
:
666 case MAPI_ORIGIN_CORE
:
674 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
675 "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
676 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
,
678 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
679 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
, (void *) tmpptr
);
681 rb_free(mod_displayname
);
686 modules_do_restart(void *unused
)
688 unsigned int modnum
= 0;
689 rb_dlink_node
*ptr
, *nptr
;
691 mod_remember_clicaps();
693 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, module_list
.head
)
695 struct module *mod
= ptr
->data
;
696 if(!unload_one_module(mod
->name
, false))
698 ilog(L_MAIN
, "Module Restart: %s was not unloaded %s",
700 mod
->core
? "(core module)" : "");
703 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
704 "Module Restart: %s failed to unload",
712 load_all_modules(false);
713 load_core_modules(false);
716 mod_notify_clicaps();
718 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
719 "Module Restart: %u modules unloaded, %lu modules loaded",
720 modnum
, rb_dlink_list_length(&module_list
));
721 ilog(L_MAIN
, "Module Restart: %u modules unloaded, %lu modules loaded", modnum
, rb_dlink_list_length(&module_list
));