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