1998-08-09 13:42:02 +02:00
|
|
|
/* GMODULE - GLIB wrapper code for dynamic module loading
|
|
|
|
* Copyright (C) 1998 Tim Janik
|
1998-08-09 10:32:18 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
1998-08-10 03:36:18 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
1998-08-09 10:32:18 +02:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include "gmodule.h"
|
|
|
|
#include "gmoduleconf.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* We maintain a list of modules, so we can reference count them.
|
|
|
|
* That's needed because some platforms don't support refernce counts on
|
1998-08-10 03:36:18 +02:00
|
|
|
* modules e.g. the shl_* implementation of HP-UX
|
|
|
|
* (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
|
1998-08-09 10:32:18 +02:00
|
|
|
* Also, the module for the program itself is kept seperatedly for
|
1998-08-10 03:36:18 +02:00
|
|
|
* faster access and because it has special semantics.
|
1998-08-09 10:32:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* --- structures --- */
|
|
|
|
struct _GModule
|
|
|
|
{
|
|
|
|
gchar *file_name;
|
|
|
|
gpointer handle;
|
|
|
|
guint ref_count;
|
1998-08-09 13:39:50 +02:00
|
|
|
GModuleDeInit de_init;
|
1998-08-09 10:32:18 +02:00
|
|
|
GModule *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* --- prototypes --- */
|
|
|
|
static gpointer _g_module_open (const gchar *file_name,
|
|
|
|
gboolean bind_lazy);
|
1998-08-10 03:36:18 +02:00
|
|
|
static void _g_module_close (gpointer handle,
|
1998-08-09 10:32:18 +02:00
|
|
|
gboolean is_unref);
|
|
|
|
static gpointer _g_module_self (void);
|
1998-08-10 03:36:18 +02:00
|
|
|
static gpointer _g_module_symbol (gpointer handle,
|
1998-08-09 10:32:18 +02:00
|
|
|
const gchar *symbol_name);
|
|
|
|
static inline void g_module_set_error (const gchar *error);
|
|
|
|
static inline GModule* g_module_find_by_handle (gpointer handle);
|
|
|
|
static inline GModule* g_module_find_by_name (const gchar *name);
|
|
|
|
|
|
|
|
|
|
|
|
/* --- variables --- */
|
|
|
|
static GModule *modules = NULL;
|
|
|
|
static GModule *main_module = NULL;
|
|
|
|
static gchar *module_error = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* --- inline functions --- */
|
|
|
|
static inline GModule*
|
|
|
|
g_module_find_by_handle (gpointer handle)
|
|
|
|
{
|
|
|
|
GModule *module;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 15:13:12 +02:00
|
|
|
if (main_module && main_module->handle == handle)
|
|
|
|
return main_module;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
for (module = modules; module; module = module->next)
|
|
|
|
if (handle == module->handle)
|
|
|
|
return module;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GModule*
|
|
|
|
g_module_find_by_name (const gchar *name)
|
|
|
|
{
|
|
|
|
GModule *module;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
for (module = modules; module; module = module->next)
|
|
|
|
if (strcmp (name, module->file_name) == 0)
|
|
|
|
return module;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
g_module_set_error (const gchar *error)
|
|
|
|
{
|
|
|
|
if (module_error)
|
|
|
|
g_free (module_error);
|
|
|
|
if (error)
|
|
|
|
module_error = g_strdup (error);
|
|
|
|
else
|
|
|
|
module_error = NULL;
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- include platform specifc code --- */
|
|
|
|
#define CHECK_ERROR(rv) { g_module_set_error (NULL); }
|
|
|
|
#if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
|
|
|
|
#include "gmodule-dl.c"
|
|
|
|
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
|
|
|
|
#include "gmodule-dld.c"
|
|
|
|
#else
|
|
|
|
#undef CHECK_ERROR
|
|
|
|
#define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
|
|
|
|
#endif /* no implementation */
|
|
|
|
|
|
|
|
|
|
|
|
/* --- functions --- */
|
|
|
|
gboolean
|
|
|
|
g_module_supported (void)
|
|
|
|
{
|
|
|
|
CHECK_ERROR (FALSE);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GModule*
|
|
|
|
g_module_open (const gchar *file_name,
|
|
|
|
GModuleFlags flags)
|
|
|
|
{
|
|
|
|
GModule *module;
|
|
|
|
gpointer handle;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
CHECK_ERROR (NULL);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
if (!file_name)
|
|
|
|
{
|
|
|
|
if (!main_module)
|
|
|
|
{
|
|
|
|
handle = _g_module_self ();
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
main_module = g_new (GModule, 1);
|
|
|
|
main_module->file_name = NULL;
|
1998-08-09 13:39:50 +02:00
|
|
|
main_module->handle = handle;
|
1998-08-09 10:32:18 +02:00
|
|
|
main_module->ref_count = 1;
|
1998-08-09 13:39:50 +02:00
|
|
|
main_module->de_init = NULL;
|
1998-08-09 10:32:18 +02:00
|
|
|
main_module->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return main_module;
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
/* we first search the module list by name */
|
|
|
|
module = g_module_find_by_name (file_name);
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
module->ref_count++;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return module;
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
/* open the module */
|
|
|
|
handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
|
|
|
|
if (handle)
|
|
|
|
{
|
1998-08-09 13:39:50 +02:00
|
|
|
gchar *saved_error;
|
|
|
|
GModuleCheckInit check_init;
|
|
|
|
gboolean check_failed = FALSE;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
/* search the module list by handle, since file names are not unique */
|
|
|
|
module = g_module_find_by_handle (handle);
|
|
|
|
if (module)
|
|
|
|
{
|
1998-08-10 03:36:18 +02:00
|
|
|
_g_module_close (module->handle, TRUE);
|
1998-08-09 10:32:18 +02:00
|
|
|
module->ref_count++;
|
|
|
|
g_module_set_error (NULL);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return module;
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 13:39:50 +02:00
|
|
|
saved_error = module_error;
|
|
|
|
module_error = NULL;
|
|
|
|
g_module_set_error (NULL);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
module = g_new (GModule, 1);
|
|
|
|
module->file_name = g_strdup (file_name);
|
|
|
|
module->handle = handle;
|
1998-08-09 15:13:12 +02:00
|
|
|
module->ref_count = 1;
|
1998-08-09 13:39:50 +02:00
|
|
|
module->de_init = NULL;
|
1998-08-09 15:13:12 +02:00
|
|
|
module->next = modules;
|
|
|
|
modules = module;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 13:39:50 +02:00
|
|
|
/* check initialization */
|
1998-08-10 02:26:44 +02:00
|
|
|
if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
|
1998-08-09 13:39:50 +02:00
|
|
|
check_failed = check_init (module);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 15:13:12 +02:00
|
|
|
/* we don't call de_init() if the initialization check failed. */
|
1998-08-09 13:39:50 +02:00
|
|
|
if (!check_failed)
|
1998-08-10 02:26:44 +02:00
|
|
|
g_module_symbol (module, "g_module_de_init", (gpointer) &module->de_init);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 13:39:50 +02:00
|
|
|
if (check_failed)
|
|
|
|
{
|
|
|
|
g_module_close (module);
|
|
|
|
module = NULL;
|
|
|
|
g_module_set_error ("GModule initialization check failed");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_module_set_error (saved_error);
|
|
|
|
g_free (saved_error);
|
1998-08-09 10:32:18 +02:00
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 13:39:50 +02:00
|
|
|
return module;
|
1998-08-09 10:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
1998-08-10 03:36:18 +02:00
|
|
|
g_module_close (GModule *module)
|
1998-08-09 10:32:18 +02:00
|
|
|
{
|
|
|
|
CHECK_ERROR (FALSE);
|
1998-08-09 15:13:12 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
g_return_val_if_fail (module != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (module->ref_count > 0, FALSE);
|
1998-08-09 15:13:12 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
if (module != main_module)
|
|
|
|
module->ref_count--;
|
1998-08-09 15:13:12 +02:00
|
|
|
|
|
|
|
if (!module->ref_count && module->de_init)
|
|
|
|
module->de_init (module);
|
1998-08-09 10:32:18 +02:00
|
|
|
if (!module->ref_count)
|
|
|
|
{
|
|
|
|
GModule *last;
|
|
|
|
GModule *node;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
last = NULL;
|
|
|
|
node = modules;
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if (node == module)
|
|
|
|
{
|
|
|
|
if (last)
|
|
|
|
last->next = node->next;
|
|
|
|
else
|
|
|
|
modules = node->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = node;
|
|
|
|
node = last->next;
|
|
|
|
}
|
1998-08-09 13:39:50 +02:00
|
|
|
module->next = NULL;
|
1998-08-10 03:36:18 +02:00
|
|
|
|
|
|
|
_g_module_close (module->handle, FALSE);
|
1998-08-09 13:39:50 +02:00
|
|
|
g_free (module->file_name);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
g_free (module);
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return module_error == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
g_module_error (void)
|
|
|
|
{
|
|
|
|
return module_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
1998-08-10 03:36:18 +02:00
|
|
|
g_module_symbol (GModule *module,
|
|
|
|
const gchar *symbol_name,
|
|
|
|
gconstpointer *symbol)
|
1998-08-09 10:32:18 +02:00
|
|
|
{
|
|
|
|
if (symbol)
|
|
|
|
*symbol = NULL;
|
|
|
|
CHECK_ERROR (FALSE);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
g_return_val_if_fail (module != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (symbol_name != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (symbol != NULL, FALSE);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
#ifdef G_MODULE_NEED_USCORE
|
|
|
|
symbol_name = g_strconcat ("_", symbol_name, NULL);
|
1998-08-10 03:36:18 +02:00
|
|
|
*symbol = _g_module_symbol (module->handle, symbol_name);
|
1998-08-09 10:32:18 +02:00
|
|
|
g_free (symbol_name);
|
|
|
|
#else /* !G_MODULE_NEED_USCORE */
|
1998-08-10 03:36:18 +02:00
|
|
|
*symbol = _g_module_symbol (module->handle, symbol_name);
|
1998-08-09 10:32:18 +02:00
|
|
|
#endif /* !G_MODULE_NEED_USCORE */
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
if (module_error)
|
|
|
|
{
|
|
|
|
*symbol = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
g_module_name (GModule *module)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (module != NULL, NULL);
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
if (module == main_module)
|
|
|
|
return "main";
|
1998-08-10 03:36:18 +02:00
|
|
|
|
1998-08-09 10:32:18 +02:00
|
|
|
return module->file_name;
|
|
|
|
}
|