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