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