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"
44 # error "Charybdis requires loadable module support."
47 rb_dlink_list module_list
;
48 rb_dlink_list mod_paths
;
50 static const char *core_module_table
[] = {
68 #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */
75 ilog(L_MAIN
, "lt_dlinit failed");
79 /* Add the default paths we look in to the module system --nenolod */
80 mod_add_path(ircd_paths
[IRCD_PATH_MODULES
]);
81 mod_add_path(ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
]);
84 static unsigned int prev_caps
;
87 mod_remember_clicaps(void)
89 prev_caps
= capability_index_mask(cli_capindex
);
93 mod_notify_clicaps(void)
95 unsigned int cur_caps
= capability_index_mask(cli_capindex
);
96 unsigned int del
= prev_caps
& ~cur_caps
;
97 unsigned int new = cur_caps
& ~prev_caps
;
100 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * DEL :%s",
101 me
.name
, capability_index_list(cli_capindex
, del
));
103 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * NEW :%s",
104 me
.name
, capability_index_list(cli_capindex
, new));
111 * side effects - returns a module path from path
114 mod_find_path(const char *path
)
119 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
123 if(!strcmp(path
, mpath
))
134 * side effects - adds path to list
137 mod_add_path(const char *path
)
141 if(mod_find_path(path
))
144 pathst
= rb_strdup(path
);
145 rb_dlinkAddAlloc(pathst
, &mod_paths
);
152 * side effects - clear the lists of paths
155 mod_clear_paths(void)
157 rb_dlink_node
*ptr
, *next_ptr
;
159 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
162 rb_free_rb_dlink_node(ptr
);
165 mod_paths
.head
= mod_paths
.tail
= NULL
;
166 mod_paths
.length
= 0;
171 * input - module to load
172 * output - index of module on success, -1 on failure
173 * side effects - none
176 findmodule_byname(const char *name
)
179 char name_ext
[PATH_MAX
+ 1];
181 rb_strlcpy(name_ext
, name
, sizeof name_ext
);
182 rb_strlcat(name_ext
, LT_MODULE_EXT
, sizeof name_ext
);
184 RB_DLINK_FOREACH(ptr
, module_list
.head
)
186 struct module *mod
= ptr
->data
;
188 if(!irccmp(mod
->name
, name
))
191 if(!irccmp(mod
->name
, name_ext
))
198 /* load_all_modules()
205 load_all_modules(bool warn
)
207 DIR *system_module_dir
= NULL
;
208 struct dirent
*ldirent
= NULL
;
209 char module_fq_name
[PATH_MAX
+ 1];
210 size_t module_ext_len
= strlen(LT_MODULE_EXT
);
212 system_module_dir
= opendir(ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
]);
214 if(system_module_dir
== NULL
)
216 ilog(L_MAIN
, "Could not load modules from %s: %s", ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], strerror(errno
));
220 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
222 size_t len
= strlen(ldirent
->d_name
);
224 if(len
> module_ext_len
&&
225 rb_strncasecmp(ldirent
->d_name
+ (len
- module_ext_len
), LT_MODULE_EXT
, module_ext_len
) == 0)
227 (void) snprintf(module_fq_name
, sizeof(module_fq_name
), "%s%c%s",
228 ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], RB_PATH_SEPARATOR
, ldirent
->d_name
);
229 (void) load_a_module(module_fq_name
, warn
, MAPI_ORIGIN_CORE
, false);
233 (void) closedir(system_module_dir
);
236 /* load_core_modules()
240 * side effects - core modules are loaded, if any fail, kill ircd
243 load_core_modules(bool warn
)
245 char module_name
[PATH_MAX
];
249 for (i
= 0; core_module_table
[i
]; i
++)
251 snprintf(module_name
, sizeof(module_name
), "%s%c%s", ircd_paths
[IRCD_PATH_MODULES
], RB_PATH_SEPARATOR
,
252 core_module_table
[i
]);
254 if(load_a_module(module_name
, warn
, MAPI_ORIGIN_CORE
, true) == false)
257 "Error loading core module %s: terminating ircd",
258 core_module_table
[i
]);
271 load_one_module(const char *path
, int origin
, bool coremodule
)
273 char modpath
[PATH_MAX
];
274 rb_dlink_node
*pathst
;
276 if (server_state_foreground
)
277 inotice("loading module %s ...", path
);
280 origin
= MAPI_ORIGIN_CORE
;
282 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
285 const char *mpath
= pathst
->data
;
287 snprintf(modpath
, sizeof(modpath
), "%s%c%s%s", mpath
, RB_PATH_SEPARATOR
, path
, LT_MODULE_EXT
);
288 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
290 if(stat(modpath
, &statbuf
) == 0 && S_ISREG(statbuf
.st_mode
))
292 /* Regular files only please */
293 return load_a_module(modpath
, true, origin
, coremodule
);
299 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
303 static char unknown_ver
[] = "<unknown>";
304 static char unknown_description
[] = "<none>";
306 /* unload_one_module()
308 * inputs - name of module to unload
309 * - true to say modules unloaded, false to not
310 * output - true if successful, false if error
311 * side effects - module is unloaded
314 unload_one_module(const char *name
, bool warn
)
318 if((mod
= findmodule_byname(name
)) == NULL
)
322 ** XXX - The type system in C does not allow direct conversion between
323 ** data and function pointers, but as it happens, most C compilers will
324 ** safely do this, however it is a theoretical overlow to cast as we
325 ** must do here. I have library functions to take care of this, but
326 ** despite being more "correct" for the C language, this is more
327 ** practical. Removing the abuse of the ability to cast ANY pointer
328 ** to and from an integer value here will break some compilers.
331 /* Left the comment in but the code isn't here any more -larne */
332 switch (mod
->mapi_version
)
336 struct mapi_mheader_av1
*mheader
= mod
->mapi_header
;
337 if(mheader
->mapi_command_list
)
340 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
344 /* hook events are never removed, we simply lose the
345 * ability to call them --fl
347 if(mheader
->mapi_hfn_list
)
349 mapi_hfn_list_av1
*m
;
350 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
351 remove_hook(m
->hapi_name
, m
->fn
);
354 if(mheader
->mapi_unregister
)
355 mheader
->mapi_unregister();
360 struct mapi_mheader_av2
*mheader
= mod
->mapi_header
;
362 /* XXX duplicate code :( */
363 if(mheader
->mapi_command_list
)
366 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
370 /* hook events are never removed, we simply lose the
371 * ability to call them --fl
373 if(mheader
->mapi_hfn_list
)
375 mapi_hfn_list_av1
*m
;
376 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
377 remove_hook(m
->hapi_name
, m
->fn
);
380 if(mheader
->mapi_unregister
)
381 mheader
->mapi_unregister();
383 if(mheader
->mapi_cap_list
)
385 mapi_cap_list_av2
*m
;
386 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
388 struct CapabilityIndex
*idx
;
390 switch (m
->cap_index
)
392 case MAPI_CAP_CLIENT
:
395 case MAPI_CAP_SERVER
:
399 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
400 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
401 m
->cap_index
, m
->cap_name
, mod
->name
);
403 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
404 m
->cap_index
, m
->cap_name
, mod
->name
);
408 if (m
->cap_id
!= NULL
)
409 capability_orphan(idx
, m
->cap_name
);
415 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
416 "Unknown/unsupported MAPI version %d when unloading %s!",
417 mod
->mapi_version
, mod
->name
);
418 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
419 mod
->mapi_version
, mod
->name
);
423 lt_dlclose(mod
->address
);
425 rb_dlinkDelete(&mod
->node
, &module_list
);
431 ilog(L_MAIN
, "Module %s unloaded", name
);
432 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
441 * inputs - path name of module, bool to notice, int of origin, bool if core
442 * output - false if error true if success
443 * side effects - loads a module if successful
446 load_a_module(const char *path
, bool warn
, int origin
, bool core
)
450 char *mod_displayname
, *c
;
451 const char *ver
, *description
= NULL
;
455 mod_displayname
= rb_basename(path
);
457 /* Trim off the ending for the display name if we have to */
458 if((c
= rb_strcasestr(mod_displayname
, LT_MODULE_EXT
)) != NULL
)
461 tmpptr
= lt_dlopenext(path
);
465 const char *err
= lt_dlerror();
467 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
468 "Error loading module %s: %s", mod_displayname
, err
);
469 ilog(L_MAIN
, "Error loading module %s: %s", mod_displayname
, err
);
470 rb_free(mod_displayname
);
475 * _mheader is actually a struct mapi_mheader_*, but mapi_version
476 * is always the first member of this structure, so we treate it
477 * as a single int in order to determine the API version.
480 mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "_mheader");
481 if((mapi_version
== NULL
482 && (mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "__mheader")) == NULL
)
483 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
485 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
486 "Data format error: module %s has no MAPI header.",
488 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_displayname
);
489 (void) lt_dlclose(tmpptr
);
490 rb_free(mod_displayname
);
494 switch (MAPI_VERSION(*mapi_version
))
498 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*)(void *)mapi_version
; /* see above */
499 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
501 ilog(L_MAIN
, "Module %s indicated failure during load.",
503 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
504 "Module %s indicated failure during load.",
507 rb_free(mod_displayname
);
510 if(mheader
->mapi_command_list
)
513 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
517 if(mheader
->mapi_hook_list
)
520 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
521 *m
->hapi_id
= register_hook(m
->hapi_name
);
524 if(mheader
->mapi_hfn_list
)
526 mapi_hfn_list_av1
*m
;
527 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
528 add_hook(m
->hapi_name
, m
->fn
);
531 ver
= mheader
->mapi_module_version
;
536 struct mapi_mheader_av2
*mheader
= (struct mapi_mheader_av2
*)(void *)mapi_version
; /* see above */
538 /* XXX duplicated code :( */
539 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
541 ilog(L_MAIN
, "Module %s indicated failure during load.",
543 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
544 "Module %s indicated failure during load.",
547 rb_free(mod_displayname
);
551 /* Basic date code checks
553 * Don't make them fatal, but do complain about differences within a certain time frame.
554 * Later on if there are major API changes we can add fatal checks.
557 if(mheader
->mapi_datecode
!= datecode
&& mheader
->mapi_datecode
> 0)
559 long int delta
= datecode
- mheader
->mapi_datecode
;
560 if (delta
> MOD_WARN_DELTA
)
563 iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
564 mod_displayname
, delta
);
565 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
566 "Module %s build date is out of sync with ircd build date by %ld days, expect problems",
567 mod_displayname
, delta
);
571 if(mheader
->mapi_command_list
)
574 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
578 if(mheader
->mapi_hook_list
)
581 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
582 *m
->hapi_id
= register_hook(m
->hapi_name
);
585 if(mheader
->mapi_hfn_list
)
587 mapi_hfn_list_av1
*m
;
588 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
589 add_hook(m
->hapi_name
, m
->fn
);
592 /* New in MAPI v2 - version replacement */
593 ver
= mheader
->mapi_module_version
? mheader
->mapi_module_version
: ircd_version
;
594 description
= mheader
->mapi_module_description
;
596 if(mheader
->mapi_cap_list
)
598 mapi_cap_list_av2
*m
;
599 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
601 struct CapabilityIndex
*idx
;
604 switch (m
->cap_index
)
606 case MAPI_CAP_CLIENT
:
609 case MAPI_CAP_SERVER
:
613 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
614 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
615 m
->cap_index
, m
->cap_name
, mod_displayname
);
617 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
618 m
->cap_index
, m
->cap_name
, mod_displayname
);
622 result
= capability_put(idx
, m
->cap_name
, m
->cap_ownerdata
);
623 if (m
->cap_id
!= NULL
)
624 *(m
->cap_id
) = result
;
631 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
632 mod_displayname
, MAPI_VERSION(*mapi_version
));
633 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
634 "Module %s has unknown/unsupported MAPI version %d.",
635 mod_displayname
, *mapi_version
);
637 rb_free(mod_displayname
);
644 if(description
== NULL
)
645 description
= unknown_description
;
647 mod
= rb_malloc(sizeof(struct module));
648 mod
->address
= tmpptr
;
650 mod
->description
= description
;
652 mod
->name
= rb_strdup(mod_displayname
);
653 mod
->mapi_header
= mapi_version
;
654 mod
->mapi_version
= MAPI_VERSION(*mapi_version
);
655 mod
->origin
= origin
;
656 rb_dlinkAdd(mod
, &mod
->node
, &module_list
);
664 case MAPI_ORIGIN_EXTENSION
:
667 case MAPI_ORIGIN_CORE
:
675 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
676 "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
677 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
,
679 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
680 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
, (void *) tmpptr
);
682 rb_free(mod_displayname
);
687 modules_do_reload(void *info_
)
689 struct modreload
*info
= info_
;
692 char *m_bn
= rb_basename(info
->module);
693 struct Client
*source_p
= find_id(info
->id
);
695 if((mod
= findmodule_byname(m_bn
)) == NULL
)
697 if (source_p
) sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
703 check_core
= mod
->core
;
705 mod_remember_clicaps();
707 if(unload_one_module(m_bn
, true) == false)
709 if (source_p
) sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
715 if((load_one_module(m_bn
, mod
->origin
, check_core
) == false) && check_core
)
717 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
718 "Error reloading core module: %s: terminating ircd", m_bn
);
719 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", m_bn
);
723 mod_notify_clicaps();
730 modules_do_restart(void *unused
)
732 unsigned int modnum
= 0;
733 rb_dlink_node
*ptr
, *nptr
;
735 mod_remember_clicaps();
737 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, module_list
.head
)
739 struct module *mod
= ptr
->data
;
740 if(!unload_one_module(mod
->name
, false))
742 ilog(L_MAIN
, "Module Restart: %s was not unloaded %s",
744 mod
->core
? "(core module)" : "");
747 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
748 "Module Restart: %s failed to unload",
756 load_all_modules(false);
757 load_core_modules(false);
760 mod_notify_clicaps();
762 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
763 "Module Restart: %u modules unloaded, %lu modules loaded",
764 modnum
, rb_dlink_list_length(&module_list
));
765 ilog(L_MAIN
, "Module Restart: %u modules unloaded, %lu modules loaded", modnum
, rb_dlink_list_length(&module_list
));