(root)/
glib-2.79.0/
gmodule/
gmodule.c
       1  /* GMODULE - GLIB wrapper code for dynamic module loading
       2   * Copyright (C) 1998 Tim Janik
       3   *
       4   * SPDX-License-Identifier: LGPL-2.1-or-later
       5   *
       6   * This library is free software; you can redistribute it and/or
       7   * modify it under the terms of the GNU Lesser General Public
       8   * License as published by the Free Software Foundation; either
       9   * version 2.1 of the License, or (at your option) any later version.
      10   *
      11   * This library is distributed in the hope that it will be useful,
      12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
      14   * Lesser General Public License for more details.
      15   *
      16   * You should have received a copy of the GNU Lesser General Public
      17   * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18   */
      19  
      20  /*
      21   * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
      22   * file for a list of people on the GLib Team.  See the ChangeLog
      23   * files for a list of changes.  These files are distributed with
      24   * GLib at ftp://ftp.gtk.org/pub/gtk/. 
      25   */
      26  
      27  /* 
      28   * MT safe
      29   */
      30  
      31  #include "config.h"
      32  
      33  #include "glib.h"
      34  #include "gmodule.h"
      35  
      36  #include <errno.h>
      37  #include <string.h>
      38  #include <sys/types.h>
      39  #include <sys/stat.h>
      40  #include <fcntl.h>
      41  #ifdef G_OS_UNIX
      42  #include <unistd.h>
      43  #endif
      44  #ifdef G_OS_WIN32
      45  #include <io.h>		/* For open() and close() prototypes. */
      46  #endif
      47  
      48  #ifndef O_CLOEXEC
      49  #define O_CLOEXEC 0
      50  #endif
      51  
      52  #include "gmoduleconf.h"
      53  #include "gstdio.h"
      54  
      55  
      56  /**
      57   * GModule:
      58   *
      59   * The #GModule struct is an opaque data structure to represent a
      60   * [dynamically-loaded module][glib-Dynamic-Loading-of-Modules].
      61   * It should only be accessed via the following functions.
      62   */
      63  
      64  /**
      65   * GModuleCheckInit:
      66   * @module: the #GModule corresponding to the module which has just been loaded
      67   *
      68   * Specifies the type of the module initialization function.
      69   * If a module contains a function named g_module_check_init() it is called
      70   * automatically when the module is loaded. It is passed the #GModule structure
      71   * and should return %NULL on success or a string describing the initialization
      72   * error.
      73   *
      74   * Returns: %NULL on success, or a string describing the initialization error
      75   */
      76  
      77  /**
      78   * GModuleUnload:
      79   * @module: the #GModule about to be unloaded
      80   *
      81   * Specifies the type of the module function called when it is unloaded.
      82   * If a module contains a function named g_module_unload() it is called
      83   * automatically when the module is unloaded.
      84   * It is passed the #GModule structure.
      85   */
      86  
      87  /**
      88   * G_MODULE_SUFFIX:
      89   *
      90   * Expands to a shared library suffix for the current platform without the
      91   * leading dot. On Unixes this is "so", and on Windows this is "dll".
      92   *
      93   * Deprecated: 2.76: Use g_module_open() instead with @module_name as the
      94   * basename of the file_name argument. You will get the wrong results using
      95   * this macro most of the time:
      96   *
      97   * 1. The suffix on macOS is usually 'dylib', but it's 'so' when using
      98   *    Autotools, so there's no way to get the suffix correct using
      99   *    a pre-processor macro.
     100   * 2. Prefixes also vary in a platform-specific way. You may or may not have
     101   *    a 'lib' prefix for the name on Windows and on Cygwin the prefix is
     102   *    'cyg'.
     103   * 3. The library name itself can vary per platform. For instance, you may
     104   *    want to load foo-1.dll on Windows and libfoo.1.dylib on macOS.
     105   *
     106   * g_module_open() takes care of all this by searching the filesystem for
     107   * combinations of possible suffixes and prefixes.
     108   */
     109  
     110  /**
     111   * G_MODULE_EXPORT:
     112   *
     113   * Used to declare functions exported by libraries or modules.
     114   *
     115   * When compiling for Windows, it marks the symbol as `dllexport`.
     116   *
     117   * When compiling for Linux and Unices, it marks the symbol as having `default`
     118   * visibility. This is no-op unless the code is being compiled with a
     119   * non-default
     120   * [visibility flag](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility-1260)
     121   * such as `hidden`.
     122   *
     123   * This macro must only be used when compiling a shared module. Modules that
     124   * support both shared and static linking should define their own macro that
     125   * expands to %G_MODULE_EXPORT when compiling the shared module, but is empty
     126   * when compiling the static module on Windows.
     127   */
     128  
     129  /**
     130   * G_MODULE_IMPORT:
     131   *
     132   * Used to declare functions imported from modules.
     133   */
     134  
     135  /* We maintain a list of modules, so we can reference count them.
     136   * That's needed because some platforms don't support references counts on
     137   * modules. Also, the module for the program itself is kept separately for
     138   * faster access and because it has special semantics.
     139   */
     140  
     141  
     142  /* --- structures --- */
     143  struct _GModule
     144  {
     145    gchar	*file_name;
     146    gpointer handle;
     147    guint ref_count : 31;
     148    guint is_resident : 1;
     149    GModuleUnload unload;
     150    GModule *next;
     151  };
     152  
     153  
     154  /* --- prototypes --- */
     155  static gpointer _g_module_open (const gchar  *file_name,
     156                                  gboolean      bind_lazy,
     157                                  gboolean      bind_local,
     158                                  GError      **error);
     159  static void		_g_module_close		(gpointer	 handle);
     160  static gpointer		_g_module_self		(void);
     161  static gpointer		_g_module_symbol	(gpointer	 handle,
     162  						 const gchar	*symbol_name);
     163  #if (G_MODULE_IMPL != G_MODULE_IMPL_DL) && (G_MODULE_IMPL != G_MODULE_IMPL_AR)
     164  static gchar*		_g_module_build_path	(const gchar	*directory,
     165  						 const gchar	*module_name);
     166  #else
     167  /* Implementation is in gmodule-deprecated.c */
     168  gchar*		        _g_module_build_path	(const gchar	*directory,
     169  						 const gchar	*module_name);
     170  #endif
     171  static inline void	g_module_set_error	(const gchar	*error);
     172  static inline GModule*	g_module_find_by_handle (gpointer	 handle);
     173  static inline GModule*	g_module_find_by_name	(const gchar	*name);
     174  
     175  
     176  /* --- variables --- */
     177  static GModule	     *modules = NULL;
     178  static GModule	     *main_module = NULL;
     179  static GPrivate       module_error_private = G_PRIVATE_INIT (g_free);
     180  static gboolean	      module_debug_initialized = FALSE;
     181  static guint	      module_debug_flags = 0;
     182  
     183  
     184  /* --- inline functions --- */
     185  static inline GModule*
     186  g_module_find_by_handle (gpointer handle)
     187  {
     188    GModule *module;
     189    GModule *retval = NULL;
     190    
     191    if (main_module && main_module->handle == handle)
     192      retval = main_module;
     193    else
     194      for (module = modules; module; module = module->next)
     195        if (handle == module->handle)
     196  	{
     197  	  retval = module;
     198  	  break;
     199  	}
     200  
     201    return retval;
     202  }
     203  
     204  static inline GModule*
     205  g_module_find_by_name (const gchar *name)
     206  {
     207    GModule *module;
     208    GModule *retval = NULL;
     209    
     210    for (module = modules; module; module = module->next)
     211      if (strcmp (name, module->file_name) == 0)
     212  	{
     213  	  retval = module;
     214  	  break;
     215  	}
     216  
     217    return retval;
     218  }
     219  
     220  static inline void
     221  g_module_set_error_unduped (gchar *error)
     222  {
     223    g_private_replace (&module_error_private, error);
     224    errno = 0;
     225  }
     226  
     227  static inline void
     228  g_module_set_error (const gchar *error)
     229  {
     230    g_module_set_error_unduped (g_strdup (error));
     231  }
     232  
     233  
     234  /* --- include platform specific code --- */
     235  #define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error (NULL); }
     236  #if	(G_MODULE_IMPL == G_MODULE_IMPL_DL)
     237  #include "gmodule-dl.c"
     238  #elif	(G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
     239  #include "gmodule-win32.c"
     240  #elif	(G_MODULE_IMPL == G_MODULE_IMPL_AR)
     241  #include "gmodule-ar.c"
     242  #else
     243  #undef	SUPPORT_OR_RETURN
     244  #define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error ("dynamic modules are " \
     245                                                "not supported by this system"); return rv; }
     246  static gpointer
     247  _g_module_open (const gchar  *file_name,
     248                  gboolean      bind_lazy,
     249                  gboolean      bind_local,
     250                  GError      **error)
     251  {
     252    g_module_set_error (NULL);
     253    return NULL;
     254  }
     255  static void
     256  _g_module_close (gpointer handle)
     257  {
     258  }
     259  static gpointer
     260  _g_module_self (void)
     261  {
     262    return NULL;
     263  }
     264  static gpointer
     265  _g_module_symbol (gpointer	 handle,
     266  		  const gchar	*symbol_name)
     267  {
     268    return NULL;
     269  }
     270  static gchar*
     271  _g_module_build_path (const gchar *directory,
     272  		      const gchar *module_name)
     273  {
     274    return NULL;
     275  }
     276  #endif	/* no implementation */
     277  
     278  /**
     279   * G_MODULE_ERROR:
     280   *
     281   * The error domain of the #GModule API.
     282   *
     283   * Since: 2.70
     284   */
     285  G_DEFINE_QUARK (g-module-error-quark, g_module_error)
     286  
     287  /* --- functions --- */
     288  
     289  /**
     290   * g_module_supported:
     291   *
     292   * Checks if modules are supported on the current platform.
     293   *
     294   * Returns: %TRUE if modules are supported
     295   */
     296  gboolean
     297  g_module_supported (void)
     298  {
     299    SUPPORT_OR_RETURN (FALSE);
     300    
     301    return TRUE;
     302  }
     303  
     304  static gchar*
     305  parse_libtool_archive (const gchar* libtool_name)
     306  {
     307    const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
     308    const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
     309    const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
     310    gchar *lt_dlname = NULL;
     311    gboolean lt_installed = TRUE;
     312    gchar *lt_libdir = NULL;
     313    gchar *name;
     314    GTokenType token;
     315    GScanner *scanner;
     316    
     317    int fd = g_open (libtool_name, O_RDONLY | O_CLOEXEC, 0);
     318    if (fd < 0)
     319      {
     320        gchar *display_libtool_name = g_filename_display_name (libtool_name);
     321        g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive ‘%s", display_libtool_name));
     322        g_free (display_libtool_name);
     323        return NULL;
     324      }
     325    /* search libtool's dlname specification  */
     326    scanner = g_scanner_new (NULL);
     327    g_scanner_input_file (scanner, fd);
     328    scanner->config->symbol_2_token = TRUE;
     329    g_scanner_scope_add_symbol (scanner, 0, "dlname", 
     330  			      GUINT_TO_POINTER (TOKEN_DLNAME));
     331    g_scanner_scope_add_symbol (scanner, 0, "installed", 
     332  			      GUINT_TO_POINTER (TOKEN_INSTALLED));
     333    g_scanner_scope_add_symbol (scanner, 0, "libdir", 
     334  			      GUINT_TO_POINTER (TOKEN_LIBDIR));
     335    while (!g_scanner_eof (scanner))
     336      {
     337        token = g_scanner_get_next_token (scanner);
     338        if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
     339  	  token == TOKEN_LIBDIR)
     340  	{
     341  	  if (g_scanner_get_next_token (scanner) != '=' ||
     342  	      g_scanner_get_next_token (scanner) != 
     343  	      (token == TOKEN_INSTALLED ? 
     344  	       G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
     345  	    {
     346  	      gchar *display_libtool_name = g_filename_display_name (libtool_name);
     347  	      g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s", display_libtool_name));
     348  	      g_free (display_libtool_name);
     349  
     350  	      g_free (lt_dlname);
     351  	      g_free (lt_libdir);
     352  	      g_scanner_destroy (scanner);
     353  	      close (fd);
     354  
     355  	      return NULL;
     356  	    }
     357  	  else
     358  	    {
     359  	      if (token == TOKEN_DLNAME)
     360  		{
     361  		  g_free (lt_dlname);
     362  		  lt_dlname = g_strdup (scanner->value.v_string);
     363  		}
     364  	      else if (token == TOKEN_INSTALLED)
     365  		lt_installed = 
     366  		  strcmp (scanner->value.v_identifier, "yes") == 0;
     367  	      else /* token == TOKEN_LIBDIR */
     368  		{
     369  		  g_free (lt_libdir);
     370  		  lt_libdir = g_strdup (scanner->value.v_string);
     371  		}
     372  	    }
     373  	}      
     374      }
     375  
     376    if (!lt_installed)
     377      {
     378        gchar *dir = g_path_get_dirname (libtool_name);
     379        g_free (lt_libdir);
     380        lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
     381        g_free (dir);
     382      }
     383  
     384    g_clear_pointer (&scanner, g_scanner_destroy);
     385    close (g_steal_fd (&fd));
     386  
     387    if (lt_libdir == NULL || lt_dlname == NULL)
     388      {
     389        gchar *display_libtool_name = g_filename_display_name (libtool_name);
     390        g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s", display_libtool_name));
     391        g_free (display_libtool_name);
     392  
     393        return NULL;
     394      }
     395  
     396    name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
     397    
     398    g_free (lt_dlname);
     399    g_free (lt_libdir);
     400  
     401    return name;
     402  }
     403  
     404  enum
     405  {
     406    G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0,
     407    G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1
     408  };
     409  
     410  static void
     411  _g_module_debug_init (void)
     412  {
     413    const GDebugKey keys[] = {
     414      { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
     415      { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
     416    };
     417    const gchar *env;
     418  
     419    env = g_getenv ("G_DEBUG");
     420  
     421    module_debug_flags =
     422      !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
     423  
     424    module_debug_initialized = TRUE;
     425  }
     426  
     427  static GRecMutex g_module_global_lock;
     428  
     429  /**
     430   * g_module_open_full:
     431   * @file_name: (nullable): the name or path to the file containing the module,
     432   *     or %NULL to obtain a #GModule representing the main program itself
     433   * @flags: the flags used for opening the module. This can be the
     434   *     logical OR of any of the #GModuleFlags
     435   * @error: #GError.
     436   *
     437   * Opens a module. If the module has already been opened, its reference count
     438   * is incremented. If not, the module is searched in the following order:
     439   *
     440   * 1. If @file_name exists as a regular file, it is used as-is; else
     441   * 2. If @file_name doesn't have the correct suffix and/or prefix for the
     442   *    platform, then possible suffixes and prefixes will be added to the
     443   *    basename till a file is found and whatever is found will be used; else
     444   * 3. If @file_name doesn't have the ".la"-suffix, ".la" is appended. Either
     445   *    way, if a matching .la file exists (and is a libtool archive) the
     446   *    libtool archive is parsed to find the actual file name, and that is
     447   *    used.
     448   *
     449   * At the end of all this, we would have a file path that we can access on
     450   * disk, and it is opened as a module. If not, @file_name is opened as
     451   * a module verbatim in the hopes that the system implementation will somehow
     452   * be able to access it.
     453   *
     454   * Returns: a #GModule on success, or %NULL on failure
     455   *
     456   * Since: 2.70
     457   */
     458  GModule*
     459  g_module_open_full (const gchar   *file_name,
     460                      GModuleFlags   flags,
     461                      GError       **error)
     462  {
     463    GModule *module;
     464    gpointer handle = NULL;
     465    gchar *name = NULL;
     466    
     467    SUPPORT_OR_RETURN (NULL);
     468  
     469    g_return_val_if_fail (error == NULL || *error == NULL, NULL);
     470    
     471    g_rec_mutex_lock (&g_module_global_lock);
     472  
     473    if (G_UNLIKELY (!module_debug_initialized))
     474      _g_module_debug_init ();
     475  
     476    if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
     477      flags &= ~G_MODULE_BIND_LAZY;
     478  
     479    if (!file_name)
     480      {      
     481        if (!main_module)
     482  	{
     483  	  handle = _g_module_self ();
     484  /* On Android 64 bit, RTLD_DEFAULT is (void *)0x0
     485   * so it always fails to create main_module if file_name is NULL */
     486  #if !defined(__BIONIC__) || !defined(__LP64__)
     487  	  if (handle)
     488  #endif
     489  	    {
     490  	      main_module = g_new (GModule, 1);
     491  	      main_module->file_name = NULL;
     492  	      main_module->handle = handle;
     493  	      main_module->ref_count = 1;
     494  	      main_module->is_resident = TRUE;
     495  	      main_module->unload = NULL;
     496  	      main_module->next = NULL;
     497  	    }
     498  	}
     499        else
     500  	main_module->ref_count++;
     501  
     502        g_rec_mutex_unlock (&g_module_global_lock);
     503        return main_module;
     504      }
     505    
     506    /* we first search the module list by name */
     507    module = g_module_find_by_name (file_name);
     508    if (module)
     509      {
     510        module->ref_count++;
     511        
     512        g_rec_mutex_unlock (&g_module_global_lock);
     513        return module;
     514      }
     515  
     516    /* check whether we have a readable file right away */
     517    if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
     518      name = g_strdup (file_name);
     519    /* try completing file name with standard library suffix */
     520    if (!name)
     521      {
     522        char *basename, *dirname;
     523        size_t prefix_idx = 0, suffix_idx = 0;
     524        const char *prefixes[2] = {0}, *suffixes[2] = {0};
     525  
     526        basename = g_path_get_basename (file_name);
     527        dirname = g_path_get_dirname (file_name);
     528  #ifdef G_OS_WIN32
     529        if (!g_str_has_prefix (basename, "lib"))
     530          prefixes[prefix_idx++] = "lib";
     531        prefixes[prefix_idx++] = "";
     532        if (!g_str_has_suffix (basename, ".dll"))
     533          suffixes[suffix_idx++] = ".dll";
     534  #else
     535    #ifdef __CYGWIN__
     536        if (!g_str_has_prefix (basename, "cyg"))
     537          prefixes[prefix_idx++] = "cyg";
     538    #else
     539        if (!g_str_has_prefix (basename, "lib"))
     540          prefixes[prefix_idx++] = "lib";
     541        else
     542          /* People commonly pass `libfoo` as the file_name and want us to
     543           * auto-detect the suffix as .la or .so, etc. We need to also find
     544           * .dylib and .dll in those cases. */
     545          prefixes[prefix_idx++] = "";
     546    #endif
     547    #ifdef __APPLE__
     548        if (!g_str_has_suffix (basename, ".dylib") &&
     549            !g_str_has_suffix (basename, ".so"))
     550          {
     551            suffixes[suffix_idx++] = ".dylib";
     552            suffixes[suffix_idx++] = ".so";
     553          }
     554    #else
     555        if (!g_str_has_suffix (basename, ".so"))
     556          suffixes[suffix_idx++] = ".so";
     557    #endif
     558  #endif
     559        for (guint i = 0; i < prefix_idx; i++)
     560          {
     561            for (guint j = 0; j < suffix_idx; j++)
     562              {
     563                name = g_strconcat (dirname, G_DIR_SEPARATOR_S, prefixes[i],
     564                                    basename, suffixes[j], NULL);
     565                if (g_file_test (name, G_FILE_TEST_IS_REGULAR))
     566                  goto name_found;
     567                g_free (name);
     568                name = NULL;
     569              }
     570          }
     571      name_found:
     572        g_free (basename);
     573        g_free (dirname);
     574      }
     575    /* try completing by appending libtool suffix */
     576    if (!name)
     577      {
     578        name = g_strconcat (file_name, ".la", NULL);
     579        if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
     580  	{
     581  	  g_free (name);
     582  	  name = NULL;
     583  	}
     584      }
     585    /* we can't access() the file, lets hope the platform backends finds
     586     * it via library paths
     587     */
     588    if (!name)
     589      {
     590        gchar *dot = strrchr (file_name, '.');
     591        gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
     592  
     593        /* we make sure the name has a suffix using the deprecated
     594         * G_MODULE_SUFFIX for backward-compat */
     595        if (!dot || dot < slash)
     596  	name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
     597        else
     598  	name = g_strdup (file_name);
     599      }
     600  
     601    /* ok, try loading the module */
     602    g_assert (name != NULL);
     603  
     604    /* if it's a libtool archive, figure library file to load */
     605    if (g_str_has_suffix (name, ".la")) /* libtool archive? */
     606      {
     607        gchar *real_name = parse_libtool_archive (name);
     608  
     609        /* real_name might be NULL, but then module error is already set */
     610        if (real_name)
     611          {
     612            g_free (name);
     613            name = real_name;
     614          }
     615      }
     616  
     617    handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
     618                             (flags & G_MODULE_BIND_LOCAL) != 0, error);
     619    g_free (name);
     620  
     621    if (handle)
     622      {
     623        gchar *saved_error;
     624        GModuleCheckInit check_init;
     625        const gchar *check_failed = NULL;
     626        
     627        /* search the module list by handle, since file names are not unique */
     628        module = g_module_find_by_handle (handle);
     629        if (module)
     630  	{
     631  	  _g_module_close (module->handle);
     632  	  module->ref_count++;
     633  	  g_module_set_error (NULL);
     634  	  
     635  	  g_rec_mutex_unlock (&g_module_global_lock);
     636  	  return module;
     637  	}
     638        
     639        saved_error = g_strdup (g_module_error ());
     640        g_module_set_error (NULL);
     641        
     642        module = g_new (GModule, 1);
     643        module->file_name = g_strdup (file_name);
     644        module->handle = handle;
     645        module->ref_count = 1;
     646        module->is_resident = FALSE;
     647        module->unload = NULL;
     648        module->next = modules;
     649        modules = module;
     650        
     651        /* check initialization */
     652        if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
     653  	check_failed = check_init (module);
     654        
     655        /* we don't call unload() if the initialization check failed. */
     656        if (!check_failed)
     657  	g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
     658        
     659        if (check_failed)
     660  	{
     661  	  gchar *temp_error;
     662  
     663            temp_error = g_strconcat ("GModule (", file_name, ") ",
     664                                      "initialization check failed: ",
     665                                      check_failed, NULL);
     666  	  g_module_close (module);
     667  	  module = NULL;
     668            g_module_set_error (temp_error);
     669            g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_CHECK_FAILED, temp_error);
     670            g_free (temp_error);
     671  	}
     672        else
     673  	g_module_set_error (saved_error);
     674  
     675        g_free (saved_error);
     676      }
     677  
     678    if (module != NULL &&
     679        (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
     680      g_module_make_resident (module);
     681  
     682    g_rec_mutex_unlock (&g_module_global_lock);
     683    return module;
     684  }
     685  
     686  /**
     687   * g_module_open:
     688   * @file_name: (nullable): the name or path to the file containing the module,
     689   *     or %NULL to obtain a #GModule representing the main program itself
     690   * @flags: the flags used for opening the module. This can be the
     691   *     logical OR of any of the #GModuleFlags.
     692   *
     693   * A thin wrapper function around g_module_open_full()
     694   *
     695   * Returns: a #GModule on success, or %NULL on failure
     696   */
     697  GModule *
     698  g_module_open (const gchar  *file_name,
     699                 GModuleFlags  flags)
     700  {
     701    return g_module_open_full (file_name, flags, NULL);
     702  }
     703  
     704  /**
     705   * g_module_close:
     706   * @module: a #GModule to close
     707   *
     708   * Closes a module.
     709   *
     710   * Returns: %TRUE on success
     711   */
     712  gboolean
     713  g_module_close (GModule *module)
     714  {
     715    SUPPORT_OR_RETURN (FALSE);
     716    
     717    g_return_val_if_fail (module != NULL, FALSE);
     718    g_return_val_if_fail (module->ref_count > 0, FALSE);
     719    
     720    g_rec_mutex_lock (&g_module_global_lock);
     721  
     722    module->ref_count--;
     723    
     724    if (!module->ref_count && !module->is_resident && module->unload)
     725      {
     726        GModuleUnload unload;
     727  
     728        unload = module->unload;
     729        module->unload = NULL;
     730        unload (module);
     731      }
     732  
     733    if (!module->ref_count && !module->is_resident)
     734      {
     735        GModule *last;
     736        GModule *node;
     737        
     738        last = NULL;
     739        
     740        node = modules;
     741        while (node)
     742  	{
     743  	  if (node == module)
     744  	    {
     745  	      if (last)
     746  		last->next = node->next;
     747  	      else
     748  		modules = node->next;
     749  	      break;
     750  	    }
     751  	  last = node;
     752  	  node = last->next;
     753  	}
     754        module->next = NULL;
     755        
     756        _g_module_close (module->handle);
     757        g_free (module->file_name);
     758        g_free (module);
     759      }
     760    
     761    g_rec_mutex_unlock (&g_module_global_lock);
     762    return g_module_error() == NULL;
     763  }
     764  
     765  /**
     766   * g_module_make_resident:
     767   * @module: a #GModule to make permanently resident
     768   *
     769   * Ensures that a module will never be unloaded.
     770   * Any future g_module_close() calls on the module will be ignored.
     771   */
     772  void
     773  g_module_make_resident (GModule *module)
     774  {
     775    g_return_if_fail (module != NULL);
     776  
     777    module->is_resident = TRUE;
     778  }
     779  
     780  /**
     781   * g_module_error:
     782   *
     783   * Gets a string describing the last module error.
     784   *
     785   * Returns: a string describing the last module error
     786   */
     787  const gchar *
     788  g_module_error (void)
     789  {
     790    return g_private_get (&module_error_private);
     791  }
     792  
     793  /**
     794   * g_module_symbol:
     795   * @module: a #GModule
     796   * @symbol_name: the name of the symbol to find
     797   * @symbol: (out): returns the pointer to the symbol value
     798   *
     799   * Gets a symbol pointer from a module, such as one exported
     800   * by %G_MODULE_EXPORT. Note that a valid symbol can be %NULL.
     801   *
     802   * Returns: %TRUE on success
     803   */
     804  gboolean
     805  g_module_symbol (GModule     *module,
     806                   const gchar *symbol_name,
     807                   gpointer    *symbol)
     808  {
     809    const gchar *module_error;
     810  
     811    if (symbol)
     812      *symbol = NULL;
     813    SUPPORT_OR_RETURN (FALSE);
     814    
     815    g_return_val_if_fail (module != NULL, FALSE);
     816    g_return_val_if_fail (symbol_name != NULL, FALSE);
     817    g_return_val_if_fail (symbol != NULL, FALSE);
     818    
     819    g_rec_mutex_lock (&g_module_global_lock);
     820  
     821  #ifdef	G_MODULE_NEED_USCORE
     822    {
     823      gchar *name;
     824  
     825      name = g_strconcat ("_", symbol_name, NULL);
     826      *symbol = _g_module_symbol (module->handle, name);
     827      g_free (name);
     828    }
     829  #else	/* !G_MODULE_NEED_USCORE */
     830    *symbol = _g_module_symbol (module->handle, symbol_name);
     831  #endif	/* !G_MODULE_NEED_USCORE */
     832    
     833    module_error = g_module_error ();
     834    if (module_error)
     835      {
     836        gchar *error;
     837  
     838        error = g_strconcat ("'", symbol_name, "': ", module_error, NULL);
     839        g_module_set_error (error);
     840        g_free (error);
     841        *symbol = NULL;
     842      }
     843    
     844    g_rec_mutex_unlock (&g_module_global_lock);
     845    return !module_error;
     846  }
     847  
     848  /**
     849   * g_module_name:
     850   * @module: a #GModule
     851   *
     852   * Returns the filename that the module was opened with.
     853   *
     854   * If @module refers to the application itself, "main" is returned.
     855   *
     856   * Returns: (transfer none): the filename of the module
     857   */
     858  const gchar *
     859  g_module_name (GModule *module)
     860  {
     861    g_return_val_if_fail (module != NULL, NULL);
     862    
     863    if (module == main_module)
     864      return "main";
     865    
     866    return module->file_name;
     867  }
     868  
     869  /**
     870   * g_module_build_path:
     871   * @directory: (nullable): the directory where the module is. This can be
     872   *     %NULL or the empty string to indicate that the standard platform-specific
     873   *     directories will be used, though that is not recommended
     874   * @module_name: the name of the module
     875   *
     876   * A portable way to build the filename of a module. The platform-specific
     877   * prefix and suffix are added to the filename, if needed, and the result
     878   * is added to the directory, using the correct separator character.
     879   *
     880   * The directory should specify the directory where the module can be found.
     881   * It can be %NULL or an empty string to indicate that the module is in a
     882   * standard platform-specific directory, though this is not recommended
     883   * since the wrong module may be found.
     884   *
     885   * For example, calling g_module_build_path() on a Linux system with a
     886   * @directory of `/lib` and a @module_name of "mylibrary" will return
     887   * `/lib/libmylibrary.so`. On a Windows system, using `\Windows` as the
     888   * directory it will return `\Windows\mylibrary.dll`.
     889   *
     890   * Returns: the complete path of the module, including the standard library
     891   *     prefix and suffix. This should be freed when no longer needed
     892   *
     893   * Deprecated: 2.76: Use g_module_open() instead with @module_name as the
     894   * basename of the file_name argument. See %G_MODULE_SUFFIX for why.
     895   */
     896  gchar *
     897  g_module_build_path (const gchar *directory,
     898                       const gchar *module_name)
     899  {
     900    g_return_val_if_fail (module_name != NULL, NULL);
     901    
     902    return _g_module_build_path (directory, module_name);
     903  }
     904  
     905  
     906  #ifdef G_OS_WIN32
     907  
     908  /* Binary compatibility versions. Not for newly compiled code. */
     909  
     910  _GMODULE_EXTERN GModule *    g_module_open_utf8 (const gchar  *file_name,
     911                                                   GModuleFlags  flags);
     912  
     913  _GMODULE_EXTERN const gchar *g_module_name_utf8 (GModule      *module);
     914  
     915  GModule*
     916  g_module_open_utf8 (const gchar    *file_name,
     917                      GModuleFlags    flags)
     918  {
     919    return g_module_open (file_name, flags);
     920  }
     921  
     922  const gchar *
     923  g_module_name_utf8 (GModule *module)
     924  {
     925    return g_module_name (module);
     926  }
     927  
     928  #endif