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