Coverage Report

Created: 2026-07-11 07:00

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