LCOV - code coverage report
Current view: top level - glib/gmodule - gmodule.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 193 261 73.9 %
Date: 2024-04-23 05:16:05 Functions: 13 16 81.2 %
Branches: 87 138 63.0 %

           Branch data     Line data    Source code
       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                 :         15 : g_module_find_by_handle (gpointer handle)
     187                 :            : {
     188                 :            :   GModule *module;
     189                 :         15 :   GModule *retval = NULL;
     190                 :            :   
     191   [ +  +  -  + ]:         15 :   if (main_module && main_module->handle == handle)
     192                 :          0 :     retval = main_module;
     193                 :            :   else
     194         [ +  + ]:         19 :     for (module = modules; module; module = module->next)
     195         [ -  + ]:          4 :       if (handle == module->handle)
     196                 :            :         {
     197                 :          0 :           retval = module;
     198                 :          0 :           break;
     199                 :            :         }
     200                 :            : 
     201                 :         15 :   return retval;
     202                 :            : }
     203                 :            : 
     204                 :            : static inline GModule*
     205                 :         17 : g_module_find_by_name (const gchar *name)
     206                 :            : {
     207                 :            :   GModule *module;
     208                 :         17 :   GModule *retval = NULL;
     209                 :            :   
     210         [ +  + ]:         21 :   for (module = modules; module; module = module->next)
     211         [ -  + ]:          4 :     if (strcmp (name, module->file_name) == 0)
     212                 :            :         {
     213                 :          0 :           retval = module;
     214                 :          0 :           break;
     215                 :            :         }
     216                 :            : 
     217                 :         17 :   return retval;
     218                 :            : }
     219                 :            : 
     220                 :            : static inline void
     221                 :        614 : g_module_set_error_unduped (gchar *error)
     222                 :            : {
     223                 :        614 :   g_private_replace (&module_error_private, error);
     224                 :        614 :   errno = 0;
     225                 :        614 : }
     226                 :            : 
     227                 :            : static inline void
     228                 :        612 : g_module_set_error (const gchar *error)
     229                 :            : {
     230                 :        612 :   g_module_set_error_unduped (g_strdup (error));
     231                 :        612 : }
     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         [ +  + ]:          5 : 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                 :        121 : g_module_supported (void)
     298                 :            : {
     299                 :        121 :   SUPPORT_OR_RETURN (FALSE);
     300                 :            :   
     301                 :        121 :   return TRUE;
     302                 :            : }
     303                 :            : 
     304                 :            : static gchar*
     305                 :          2 : parse_libtool_archive (const gchar* libtool_name)
     306                 :            : {
     307                 :          2 :   const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
     308                 :          2 :   const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
     309                 :          2 :   const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
     310                 :          2 :   gchar *lt_dlname = NULL;
     311                 :          2 :   gboolean lt_installed = TRUE;
     312                 :          2 :   gchar *lt_libdir = NULL;
     313                 :            :   gchar *name;
     314                 :            :   GTokenType token;
     315                 :            :   GScanner *scanner;
     316                 :            :   
     317                 :          2 :   int fd = g_open (libtool_name, O_RDONLY | O_CLOEXEC, 0);
     318         [ -  + ]:          2 :   if (fd < 0)
     319                 :            :     {
     320                 :          0 :       gchar *display_libtool_name = g_filename_display_name (libtool_name);
     321                 :          0 :       g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive ‘%s’", display_libtool_name));
     322                 :          0 :       g_free (display_libtool_name);
     323                 :          0 :       return NULL;
     324                 :            :     }
     325                 :            :   /* search libtool's dlname specification  */
     326                 :          2 :   scanner = g_scanner_new (NULL);
     327                 :          2 :   g_scanner_input_file (scanner, fd);
     328                 :          2 :   scanner->config->symbol_2_token = TRUE;
     329                 :          2 :   g_scanner_scope_add_symbol (scanner, 0, "dlname", 
     330                 :          2 :                               GUINT_TO_POINTER (TOKEN_DLNAME));
     331                 :          2 :   g_scanner_scope_add_symbol (scanner, 0, "installed", 
     332                 :          2 :                               GUINT_TO_POINTER (TOKEN_INSTALLED));
     333                 :          2 :   g_scanner_scope_add_symbol (scanner, 0, "libdir", 
     334                 :          2 :                               GUINT_TO_POINTER (TOKEN_LIBDIR));
     335         [ +  + ]:          4 :   while (!g_scanner_eof (scanner))
     336                 :            :     {
     337                 :          2 :       token = g_scanner_get_next_token (scanner);
     338   [ +  -  +  -  :          2 :       if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
                   -  + ]
     339                 :            :           token == TOKEN_LIBDIR)
     340                 :            :         {
     341   [ #  #  #  # ]:          0 :           if (g_scanner_get_next_token (scanner) != '=' ||
     342                 :          0 :               g_scanner_get_next_token (scanner) != 
     343                 :            :               (token == TOKEN_INSTALLED ? 
     344         [ #  # ]:          0 :                G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
     345                 :            :             {
     346                 :          0 :               gchar *display_libtool_name = g_filename_display_name (libtool_name);
     347                 :          0 :               g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s’", display_libtool_name));
     348                 :          0 :               g_free (display_libtool_name);
     349                 :            : 
     350                 :          0 :               g_free (lt_dlname);
     351                 :          0 :               g_free (lt_libdir);
     352                 :          0 :               g_scanner_destroy (scanner);
     353                 :          0 :               close (fd);
     354                 :            : 
     355                 :          0 :               return NULL;
     356                 :            :             }
     357                 :            :           else
     358                 :            :             {
     359         [ #  # ]:          0 :               if (token == TOKEN_DLNAME)
     360                 :            :                 {
     361                 :          0 :                   g_free (lt_dlname);
     362                 :          0 :                   lt_dlname = g_strdup (scanner->value.v_string);
     363                 :            :                 }
     364         [ #  # ]:          0 :               else if (token == TOKEN_INSTALLED)
     365                 :          0 :                 lt_installed = 
     366                 :          0 :                   strcmp (scanner->value.v_identifier, "yes") == 0;
     367                 :            :               else /* token == TOKEN_LIBDIR */
     368                 :            :                 {
     369                 :          0 :                   g_free (lt_libdir);
     370                 :          0 :                   lt_libdir = g_strdup (scanner->value.v_string);
     371                 :            :                 }
     372                 :            :             }
     373                 :            :         }      
     374                 :            :     }
     375                 :            : 
     376         [ -  + ]:          2 :   if (!lt_installed)
     377                 :            :     {
     378                 :          0 :       gchar *dir = g_path_get_dirname (libtool_name);
     379                 :          0 :       g_free (lt_libdir);
     380                 :          0 :       lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
     381                 :          0 :       g_free (dir);
     382                 :            :     }
     383                 :            : 
     384                 :          2 :   g_clear_pointer (&scanner, g_scanner_destroy);
     385                 :          2 :   close (g_steal_fd (&fd));
     386                 :            : 
     387   [ -  +  -  - ]:          2 :   if (lt_libdir == NULL || lt_dlname == NULL)
     388                 :            :     {
     389                 :          2 :       gchar *display_libtool_name = g_filename_display_name (libtool_name);
     390                 :          2 :       g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s’", display_libtool_name));
     391                 :          2 :       g_free (display_libtool_name);
     392                 :            : 
     393                 :          2 :       return NULL;
     394                 :            :     }
     395                 :            : 
     396                 :          0 :   name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
     397                 :            :   
     398                 :          0 :   g_free (lt_dlname);
     399                 :          0 :   g_free (lt_libdir);
     400                 :            : 
     401                 :          0 :   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                 :         13 : _g_module_debug_init (void)
     412                 :            : {
     413                 :         13 :   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                 :         13 :   env = g_getenv ("G_DEBUG");
     420                 :            : 
     421                 :         13 :   module_debug_flags =
     422         [ +  + ]:         13 :     !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
     423                 :            : 
     424                 :         13 :   module_debug_initialized = TRUE;
     425                 :         13 : }
     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 using @file_name.
     439                 :            :  *
     440                 :            :  * Since 2.76, the search order/behavior is as follows:
     441                 :            :  *
     442                 :            :  * 1. If @file_name exists as a regular file, it is used as-is; else
     443                 :            :  * 2. If @file_name doesn't have the correct suffix and/or prefix for the
     444                 :            :  *    platform, then possible suffixes and prefixes will be added to the
     445                 :            :  *    basename till a file is found and whatever is found will be used; else
     446                 :            :  * 3. If @file_name doesn't have the ".la"-suffix, ".la" is appended. Either
     447                 :            :  *    way, if a matching .la file exists (and is a libtool archive) the
     448                 :            :  *    libtool archive is parsed to find the actual file name, and that is
     449                 :            :  *    used.
     450                 :            :  *
     451                 :            :  * If, at the end of all this, we have a file path that we can access on disk,
     452                 :            :  * it is opened as a module. If not, @file_name is attempted to be opened as a
     453                 :            :  * module verbatim in the hopes that the system implementation will somehow be
     454                 :            :  * able to access it. If that is not possible, %NULL is returned.
     455                 :            :  *
     456                 :            :  * Note that this behaviour was different prior to 2.76, but there is some
     457                 :            :  * overlap in functionality. If backwards compatibility is an issue, kindly
     458                 :            :  * consult earlier #GModule documentation for the prior search order/behavior
     459                 :            :  * of @file_name.
     460                 :            :  *
     461                 :            :  * Returns: a #GModule on success, or %NULL on failure
     462                 :            :  *
     463                 :            :  * Since: 2.70
     464                 :            :  */
     465                 :            : GModule*
     466                 :         23 : g_module_open_full (const gchar   *file_name,
     467                 :            :                     GModuleFlags   flags,
     468                 :            :                     GError       **error)
     469                 :            : {
     470                 :            :   GModule *module;
     471                 :         23 :   gpointer handle = NULL;
     472                 :         23 :   gchar *name = NULL;
     473                 :            :   
     474                 :         23 :   SUPPORT_OR_RETURN (NULL);
     475                 :            : 
     476                 :         23 :   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
     477                 :            :   
     478                 :         23 :   g_rec_mutex_lock (&g_module_global_lock);
     479                 :            : 
     480         [ +  + ]:         23 :   if (G_UNLIKELY (!module_debug_initialized))
     481                 :         13 :     _g_module_debug_init ();
     482                 :            : 
     483         [ -  + ]:         23 :   if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
     484                 :          0 :     flags &= ~G_MODULE_BIND_LAZY;
     485                 :            : 
     486         [ +  + ]:         23 :   if (!file_name)
     487                 :            :     {      
     488         [ +  - ]:          6 :       if (!main_module)
     489                 :            :         {
     490                 :          6 :           handle = _g_module_self ();
     491                 :            : /* On Android 64 bit, RTLD_DEFAULT is (void *)0x0
     492                 :            :  * so it always fails to create main_module if file_name is NULL */
     493                 :            : #if !defined(__ANDROID__) || !defined(__LP64__)
     494         [ +  - ]:          6 :           if (handle)
     495                 :            : #endif
     496                 :            :             {
     497                 :          6 :               main_module = g_new (GModule, 1);
     498                 :          6 :               main_module->file_name = NULL;
     499                 :          6 :               main_module->handle = handle;
     500                 :          6 :               main_module->ref_count = 1;
     501                 :          6 :               main_module->is_resident = TRUE;
     502                 :          6 :               main_module->unload = NULL;
     503                 :          6 :               main_module->next = NULL;
     504                 :            :             }
     505                 :            :         }
     506                 :            :       else
     507                 :          0 :         main_module->ref_count++;
     508                 :            : 
     509                 :          6 :       g_rec_mutex_unlock (&g_module_global_lock);
     510                 :          6 :       return main_module;
     511                 :            :     }
     512                 :            :   
     513                 :            :   /* we first search the module list by name */
     514                 :         17 :   module = g_module_find_by_name (file_name);
     515         [ -  + ]:         17 :   if (module)
     516                 :            :     {
     517                 :          0 :       module->ref_count++;
     518                 :            :       
     519                 :          0 :       g_rec_mutex_unlock (&g_module_global_lock);
     520                 :          0 :       return module;
     521                 :            :     }
     522                 :            : 
     523                 :            :   /* check whether we have a readable file right away */
     524         [ +  + ]:         17 :   if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
     525                 :          5 :     name = g_strdup (file_name);
     526                 :            :   /* try completing file name with standard library suffix */
     527         [ +  + ]:         17 :   if (!name)
     528                 :            :     {
     529                 :            :       char *basename, *dirname;
     530                 :         12 :       size_t prefix_idx = 0, suffix_idx = 0;
     531                 :         12 :       const char *prefixes[2] = {0}, *suffixes[2] = {0};
     532                 :            : 
     533                 :         12 :       basename = g_path_get_basename (file_name);
     534                 :         12 :       dirname = g_path_get_dirname (file_name);
     535                 :            : #ifdef G_OS_WIN32
     536                 :            :       if (!g_str_has_prefix (basename, "lib"))
     537                 :            :         prefixes[prefix_idx++] = "lib";
     538                 :            :       prefixes[prefix_idx++] = "";
     539                 :            :       if (!g_str_has_suffix (basename, ".dll"))
     540                 :            :         suffixes[suffix_idx++] = ".dll";
     541                 :            : #else
     542                 :            :   #ifdef __CYGWIN__
     543                 :            :       if (!g_str_has_prefix (basename, "cyg"))
     544                 :            :         prefixes[prefix_idx++] = "cyg";
     545                 :            :   #else
     546   [ +  -  -  +  :         12 :       if (!g_str_has_prefix (basename, "lib"))
             +  -  -  + ]
     547                 :          0 :         prefixes[prefix_idx++] = "lib";
     548                 :            :       else
     549                 :            :         /* People commonly pass `libfoo` as the file_name and want us to
     550                 :            :          * auto-detect the suffix as .la or .so, etc. We need to also find
     551                 :            :          * .dylib and .dll in those cases. */
     552                 :         12 :         prefixes[prefix_idx++] = "";
     553                 :            :   #endif
     554                 :            :   #ifdef __APPLE__
     555                 :            :       if (!g_str_has_suffix (basename, ".dylib") &&
     556                 :            :           !g_str_has_suffix (basename, ".so"))
     557                 :            :         {
     558                 :            :           suffixes[suffix_idx++] = ".dylib";
     559                 :            :           suffixes[suffix_idx++] = ".so";
     560                 :            :         }
     561                 :            :   #else
     562   [ +  -  -  +  :         12 :       if (!g_str_has_suffix (basename, ".so"))
             +  -  +  - ]
     563                 :         12 :         suffixes[suffix_idx++] = ".so";
     564                 :            :   #endif
     565                 :            : #endif
     566         [ +  + ]:         19 :       for (guint i = 0; i < prefix_idx; i++)
     567                 :            :         {
     568         [ +  + ]:         19 :           for (guint j = 0; j < suffix_idx; j++)
     569                 :            :             {
     570                 :         12 :               name = g_strconcat (dirname, G_DIR_SEPARATOR_S, prefixes[i],
     571                 :            :                                   basename, suffixes[j], NULL);
     572         [ +  + ]:         12 :               if (g_file_test (name, G_FILE_TEST_IS_REGULAR))
     573                 :          5 :                 goto name_found;
     574                 :          7 :               g_free (name);
     575                 :          7 :               name = NULL;
     576                 :            :             }
     577                 :            :         }
     578                 :          7 :     name_found:
     579                 :         12 :       g_free (basename);
     580                 :         12 :       g_free (dirname);
     581                 :            :     }
     582                 :            :   /* try completing by appending libtool suffix */
     583         [ +  + ]:         17 :   if (!name)
     584                 :            :     {
     585                 :          7 :       name = g_strconcat (file_name, ".la", NULL);
     586         [ +  - ]:          7 :       if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
     587                 :            :         {
     588                 :          7 :           g_free (name);
     589                 :          7 :           name = NULL;
     590                 :            :         }
     591                 :            :     }
     592                 :            :   /* we can't access() the file, lets hope the platform backends finds
     593                 :            :    * it via library paths
     594                 :            :    */
     595         [ +  + ]:         17 :   if (!name)
     596                 :            :     {
     597                 :          7 :       gchar *dot = strrchr (file_name, '.');
     598                 :          7 :       gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
     599                 :            : 
     600                 :            :       /* we make sure the name has a suffix using the deprecated
     601                 :            :        * G_MODULE_SUFFIX for backward-compat */
     602   [ +  -  -  + ]:          7 :       if (!dot || dot < slash)
     603                 :          0 :         name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
     604                 :            :       else
     605                 :          7 :         name = g_strdup (file_name);
     606                 :            :     }
     607                 :            : 
     608                 :            :   /* ok, try loading the module */
     609                 :         17 :   g_assert (name != NULL);
     610                 :            : 
     611                 :            :   /* if it's a libtool archive, figure library file to load */
     612   [ +  -  -  +  :         17 :   if (g_str_has_suffix (name, ".la")) /* libtool archive? */
             +  -  +  + ]
     613                 :            :     {
     614                 :          2 :       gchar *real_name = parse_libtool_archive (name);
     615                 :            : 
     616                 :            :       /* real_name might be NULL, but then module error is already set */
     617         [ -  + ]:          2 :       if (real_name)
     618                 :            :         {
     619                 :          0 :           g_free (name);
     620                 :          0 :           name = real_name;
     621                 :            :         }
     622                 :            :     }
     623                 :            : 
     624                 :         17 :   handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
     625                 :         17 :                            (flags & G_MODULE_BIND_LOCAL) != 0, error);
     626                 :         17 :   g_free (name);
     627                 :            : 
     628         [ +  + ]:         17 :   if (handle)
     629                 :            :     {
     630                 :            :       gchar *saved_error;
     631                 :            :       GModuleCheckInit check_init;
     632                 :         15 :       const gchar *check_failed = NULL;
     633                 :            :       
     634                 :            :       /* search the module list by handle, since file names are not unique */
     635                 :         15 :       module = g_module_find_by_handle (handle);
     636         [ -  + ]:         15 :       if (module)
     637                 :            :         {
     638                 :          0 :           _g_module_close (module->handle);
     639                 :          0 :           module->ref_count++;
     640                 :          0 :           g_module_set_error (NULL);
     641                 :            :           
     642                 :          0 :           g_rec_mutex_unlock (&g_module_global_lock);
     643                 :          0 :           return module;
     644                 :            :         }
     645                 :            :       
     646                 :         15 :       saved_error = g_strdup (g_module_error ());
     647                 :         15 :       g_module_set_error (NULL);
     648                 :            :       
     649                 :         15 :       module = g_new (GModule, 1);
     650                 :         15 :       module->file_name = g_strdup (file_name);
     651                 :         15 :       module->handle = handle;
     652                 :         15 :       module->ref_count = 1;
     653                 :         15 :       module->is_resident = FALSE;
     654                 :         15 :       module->unload = NULL;
     655                 :         15 :       module->next = modules;
     656                 :         15 :       modules = module;
     657                 :            :       
     658                 :            :       /* check initialization */
     659   [ +  +  +  - ]:         15 :       if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
     660                 :          2 :         check_failed = check_init (module);
     661                 :            :       
     662                 :            :       /* we don't call unload() if the initialization check failed. */
     663         [ +  - ]:         15 :       if (!check_failed)
     664                 :         15 :         g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
     665                 :            :       
     666         [ -  + ]:         15 :       if (check_failed)
     667                 :            :         {
     668                 :            :           gchar *temp_error;
     669                 :            : 
     670                 :          0 :           temp_error = g_strconcat ("GModule (", file_name, ") ",
     671                 :            :                                     "initialization check failed: ",
     672                 :            :                                     check_failed, NULL);
     673                 :          0 :           g_module_close (module);
     674                 :          0 :           module = NULL;
     675                 :          0 :           g_module_set_error (temp_error);
     676                 :          0 :           g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_CHECK_FAILED, temp_error);
     677                 :          0 :           g_free (temp_error);
     678                 :            :         }
     679                 :            :       else
     680                 :         15 :         g_module_set_error (saved_error);
     681                 :            : 
     682                 :         15 :       g_free (saved_error);
     683                 :            :     }
     684                 :            : 
     685         [ +  + ]:         17 :   if (module != NULL &&
     686         [ -  + ]:         15 :       (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
     687                 :          0 :     g_module_make_resident (module);
     688                 :            : 
     689                 :         17 :   g_rec_mutex_unlock (&g_module_global_lock);
     690                 :         17 :   return module;
     691                 :            : }
     692                 :            : 
     693                 :            : /**
     694                 :            :  * g_module_open:
     695                 :            :  * @file_name: (nullable): the name or path to the file containing the module,
     696                 :            :  *     or %NULL to obtain a #GModule representing the main program itself
     697                 :            :  * @flags: the flags used for opening the module. This can be the
     698                 :            :  *     logical OR of any of the #GModuleFlags.
     699                 :            :  *
     700                 :            :  * A thin wrapper function around g_module_open_full()
     701                 :            :  *
     702                 :            :  * Returns: a #GModule on success, or %NULL on failure
     703                 :            :  */
     704                 :            : GModule *
     705                 :         11 : g_module_open (const gchar  *file_name,
     706                 :            :                GModuleFlags  flags)
     707                 :            : {
     708                 :         11 :   return g_module_open_full (file_name, flags, NULL);
     709                 :            : }
     710                 :            : 
     711                 :            : /**
     712                 :            :  * g_module_close:
     713                 :            :  * @module: a #GModule to close
     714                 :            :  *
     715                 :            :  * Closes a module.
     716                 :            :  *
     717                 :            :  * Returns: %TRUE on success
     718                 :            :  */
     719                 :            : gboolean
     720                 :         17 : g_module_close (GModule *module)
     721                 :            : {
     722                 :         17 :   SUPPORT_OR_RETURN (FALSE);
     723                 :            :   
     724                 :         17 :   g_return_val_if_fail (module != NULL, FALSE);
     725                 :         17 :   g_return_val_if_fail (module->ref_count > 0, FALSE);
     726                 :            :   
     727                 :         17 :   g_rec_mutex_lock (&g_module_global_lock);
     728                 :            : 
     729                 :         17 :   module->ref_count--;
     730                 :            :   
     731   [ +  -  +  +  :         17 :   if (!module->ref_count && !module->is_resident && module->unload)
                   +  + ]
     732                 :            :     {
     733                 :            :       GModuleUnload unload;
     734                 :            : 
     735                 :          2 :       unload = module->unload;
     736                 :          2 :       module->unload = NULL;
     737                 :          2 :       unload (module);
     738                 :            :     }
     739                 :            : 
     740   [ +  -  +  + ]:         17 :   if (!module->ref_count && !module->is_resident)
     741                 :            :     {
     742                 :            :       GModule *last;
     743                 :            :       GModule *node;
     744                 :            :       
     745                 :         15 :       last = NULL;
     746                 :            :       
     747                 :         15 :       node = modules;
     748         [ +  - ]:         19 :       while (node)
     749                 :            :         {
     750         [ +  + ]:         19 :           if (node == module)
     751                 :            :             {
     752         [ +  + ]:         15 :               if (last)
     753                 :          4 :                 last->next = node->next;
     754                 :            :               else
     755                 :         11 :                 modules = node->next;
     756                 :         15 :               break;
     757                 :            :             }
     758                 :          4 :           last = node;
     759                 :          4 :           node = last->next;
     760                 :            :         }
     761                 :         15 :       module->next = NULL;
     762                 :            :       
     763                 :         15 :       _g_module_close (module->handle);
     764                 :         15 :       g_free (module->file_name);
     765                 :         15 :       g_free (module);
     766                 :            :     }
     767                 :            :   
     768                 :         17 :   g_rec_mutex_unlock (&g_module_global_lock);
     769                 :         17 :   return g_module_error() == NULL;
     770                 :            : }
     771                 :            : 
     772                 :            : /**
     773                 :            :  * g_module_make_resident:
     774                 :            :  * @module: a #GModule to make permanently resident
     775                 :            :  *
     776                 :            :  * Ensures that a module will never be unloaded.
     777                 :            :  * Any future g_module_close() calls on the module will be ignored.
     778                 :            :  */
     779                 :            : void
     780                 :          0 : g_module_make_resident (GModule *module)
     781                 :            : {
     782                 :          0 :   g_return_if_fail (module != NULL);
     783                 :            : 
     784                 :          0 :   module->is_resident = TRUE;
     785                 :            : }
     786                 :            : 
     787                 :            : /**
     788                 :            :  * g_module_error:
     789                 :            :  *
     790                 :            :  * Gets a string describing the last module error.
     791                 :            :  *
     792                 :            :  * Returns: a string describing the last module error
     793                 :            :  */
     794                 :            : const gchar *
     795                 :        391 : g_module_error (void)
     796                 :            : {
     797                 :        391 :   return g_private_get (&module_error_private);
     798                 :            : }
     799                 :            : 
     800                 :            : /**
     801                 :            :  * g_module_symbol:
     802                 :            :  * @module: a #GModule
     803                 :            :  * @symbol_name: the name of the symbol to find
     804                 :            :  * @symbol: (out): returns the pointer to the symbol value
     805                 :            :  *
     806                 :            :  * Gets a symbol pointer from a module, such as one exported
     807                 :            :  * by %G_MODULE_EXPORT. Note that a valid symbol can be %NULL.
     808                 :            :  *
     809                 :            :  * Returns: %TRUE on success
     810                 :            :  */
     811                 :            : gboolean
     812                 :        359 : g_module_symbol (GModule     *module,
     813                 :            :                  const gchar *symbol_name,
     814                 :            :                  gpointer    *symbol)
     815                 :            : {
     816                 :            :   const gchar *module_error;
     817                 :            : 
     818         [ +  - ]:        359 :   if (symbol)
     819                 :        359 :     *symbol = NULL;
     820                 :        359 :   SUPPORT_OR_RETURN (FALSE);
     821                 :            :   
     822                 :        359 :   g_return_val_if_fail (module != NULL, FALSE);
     823                 :        359 :   g_return_val_if_fail (symbol_name != NULL, FALSE);
     824                 :        359 :   g_return_val_if_fail (symbol != NULL, FALSE);
     825                 :            :   
     826                 :        359 :   g_rec_mutex_lock (&g_module_global_lock);
     827                 :            : 
     828                 :            : #ifdef  G_MODULE_NEED_USCORE
     829                 :            :   {
     830                 :            :     gchar *name;
     831                 :            : 
     832                 :            :     name = g_strconcat ("_", symbol_name, NULL);
     833                 :            :     *symbol = _g_module_symbol (module->handle, name);
     834                 :            :     g_free (name);
     835                 :            :   }
     836                 :            : #else   /* !G_MODULE_NEED_USCORE */
     837                 :        359 :   *symbol = _g_module_symbol (module->handle, symbol_name);
     838                 :            : #endif  /* !G_MODULE_NEED_USCORE */
     839                 :            :   
     840                 :        359 :   module_error = g_module_error ();
     841         [ +  + ]:        359 :   if (module_error)
     842                 :            :     {
     843                 :            :       gchar *error;
     844                 :            : 
     845                 :         30 :       error = g_strconcat ("'", symbol_name, "': ", module_error, NULL);
     846                 :         30 :       g_module_set_error (error);
     847                 :         30 :       g_free (error);
     848                 :         30 :       *symbol = NULL;
     849                 :            :     }
     850                 :            :   
     851                 :        359 :   g_rec_mutex_unlock (&g_module_global_lock);
     852                 :        359 :   return !module_error;
     853                 :            : }
     854                 :            : 
     855                 :            : /**
     856                 :            :  * g_module_name:
     857                 :            :  * @module: a #GModule
     858                 :            :  *
     859                 :            :  * Returns the filename that the module was opened with.
     860                 :            :  *
     861                 :            :  * If @module refers to the application itself, "main" is returned.
     862                 :            :  *
     863                 :            :  * Returns: (transfer none): the filename of the module
     864                 :            :  */
     865                 :            : const gchar *
     866                 :          0 : g_module_name (GModule *module)
     867                 :            : {
     868                 :          0 :   g_return_val_if_fail (module != NULL, NULL);
     869                 :            :   
     870         [ #  # ]:          0 :   if (module == main_module)
     871                 :          0 :     return "main";
     872                 :            :   
     873                 :          0 :   return module->file_name;
     874                 :            : }
     875                 :            : 
     876                 :            : /**
     877                 :            :  * g_module_build_path:
     878                 :            :  * @directory: (nullable): the directory where the module is. This can be
     879                 :            :  *     %NULL or the empty string to indicate that the standard platform-specific
     880                 :            :  *     directories will be used, though that is not recommended
     881                 :            :  * @module_name: the name of the module
     882                 :            :  *
     883                 :            :  * A portable way to build the filename of a module. The platform-specific
     884                 :            :  * prefix and suffix are added to the filename, if needed, and the result
     885                 :            :  * is added to the directory, using the correct separator character.
     886                 :            :  *
     887                 :            :  * The directory should specify the directory where the module can be found.
     888                 :            :  * It can be %NULL or an empty string to indicate that the module is in a
     889                 :            :  * standard platform-specific directory, though this is not recommended
     890                 :            :  * since the wrong module may be found.
     891                 :            :  *
     892                 :            :  * For example, calling g_module_build_path() on a Linux system with a
     893                 :            :  * @directory of `/lib` and a @module_name of "mylibrary" will return
     894                 :            :  * `/lib/libmylibrary.so`. On a Windows system, using `\Windows` as the
     895                 :            :  * directory it will return `\Windows\mylibrary.dll`.
     896                 :            :  *
     897                 :            :  * Returns: the complete path of the module, including the standard library
     898                 :            :  *     prefix and suffix. This should be freed when no longer needed
     899                 :            :  *
     900                 :            :  * Deprecated: 2.76: Use g_module_open() instead with @module_name as the
     901                 :            :  * basename of the file_name argument. See %G_MODULE_SUFFIX for why.
     902                 :            :  */
     903                 :            : gchar *
     904                 :          0 : g_module_build_path (const gchar *directory,
     905                 :            :                      const gchar *module_name)
     906                 :            : {
     907                 :          0 :   g_return_val_if_fail (module_name != NULL, NULL);
     908                 :            :   
     909                 :          0 :   return _g_module_build_path (directory, module_name);
     910                 :            : }
     911                 :            : 
     912                 :            : 
     913                 :            : #ifdef G_OS_WIN32
     914                 :            : 
     915                 :            : /* Binary compatibility versions. Not for newly compiled code. */
     916                 :            : 
     917                 :            : _GMODULE_EXTERN GModule *    g_module_open_utf8 (const gchar  *file_name,
     918                 :            :                                                  GModuleFlags  flags);
     919                 :            : 
     920                 :            : _GMODULE_EXTERN const gchar *g_module_name_utf8 (GModule      *module);
     921                 :            : 
     922                 :            : GModule*
     923                 :            : g_module_open_utf8 (const gchar    *file_name,
     924                 :            :                     GModuleFlags    flags)
     925                 :            : {
     926                 :            :   return g_module_open (file_name, flags);
     927                 :            : }
     928                 :            : 
     929                 :            : const gchar *
     930                 :            : g_module_name_utf8 (GModule *module)
     931                 :            : {
     932                 :            :   return g_module_name (module);
     933                 :            : }
     934                 :            : 
     935                 :            : #endif

Generated by: LCOV version 1.14