fix include order
[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 #include "gmoduleconf.h"
33 #include "gstdio.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 #if defined (G_OS_WIN32)
45 #include <io.h>         /* For open() and close() prototypes. */
46 #endif
47
48
49 /* We maintain a list of modules, so we can reference count them.
50  * That's needed because some platforms don't support refernce counts on
51  * modules e.g. the shl_* implementation of HP-UX
52  * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
53  * Also, the module for the program itself is kept seperatedly for
54  * faster access and because it has special semantics.
55  */
56
57
58 /* --- structures --- */
59 struct _GModule
60 {
61   gchar *file_name;
62 #ifdef G_OS_WIN32
63   gchar *cp_file_name;
64 #endif
65   gpointer handle;
66   guint ref_count : 31;
67   guint is_resident : 1;
68   GModuleUnload unload;
69   GModule *next;
70 };
71
72
73 /* --- prototypes --- */
74 static gpointer         _g_module_open          (const gchar    *file_name,
75                                                  gboolean        bind_lazy,
76                                                  gboolean        bind_local);
77 static void             _g_module_close         (gpointer        handle,
78                                                  gboolean        is_unref);
79 static gpointer         _g_module_self          (void);
80 static gpointer         _g_module_symbol        (gpointer        handle,
81                                                  const gchar    *symbol_name);
82 static gchar*           _g_module_build_path    (const gchar    *directory,
83                                                  const gchar    *module_name);
84 static inline void      g_module_set_error      (const gchar    *error);
85 static inline GModule*  g_module_find_by_handle (gpointer        handle);
86 static inline GModule*  g_module_find_by_name   (const gchar    *name);
87
88
89 /* --- variables --- */
90 static GModule       *modules = NULL;
91 static GModule       *main_module = NULL;
92 static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
93 static gboolean       module_debug_initialized = FALSE;
94 static guint          module_debug_flags = 0;
95
96
97 /* --- inline functions --- */
98 static inline GModule*
99 g_module_find_by_handle (gpointer handle)
100 {
101   GModule *module;
102   GModule *retval = NULL;
103   
104   if (main_module && main_module->handle == handle)
105     retval = main_module;
106   else
107     for (module = modules; module; module = module->next)
108       if (handle == module->handle)
109         {
110           retval = module;
111           break;
112         }
113
114   return retval;
115 }
116
117 static inline GModule*
118 g_module_find_by_name (const gchar *name)
119 {
120   GModule *module;
121   GModule *retval = NULL;
122   
123   for (module = modules; module; module = module->next)
124     if (strcmp (name, module->file_name) == 0)
125         {
126           retval = module;
127           break;
128         }
129
130   return retval;
131 }
132
133 static inline void
134 g_module_set_error_unduped (gchar *error)
135 {
136   g_static_private_set (&module_error_private, error, g_free);
137   errno = 0;
138 }
139
140 static inline void
141 g_module_set_error (const gchar *error)
142 {
143   g_module_set_error_unduped (g_strdup (error));
144 }
145
146
147 /* --- include platform specifc code --- */
148 #define SUPPORT_OR_RETURN(rv)   { g_module_set_error (NULL); }
149 #if     (G_MODULE_IMPL == G_MODULE_IMPL_DL)
150 #include "gmodule-dl.c"
151 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
152 #include "gmodule-dld.c"
153 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
154 #include "gmodule-win32.c"
155 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_DYLD)
156 #include "gmodule-dyld.c"
157 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_AR)
158 #include "gmodule-ar.c"
159 #else
160 #undef  SUPPORT_OR_RETURN
161 #define SUPPORT_OR_RETURN(rv)   { g_module_set_error ("dynamic modules are " \
162                                               "not supported by this system"); return rv; }
163 static gpointer
164 _g_module_open (const gchar     *file_name,
165                 gboolean         bind_lazy,
166                 gboolean         bind_local)
167 {
168   return NULL;
169 }
170 static void
171 _g_module_close (gpointer        handle,
172                  gboolean        is_unref)
173 {
174 }
175 static gpointer
176 _g_module_self (void)
177 {
178   return NULL;
179 }
180 static gpointer
181 _g_module_symbol (gpointer       handle,
182                   const gchar   *symbol_name)
183 {
184   return NULL;
185 }
186 static gchar*
187 _g_module_build_path (const gchar *directory,
188                       const gchar *module_name)
189 {
190   return NULL;
191 }
192 #endif  /* no implementation */
193
194 /* --- functions --- */
195 gboolean
196 g_module_supported (void)
197 {
198   SUPPORT_OR_RETURN (FALSE);
199   
200   return TRUE;
201 }
202
203 static gchar*
204 parse_libtool_archive (const gchar* libtool_name)
205 {
206   const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
207   const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
208   const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
209   gchar *lt_dlname = NULL;
210   gboolean lt_installed = TRUE;
211   gchar *lt_libdir = NULL;
212   gchar *name;
213   GTokenType token;
214   GScanner *scanner;
215   
216   int fd = g_open (libtool_name, O_RDONLY, 0);
217   if (fd < 0)
218     {
219       gchar *display_libtool_name = g_filename_display_name (libtool_name);
220       g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", display_libtool_name));
221       g_free (display_libtool_name);
222       return NULL;
223     }
224   /* search libtool's dlname specification  */
225   scanner = g_scanner_new (NULL);
226   g_scanner_input_file (scanner, fd);
227   scanner->config->symbol_2_token = TRUE;
228   g_scanner_scope_add_symbol (scanner, 0, "dlname", 
229                               GUINT_TO_POINTER (TOKEN_DLNAME));
230   g_scanner_scope_add_symbol (scanner, 0, "installed", 
231                               GUINT_TO_POINTER (TOKEN_INSTALLED));
232   g_scanner_scope_add_symbol (scanner, 0, "libdir", 
233                               GUINT_TO_POINTER (TOKEN_LIBDIR));
234   while (!g_scanner_eof (scanner))
235     {
236       token = g_scanner_get_next_token (scanner);
237       if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
238           token == TOKEN_LIBDIR)
239         {
240           if (g_scanner_get_next_token (scanner) != '=' ||
241               g_scanner_get_next_token (scanner) != 
242               (token == TOKEN_INSTALLED ? 
243                G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
244             {
245               gchar *display_libtool_name = g_filename_display_name (libtool_name);
246               g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", display_libtool_name));
247               g_free (display_libtool_name);
248
249               g_free (lt_dlname);
250               g_free (lt_libdir);
251               g_scanner_destroy (scanner);
252               close (fd);
253
254               return NULL;
255             }
256           else
257             {
258               if (token == TOKEN_DLNAME)
259                 {
260                   g_free (lt_dlname);
261                   lt_dlname = g_strdup (scanner->value.v_string);
262                 }
263               else if (token == TOKEN_INSTALLED)
264                 lt_installed = 
265                   strcmp (scanner->value.v_identifier, "yes") == 0;
266               else /* token == TOKEN_LIBDIR */
267                 {
268                   g_free (lt_libdir);
269                   lt_libdir = g_strdup (scanner->value.v_string);
270                 }
271             }
272         }      
273     }
274
275   if (!lt_installed)
276     {
277       gchar *dir = g_path_get_dirname (libtool_name);
278       g_free (lt_libdir);
279       lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
280       g_free (dir);
281     }
282
283   name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
284   
285   g_free (lt_dlname);
286   g_free (lt_libdir);
287   g_scanner_destroy (scanner);
288   close (fd);
289
290   return name;
291 }
292
293 static inline gboolean
294 str_check_suffix (const gchar* string,
295                   const gchar* suffix)
296 {
297   gsize string_len = strlen (string);    
298   gsize suffix_len = strlen (suffix);    
299
300   return string_len >= suffix_len && 
301     strcmp (string + string_len - suffix_len, suffix) == 0;
302 }
303
304 enum
305 {
306   G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0,
307   G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1
308 };
309
310 static void
311 _g_module_debug_init (void)
312 {
313   const GDebugKey keys[] = {
314     { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
315     { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
316   };
317   const gchar *env;
318
319   env = g_getenv ("G_DEBUG");
320
321   module_debug_flags =
322     !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
323
324   module_debug_initialized = TRUE;
325 }
326
327 static GStaticRecMutex g_module_global_lock = G_STATIC_REC_MUTEX_INIT;
328
329 GModule*
330 g_module_open (const gchar    *file_name,
331                GModuleFlags    flags)
332 {
333   GModule *module;
334   gpointer handle = NULL;
335   gchar *name = NULL;
336   
337   SUPPORT_OR_RETURN (NULL);
338   
339   g_static_rec_mutex_lock (&g_module_global_lock);
340
341   if (G_UNLIKELY (!module_debug_initialized))
342     _g_module_debug_init ();
343
344   if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
345     flags &= ~G_MODULE_BIND_LAZY;
346
347   if (!file_name)
348     {      
349       if (!main_module)
350         {
351           handle = _g_module_self ();
352           if (handle)
353             {
354               main_module = g_new (GModule, 1);
355               main_module->file_name = NULL;
356 #ifdef G_OS_WIN32
357               main_module->cp_file_name = NULL;
358 #endif
359               main_module->handle = handle;
360               main_module->ref_count = 1;
361               main_module->is_resident = TRUE;
362               main_module->unload = NULL;
363               main_module->next = NULL;
364             }
365         }
366       else
367         main_module->ref_count++;
368
369       g_static_rec_mutex_unlock (&g_module_global_lock);
370       return main_module;
371     }
372   
373   /* we first search the module list by name */
374   module = g_module_find_by_name (file_name);
375   if (module)
376     {
377       module->ref_count++;
378       
379       g_static_rec_mutex_unlock (&g_module_global_lock);
380       return module;
381     }
382
383   /* check whether we have a readable file right away */
384   if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
385     name = g_strdup (file_name);
386   /* try completing file name with standard library suffix */
387   if (!name)
388     {
389       name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
390       if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
391         {
392           g_free (name);
393           name = NULL;
394         }
395     }
396   /* try completing by appending libtool suffix */
397   if (!name)
398     {
399       name = g_strconcat (file_name, ".la", NULL);
400       if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
401         {
402           g_free (name);
403           name = NULL;
404         }
405     }
406   /* we can't access() the file, lets hope the platform backends finds
407    * it via library paths
408    */
409   if (!name)
410     {
411       gchar *dot = strrchr (file_name, '.');
412       gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
413       
414       /* make sure the name has a suffix */
415       if (!dot || dot < slash)
416         name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
417       else
418         name = g_strdup (file_name);
419     }
420
421   /* ok, try loading the module */
422   if (name)
423     {
424       /* if it's a libtool archive, figure library file to load */
425       if (str_check_suffix (name, ".la")) /* libtool archive? */
426         {
427           gchar *real_name = parse_libtool_archive (name);
428
429           /* real_name might be NULL, but then module error is already set */
430           g_free (name);
431           name = real_name;
432         }
433       if (name)
434         handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
435                         (flags & G_MODULE_BIND_LOCAL) != 0);
436     }
437   else
438     {
439       gchar *display_file_name = g_filename_display_name (file_name);
440       g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", display_file_name));
441       g_free (display_file_name);
442     }
443   g_free (name);
444
445   if (handle)
446     {
447       gchar *saved_error;
448       GModuleCheckInit check_init;
449       const gchar *check_failed = NULL;
450       
451       /* search the module list by handle, since file names are not unique */
452       module = g_module_find_by_handle (handle);
453       if (module)
454         {
455           _g_module_close (module->handle, TRUE);
456           module->ref_count++;
457           g_module_set_error (NULL);
458           
459           g_static_rec_mutex_unlock (&g_module_global_lock);
460           return module;
461         }
462       
463       saved_error = g_strdup (g_module_error ());
464       g_module_set_error (NULL);
465       
466       module = g_new (GModule, 1);
467       module->file_name = g_strdup (file_name);
468 #ifdef G_OS_WIN32
469       module->cp_file_name = g_locale_from_utf8 (file_name, -1,
470                                                  NULL, NULL, NULL);
471 #endif
472       module->handle = handle;
473       module->ref_count = 1;
474       module->is_resident = FALSE;
475       module->unload = NULL;
476       module->next = modules;
477       modules = module;
478       
479       /* check initialization */
480       if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
481         check_failed = check_init (module);
482       
483       /* we don't call unload() if the initialization check failed. */
484       if (!check_failed)
485         g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
486       
487       if (check_failed)
488         {
489           gchar *error;
490
491           error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
492           g_module_close (module);
493           module = NULL;
494           g_module_set_error (error);
495           g_free (error);
496         }
497       else
498         g_module_set_error (saved_error);
499
500       g_free (saved_error);
501     }
502
503   if (module != NULL &&
504       (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
505     g_module_make_resident (module);
506
507   g_static_rec_mutex_unlock (&g_module_global_lock);
508   return module;
509 }
510
511 #ifdef G_OS_WIN32
512
513 #undef g_module_open
514
515 GModule*
516 g_module_open (const gchar    *file_name,
517                GModuleFlags    flags)
518 {
519   gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
520   GModule *retval = g_module_open_utf8 (utf8_file_name, flags);
521
522   g_free (utf8_file_name);
523
524   return retval;
525 }
526
527 #endif
528
529 gboolean
530 g_module_close (GModule        *module)
531 {
532   SUPPORT_OR_RETURN (FALSE);
533   
534   g_return_val_if_fail (module != NULL, FALSE);
535   g_return_val_if_fail (module->ref_count > 0, FALSE);
536   
537   g_static_rec_mutex_lock (&g_module_global_lock);
538
539   module->ref_count--;
540   
541   if (!module->ref_count && !module->is_resident && module->unload)
542     {
543       GModuleUnload unload;
544
545       unload = module->unload;
546       module->unload = NULL;
547       unload (module);
548     }
549
550   if (!module->ref_count && !module->is_resident)
551     {
552       GModule *last;
553       GModule *node;
554       
555       last = NULL;
556       
557       node = modules;
558       while (node)
559         {
560           if (node == module)
561             {
562               if (last)
563                 last->next = node->next;
564               else
565                 modules = node->next;
566               break;
567             }
568           last = node;
569           node = last->next;
570         }
571       module->next = NULL;
572       
573       _g_module_close (module->handle, FALSE);
574       g_free (module->file_name);
575 #ifdef G_OS_WIN32
576       g_free (module->cp_file_name);
577 #endif
578       g_free (module);
579     }
580   
581   g_static_rec_mutex_unlock (&g_module_global_lock);
582   return g_module_error() == NULL;
583 }
584
585 void
586 g_module_make_resident (GModule *module)
587 {
588   g_return_if_fail (module != NULL);
589
590   module->is_resident = TRUE;
591 }
592
593 G_CONST_RETURN gchar*
594 g_module_error (void)
595 {
596   return g_static_private_get (&module_error_private);
597 }
598
599 gboolean
600 g_module_symbol (GModule        *module,
601                  const gchar    *symbol_name,
602                  gpointer       *symbol)
603 {
604   const gchar *module_error;
605
606   if (symbol)
607     *symbol = NULL;
608   SUPPORT_OR_RETURN (FALSE);
609   
610   g_return_val_if_fail (module != NULL, FALSE);
611   g_return_val_if_fail (symbol_name != NULL, FALSE);
612   g_return_val_if_fail (symbol != NULL, FALSE);
613   
614   g_static_rec_mutex_lock (&g_module_global_lock);
615
616 #ifdef  G_MODULE_NEED_USCORE
617   {
618     gchar *name;
619
620     name = g_strconcat ("_", symbol_name, NULL);
621     *symbol = _g_module_symbol (module->handle, name);
622     g_free (name);
623   }
624 #else   /* !G_MODULE_NEED_USCORE */
625   *symbol = _g_module_symbol (module->handle, symbol_name);
626 #endif  /* !G_MODULE_NEED_USCORE */
627   
628   module_error = g_module_error ();
629   if (module_error)
630     {
631       gchar *error;
632
633       error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
634       g_module_set_error (error);
635       g_free (error);
636       *symbol = NULL;
637     }
638   
639   g_static_rec_mutex_unlock (&g_module_global_lock);
640   return !module_error;
641 }
642
643 G_CONST_RETURN gchar*
644 g_module_name (GModule *module)
645 {
646   g_return_val_if_fail (module != NULL, NULL);
647   
648   if (module == main_module)
649     return "main";
650   
651   return module->file_name;
652 }
653
654 #ifdef G_OS_WIN32
655
656 #undef g_module_name
657
658 G_CONST_RETURN gchar*
659 g_module_name (GModule *module)
660 {
661   g_return_val_if_fail (module != NULL, NULL);
662   
663   if (module == main_module)
664     return "main";
665   
666   return module->cp_file_name;
667 }
668
669 #endif
670
671 gchar*
672 g_module_build_path (const gchar *directory,
673                      const gchar *module_name)
674 {
675   g_return_val_if_fail (module_name != NULL, NULL);
676   
677   return _g_module_build_path (directory, module_name);
678 }