modules: serious cleanups

This commit is contained in:
William Pitcock 2016-06-18 00:52:16 -05:00
parent 92dad4831d
commit e55a9d6abc
3 changed files with 41 additions and 80 deletions

View file

@ -46,6 +46,7 @@ struct module
int origin; /* Ditto */ int origin; /* Ditto */
int mapi_version; int mapi_version;
void *mapi_header; /* actually struct mapi_mheader_av<mapi_version> */ void *mapi_header; /* actually struct mapi_mheader_av<mapi_version> */
rb_dlink_node node;
}; };
#define MAPI_MAGIC_HDR 0x4D410000 #define MAPI_MAGIC_HDR 0x4D410000
@ -133,12 +134,10 @@ extern void load_core_modules(bool);
extern bool unload_one_module(const char *, bool); extern bool unload_one_module(const char *, bool);
extern bool load_one_module(const char *, int, bool); extern bool load_one_module(const char *, int, bool);
extern bool load_a_module(const char *, bool, int, bool); extern bool load_a_module(const char *, bool, int, bool);
extern int findmodule_byname(const char *); extern struct module *findmodule_byname(const char *);
extern void init_modules(void); extern void init_modules(void);
/* Misc externs */ extern rb_dlink_list module_list;
extern struct module **modlist; extern rb_dlink_list mod_paths;
extern int num_mods;
extern int max_mods;
#endif /* INCLUDED_modules_h */ #endif /* INCLUDED_modules_h */

View file

@ -43,7 +43,8 @@
# error "Charybdis requires loadable module support." # error "Charybdis requires loadable module support."
#endif #endif
struct module **modlist = NULL; rb_dlink_list module_list;
rb_dlink_list mod_paths;
static const char *core_module_table[] = { static const char *core_module_table[] = {
"m_ban", "m_ban",
@ -65,12 +66,6 @@ static const char *core_module_table[] = {
#define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */ #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */
#define MODS_INCREMENT 10
int num_mods = 0;
int max_mods = MODS_INCREMENT;
static rb_dlink_list mod_paths;
void void
init_modules(void) init_modules(void)
{ {
@ -80,8 +75,6 @@ init_modules(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT));
/* Add the default paths we look in to the module system --nenolod */ /* Add the default paths we look in to the module system --nenolod */
mod_add_path(ircd_paths[IRCD_PATH_MODULES]); mod_add_path(ircd_paths[IRCD_PATH_MODULES]);
mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
@ -155,25 +148,27 @@ mod_clear_paths(void)
* output - index of module on success, -1 on failure * output - index of module on success, -1 on failure
* side effects - none * side effects - none
*/ */
int struct module *
findmodule_byname(const char *name) findmodule_byname(const char *name)
{ {
int i; rb_dlink_node *ptr;
char name_ext[PATH_MAX + 1]; char name_ext[PATH_MAX + 1];
rb_strlcpy(name_ext, name, sizeof name_ext); rb_strlcpy(name_ext, name, sizeof name_ext);
rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext); rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext);
for (i = 0; i < num_mods; i++) RB_DLINK_FOREACH(ptr, module_list.head)
{ {
if(!irccmp(modlist[i]->name, name)) struct module *mod = ptr->data;
return i;
if(!irccmp(modlist[i]->name, name_ext)) if(!irccmp(mod->name, name))
return i; return mod;
if(!irccmp(mod->name, name_ext))
return mod;
} }
return -1; return NULL;
} }
/* load_all_modules() /* load_all_modules()
@ -190,8 +185,6 @@ load_all_modules(bool warn)
char module_fq_name[PATH_MAX + 1]; char module_fq_name[PATH_MAX + 1];
size_t module_ext_len = strlen(LT_MODULE_EXT); size_t module_ext_len = strlen(LT_MODULE_EXT);
max_mods = MODS_INCREMENT;
system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
if(system_module_dir == NULL) if(system_module_dir == NULL)
@ -283,8 +276,6 @@ load_one_module(const char *path, int origin, bool coremodule)
return false; return false;
} }
static void increase_modlist(void);
static char unknown_ver[] = "<unknown>"; static char unknown_ver[] = "<unknown>";
static char unknown_description[] = "<none>"; static char unknown_description[] = "<none>";
@ -298,9 +289,9 @@ static char unknown_description[] = "<none>";
bool bool
unload_one_module(const char *name, bool warn) unload_one_module(const char *name, bool warn)
{ {
int modindex; struct module *mod;
if((modindex = findmodule_byname(name)) == -1) if((mod = findmodule_byname(name)) == NULL)
return false; return false;
/* /*
@ -314,11 +305,11 @@ unload_one_module(const char *name, bool warn)
** -jmallett ** -jmallett
*/ */
/* Left the comment in but the code isn't here any more -larne */ /* Left the comment in but the code isn't here any more -larne */
switch (modlist[modindex]->mapi_version) switch (mod->mapi_version)
{ {
case 1: case 1:
{ {
struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header; struct mapi_mheader_av1 *mheader = mod->mapi_header;
if(mheader->mapi_command_list) if(mheader->mapi_command_list)
{ {
struct Message **m; struct Message **m;
@ -342,7 +333,7 @@ unload_one_module(const char *name, bool warn)
} }
case 2: case 2:
{ {
struct mapi_mheader_av2 *mheader = modlist[modindex]->mapi_header; struct mapi_mheader_av2 *mheader = mod->mapi_header;
/* XXX duplicate code :( */ /* XXX duplicate code :( */
if(mheader->mapi_command_list) if(mheader->mapi_command_list)
@ -383,10 +374,10 @@ unload_one_module(const char *name, bool warn)
default: default:
sendto_realops_snomask(SNO_GENERAL, L_ALL, sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
m->cap_index, m->cap_name, modlist[modindex]->name); m->cap_index, m->cap_name, mod->name);
ilog(L_MAIN, ilog(L_MAIN,
"Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
m->cap_index, m->cap_name, modlist[modindex]->name); m->cap_index, m->cap_name, mod->name);
continue; continue;
} }
@ -402,21 +393,17 @@ unload_one_module(const char *name, bool warn)
default: default:
sendto_realops_snomask(SNO_GENERAL, L_ALL, sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unknown/unsupported MAPI version %d when unloading %s!", "Unknown/unsupported MAPI version %d when unloading %s!",
modlist[modindex]->mapi_version, modlist[modindex]->name); mod->mapi_version, mod->name);
ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!", ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
modlist[modindex]->mapi_version, modlist[modindex]->name); mod->mapi_version, mod->name);
break; break;
} }
lt_dlclose(modlist[modindex]->address); lt_dlclose(mod->address);
rb_free(modlist[modindex]->name); rb_dlinkDelete(&mod->node, &module_list);
rb_free(modlist[modindex]); rb_free(mod->name);
memmove(&modlist[modindex], &modlist[modindex + 1], rb_free(mod);
sizeof(struct module *) * ((num_mods - 1) - modindex));
if(num_mods != 0)
num_mods--;
if(warn) if(warn)
{ {
@ -437,6 +424,7 @@ unload_one_module(const char *name, bool warn)
bool bool
load_a_module(const char *path, bool warn, int origin, bool core) load_a_module(const char *path, bool warn, int origin, bool core)
{ {
struct module *mod;
lt_dlhandle tmpptr; lt_dlhandle tmpptr;
char *mod_displayname, *c; char *mod_displayname, *c;
const char *ver, *description = NULL; const char *ver, *description = NULL;
@ -639,18 +627,16 @@ load_a_module(const char *path, bool warn, int origin, bool core)
if(description == NULL) if(description == NULL)
description = unknown_description; description = unknown_description;
increase_modlist(); mod = rb_malloc(sizeof(struct module));
mod->address = tmpptr;
modlist[num_mods] = rb_malloc(sizeof(struct module)); mod->version = ver;
modlist[num_mods]->address = tmpptr; mod->description = description;
modlist[num_mods]->version = ver; mod->core = core;
modlist[num_mods]->description = description; mod->name = rb_strdup(mod_displayname);
modlist[num_mods]->core = core; mod->mapi_header = mapi_version;
modlist[num_mods]->name = rb_strdup(mod_displayname); mod->mapi_version = MAPI_VERSION(*mapi_version);
modlist[num_mods]->mapi_header = mapi_version; mod->origin = origin;
modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version); rb_dlinkAdd(mod, &mod->node, &module_list);
modlist[num_mods]->origin = origin;
num_mods++;
if(warn) if(warn)
{ {
@ -679,27 +665,3 @@ load_a_module(const char *path, bool warn, int origin, bool core)
rb_free(mod_displayname); rb_free(mod_displayname);
return true; return true;
} }
/*
* increase_modlist
*
* inputs - NONE
* output - NONE
* side effects - expand the size of modlist if necessary
*/
static void
increase_modlist(void)
{
struct module **new_modlist = NULL;
if((num_mods + 1) < max_mods)
return;
new_modlist = (struct module **) rb_malloc(sizeof(struct module *) *
(max_mods + MODS_INCREMENT));
memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods);
rb_free(modlist);
modlist = new_modlist;
max_mods += MODS_INCREMENT;
}

View file

@ -300,7 +300,7 @@ conf_set_modules_module(void *data)
m_bn = rb_basename((char *) data); m_bn = rb_basename((char *) data);
if(findmodule_byname(m_bn) == -1) if(findmodule_byname(m_bn) == NULL)
load_one_module((char *) data, MAPI_ORIGIN_EXTENSION, false); load_one_module((char *) data, MAPI_ORIGIN_EXTENSION, false);
rb_free(m_bn); rb_free(m_bn);