Coverage Report

Created: 2025-07-23 08:13

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