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
]);
87 * side effects - returns a module path from path
90 mod_find_path(const char *path
)
95 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
99 if(!strcmp(path
, mpath
))
110 * side effects - adds path to list
113 mod_add_path(const char *path
)
117 if(mod_find_path(path
))
120 pathst
= rb_strdup(path
);
121 rb_dlinkAddAlloc(pathst
, &mod_paths
);
128 * side effects - clear the lists of paths
131 mod_clear_paths(void)
133 rb_dlink_node
*ptr
, *next_ptr
;
135 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
138 rb_free_rb_dlink_node(ptr
);
141 mod_paths
.head
= mod_paths
.tail
= NULL
;
142 mod_paths
.length
= 0;
147 * input - module to load
148 * output - index of module on success, -1 on failure
149 * side effects - none
152 findmodule_byname(const char *name
)
155 char name_ext
[PATH_MAX
+ 1];
157 rb_strlcpy(name_ext
, name
, sizeof name_ext
);
158 rb_strlcat(name_ext
, LT_MODULE_EXT
, sizeof name_ext
);
160 RB_DLINK_FOREACH(ptr
, module_list
.head
)
162 struct module *mod
= ptr
->data
;
164 if(!irccmp(mod
->name
, name
))
167 if(!irccmp(mod
->name
, name_ext
))
174 /* load_all_modules()
181 load_all_modules(bool warn
)
183 DIR *system_module_dir
= NULL
;
184 struct dirent
*ldirent
= NULL
;
185 char module_fq_name
[PATH_MAX
+ 1];
186 size_t module_ext_len
= strlen(LT_MODULE_EXT
);
188 system_module_dir
= opendir(ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
]);
190 if(system_module_dir
== NULL
)
192 ilog(L_MAIN
, "Could not load modules from %s: %s", ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], strerror(errno
));
196 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
198 size_t len
= strlen(ldirent
->d_name
);
200 if(len
> module_ext_len
&&
201 rb_strncasecmp(ldirent
->d_name
+ (len
- module_ext_len
), LT_MODULE_EXT
, module_ext_len
) == 0)
203 (void) snprintf(module_fq_name
, sizeof(module_fq_name
), "%s%c%s",
204 ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
], RB_PATH_SEPARATOR
, ldirent
->d_name
);
205 (void) load_a_module(module_fq_name
, warn
, MAPI_ORIGIN_CORE
, false);
209 (void) closedir(system_module_dir
);
212 /* load_core_modules()
216 * side effects - core modules are loaded, if any fail, kill ircd
219 load_core_modules(bool warn
)
221 char module_name
[PATH_MAX
];
225 for (i
= 0; core_module_table
[i
]; i
++)
227 snprintf(module_name
, sizeof(module_name
), "%s%c%s", ircd_paths
[IRCD_PATH_MODULES
], RB_PATH_SEPARATOR
,
228 core_module_table
[i
]);
230 if(load_a_module(module_name
, warn
, MAPI_ORIGIN_CORE
, true) == false)
233 "Error loading core module %s: terminating ircd",
234 core_module_table
[i
]);
247 load_one_module(const char *path
, int origin
, bool coremodule
)
249 char modpath
[PATH_MAX
];
250 rb_dlink_node
*pathst
;
252 if (server_state_foreground
)
253 inotice("loading module %s ...", path
);
256 origin
= MAPI_ORIGIN_CORE
;
258 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
261 const char *mpath
= pathst
->data
;
263 snprintf(modpath
, sizeof(modpath
), "%s%c%s%s", mpath
, RB_PATH_SEPARATOR
, path
, LT_MODULE_EXT
);
264 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
266 if(stat(modpath
, &statbuf
) == 0 && S_ISREG(statbuf
.st_mode
))
268 /* Regular files only please */
269 return load_a_module(modpath
, true, origin
, coremodule
);
275 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
279 static char unknown_ver
[] = "<unknown>";
280 static char unknown_description
[] = "<none>";
282 /* unload_one_module()
284 * inputs - name of module to unload
285 * - true to say modules unloaded, false to not
286 * output - true if successful, false if error
287 * side effects - module is unloaded
290 unload_one_module(const char *name
, bool warn
)
294 if((mod
= findmodule_byname(name
)) == NULL
)
301 ** XXX - The type system in C does not allow direct conversion between
302 ** data and function pointers, but as it happens, most C compilers will
303 ** safely do this, however it is a theoretical overlow to cast as we
304 ** must do here. I have library functions to take care of this, but
305 ** despite being more "correct" for the C language, this is more
306 ** practical. Removing the abuse of the ability to cast ANY pointer
307 ** to and from an integer value here will break some compilers.
310 /* Left the comment in but the code isn't here any more -larne */
311 switch (mod
->mapi_version
)
315 struct mapi_mheader_av1
*mheader
= mod
->mapi_header
;
316 if(mheader
->mapi_command_list
)
319 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
323 /* hook events are never removed, we simply lose the
324 * ability to call them --fl
326 if(mheader
->mapi_hfn_list
)
328 mapi_hfn_list_av1
*m
;
329 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
330 remove_hook(m
->hapi_name
, m
->fn
);
333 if(mheader
->mapi_unregister
)
334 mheader
->mapi_unregister();
339 struct mapi_mheader_av2
*mheader
= mod
->mapi_header
;
341 /* XXX duplicate code :( */
342 if(mheader
->mapi_command_list
)
345 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
349 /* hook events are never removed, we simply lose the
350 * ability to call them --fl
352 if(mheader
->mapi_hfn_list
)
354 mapi_hfn_list_av1
*m
;
355 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
356 remove_hook(m
->hapi_name
, m
->fn
);
359 if(mheader
->mapi_unregister
)
360 mheader
->mapi_unregister();
362 if(mheader
->mapi_cap_list
)
364 mapi_cap_list_av2
*m
;
365 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
367 struct CapabilityIndex
*idx
;
369 switch (m
->cap_index
)
371 case MAPI_CAP_CLIENT
:
374 case MAPI_CAP_SERVER
:
378 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
379 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
380 m
->cap_index
, m
->cap_name
, mod
->name
);
382 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
383 m
->cap_index
, m
->cap_name
, mod
->name
);
387 if (m
->cap_id
!= NULL
)
389 capability_orphan(idx
, m
->cap_name
);
390 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * DEL :%s", me
.name
, m
->cap_name
);
397 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
398 "Unknown/unsupported MAPI version %d when unloading %s!",
399 mod
->mapi_version
, mod
->name
);
400 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
401 mod
->mapi_version
, mod
->name
);
405 lt_dlclose(mod
->address
);
407 rb_dlinkDelete(&mod
->node
, &module_list
);
413 ilog(L_MAIN
, "Module %s unloaded", name
);
414 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
423 * inputs - path name of module, bool to notice, int of origin, bool if core
424 * output - false if error true if success
425 * side effects - loads a module if successful
428 load_a_module(const char *path
, bool warn
, int origin
, bool core
)
432 char *mod_displayname
, *c
;
433 const char *ver
, *description
= NULL
;
434 size_t module_ext_len
= strlen(LT_MODULE_EXT
);
438 mod_displayname
= rb_basename(path
);
440 /* Trim off the ending for the display name if we have to */
441 if((c
= rb_strcasestr(mod_displayname
, LT_MODULE_EXT
)) != NULL
)
444 tmpptr
= lt_dlopenext(path
);
448 const char *err
= lt_dlerror();
450 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
451 "Error loading module %s: %s", mod_displayname
, err
);
452 ilog(L_MAIN
, "Error loading module %s: %s", mod_displayname
, err
);
453 rb_free(mod_displayname
);
458 * _mheader is actually a struct mapi_mheader_*, but mapi_version
459 * is always the first member of this structure, so we treate it
460 * as a single int in order to determine the API version.
463 mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "_mheader");
464 if((mapi_version
== NULL
465 && (mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "__mheader")) == NULL
)
466 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
468 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
469 "Data format error: module %s has no MAPI header.",
471 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_displayname
);
472 (void) lt_dlclose(tmpptr
);
473 rb_free(mod_displayname
);
477 switch (MAPI_VERSION(*mapi_version
))
481 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*)(void *)mapi_version
; /* see above */
482 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
484 ilog(L_MAIN
, "Module %s indicated failure during load.",
486 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
487 "Module %s indicated failure during load.",
490 rb_free(mod_displayname
);
493 if(mheader
->mapi_command_list
)
496 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
500 if(mheader
->mapi_hook_list
)
503 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
504 *m
->hapi_id
= register_hook(m
->hapi_name
);
507 if(mheader
->mapi_hfn_list
)
509 mapi_hfn_list_av1
*m
;
510 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
511 add_hook(m
->hapi_name
, m
->fn
);
514 ver
= mheader
->mapi_module_version
;
519 struct mapi_mheader_av2
*mheader
= (struct mapi_mheader_av2
*)(void *)mapi_version
; /* see above */
521 /* XXX duplicated code :( */
522 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
524 ilog(L_MAIN
, "Module %s indicated failure during load.",
526 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
527 "Module %s indicated failure during load.",
530 rb_free(mod_displayname
);
534 /* Basic date code checks
536 * Don't make them fatal, but do complain about differences within a certain time frame.
537 * Later on if there are major API changes we can add fatal checks.
540 if(mheader
->mapi_datecode
!= datecode
&& mheader
->mapi_datecode
> 0)
542 long int delta
= datecode
- mheader
->mapi_datecode
;
543 if (delta
> MOD_WARN_DELTA
)
546 iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
547 mod_displayname
, delta
);
548 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
549 "Module %s build date is out of sync with ircd build date by %ld days, expect problems",
550 mod_displayname
, delta
);
554 if(mheader
->mapi_command_list
)
557 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
561 if(mheader
->mapi_hook_list
)
564 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
565 *m
->hapi_id
= register_hook(m
->hapi_name
);
568 if(mheader
->mapi_hfn_list
)
570 mapi_hfn_list_av1
*m
;
571 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
572 add_hook(m
->hapi_name
, m
->fn
);
575 /* New in MAPI v2 - version replacement */
576 ver
= mheader
->mapi_module_version
? mheader
->mapi_module_version
: ircd_version
;
577 description
= mheader
->mapi_module_description
;
579 if(mheader
->mapi_cap_list
)
581 mapi_cap_list_av2
*m
;
582 for (m
= mheader
->mapi_cap_list
; m
->cap_name
; ++m
)
584 struct CapabilityIndex
*idx
;
587 switch (m
->cap_index
)
589 case MAPI_CAP_CLIENT
:
592 case MAPI_CAP_SERVER
:
596 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
597 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
598 m
->cap_index
, m
->cap_name
, mod_displayname
);
600 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
601 m
->cap_index
, m
->cap_name
, mod_displayname
);
605 result
= capability_put(idx
, m
->cap_name
, m
->cap_ownerdata
);
606 if (m
->cap_id
!= NULL
)
608 *(m
->cap_id
) = result
;
609 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY
, ":%s CAP * ADD :%s", me
.name
, m
->cap_name
);
617 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
618 mod_displayname
, MAPI_VERSION(*mapi_version
));
619 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
620 "Module %s has unknown/unsupported MAPI version %d.",
621 mod_displayname
, *mapi_version
);
623 rb_free(mod_displayname
);
630 if(description
== NULL
)
631 description
= unknown_description
;
633 mod
= rb_malloc(sizeof(struct module));
634 mod
->address
= tmpptr
;
636 mod
->description
= description
;
638 mod
->name
= rb_strdup(mod_displayname
);
639 mod
->mapi_header
= mapi_version
;
640 mod
->mapi_version
= MAPI_VERSION(*mapi_version
);
641 mod
->origin
= origin
;
642 rb_dlinkAdd(mod
, &mod
->node
, &module_list
);
650 case MAPI_ORIGIN_EXTENSION
:
653 case MAPI_ORIGIN_CORE
:
661 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
662 "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
663 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
,
665 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
666 mod_displayname
, ver
, MAPI_VERSION(*mapi_version
), o
, description
, (void *) tmpptr
);
668 rb_free(mod_displayname
);
673 modules_do_restart(void *unused
)
675 unsigned int modnum
= 0;
676 rb_dlink_node
*ptr
, *nptr
;
678 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, module_list
.head
)
680 struct module *mod
= ptr
->data
;
681 if(!unload_one_module(mod
->name
, false))
683 ilog(L_MAIN
, "Module Restart: %s was not unloaded %s",
685 mod
->core
? "(core module)" : "");
688 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
689 "Module Restart: %s failed to unload",
697 load_all_modules(false);
698 load_core_modules(false);
701 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
702 "Module Restart: %u modules unloaded, %lu modules loaded",
703 modnum
, rb_dlink_list_length(&module_list
));
704 ilog(L_MAIN
, "Module Restart: %u modules unloaded, %lu modules loaded", modnum
, rb_dlink_list_length(&module_list
));