applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[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 #include        "gmodule.h"
32 #include        "gmoduleconf.h"
33 #include        <errno.h>
34 #include        <string.h>
35
36
37 /* We maintain a list of modules, so we can reference count them.
38  * That's needed because some platforms don't support refernce counts on
39  * modules e.g. the shl_* implementation of HP-UX
40  * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
41  * Also, the module for the program itself is kept seperatedly for
42  * faster access and because it has special semantics.
43  */
44
45
46 /* --- structures --- */
47 struct _GModule
48 {
49   gchar *file_name;
50   gpointer handle;
51   guint ref_count : 31;
52   guint is_resident : 1;
53   GModuleUnload unload;
54   GModule *next;
55 };
56
57
58 /* --- prototypes --- */
59 static gpointer         _g_module_open          (const gchar    *file_name,
60                                                  gboolean        bind_lazy);
61 static void             _g_module_close         (gpointer        handle,
62                                                  gboolean        is_unref);
63 static gpointer         _g_module_self          (void);
64 static gpointer         _g_module_symbol        (gpointer        handle,
65                                                  const gchar    *symbol_name);
66 static gchar*           _g_module_build_path    (const gchar    *directory,
67                                                  const gchar    *module_name);
68 static inline void      g_module_set_error      (const gchar    *error);
69 static inline GModule*  g_module_find_by_handle (gpointer        handle);
70 static inline GModule*  g_module_find_by_name   (const gchar    *name);
71
72
73 /* --- variables --- */
74 G_LOCK_DEFINE_STATIC (GModule);
75 const char           *g_log_domain_gmodule = "GModule";
76 static GModule       *modules = NULL;
77 static GModule       *main_module = NULL;
78 static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
79
80
81 /* --- inline functions --- */
82 static inline GModule*
83 g_module_find_by_handle (gpointer handle)
84 {
85   GModule *module;
86   GModule *retval = NULL;
87   
88   G_LOCK (GModule);
89   if (main_module && main_module->handle == handle)
90     retval = main_module;
91   else
92     for (module = modules; module; module = module->next)
93       if (handle == module->handle)
94         {
95           retval = module;
96           break;
97         }
98   G_UNLOCK (GModule);
99
100   return retval;
101 }
102
103 static inline GModule*
104 g_module_find_by_name (const gchar *name)
105 {
106   GModule *module;
107   GModule *retval = NULL;
108   
109   G_LOCK (GModule);
110   for (module = modules; module; module = module->next)
111     if (strcmp (name, module->file_name) == 0)
112         {
113           retval = module;
114           break;
115         }
116   G_UNLOCK (GModule);
117
118   return retval;
119 }
120
121 static inline void
122 g_module_set_error (const gchar *error)
123 {
124   g_static_private_set (&module_error_private, g_strdup (error), g_free);
125   errno = 0;
126 }
127
128
129 /* --- include platform specifc code --- */
130 #define SUPPORT_OR_RETURN(rv)   { g_module_set_error (NULL); }
131 #if     (G_MODULE_IMPL == G_MODULE_IMPL_DL)
132 #include "gmodule-dl.c"
133 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
134 #include "gmodule-dld.c"
135 #elif   (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
136 #include "gmodule-win32.c"
137 #else
138 #undef  SUPPORT_OR_RETURN
139 #define SUPPORT_OR_RETURN(rv)   { g_module_set_error ("dynamic modules are " \
140                                               "not supported by this system"); return rv; }
141 static gpointer
142 _g_module_open (const gchar     *file_name,
143                 gboolean         bind_lazy)
144 {
145   return NULL;
146 }
147 static void
148 _g_module_close (gpointer        handle,
149                  gboolean        is_unref)
150 {
151 }
152 static gpointer
153 _g_module_self (void)
154 {
155   return NULL;
156 }
157 static gpointer
158 _g_module_symbol (gpointer       handle,
159                   const gchar   *symbol_name)
160 {
161   return NULL;
162 }
163 static gchar*
164 _g_module_build_path (const gchar *directory,
165                       const gchar *module_name)
166 {
167   return NULL;
168 }
169 #endif  /* no implementation */
170
171 /* --- functions --- */
172 gboolean
173 g_module_supported (void)
174 {
175   SUPPORT_OR_RETURN (FALSE);
176   
177   return TRUE;
178 }
179
180 GModule*
181 g_module_open (const gchar    *file_name,
182                GModuleFlags    flags)
183 {
184   GModule *module;
185   gpointer handle;
186   
187   SUPPORT_OR_RETURN (NULL);
188   
189   if (!file_name)
190     {      
191       G_LOCK (GModule);
192       if (!main_module)
193         {
194           handle = _g_module_self ();
195           if (handle)
196             {
197               main_module = g_new (GModule, 1);
198               main_module->file_name = NULL;
199               main_module->handle = handle;
200               main_module->ref_count = 1;
201               main_module->is_resident = TRUE;
202               main_module->unload = NULL;
203               main_module->next = NULL;
204             }
205         }
206       G_UNLOCK (GModule);
207
208       return main_module;
209     }
210   
211   /* we first search the module list by name */
212   module = g_module_find_by_name (file_name);
213   if (module)
214     {
215       module->ref_count++;
216       
217       return module;
218     }
219   
220   /* open the module */
221   handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
222   if (handle)
223     {
224       gchar *saved_error;
225       GModuleCheckInit check_init;
226       const gchar *check_failed = NULL;
227       
228       /* search the module list by handle, since file names are not unique */
229       module = g_module_find_by_handle (handle);
230       if (module)
231         {
232           _g_module_close (module->handle, TRUE);
233           module->ref_count++;
234           g_module_set_error (NULL);
235           
236           return module;
237         }
238       
239       saved_error = g_strdup (g_module_error ());
240       g_module_set_error (NULL);
241       
242       module = g_new (GModule, 1);
243       module->file_name = g_strdup (file_name);
244       module->handle = handle;
245       module->ref_count = 1;
246       module->is_resident = FALSE;
247       module->unload = NULL;
248       G_LOCK (GModule);
249       module->next = modules;
250       modules = module;
251       G_UNLOCK (GModule);
252       
253       /* check initialization */
254       if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
255         check_failed = check_init (module);
256       
257       /* we don't call unload() if the initialization check failed. */
258       if (!check_failed)
259         g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
260       
261       if (check_failed)
262         {
263           gchar *error;
264
265           error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
266           g_module_close (module);
267           module = NULL;
268           g_module_set_error (error);
269           g_free (error);
270         }
271       else
272         g_module_set_error (saved_error);
273
274       g_free (saved_error);
275     }
276   
277   return module;
278 }
279
280 gboolean
281 g_module_close (GModule        *module)
282 {
283   SUPPORT_OR_RETURN (FALSE);
284   
285   g_return_val_if_fail (module != NULL, FALSE);
286   g_return_val_if_fail (module->ref_count > 0, FALSE);
287   
288   module->ref_count--;
289   
290   if (!module->ref_count && !module->is_resident && module->unload)
291     {
292       GModuleUnload unload;
293
294       unload = module->unload;
295       module->unload = NULL;
296       unload (module);
297     }
298
299   if (!module->ref_count && !module->is_resident)
300     {
301       GModule *last;
302       GModule *node;
303       
304       last = NULL;
305       
306       G_LOCK (GModule);
307       node = modules;
308       while (node)
309         {
310           if (node == module)
311             {
312               if (last)
313                 last->next = node->next;
314               else
315                 modules = node->next;
316               break;
317             }
318           last = node;
319           node = last->next;
320         }
321       module->next = NULL;
322       G_UNLOCK (GModule);
323       
324       _g_module_close (module->handle, FALSE);
325       g_free (module->file_name);
326       
327       g_free (module);
328     }
329   
330   return g_module_error() == NULL;
331 }
332
333 void
334 g_module_make_resident (GModule *module)
335 {
336   g_return_if_fail (module != NULL);
337
338   module->is_resident = TRUE;
339 }
340
341 gchar*
342 g_module_error (void)
343 {
344   return g_static_private_get (&module_error_private);
345 }
346
347 gboolean
348 g_module_symbol (GModule        *module,
349                  const gchar    *symbol_name,
350                  gpointer       *symbol)
351 {
352   gchar *module_error;
353
354   if (symbol)
355     *symbol = NULL;
356   SUPPORT_OR_RETURN (FALSE);
357   
358   g_return_val_if_fail (module != NULL, FALSE);
359   g_return_val_if_fail (symbol_name != NULL, FALSE);
360   g_return_val_if_fail (symbol != NULL, FALSE);
361   
362 #ifdef  G_MODULE_NEED_USCORE
363   {
364     gchar *name;
365
366     name = g_strconcat ("_", symbol_name, NULL);
367     *symbol = _g_module_symbol (module->handle, name);
368     g_free (name);
369   }
370 #else   /* !G_MODULE_NEED_USCORE */
371   *symbol = _g_module_symbol (module->handle, symbol_name);
372 #endif  /* !G_MODULE_NEED_USCORE */
373   
374   module_error = g_module_error ();
375   if (module_error)
376     {
377       gchar *error;
378
379       error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
380       g_module_set_error (error);
381       g_free (error);
382       *symbol = NULL;
383
384       return FALSE;
385     }
386   
387   return TRUE;
388 }
389
390 gchar*
391 g_module_name (GModule *module)
392 {
393   g_return_val_if_fail (module != NULL, NULL);
394   
395   if (module == main_module)
396     return "main";
397   
398   return module->file_name;
399 }
400
401 gchar*
402 g_module_build_path (const gchar *directory,
403                      const gchar *module_name)
404 {
405   g_return_val_if_fail (module_name != NULL, NULL);
406   
407   return _g_module_build_path (directory, module_name);
408 }