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