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