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