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