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