summaryrefslogtreecommitdiff
path: root/main/loader.c
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-08-30 13:40:27 +0000
committerDavid M. Lee <dlee@digium.com>2013-08-30 13:40:27 +0000
commit9bed50db41690339ca64bdcc6d6bc1c6edfebe4a (patch)
tree7316204731ab8415ce3ada9a51589c65dacdff58 /main/loader.c
parent7f547872e41ee53ad85a5b0820a8c0ff7c7f1c04 (diff)
optional_api: Fix linking problems between modules that export global symbols
With the new work in Asterisk 12, there are some uses of the optional_api that are prone to failure. The details are rather involved, and captured on [the wiki][1]. This patch addresses the issue by removing almost all of the magic from the optional API implementation. Instead of relying on weak symbol resolution, a new optional_api.c module was added to Asterisk core. For modules providing an optional API, the pointer to the implementation function is registered with the core. For modules that use an optional API, a pointer to a stub function, along with a optional_ref function pointer are registered with the core. The optional_ref function pointers is set to the implementation function when it's provided, or the stub function when it's now. Since the implementation no longer relies on magic, it is now supported on all platforms. In the spirit of choice, an OPTIONAL_API flag was added, so we can disable the optional_api if needed (maybe it's buggy on some bizarre platform I haven't tested on) The AST_OPTIONAL_API*() macros themselves remained unchanged, so existing code could remain unchanged. But to help with debugging the optional_api, the patch limits the #include of optional API's to just the modules using the API. This also reduces resource waste maintaining optional_ref pointers that aren't used. Other changes made as a part of this patch: * The stubs for http_websocket that wrap system calls set errno to ENOSYS. * res_http_websocket now properly increments module use count. * In loader.c, the while() wrappers around dlclose() were removed. The while(!dlclose()) is actually an anti-pattern, which can lead to infinite loops if the module you're attempting to unload exports a symbol that was directly linked to. * The special handling of nonoptreq on systems without weak symbol support was removed, since we no longer rely on weak symbols for optional_api. [1]: https://wiki.asterisk.org/wiki/x/wACUAQ (closes issue ASTERISK-22296) Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/2797/ ........ Merged revisions 397989 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@397990 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/loader.c')
-rw-r--r--main/loader.c102
1 files changed, 76 insertions, 26 deletions
diff --git a/main/loader.c b/main/loader.c
index ddfbffe9f..4065043aa 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -185,6 +185,8 @@ void ast_module_register(const struct ast_module_info *info)
mod = resource_being_loaded;
}
+ ast_verb(5, "Registering module %s\n", info->name);
+
mod->info = info;
AST_LIST_HEAD_INIT(&mod->users);
@@ -230,6 +232,7 @@ void ast_module_unregister(const struct ast_module_info *info)
AST_LIST_UNLOCK(&module_list);
if (mod) {
+ ast_verb(5, "Unregistering module %s\n", info->name);
AST_LIST_HEAD_DESTROY(&mod->users);
ast_free(mod);
}
@@ -403,16 +406,83 @@ static struct ast_module *find_resource(const char *resource, int do_lock)
}
#ifdef LOADABLE_MODULES
+
+/*!
+ * \brief dlclose(), with failure logging.
+ */
+static void logged_dlclose(const char *name, void *lib)
+{
+ if (dlclose(lib) != 0) {
+ ast_log(LOG_WARNING, "Failed to unload %s: %s\n",
+ name, dlerror());
+ }
+}
+
+#if defined(HAVE_RTLD_NOLOAD)
+/*!
+ * \brief Check to see if the given resource is loaded.
+ *
+ * \param resource_name Name of the resource, including .so suffix.
+ * \return False (0) if module is not loaded.
+ * \return True (non-zero) if module is loaded.
+ */
+static int is_module_loaded(const char *resource_name)
+{
+ char fn[PATH_MAX] = "";
+ void *lib;
+
+ ast_verb(10, "Checking if %s is loaded\n", resource_name);
+
+ snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_MODULE_DIR,
+ resource_name);
+
+ lib = dlopen(fn, RTLD_LAZY | RTLD_NOLOAD);
+
+ if (lib) {
+ ast_verb(10, " %s loaded\n", resource_name);
+ logged_dlclose(resource_name, lib);
+ return 1;
+ }
+
+ ast_verb(10, " %s not loaded\n", resource_name);
+ return 0;
+}
+#endif
+
static void unload_dynamic_module(struct ast_module *mod)
{
+ char *name = ast_strdupa(ast_module_name(mod));
void *lib = mod->lib;
/* WARNING: the structure pointed to by mod is going to
disappear when this operation succeeds, so we can't
dereference it */
- if (lib)
- while (!dlclose(lib));
+ if (!lib) {
+ return;
+ }
+
+ logged_dlclose(name, lib);
+
+ /* There are several situations where the module might still be resident
+ * in memory.
+ *
+ * If somehow there was another dlopen() on the same module (unlikely,
+ * since that all is supposed to happen in loader.c).
+ *
+ * Or the lazy resolution of a global symbol (very likely, since that is
+ * how we load all of our modules that export global symbols).
+ *
+ * Avoid the temptation of repeating the dlclose(). The other code that
+ * dlopened the module still has its module reference, and should close
+ * it itself. In other situations, dlclose() will happily return success
+ * for as many times as you wish to call it.
+ */
+#if defined(HAVE_RTLD_NOLOAD)
+ if (is_module_loaded(name)) {
+ ast_log(LOG_WARNING, "Module '%s' could not be completely unloaded\n", name);
+ }
+#endif
}
static enum ast_module_load_result load_resource(const char *resource_name, unsigned int global_symbols_only, struct ast_heap *resource_heap, int required);
@@ -461,7 +531,7 @@ static struct ast_module *load_dynamic_module(const char *resource_in, unsigned
if (resource_being_loaded != (mod = AST_LIST_LAST(&module_list))) {
ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);
/* no, it did not, so close it and return */
- while (!dlclose(lib));
+ logged_dlclose(resource_in, lib);
/* note that the module's destructor will call ast_module_unregister(),
which will free the structure we allocated in resource_being_loaded */
return NULL;
@@ -472,32 +542,11 @@ static struct ast_module *load_dynamic_module(const char *resource_in, unsigned
/* if we are being asked only to load modules that provide global symbols,
and this one does not, then close it and return */
if (global_symbols_only && !wants_global) {
- while (!dlclose(lib));
+ logged_dlclose(resource_in, lib);
return NULL;
}
- /* This section is a workaround for a gcc 4.1 bug that has already been
- * fixed in later versions. Unfortunately, some distributions, such as
- * RHEL/CentOS 5, distribute gcc 4.1, so we're stuck with having to deal
- * with this issue. This basically ensures that optional_api modules are
- * loaded before any module which requires their functionality. */
-#if !defined(HAVE_ATTRIBUTE_weak_import) && !defined(HAVE_ATTRIBUTE_weakref)
- if (!ast_strlen_zero(mod->info->nonoptreq)) {
- /* Force any required dependencies to load */
- char *each, *required_resource = ast_strdupa(mod->info->nonoptreq);
- while ((each = strsep(&required_resource, ","))) {
- struct ast_module *dependency;
- each = ast_strip(each);
- dependency = find_resource(each, 0);
- /* Is it already loaded? */
- if (!dependency) {
- load_resource(each, global_symbols_only, resource_heap, 1);
- }
- }
- }
-#endif
-
- while (!dlclose(lib));
+ logged_dlclose(resource_in, lib);
resource_being_loaded = NULL;
/* start the load process again */
@@ -523,6 +572,7 @@ static struct ast_module *load_dynamic_module(const char *resource_in, unsigned
return AST_LIST_LAST(&module_list);
}
+
#endif
void ast_module_shutdown(void)