Blame gmodule/gmodule.c

Packit ae235b
/* GMODULE - GLIB wrapper code for dynamic module loading
Packit ae235b
 * Copyright (C) 1998 Tim Janik
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* 
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
#include "gmodule.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>		/* For open() and close() prototypes. */
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gmoduleconf.h"
Packit ae235b
#include "gstdio.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:modules
Packit ae235b
 * @title: Dynamic Loading of Modules
Packit ae235b
 * @short_description: portable method for dynamically loading 'plug-ins'
Packit ae235b
 *
Packit ae235b
 * These functions provide a portable way to dynamically load object files
Packit ae235b
 * (commonly known as 'plug-ins'). The current implementation supports all
Packit ae235b
 * systems that provide an implementation of dlopen() (e.g. Linux/Sun), as
Packit ae235b
 * well as Windows platforms via DLLs.
Packit ae235b
 *
Packit ae235b
 * A program which wants to use these functions must be linked to the
Packit ae235b
 * libraries output by the command `pkg-config --libs gmodule-2.0`.
Packit ae235b
 *
Packit ae235b
 * To use them you must first determine whether dynamic loading
Packit ae235b
 * is supported on the platform by calling g_module_supported().
Packit ae235b
 * If it is, you can open a module with g_module_open(),
Packit ae235b
 * find the module's symbols (e.g. function names) with g_module_symbol(),
Packit ae235b
 * and later close the module with g_module_close().
Packit ae235b
 * g_module_name() will return the file name of a currently opened module.
Packit ae235b
 *
Packit ae235b
 * If any of the above functions fail, the error status can be found with
Packit ae235b
 * g_module_error().
Packit ae235b
 *
Packit ae235b
 * The #GModule implementation features reference counting for opened modules,
Packit ae235b
 * and supports hook functions within a module which are called when the
Packit ae235b
 * module is loaded and unloaded (see #GModuleCheckInit and #GModuleUnload).
Packit ae235b
 *
Packit ae235b
 * If your module introduces static data to common subsystems in the running
Packit ae235b
 * program, e.g. through calling
Packit ae235b
 * `g_quark_from_static_string ("my-module-stuff")`,
Packit ae235b
 * it must ensure that it is never unloaded, by calling g_module_make_resident().
Packit ae235b
 *
Packit ae235b
 * Example: Calling a function defined in a GModule
Packit ae235b
 * |[ 
Packit ae235b
 * // the function signature for 'say_hello'
Packit ae235b
 * typedef void (* SayHelloFunc) (const char *message);
Packit ae235b
 *
Packit ae235b
 * gboolean
Packit ae235b
 * just_say_hello (const char *filename, GError **error)
Packit ae235b
 * {
Packit ae235b
 *   SayHelloFunc  say_hello;
Packit ae235b
 *   GModule      *module;
Packit ae235b
 *
Packit ae235b
 *   module = g_module_open (filename, G_MODULE_BIND_LAZY);
Packit ae235b
 *   if (!module)
Packit ae235b
 *     {
Packit ae235b
 *       g_set_error (error, FOO_ERROR, FOO_ERROR_BLAH,
Packit ae235b
 *                    "%s", g_module_error ());
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   if (!g_module_symbol (module, "say_hello", (gpointer *)&say_hello))
Packit ae235b
 *     {
Packit ae235b
 *       g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN,
Packit ae235b
 *                    "%s: %s", filename, g_module_error ());
Packit ae235b
 *       if (!g_module_close (module))
Packit ae235b
 *         g_warning ("%s: %s", filename, g_module_error ());
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   if (say_hello == NULL)
Packit ae235b
 *     {
Packit ae235b
 *       g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN,
Packit ae235b
 *                    "symbol say_hello is NULL");
Packit ae235b
 *       if (!g_module_close (module))
Packit ae235b
 *         g_warning ("%s: %s", filename, g_module_error ());
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   // call our function in the module
Packit ae235b
 *   say_hello ("Hello world!");
Packit ae235b
 *
Packit ae235b
 *   if (!g_module_close (module))
Packit ae235b
 *     g_warning ("%s: %s", filename, g_module_error ());
Packit ae235b
 *   return TRUE;
Packit ae235b
 *  }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GModule:
Packit ae235b
 *
Packit ae235b
 * The #GModule struct is an opaque data structure to represent a
Packit ae235b
 * [dynamically-loaded module][glib-Dynamic-Loading-of-Modules].
Packit ae235b
 * It should only be accessed via the following functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GModuleCheckInit:
Packit ae235b
 * @module: the #GModule corresponding to the module which has just been loaded
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the module initialization function.
Packit ae235b
 * If a module contains a function named g_module_check_init() it is called
Packit ae235b
 * automatically when the module is loaded. It is passed the #GModule structure
Packit ae235b
 * and should return %NULL on success or a string describing the initialization
Packit ae235b
 * error.
Packit ae235b
 *
Packit ae235b
 * Returns: %NULL on success, or a string describing the initialization error
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GModuleUnload:
Packit ae235b
 * @module: the #GModule about to be unloaded
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the module function called when it is unloaded.
Packit ae235b
 * If a module contains a function named g_module_unload() it is called
Packit ae235b
 * automatically when the module is unloaded.
Packit ae235b
 * It is passed the #GModule structure.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_MODULE_SUFFIX:
Packit ae235b
 *
Packit ae235b
 * Expands to the proper shared library suffix for the current platform
Packit ae235b
 * without the leading dot. For most Unices and Linux this is "so", and
Packit ae235b
 * for Windows this is "dll".
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_MODULE_EXPORT:
Packit ae235b
 *
Packit ae235b
 * Used to declare functions exported by libraries or modules.
Packit ae235b
 *
Packit ae235b
 * When compiling for Windows, it marks the symbol as `dllexport`.
Packit ae235b
 *
Packit ae235b
 * When compiling for Linux and Unices, it marks the symbol as having `default`
Packit ae235b
 * visibility. This is no-op unless the code is being compiled with a
Packit ae235b
 * non-default
Packit ae235b
 * [visibility flag](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility-1260)
Packit ae235b
 * such as `hidden`.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_MODULE_IMPORT:
Packit ae235b
 *
Packit ae235b
 * Used to declare functions imported from modules.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* We maintain a list of modules, so we can reference count them.
Packit ae235b
 * That's needed because some platforms don't support references counts on
Packit ae235b
 * modules. Also, the module for the program itself is kept seperately for
Packit ae235b
 * faster access and because it has special semantics.
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
/* --- structures --- */
Packit ae235b
struct _GModule
Packit ae235b
{
Packit ae235b
  gchar	*file_name;
Packit ae235b
  gpointer handle;
Packit ae235b
  guint ref_count : 31;
Packit ae235b
  guint is_resident : 1;
Packit ae235b
  GModuleUnload unload;
Packit ae235b
  GModule *next;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
/* --- prototypes --- */
Packit ae235b
static gpointer		_g_module_open		(const gchar	*file_name,
Packit ae235b
						 gboolean	 bind_lazy,
Packit ae235b
						 gboolean	 bind_local);
Packit ae235b
static void		_g_module_close		(gpointer	 handle,
Packit ae235b
						 gboolean	 is_unref);
Packit ae235b
static gpointer		_g_module_self		(void);
Packit ae235b
static gpointer		_g_module_symbol	(gpointer	 handle,
Packit ae235b
						 const gchar	*symbol_name);
Packit ae235b
static gchar*		_g_module_build_path	(const gchar	*directory,
Packit ae235b
						 const gchar	*module_name);
Packit ae235b
static inline void	g_module_set_error	(const gchar	*error);
Packit ae235b
static inline GModule*	g_module_find_by_handle (gpointer	 handle);
Packit ae235b
static inline GModule*	g_module_find_by_name	(const gchar	*name);
Packit ae235b
Packit ae235b
Packit ae235b
/* --- variables --- */
Packit ae235b
static GModule	     *modules = NULL;
Packit ae235b
static GModule	     *main_module = NULL;
Packit ae235b
static GPrivate       module_error_private = G_PRIVATE_INIT (g_free);
Packit ae235b
static gboolean	      module_debug_initialized = FALSE;
Packit ae235b
static guint	      module_debug_flags = 0;
Packit ae235b
Packit ae235b
Packit ae235b
/* --- inline functions --- */
Packit ae235b
static inline GModule*
Packit ae235b
g_module_find_by_handle (gpointer handle)
Packit ae235b
{
Packit ae235b
  GModule *module;
Packit ae235b
  GModule *retval = NULL;
Packit ae235b
  
Packit ae235b
  if (main_module && main_module->handle == handle)
Packit ae235b
    retval = main_module;
Packit ae235b
  else
Packit ae235b
    for (module = modules; module; module = module->next)
Packit ae235b
      if (handle == module->handle)
Packit ae235b
	{
Packit ae235b
	  retval = module;
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline GModule*
Packit ae235b
g_module_find_by_name (const gchar *name)
Packit ae235b
{
Packit ae235b
  GModule *module;
Packit ae235b
  GModule *retval = NULL;
Packit ae235b
  
Packit ae235b
  for (module = modules; module; module = module->next)
Packit ae235b
    if (strcmp (name, module->file_name) == 0)
Packit ae235b
	{
Packit ae235b
	  retval = module;
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_module_set_error_unduped (gchar *error)
Packit ae235b
{
Packit ae235b
  g_private_replace (&module_error_private, error);
Packit ae235b
  errno = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
g_module_set_error (const gchar *error)
Packit ae235b
{
Packit ae235b
  g_module_set_error_unduped (g_strdup (error));
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* --- include platform specifc code --- */
Packit ae235b
#define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error (NULL); }
Packit ae235b
#if	(G_MODULE_IMPL == G_MODULE_IMPL_DL)
Packit ae235b
#include "gmodule-dl.c"
Packit ae235b
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
Packit ae235b
#include "gmodule-win32.c"
Packit ae235b
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_DYLD)
Packit ae235b
#include "gmodule-dyld.c"
Packit ae235b
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_AR)
Packit ae235b
#include "gmodule-ar.c"
Packit ae235b
#else
Packit ae235b
#undef	SUPPORT_OR_RETURN
Packit ae235b
#define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error ("dynamic modules are " \
Packit ae235b
                                              "not supported by this system"); return rv; }
Packit ae235b
static gpointer
Packit ae235b
_g_module_open (const gchar	*file_name,
Packit ae235b
		gboolean	 bind_lazy,
Packit ae235b
		gboolean	 bind_local)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
static void
Packit ae235b
_g_module_close	(gpointer	 handle,
Packit ae235b
		 gboolean	 is_unref)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
static gpointer
Packit ae235b
_g_module_self (void)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
static gpointer
Packit ae235b
_g_module_symbol (gpointer	 handle,
Packit ae235b
		  const gchar	*symbol_name)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
static gchar*
Packit ae235b
_g_module_build_path (const gchar *directory,
Packit ae235b
		      const gchar *module_name)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
#endif	/* no implementation */
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_supported:
Packit ae235b
 *
Packit ae235b
 * Checks if modules are supported on the current platform.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if modules are supported
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_module_supported (void)
Packit ae235b
{
Packit ae235b
  SUPPORT_OR_RETURN (FALSE);
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
parse_libtool_archive (const gchar* libtool_name)
Packit ae235b
{
Packit ae235b
  const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
Packit ae235b
  const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
Packit ae235b
  const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
Packit ae235b
  gchar *lt_dlname = NULL;
Packit ae235b
  gboolean lt_installed = TRUE;
Packit ae235b
  gchar *lt_libdir = NULL;
Packit ae235b
  gchar *name;
Packit ae235b
  GTokenType token;
Packit ae235b
  GScanner *scanner;
Packit ae235b
  
Packit ae235b
  int fd = g_open (libtool_name, O_RDONLY, 0);
Packit ae235b
  if (fd < 0)
Packit ae235b
    {
Packit ae235b
      gchar *display_libtool_name = g_filename_display_name (libtool_name);
Packit ae235b
      g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", display_libtool_name));
Packit ae235b
      g_free (display_libtool_name);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  /* search libtool's dlname specification  */
Packit ae235b
  scanner = g_scanner_new (NULL);
Packit ae235b
  g_scanner_input_file (scanner, fd);
Packit ae235b
  scanner->config->symbol_2_token = TRUE;
Packit ae235b
  g_scanner_scope_add_symbol (scanner, 0, "dlname", 
Packit ae235b
			      GUINT_TO_POINTER (TOKEN_DLNAME));
Packit ae235b
  g_scanner_scope_add_symbol (scanner, 0, "installed", 
Packit ae235b
			      GUINT_TO_POINTER (TOKEN_INSTALLED));
Packit ae235b
  g_scanner_scope_add_symbol (scanner, 0, "libdir", 
Packit ae235b
			      GUINT_TO_POINTER (TOKEN_LIBDIR));
Packit ae235b
  while (!g_scanner_eof (scanner))
Packit ae235b
    {
Packit ae235b
      token = g_scanner_get_next_token (scanner);
Packit ae235b
      if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
Packit ae235b
	  token == TOKEN_LIBDIR)
Packit ae235b
	{
Packit ae235b
	  if (g_scanner_get_next_token (scanner) != '=' ||
Packit ae235b
	      g_scanner_get_next_token (scanner) != 
Packit ae235b
	      (token == TOKEN_INSTALLED ? 
Packit ae235b
	       G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
Packit ae235b
	    {
Packit ae235b
	      gchar *display_libtool_name = g_filename_display_name (libtool_name);
Packit ae235b
	      g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", display_libtool_name));
Packit ae235b
	      g_free (display_libtool_name);
Packit ae235b
Packit ae235b
	      g_free (lt_dlname);
Packit ae235b
	      g_free (lt_libdir);
Packit ae235b
	      g_scanner_destroy (scanner);
Packit ae235b
	      close (fd);
Packit ae235b
Packit ae235b
	      return NULL;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      if (token == TOKEN_DLNAME)
Packit ae235b
		{
Packit ae235b
		  g_free (lt_dlname);
Packit ae235b
		  lt_dlname = g_strdup (scanner->value.v_string);
Packit ae235b
		}
Packit ae235b
	      else if (token == TOKEN_INSTALLED)
Packit ae235b
		lt_installed = 
Packit ae235b
		  strcmp (scanner->value.v_identifier, "yes") == 0;
Packit ae235b
	      else /* token == TOKEN_LIBDIR */
Packit ae235b
		{
Packit ae235b
		  g_free (lt_libdir);
Packit ae235b
		  lt_libdir = g_strdup (scanner->value.v_string);
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}      
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!lt_installed)
Packit ae235b
    {
Packit ae235b
      gchar *dir = g_path_get_dirname (libtool_name);
Packit ae235b
      g_free (lt_libdir);
Packit ae235b
      lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
Packit ae235b
      g_free (dir);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
Packit ae235b
  
Packit ae235b
  g_free (lt_dlname);
Packit ae235b
  g_free (lt_libdir);
Packit ae235b
  g_scanner_destroy (scanner);
Packit ae235b
  close (fd);
Packit ae235b
Packit ae235b
  return name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline gboolean
Packit ae235b
str_check_suffix (const gchar* string,
Packit ae235b
		  const gchar* suffix)
Packit ae235b
{
Packit ae235b
  gsize string_len = strlen (string);    
Packit ae235b
  gsize suffix_len = strlen (suffix);    
Packit ae235b
Packit ae235b
  return string_len >= suffix_len && 
Packit ae235b
    strcmp (string + string_len - suffix_len, suffix) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0,
Packit ae235b
  G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
_g_module_debug_init (void)
Packit ae235b
{
Packit ae235b
  const GDebugKey keys[] = {
Packit ae235b
    { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
Packit ae235b
    { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
Packit ae235b
  };
Packit ae235b
  const gchar *env;
Packit ae235b
Packit ae235b
  env = g_getenv ("G_DEBUG");
Packit ae235b
Packit ae235b
  module_debug_flags =
Packit ae235b
    !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
Packit ae235b
Packit ae235b
  module_debug_initialized = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GRecMutex g_module_global_lock;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_open:
Packit ae235b
 * @file_name: (nullable): the name of the file containing the module, or %NULL
Packit ae235b
 *     to obtain a #GModule representing the main program itself
Packit ae235b
 * @flags: the flags used for opening the module. This can be the
Packit ae235b
 *     logical OR of any of the #GModuleFlags
Packit ae235b
 *
Packit ae235b
 * Opens a module. If the module has already been opened,
Packit ae235b
 * its reference count is incremented.
Packit ae235b
 *
Packit ae235b
 * First of all g_module_open() tries to open @file_name as a module.
Packit ae235b
 * If that fails and @file_name has the ".la"-suffix (and is a libtool
Packit ae235b
 * archive) it tries to open the corresponding module. If that fails
Packit ae235b
 * and it doesn't have the proper module suffix for the platform
Packit ae235b
 * (#G_MODULE_SUFFIX), this suffix will be appended and the corresponding
Packit ae235b
 * module will be opended. If that fails and @file_name doesn't have the
Packit ae235b
 * ".la"-suffix, this suffix is appended and g_module_open() tries to open
Packit ae235b
 * the corresponding module. If eventually that fails as well, %NULL is
Packit ae235b
 * returned.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GModule on success, or %NULL on failure
Packit ae235b
 */
Packit ae235b
GModule*
Packit ae235b
g_module_open (const gchar    *file_name,
Packit ae235b
	       GModuleFlags    flags)
Packit ae235b
{
Packit ae235b
  GModule *module;
Packit ae235b
  gpointer handle = NULL;
Packit ae235b
  gchar *name = NULL;
Packit ae235b
  
Packit ae235b
  SUPPORT_OR_RETURN (NULL);
Packit ae235b
  
Packit ae235b
  g_rec_mutex_lock (&g_module_global_lock);
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (!module_debug_initialized))
Packit ae235b
    _g_module_debug_init ();
Packit ae235b
Packit ae235b
  if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
Packit ae235b
    flags &= ~G_MODULE_BIND_LAZY;
Packit ae235b
Packit ae235b
  if (!file_name)
Packit ae235b
    {      
Packit ae235b
      if (!main_module)
Packit ae235b
	{
Packit ae235b
	  handle = _g_module_self ();
Packit ae235b
/* On Android 64 bit, RTLD_DEFAULT is (void *)0x0
Packit ae235b
 * so it always fails to create main_module if file_name is NULL */
Packit ae235b
#if !defined(__BIONIC__) || !defined(__LP64__)
Packit ae235b
	  if (handle)
Packit ae235b
#endif
Packit ae235b
	    {
Packit ae235b
	      main_module = g_new (GModule, 1);
Packit ae235b
	      main_module->file_name = NULL;
Packit ae235b
	      main_module->handle = handle;
Packit ae235b
	      main_module->ref_count = 1;
Packit ae235b
	      main_module->is_resident = TRUE;
Packit ae235b
	      main_module->unload = NULL;
Packit ae235b
	      main_module->next = NULL;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	main_module->ref_count++;
Packit ae235b
Packit ae235b
      g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
      return main_module;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* we first search the module list by name */
Packit ae235b
  module = g_module_find_by_name (file_name);
Packit ae235b
  if (module)
Packit ae235b
    {
Packit ae235b
      module->ref_count++;
Packit ae235b
      
Packit ae235b
      g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
      return module;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* check whether we have a readable file right away */
Packit ae235b
  if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
Packit ae235b
    name = g_strdup (file_name);
Packit ae235b
  /* try completing file name with standard library suffix */
Packit ae235b
  if (!name)
Packit ae235b
    {
Packit ae235b
      name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
Packit ae235b
      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
Packit ae235b
	{
Packit ae235b
	  g_free (name);
Packit ae235b
	  name = NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  /* try completing by appending libtool suffix */
Packit ae235b
  if (!name)
Packit ae235b
    {
Packit ae235b
      name = g_strconcat (file_name, ".la", NULL);
Packit ae235b
      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
Packit ae235b
	{
Packit ae235b
	  g_free (name);
Packit ae235b
	  name = NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  /* we can't access() the file, lets hope the platform backends finds
Packit ae235b
   * it via library paths
Packit ae235b
   */
Packit ae235b
  if (!name)
Packit ae235b
    {
Packit ae235b
      gchar *dot = strrchr (file_name, '.');
Packit ae235b
      gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
Packit ae235b
      
Packit ae235b
      /* make sure the name has a suffix */
Packit ae235b
      if (!dot || dot < slash)
Packit ae235b
	name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
Packit ae235b
      else
Packit ae235b
	name = g_strdup (file_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* ok, try loading the module */
Packit ae235b
  if (name)
Packit ae235b
    {
Packit ae235b
      /* if it's a libtool archive, figure library file to load */
Packit ae235b
      if (str_check_suffix (name, ".la")) /* libtool archive? */
Packit ae235b
	{
Packit ae235b
	  gchar *real_name = parse_libtool_archive (name);
Packit ae235b
Packit ae235b
	  /* real_name might be NULL, but then module error is already set */
Packit ae235b
	  if (real_name)
Packit ae235b
	    {
Packit ae235b
	      g_free (name);
Packit ae235b
	      name = real_name;
Packit ae235b
            }
Packit ae235b
	}
Packit ae235b
      if (name)
Packit ae235b
	handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
Packit ae235b
			(flags & G_MODULE_BIND_LOCAL) != 0);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gchar *display_file_name = g_filename_display_name (file_name);
Packit ae235b
      g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", display_file_name));
Packit ae235b
      g_free (display_file_name);
Packit ae235b
    }
Packit ae235b
  g_free (name);
Packit ae235b
Packit ae235b
  if (handle)
Packit ae235b
    {
Packit ae235b
      gchar *saved_error;
Packit ae235b
      GModuleCheckInit check_init;
Packit ae235b
      const gchar *check_failed = NULL;
Packit ae235b
      
Packit ae235b
      /* search the module list by handle, since file names are not unique */
Packit ae235b
      module = g_module_find_by_handle (handle);
Packit ae235b
      if (module)
Packit ae235b
	{
Packit ae235b
	  _g_module_close (module->handle, TRUE);
Packit ae235b
	  module->ref_count++;
Packit ae235b
	  g_module_set_error (NULL);
Packit ae235b
	  
Packit ae235b
	  g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
	  return module;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      saved_error = g_strdup (g_module_error ());
Packit ae235b
      g_module_set_error (NULL);
Packit ae235b
      
Packit ae235b
      module = g_new (GModule, 1);
Packit ae235b
      module->file_name = g_strdup (file_name);
Packit ae235b
      module->handle = handle;
Packit ae235b
      module->ref_count = 1;
Packit ae235b
      module->is_resident = FALSE;
Packit ae235b
      module->unload = NULL;
Packit ae235b
      module->next = modules;
Packit ae235b
      modules = module;
Packit ae235b
      
Packit ae235b
      /* check initialization */
Packit ae235b
      if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
Packit ae235b
	check_failed = check_init (module);
Packit ae235b
      
Packit ae235b
      /* we don't call unload() if the initialization check failed. */
Packit ae235b
      if (!check_failed)
Packit ae235b
	g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
Packit ae235b
      
Packit ae235b
      if (check_failed)
Packit ae235b
	{
Packit ae235b
	  gchar *error;
Packit ae235b
Packit ae235b
	  error = g_strconcat ("GModule (", file_name, ") ",
Packit ae235b
                               "initialization check failed: ",
Packit ae235b
                               check_failed, NULL);
Packit ae235b
	  g_module_close (module);
Packit ae235b
	  module = NULL;
Packit ae235b
	  g_module_set_error (error);
Packit ae235b
	  g_free (error);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	g_module_set_error (saved_error);
Packit ae235b
Packit ae235b
      g_free (saved_error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (module != NULL &&
Packit ae235b
      (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
Packit ae235b
    g_module_make_resident (module);
Packit ae235b
Packit ae235b
  g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
  return module;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_close:
Packit ae235b
 * @module: a #GModule to close
Packit ae235b
 *
Packit ae235b
 * Closes a module.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_module_close (GModule *module)
Packit ae235b
{
Packit ae235b
  SUPPORT_OR_RETURN (FALSE);
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (module != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (module->ref_count > 0, FALSE);
Packit ae235b
  
Packit ae235b
  g_rec_mutex_lock (&g_module_global_lock);
Packit ae235b
Packit ae235b
  module->ref_count--;
Packit ae235b
  
Packit ae235b
  if (!module->ref_count && !module->is_resident && module->unload)
Packit ae235b
    {
Packit ae235b
      GModuleUnload unload;
Packit ae235b
Packit ae235b
      unload = module->unload;
Packit ae235b
      module->unload = NULL;
Packit ae235b
      unload (module);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!module->ref_count && !module->is_resident)
Packit ae235b
    {
Packit ae235b
      GModule *last;
Packit ae235b
      GModule *node;
Packit ae235b
      
Packit ae235b
      last = NULL;
Packit ae235b
      
Packit ae235b
      node = modules;
Packit ae235b
      while (node)
Packit ae235b
	{
Packit ae235b
	  if (node == module)
Packit ae235b
	    {
Packit ae235b
	      if (last)
Packit ae235b
		last->next = node->next;
Packit ae235b
	      else
Packit ae235b
		modules = node->next;
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
	  last = node;
Packit ae235b
	  node = last->next;
Packit ae235b
	}
Packit ae235b
      module->next = NULL;
Packit ae235b
      
Packit ae235b
      _g_module_close (module->handle, FALSE);
Packit ae235b
      g_free (module->file_name);
Packit ae235b
      g_free (module);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
  return g_module_error() == NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_make_resident:
Packit ae235b
 * @module: a #GModule to make permanently resident
Packit ae235b
 *
Packit ae235b
 * Ensures that a module will never be unloaded.
Packit ae235b
 * Any future g_module_close() calls on the module will be ignored.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_module_make_resident (GModule *module)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (module != NULL);
Packit ae235b
Packit ae235b
  module->is_resident = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_error:
Packit ae235b
 *
Packit ae235b
 * Gets a string describing the last module error.
Packit ae235b
 *
Packit ae235b
 * Returns: a string describing the last module error
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_module_error (void)
Packit ae235b
{
Packit ae235b
  return g_private_get (&module_error_private);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_symbol:
Packit ae235b
 * @module: a #GModule
Packit ae235b
 * @symbol_name: the name of the symbol to find
Packit ae235b
 * @symbol: (out): returns the pointer to the symbol value
Packit ae235b
 *
Packit ae235b
 * Gets a symbol pointer from a module, such as one exported
Packit ae235b
 * by #G_MODULE_EXPORT. Note that a valid symbol can be %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_module_symbol (GModule     *module,
Packit ae235b
                 const gchar *symbol_name,
Packit ae235b
                 gpointer    *symbol)
Packit ae235b
{
Packit ae235b
  const gchar *module_error;
Packit ae235b
Packit ae235b
  if (symbol)
Packit ae235b
    *symbol = NULL;
Packit ae235b
  SUPPORT_OR_RETURN (FALSE);
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (module != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (symbol_name != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (symbol != NULL, FALSE);
Packit ae235b
  
Packit ae235b
  g_rec_mutex_lock (&g_module_global_lock);
Packit ae235b
Packit ae235b
#ifdef	G_MODULE_NEED_USCORE
Packit ae235b
  {
Packit ae235b
    gchar *name;
Packit ae235b
Packit ae235b
    name = g_strconcat ("_", symbol_name, NULL);
Packit ae235b
    *symbol = _g_module_symbol (module->handle, name);
Packit ae235b
    g_free (name);
Packit ae235b
  }
Packit ae235b
#else	/* !G_MODULE_NEED_USCORE */
Packit ae235b
  *symbol = _g_module_symbol (module->handle, symbol_name);
Packit ae235b
#endif	/* !G_MODULE_NEED_USCORE */
Packit ae235b
  
Packit ae235b
  module_error = g_module_error ();
Packit ae235b
  if (module_error)
Packit ae235b
    {
Packit ae235b
      gchar *error;
Packit ae235b
Packit ae235b
      error = g_strconcat ("'", symbol_name, "': ", module_error, NULL);
Packit ae235b
      g_module_set_error (error);
Packit ae235b
      g_free (error);
Packit ae235b
      *symbol = NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_rec_mutex_unlock (&g_module_global_lock);
Packit ae235b
  return !module_error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_name:
Packit ae235b
 * @module: a #GModule
Packit ae235b
 *
Packit ae235b
 * Returns the filename that the module was opened with.
Packit ae235b
 *
Packit ae235b
 * If @module refers to the application itself, "main" is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the filename of the module
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_module_name (GModule *module)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (module != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (module == main_module)
Packit ae235b
    return "main";
Packit ae235b
  
Packit ae235b
  return module->file_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_module_build_path:
Packit ae235b
 * @directory: (nullable): the directory where the module is. This can be
Packit ae235b
 *     %NULL or the empty string to indicate that the standard platform-specific
Packit ae235b
 *     directories will be used, though that is not recommended
Packit ae235b
 * @module_name: the name of the module
Packit ae235b
 *
Packit ae235b
 * A portable way to build the filename of a module. The platform-specific
Packit ae235b
 * prefix and suffix are added to the filename, if needed, and the result
Packit ae235b
 * is added to the directory, using the correct separator character.
Packit ae235b
 *
Packit ae235b
 * The directory should specify the directory where the module can be found.
Packit ae235b
 * It can be %NULL or an empty string to indicate that the module is in a
Packit ae235b
 * standard platform-specific directory, though this is not recommended
Packit ae235b
 * since the wrong module may be found.
Packit ae235b
 *
Packit ae235b
 * For example, calling g_module_build_path() on a Linux system with a
Packit ae235b
 * @directory of `/lib` and a @module_name of "mylibrary" will return
Packit ae235b
 * `/lib/libmylibrary.so`. On a Windows system, using `\Windows` as the
Packit ae235b
 * directory it will return `\Windows\mylibrary.dll`.
Packit ae235b
 *
Packit ae235b
 * Returns: the complete path of the module, including the standard library
Packit ae235b
 *     prefix and suffix. This should be freed when no longer needed
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_module_build_path (const gchar *directory,
Packit ae235b
                     const gchar *module_name)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (module_name != NULL, NULL);
Packit ae235b
  
Packit ae235b
  return _g_module_build_path (directory, module_name);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN GModule *    g_module_open_utf8 (const gchar  *file_name,
Packit ae235b
                                              GModuleFlags  flags);
Packit ae235b
Packit ae235b
_GLIB_EXTERN const gchar *g_module_name_utf8 (GModule      *module);
Packit ae235b
Packit ae235b
GModule*
Packit ae235b
g_module_open_utf8 (const gchar    *file_name,
Packit ae235b
                    GModuleFlags    flags)
Packit ae235b
{
Packit ae235b
  return g_module_open (file_name, flags);
Packit ae235b
}
Packit ae235b
Packit ae235b
const gchar *
Packit ae235b
g_module_name_utf8 (GModule *module)
Packit ae235b
{
Packit ae235b
  return g_module_name (module);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif