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