Coverage Report

Created: 2025-07-12 07:23

/src/pango/subprojects/glib/gmodule/gmodule.c
Line
Count
Source (jump to first uncovered line)
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](modules.html#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
0
{
188
0
  GModule *module;
189
0
  GModule *retval = NULL;
190
  
191
0
  if (main_module && main_module->handle == handle)
192
0
    retval = main_module;
193
0
  else
194
0
    for (module = modules; module; module = module->next)
195
0
      if (handle == module->handle)
196
0
  {
197
0
    retval = module;
198
0
    break;
199
0
  }
200
201
0
  return retval;
202
0
}
203
204
static inline GModule*
205
g_module_find_by_name (const gchar *name)
206
0
{
207
0
  GModule *module;
208
0
  GModule *retval = NULL;
209
  
210
0
  for (module = modules; module; module = module->next)
211
0
    if (strcmp (name, module->file_name) == 0)
212
0
  {
213
0
    retval = module;
214
0
    break;
215
0
  }
216
217
0
  return retval;
218
0
}
219
220
static inline void
221
g_module_set_error_unduped (gchar *error)
222
0
{
223
0
  g_private_replace (&module_error_private, error);
224
0
  errno = 0;
225
0
}
226
227
static inline void
228
g_module_set_error (const gchar *error)
229
0
{
230
0
  g_module_set_error_unduped (g_strdup (error));
231
0
}
232
233
234
/* --- include platform specific code --- */
235
0
#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
0
{
299
0
  SUPPORT_OR_RETURN (FALSE);
300
  
301
0
  return TRUE;
302
0
}
303
304
static gchar*
305
parse_libtool_archive (const gchar* libtool_name)
306
0
{
307
0
  const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
308
0
  const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
309
0
  const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
310
0
  gchar *lt_dlname = NULL;
311
0
  gboolean lt_installed = TRUE;
312
0
  gchar *lt_libdir = NULL;
313
0
  gchar *name;
314
0
  GTokenType token;
315
0
  GScanner *scanner;
316
  
317
0
  int fd = g_open (libtool_name, O_RDONLY | O_CLOEXEC, 0);
318
0
  if (fd < 0)
319
0
    {
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
0
    }
325
  /* search libtool's dlname specification  */
326
0
  scanner = g_scanner_new (NULL);
327
0
  g_scanner_input_file (scanner, fd);
328
0
  scanner->config->symbol_2_token = TRUE;
329
0
  g_scanner_scope_add_symbol (scanner, 0, "dlname", 
330
0
            GUINT_TO_POINTER (TOKEN_DLNAME));
331
0
  g_scanner_scope_add_symbol (scanner, 0, "installed", 
332
0
            GUINT_TO_POINTER (TOKEN_INSTALLED));
333
0
  g_scanner_scope_add_symbol (scanner, 0, "libdir", 
334
0
            GUINT_TO_POINTER (TOKEN_LIBDIR));
335
0
  while (!g_scanner_eof (scanner))
336
0
    {
337
0
      token = g_scanner_get_next_token (scanner);
338
0
      if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
339
0
    token == TOKEN_LIBDIR)
340
0
  {
341
0
    if (g_scanner_get_next_token (scanner) != '=' ||
342
0
        g_scanner_get_next_token (scanner) != 
343
0
        (token == TOKEN_INSTALLED ? 
344
0
         G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
345
0
      {
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
0
      }
357
0
    else
358
0
      {
359
0
        if (token == TOKEN_DLNAME)
360
0
    {
361
0
      g_free (lt_dlname);
362
0
      lt_dlname = g_strdup (scanner->value.v_string);
363
0
    }
364
0
        else if (token == TOKEN_INSTALLED)
365
0
    lt_installed = 
366
0
      strcmp (scanner->value.v_identifier, "yes") == 0;
367
0
        else /* token == TOKEN_LIBDIR */
368
0
    {
369
0
      g_free (lt_libdir);
370
0
      lt_libdir = g_strdup (scanner->value.v_string);
371
0
    }
372
0
      }
373
0
  }      
374
0
    }
375
376
0
  if (!lt_installed)
377
0
    {
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
0
    }
383
384
0
  g_clear_pointer (&scanner, g_scanner_destroy);
385
0
  close (g_steal_fd (&fd));
386
387
0
  if (lt_libdir == NULL || lt_dlname == NULL)
388
0
    {
389
0
      gchar *display_libtool_name = g_filename_display_name (libtool_name);
390
0
      g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ā€˜%s’", display_libtool_name));
391
0
      g_free (display_libtool_name);
392
393
0
      return NULL;
394
0
    }
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
0
}
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
0
{
413
0
  const GDebugKey keys[] = {
414
0
    { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
415
0
    { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
416
0
  };
417
0
  const gchar *env;
418
419
0
  env = g_getenv ("G_DEBUG");
420
421
0
  module_debug_flags =
422
0
    !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
423
424
0
  module_debug_initialized = TRUE;
425
0
}
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
g_module_open_full (const gchar   *file_name,
467
                    GModuleFlags   flags,
468
                    GError       **error)
469
0
{
470
0
  GModule *module;
471
0
  gpointer handle = NULL;
472
0
  gchar *name = NULL;
473
  
474
0
  SUPPORT_OR_RETURN (NULL);
475
476
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
477
  
478
0
  g_rec_mutex_lock (&g_module_global_lock);
479
480
0
  if (G_UNLIKELY (!module_debug_initialized))
481
0
    _g_module_debug_init ();
482
483
0
  if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
484
0
    flags &= (unsigned) ~G_MODULE_BIND_LAZY;
485
486
0
  if (!file_name)
487
0
    {      
488
0
      if (!main_module)
489
0
  {
490
0
    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
0
#if !defined(__ANDROID__) || !defined(__LP64__)
494
0
    if (handle)
495
0
#endif
496
0
      {
497
0
        main_module = g_new (GModule, 1);
498
0
        main_module->file_name = NULL;
499
0
        main_module->handle = handle;
500
0
        main_module->ref_count = 1;
501
0
        main_module->is_resident = TRUE;
502
0
        main_module->unload = NULL;
503
0
        main_module->next = NULL;
504
0
      }
505
0
  }
506
0
      else
507
0
  main_module->ref_count++;
508
509
0
      g_rec_mutex_unlock (&g_module_global_lock);
510
0
      return main_module;
511
0
    }
512
  
513
  /* we first search the module list by name */
514
0
  module = g_module_find_by_name (file_name);
515
0
  if (module)
516
0
    {
517
0
      module->ref_count++;
518
      
519
0
      g_rec_mutex_unlock (&g_module_global_lock);
520
0
      return module;
521
0
    }
522
523
  /* check whether we have a readable file right away */
524
0
  if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
525
0
    name = g_strdup (file_name);
526
  /* try completing file name with standard library suffix */
527
0
  if (!name)
528
0
    {
529
0
      char *basename, *dirname;
530
0
      size_t prefix_idx = 0, suffix_idx = 0;
531
0
      const char *prefixes[2] = {0}, *suffixes[2] = {0};
532
533
0
      basename = g_path_get_basename (file_name);
534
0
      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
0
      if (!g_str_has_prefix (basename, "lib"))
547
0
        prefixes[prefix_idx++] = "lib";
548
0
      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
0
        prefixes[prefix_idx++] = "";
553
0
  #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
0
      if (!g_str_has_suffix (basename, ".so"))
563
0
        suffixes[suffix_idx++] = ".so";
564
0
  #endif
565
0
#endif
566
0
      for (guint i = 0; i < prefix_idx; i++)
567
0
        {
568
0
          for (guint j = 0; j < suffix_idx; j++)
569
0
            {
570
0
              name = g_strconcat (dirname, G_DIR_SEPARATOR_S, prefixes[i],
571
0
                                  basename, suffixes[j], NULL);
572
0
              if (g_file_test (name, G_FILE_TEST_IS_REGULAR))
573
0
                goto name_found;
574
0
              g_free (name);
575
0
              name = NULL;
576
0
            }
577
0
        }
578
0
    name_found:
579
0
      g_free (basename);
580
0
      g_free (dirname);
581
0
    }
582
  /* try completing by appending libtool suffix */
583
0
  if (!name)
584
0
    {
585
0
      name = g_strconcat (file_name, ".la", NULL);
586
0
      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
587
0
  {
588
0
    g_free (name);
589
0
    name = NULL;
590
0
  }
591
0
    }
592
  /* we can't access() the file, lets hope the platform backends finds
593
   * it via library paths
594
   */
595
0
  if (!name)
596
0
    {
597
0
      gchar *dot = strrchr (file_name, '.');
598
0
      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
0
      if (!dot || dot < slash)
603
0
  name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
604
0
      else
605
0
  name = g_strdup (file_name);
606
0
    }
607
608
  /* ok, try loading the module */
609
0
  g_assert (name != NULL);
610
611
  /* if it's a libtool archive, figure library file to load */
612
0
  if (g_str_has_suffix (name, ".la")) /* libtool archive? */
613
0
    {
614
0
      gchar *real_name = parse_libtool_archive (name);
615
616
      /* real_name might be NULL, but then module error is already set */
617
0
      if (real_name)
618
0
        {
619
0
          g_free (name);
620
0
          name = real_name;
621
0
        }
622
0
    }
623
624
0
  handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
625
0
                           (flags & G_MODULE_BIND_LOCAL) != 0, error);
626
0
  g_free (name);
627
628
0
  if (handle)
629
0
    {
630
0
      gchar *saved_error;
631
0
      GModuleCheckInit check_init;
632
0
      const gchar *check_failed = NULL;
633
      
634
      /* search the module list by handle, since file names are not unique */
635
0
      module = g_module_find_by_handle (handle);
636
0
      if (module)
637
0
  {
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
0
  }
645
      
646
0
      saved_error = g_strdup (g_module_error ());
647
0
      g_module_set_error (NULL);
648
      
649
0
      module = g_new (GModule, 1);
650
0
      module->file_name = g_strdup (file_name);
651
0
      module->handle = handle;
652
0
      module->ref_count = 1;
653
0
      module->is_resident = FALSE;
654
0
      module->unload = NULL;
655
0
      module->next = modules;
656
0
      modules = module;
657
      
658
      /* check initialization */
659
0
      if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
660
0
  check_failed = check_init (module);
661
      
662
      /* we don't call unload() if the initialization check failed. */
663
0
      if (!check_failed)
664
0
  g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
665
      
666
0
      if (check_failed)
667
0
  {
668
0
    gchar *temp_error;
669
670
0
          temp_error = g_strconcat ("GModule (", file_name, ") ",
671
0
                                    "initialization check failed: ",
672
0
                                    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
0
  }
679
0
      else
680
0
  g_module_set_error (saved_error);
681
682
0
      g_free (saved_error);
683
0
    }
684
685
0
  if (module != NULL &&
686
0
      (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
687
0
    g_module_make_resident (module);
688
689
0
  g_rec_mutex_unlock (&g_module_global_lock);
690
0
  return module;
691
0
}
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
g_module_open (const gchar  *file_name,
706
               GModuleFlags  flags)
707
0
{
708
0
  return g_module_open_full (file_name, flags, NULL);
709
0
}
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
g_module_close (GModule *module)
721
0
{
722
0
  SUPPORT_OR_RETURN (FALSE);
723
  
724
0
  g_return_val_if_fail (module != NULL, FALSE);
725
0
  g_return_val_if_fail (module->ref_count > 0, FALSE);
726
  
727
0
  g_rec_mutex_lock (&g_module_global_lock);
728
729
0
  module->ref_count--;
730
  
731
0
  if (!module->ref_count && !module->is_resident && module->unload)
732
0
    {
733
0
      GModuleUnload unload;
734
735
0
      unload = module->unload;
736
0
      module->unload = NULL;
737
0
      unload (module);
738
0
    }
739
740
0
  if (!module->ref_count && !module->is_resident)
741
0
    {
742
0
      GModule *last;
743
0
      GModule *node;
744
      
745
0
      last = NULL;
746
      
747
0
      node = modules;
748
0
      while (node)
749
0
  {
750
0
    if (node == module)
751
0
      {
752
0
        if (last)
753
0
    last->next = node->next;
754
0
        else
755
0
    modules = node->next;
756
0
        break;
757
0
      }
758
0
    last = node;
759
0
    node = last->next;
760
0
  }
761
0
      module->next = NULL;
762
      
763
0
      _g_module_close (module->handle);
764
0
      g_free (module->file_name);
765
0
      g_free (module);
766
0
    }
767
  
768
0
  g_rec_mutex_unlock (&g_module_global_lock);
769
0
  return g_module_error() == NULL;
770
0
}
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
g_module_make_resident (GModule *module)
781
0
{
782
0
  g_return_if_fail (module != NULL);
783
784
0
  module->is_resident = TRUE;
785
0
}
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
g_module_error (void)
796
0
{
797
0
  return g_private_get (&module_error_private);
798
0
}
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
g_module_symbol (GModule     *module,
813
                 const gchar *symbol_name,
814
                 gpointer    *symbol)
815
0
{
816
0
  const gchar *module_error;
817
818
0
  if (symbol)
819
0
    *symbol = NULL;
820
0
  SUPPORT_OR_RETURN (FALSE);
821
  
822
0
  g_return_val_if_fail (module != NULL, FALSE);
823
0
  g_return_val_if_fail (symbol_name != NULL, FALSE);
824
0
  g_return_val_if_fail (symbol != NULL, FALSE);
825
  
826
0
  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
0
  *symbol = _g_module_symbol (module->handle, symbol_name);
838
0
#endif  /* !G_MODULE_NEED_USCORE */
839
  
840
0
  module_error = g_module_error ();
841
0
  if (module_error)
842
0
    {
843
0
      gchar *error;
844
845
0
      error = g_strconcat ("'", symbol_name, "': ", module_error, NULL);
846
0
      g_module_set_error (error);
847
0
      g_free (error);
848
0
      *symbol = NULL;
849
0
    }
850
  
851
0
  g_rec_mutex_unlock (&g_module_global_lock);
852
0
  return !module_error;
853
0
}
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
g_module_name (GModule *module)
867
0
{
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
0
}
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
g_module_build_path (const gchar *directory,
905
                     const gchar *module_name)
906
0
{
907
0
  g_return_val_if_fail (module_name != NULL, NULL);
908
  
909
0
  return _g_module_build_path (directory, module_name);
910
0
}
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