Skip to content
This repository was archived by the owner on Jun 6, 2021. It is now read-only.

Commit

Permalink
Rework error handling in module_load/linker_open_ext to remove assert…
Browse files Browse the repository at this point in the history
…ion failure when loading a non-module module
  • Loading branch information
Stephen Bennett committed Mar 14, 2011
1 parent 6c80321 commit c46368c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LINKER_H
#define LINKER_H

extern mowgli_module_t *linker_open_ext(const char *path);
extern mowgli_module_t *linker_open_ext(const char *path, char *errbuf, int errlen);

#endif

Expand Down
16 changes: 15 additions & 1 deletion libathemecore/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* the extension is appended if it's not already there.
* a shared module is loaded into the application's memory space
*/
mowgli_module_t *linker_open_ext(const char *path)
mowgli_module_t *linker_open_ext(const char *path, char *errbuf, int errlen)
{
char *buf = smalloc(strlen(path) + 20);
void *ret;
Expand All @@ -55,8 +55,22 @@ mowgli_module_t *linker_open_ext(const char *path)
if (!strstr(buf, PLATFORM_SUFFIX))
strlcat(buf, PLATFORM_SUFFIX, strlen(path) + 20);

/* Don't try to open a file that doesn't exist. */
struct stat s;
if (0 != stat(buf, &s))
{
strlcpy(errbuf, strerror(errno), errlen);
return NULL;
}

ret = mowgli_module_open(buf);
free(buf);

if (!ret)
{
strlcpy(errbuf, dlerror(), errlen);
}

return ret;
}

Expand Down
23 changes: 11 additions & 12 deletions libathemecore/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mowgli_list_t modules, modules_inprogress;

module_t *modtarget = NULL;

static module_t *module_load_internal(const char *pathname, char *errbuf);
static module_t *module_load_internal(const char *pathname, char *errbuf, int errlen);

void modules_init(void)
{
Expand Down Expand Up @@ -78,7 +78,7 @@ module_t *module_load(const char *filespec)
return NULL;
}

m = module_load_internal(pathname, errbuf);
m = module_load_internal(pathname, errbuf, sizeof errbuf);

if (!m)
{
Expand Down Expand Up @@ -114,7 +114,7 @@ module_t *module_load(const char *filespec)
* module_load_internal: the part of module_load that deals with 'real' shared
* object modules.
*/
static module_t *module_load_internal(const char *pathname, char *errbuf)
static module_t *module_load_internal(const char *pathname, char *errbuf, int errlen)
{
mowgli_node_t *n;
module_t *m, *old_modtarget;
Expand All @@ -123,46 +123,45 @@ static module_t *module_load_internal(const char *pathname, char *errbuf)
#if defined(HAVE_DLINFO) && !defined(__UCLIBC__)
struct link_map *map;
#endif
char linker_errbuf[BUFSIZE];

handle = linker_open_ext(pathname);
handle = linker_open_ext(pathname, linker_errbuf, BUFSIZE);

if (!handle)
{
char *errp = sstrdup(dlerror());
snprintf(errbuf, BUFSIZE, "module_load(): error while loading %s: \2%s\2", pathname, errp);
free(errp);
snprintf(errbuf, errlen, "module_load(): error while loading %s: \2%s\2", pathname, linker_errbuf);
return NULL;
}

h = (v4_moduleheader_t *) mowgli_module_symbol(handle, "_header");

if (h == NULL || h->atheme_mod != MAPI_ATHEME_MAGIC)
{
snprintf(errbuf, BUFSIZE, "module_load(): \2%s\2: Attempted to load an incompatible module. Aborting.", pathname);
snprintf(errbuf, errlen, "module_load(): \2%s\2: Attempted to load an incompatible module. Aborting.", pathname);

mowgli_module_close(handle);
return NULL;
}

if (h->abi_ver != MAPI_ATHEME_V4)
{
snprintf(errbuf, BUFSIZE, "module_load(): \2%s\2: MAPI version mismatch (%u != %u), please recompile.", pathname, h->abi_ver, MAPI_ATHEME_V4);
snprintf(errbuf, errlen, "module_load(): \2%s\2: MAPI version mismatch (%u != %u), please recompile.", pathname, h->abi_ver, MAPI_ATHEME_V4);

mowgli_module_close(handle);
return NULL;
}

if (h->abi_rev != CURRENT_ABI_REVISION)
{
snprintf(errbuf, BUFSIZE, "module_load(): \2%s\2: ABI revision mismatch (%u != %u), please recompile.", pathname, h->abi_rev, CURRENT_ABI_REVISION);
snprintf(errbuf, errlen, "module_load(): \2%s\2: ABI revision mismatch (%u != %u), please recompile.", pathname, h->abi_rev, CURRENT_ABI_REVISION);

mowgli_module_close(handle);
return NULL;
}

if (module_find_published(h->name))
{
snprintf(errbuf, BUFSIZE, "module_load(): \2%s\2: Published name \2%s\2 already exists.", pathname, h->name);
snprintf(errbuf, errlen, "module_load(): \2%s\2: Published name \2%s\2 already exists.", pathname, h->name);

mowgli_module_close(handle);
return NULL;
Expand Down Expand Up @@ -206,7 +205,7 @@ static module_t *module_load_internal(const char *pathname, char *errbuf)

if (m->mflags & MODTYPE_FAIL)
{
snprintf(errbuf, BUFSIZE, "module_load(): module \2%s\2 init failed", pathname);
snprintf(errbuf, errlen, "module_load(): module \2%s\2 init failed", pathname);
module_unload(m, MODULE_UNLOAD_INTENT_PERM);
return NULL;
}
Expand Down

0 comments on commit c46368c

Please sign in to comment.