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