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