Released GLib 1.1.8
[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 static G_LOCK_DEFINE (g_module_global);
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 (g_module_global);
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 (g_module_global);
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 (g_module_global);
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 (g_module_global);
110
111   return retval;
112 }
113
114 static inline void
115 g_module_set_error (const gchar *error)
116 {
117   gchar* module_error = g_static_private_get (&module_error_private);
118   if (module_error)
119     g_free (module_error);
120   if (error)
121     module_error = g_strdup (error);
122   else
123     module_error = NULL;
124   errno = 0;
125   g_static_private_set (&module_error_private, module_error, g_free);
126 }
127
128
129 /* --- include platform specifc code --- */
130 #define CHECK_ERROR(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  CHECK_ERROR
139 #define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
140 static gpointer
141 _g_module_open (const gchar     *file_name,
142                 gboolean         bind_lazy)
143 {
144   return NULL;
145 }
146 static void
147 _g_module_close (gpointer        handle,
148                  gboolean        is_unref)
149 {
150 }
151 static gpointer
152 _g_module_self (void)
153 {
154   return NULL;
155 }
156 static gpointer
157 _g_module_symbol (gpointer       handle,
158                   const gchar   *symbol_name)
159 {
160   return NULL;
161 }
162 static gchar*
163 _g_module_build_path (const gchar *directory,
164                       const gchar *module_name)
165 {
166   return NULL;
167 }
168 #endif  /* no implementation */
169
170 #if defined (NATIVE_WIN32) && defined (__LCC__)
171 int __stdcall 
172 LibMain (void         *hinstDll,
173          unsigned long dwReason,
174          void         *reserved)
175 {
176   return 1;
177 }
178 #endif /* NATIVE_WIN32 && __LCC__ */
179
180
181 /* --- functions --- */
182 gboolean
183 g_module_supported (void)
184 {
185   CHECK_ERROR (FALSE);
186   
187   return TRUE;
188 }
189
190 GModule*
191 g_module_open (const gchar    *file_name,
192                GModuleFlags    flags)
193 {
194   GModule *module;
195   gpointer handle;
196   
197   CHECK_ERROR (NULL);
198   
199   if (!file_name)
200     {      
201       g_lock (g_module_global);
202       if (!main_module)
203         {
204           handle = _g_module_self ();
205           if (handle)
206             {
207               main_module = g_new (GModule, 1);
208               main_module->file_name = NULL;
209               main_module->handle = handle;
210               main_module->ref_count = 1;
211               main_module->is_resident = TRUE;
212               main_module->unload = NULL;
213               main_module->next = NULL;
214             }
215         }
216       g_unlock (g_module_global);
217
218       return main_module;
219     }
220   
221   /* we first search the module list by name */
222   module = g_module_find_by_name (file_name);
223   if (module)
224     {
225       module->ref_count++;
226       
227       return module;
228     }
229   
230   /* open the module */
231   handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
232   if (handle)
233     {
234       gchar *saved_error;
235       GModuleCheckInit check_init;
236       const gchar *check_failed = NULL;
237       
238       /* search the module list by handle, since file names are not unique */
239       module = g_module_find_by_handle (handle);
240       if (module)
241         {
242           _g_module_close (module->handle, TRUE);
243           module->ref_count++;
244           g_module_set_error (NULL);
245           
246           return module;
247         }
248       
249       saved_error = g_module_error();
250       g_static_private_set (&module_error_private, NULL, NULL);
251       g_module_set_error (NULL);
252       
253       module = g_new (GModule, 1);
254       module->file_name = g_strdup (file_name);
255       module->handle = handle;
256       module->ref_count = 1;
257       module->is_resident = FALSE;
258       module->unload = NULL;
259       g_lock (g_module_global);
260       module->next = modules;
261       modules = module;
262       g_unlock (g_module_global);
263       
264       /* check initialization */
265       if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
266         check_failed = check_init (module);
267       
268       /* we don't call unload() if the initialization check failed. */
269       if (!check_failed)
270         g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
271       
272       if (check_failed)
273         {
274           gchar *error;
275
276           error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
277           g_module_close (module);
278           module = NULL;
279           g_module_set_error (error);
280           g_free (error);
281         }
282       else
283         g_module_set_error (saved_error);
284       g_free (saved_error);
285     }
286   
287   return module;
288 }
289
290 gboolean
291 g_module_close (GModule        *module)
292 {
293   CHECK_ERROR (FALSE);
294   
295   g_return_val_if_fail (module != NULL, FALSE);
296   g_return_val_if_fail (module->ref_count > 0, FALSE);
297   
298   module->ref_count--;
299   
300   if (!module->ref_count && !module->is_resident && module->unload)
301     {
302       GModuleUnload unload;
303
304       unload = module->unload;
305       module->unload = NULL;
306       unload (module);
307     }
308
309   if (!module->ref_count && !module->is_resident)
310     {
311       GModule *last;
312       GModule *node;
313       
314       last = NULL;
315       
316       g_lock (g_module_global);
317       node = modules;
318       while (node)
319         {
320           if (node == module)
321             {
322               if (last)
323                 last->next = node->next;
324               else
325                 modules = node->next;
326               break;
327             }
328           last = node;
329           node = last->next;
330         }
331       module->next = NULL;
332       g_unlock (g_module_global);
333       
334       _g_module_close (module->handle, FALSE);
335       g_free (module->file_name);
336       
337       g_free (module);
338     }
339   
340   return g_module_error() == NULL;
341 }
342
343 void
344 g_module_make_resident (GModule *module)
345 {
346   g_return_if_fail (module != NULL);
347
348   module->is_resident = TRUE;
349 }
350
351 gchar*
352 g_module_error (void)
353 {
354   return g_static_private_get (&module_error_private);
355 }
356
357 gboolean
358 g_module_symbol (GModule        *module,
359                  const gchar    *symbol_name,
360                  gpointer       *symbol)
361 {
362   gchar *module_error;
363   if (symbol)
364     *symbol = NULL;
365   CHECK_ERROR (FALSE);
366   
367   g_return_val_if_fail (module != NULL, FALSE);
368   g_return_val_if_fail (symbol_name != NULL, FALSE);
369   g_return_val_if_fail (symbol != NULL, FALSE);
370   
371 #ifdef  G_MODULE_NEED_USCORE
372   {
373     gchar *name;
374
375     name = g_strconcat ("_", symbol_name, NULL);
376     *symbol = _g_module_symbol (module->handle, name);
377     g_free (name);
378   }
379 #else   /* !G_MODULE_NEED_USCORE */
380   *symbol = _g_module_symbol (module->handle, symbol_name);
381 #endif  /* !G_MODULE_NEED_USCORE */
382   
383   if ((module_error = g_module_error()))
384     {
385       gchar *error;
386
387       error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
388       g_module_set_error (error);
389       g_free (error);
390       *symbol = NULL;
391       return FALSE;
392     }
393   
394   return TRUE;
395 }
396
397 gchar*
398 g_module_name (GModule *module)
399 {
400   g_return_val_if_fail (module != NULL, NULL);
401   
402   if (module == main_module)
403     return "main";
404   
405   return module->file_name;
406 }
407
408 gchar*
409 g_module_build_path (const gchar *directory,
410                      const gchar *module_name)
411 {
412   g_return_val_if_fail (module_name != NULL, NULL);
413   
414   return _g_module_build_path (directory, module_name);
415 }