sl@0
|
1 |
/* GMODULE - GLIB wrapper code for dynamic module loading
|
sl@0
|
2 |
* Copyright (C) 1998 Tim Janik
|
sl@0
|
3 |
* Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
7 |
* License as published by the Free Software Foundation; either
|
sl@0
|
8 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
9 |
*
|
sl@0
|
10 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
13 |
* Lesser General Public License for more details.
|
sl@0
|
14 |
*
|
sl@0
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
sl@0
|
16 |
* License along with this library; if not, write to the
|
sl@0
|
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
sl@0
|
18 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
19 |
*/
|
sl@0
|
20 |
|
sl@0
|
21 |
/*
|
sl@0
|
22 |
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
sl@0
|
23 |
* file for a list of people on the GLib Team. See the ChangeLog
|
sl@0
|
24 |
* files for a list of changes. These files are distributed with
|
sl@0
|
25 |
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
sl@0
|
26 |
*/
|
sl@0
|
27 |
|
sl@0
|
28 |
/*
|
sl@0
|
29 |
* MT safe
|
sl@0
|
30 |
*/
|
sl@0
|
31 |
|
sl@0
|
32 |
#include "config.h"
|
sl@0
|
33 |
|
sl@0
|
34 |
#include "glib.h"
|
sl@0
|
35 |
#include "gmodule.h"
|
sl@0
|
36 |
|
sl@0
|
37 |
#include <errno.h>
|
sl@0
|
38 |
#include <string.h>
|
sl@0
|
39 |
#include <sys/types.h>
|
sl@0
|
40 |
#include <sys/stat.h>
|
sl@0
|
41 |
#include <fcntl.h>
|
sl@0
|
42 |
#ifdef HAVE_UNISTD_H
|
sl@0
|
43 |
#include <unistd.h>
|
sl@0
|
44 |
#endif
|
sl@0
|
45 |
#ifdef G_OS_WIN32
|
sl@0
|
46 |
#include <io.h> /* For open() and close() prototypes. */
|
sl@0
|
47 |
#endif
|
sl@0
|
48 |
|
sl@0
|
49 |
#include "gmoduleconf.h"
|
sl@0
|
50 |
#include "gstdio.h"
|
sl@0
|
51 |
|
sl@0
|
52 |
#ifdef __SYMBIAN32__
|
sl@0
|
53 |
#include "gmodule_wsd.h"
|
sl@0
|
54 |
#endif /* __SYMBIAN32__ */
|
sl@0
|
55 |
|
sl@0
|
56 |
/* We maintain a list of modules, so we can reference count them.
|
sl@0
|
57 |
* That's needed because some platforms don't support refernce counts on
|
sl@0
|
58 |
* modules e.g. the shl_* implementation of HP-UX
|
sl@0
|
59 |
* (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
|
sl@0
|
60 |
* Also, the module for the program itself is kept seperatedly for
|
sl@0
|
61 |
* faster access and because it has special semantics.
|
sl@0
|
62 |
*/
|
sl@0
|
63 |
|
sl@0
|
64 |
|
sl@0
|
65 |
/* --- structures --- */
|
sl@0
|
66 |
struct _GModule
|
sl@0
|
67 |
{
|
sl@0
|
68 |
gchar *file_name;
|
sl@0
|
69 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
70 |
gchar *cp_file_name;
|
sl@0
|
71 |
#endif
|
sl@0
|
72 |
gpointer handle;
|
sl@0
|
73 |
guint ref_count : 31;
|
sl@0
|
74 |
guint is_resident : 1;
|
sl@0
|
75 |
GModuleUnload unload;
|
sl@0
|
76 |
GModule *next;
|
sl@0
|
77 |
};
|
sl@0
|
78 |
|
sl@0
|
79 |
|
sl@0
|
80 |
/* --- prototypes --- */
|
sl@0
|
81 |
static gpointer _g_module_open (const gchar *file_name,
|
sl@0
|
82 |
gboolean bind_lazy,
|
sl@0
|
83 |
gboolean bind_local);
|
sl@0
|
84 |
static void _g_module_close (gpointer handle,
|
sl@0
|
85 |
gboolean is_unref);
|
sl@0
|
86 |
static gpointer _g_module_self (void);
|
sl@0
|
87 |
static gpointer _g_module_symbol (gpointer handle,
|
sl@0
|
88 |
const gchar *symbol_name);
|
sl@0
|
89 |
static gchar* _g_module_build_path (const gchar *directory,
|
sl@0
|
90 |
const gchar *module_name);
|
sl@0
|
91 |
static inline void g_module_set_error (const gchar *error);
|
sl@0
|
92 |
static inline GModule* g_module_find_by_handle (gpointer handle);
|
sl@0
|
93 |
static inline GModule* g_module_find_by_name (const gchar *name);
|
sl@0
|
94 |
|
sl@0
|
95 |
|
sl@0
|
96 |
/* --- variables --- */
|
sl@0
|
97 |
#if EMULATOR
|
sl@0
|
98 |
|
sl@0
|
99 |
PLS(modules,gmodule,GModule *)
|
sl@0
|
100 |
PLS(main_module,gmodule,GModule *)
|
sl@0
|
101 |
PLS(module_error_private,gmodule,GStaticPrivate)
|
sl@0
|
102 |
PLS(module_debug_initialized,gmodule,gboolean)
|
sl@0
|
103 |
PLS(module_debug_flags,gmodule,guint)
|
sl@0
|
104 |
|
sl@0
|
105 |
#define modules (*FUNCTION_NAME(modules,gmodule)())
|
sl@0
|
106 |
#define main_module (*FUNCTION_NAME(main_module,gmodule)())
|
sl@0
|
107 |
#define module_error_private (*FUNCTION_NAME(module_error_private,gmodule)())
|
sl@0
|
108 |
#define module_debug_initialized (*FUNCTION_NAME(module_debug_initialized,gmodule)())
|
sl@0
|
109 |
#define module_debug_flags (*FUNCTION_NAME(module_debug_flags,gmodule)())
|
sl@0
|
110 |
|
sl@0
|
111 |
#else
|
sl@0
|
112 |
static GModule *modules = NULL;
|
sl@0
|
113 |
static GModule *main_module = NULL;
|
sl@0
|
114 |
static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
|
sl@0
|
115 |
static gboolean module_debug_initialized = FALSE;
|
sl@0
|
116 |
static guint module_debug_flags = 0;
|
sl@0
|
117 |
#endif /* EMULATOR */
|
sl@0
|
118 |
|
sl@0
|
119 |
/* --- inline functions --- */
|
sl@0
|
120 |
static inline GModule*
|
sl@0
|
121 |
g_module_find_by_handle (gpointer handle)
|
sl@0
|
122 |
{
|
sl@0
|
123 |
GModule *module;
|
sl@0
|
124 |
GModule *retval = NULL;
|
sl@0
|
125 |
|
sl@0
|
126 |
if (main_module && main_module->handle == handle)
|
sl@0
|
127 |
retval = main_module;
|
sl@0
|
128 |
else
|
sl@0
|
129 |
for (module = modules; module; module = module->next)
|
sl@0
|
130 |
if (handle == module->handle)
|
sl@0
|
131 |
{
|
sl@0
|
132 |
retval = module;
|
sl@0
|
133 |
break;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
return retval;
|
sl@0
|
137 |
}
|
sl@0
|
138 |
|
sl@0
|
139 |
static inline GModule*
|
sl@0
|
140 |
g_module_find_by_name (const gchar *name)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
GModule *module;
|
sl@0
|
143 |
GModule *retval = NULL;
|
sl@0
|
144 |
|
sl@0
|
145 |
for (module = modules; module; module = module->next)
|
sl@0
|
146 |
if (strcmp (name, module->file_name) == 0)
|
sl@0
|
147 |
{
|
sl@0
|
148 |
retval = module;
|
sl@0
|
149 |
break;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
|
sl@0
|
152 |
return retval;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
|
sl@0
|
155 |
static inline void
|
sl@0
|
156 |
g_module_set_error_unduped (gchar *error)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
g_static_private_set (&module_error_private, error, g_free);
|
sl@0
|
159 |
errno = 0;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
static inline void
|
sl@0
|
163 |
g_module_set_error (const gchar *error)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
g_module_set_error_unduped (g_strdup (error));
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
|
sl@0
|
169 |
/* --- include platform specifc code --- */
|
sl@0
|
170 |
#define SUPPORT_OR_RETURN(rv) { g_module_set_error (NULL); }
|
sl@0
|
171 |
#if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
|
sl@0
|
172 |
#include "gmodule-dl.c"
|
sl@0
|
173 |
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
|
sl@0
|
174 |
#include "gmodule-dld.c"
|
sl@0
|
175 |
#elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
|
sl@0
|
176 |
#include "gmodule-win32.c"
|
sl@0
|
177 |
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DYLD)
|
sl@0
|
178 |
#include "gmodule-dyld.c"
|
sl@0
|
179 |
#elif (G_MODULE_IMPL == G_MODULE_IMPL_AR)
|
sl@0
|
180 |
#include "gmodule-ar.c"
|
sl@0
|
181 |
#else
|
sl@0
|
182 |
#undef SUPPORT_OR_RETURN
|
sl@0
|
183 |
#define SUPPORT_OR_RETURN(rv) { g_module_set_error ("dynamic modules are " \
|
sl@0
|
184 |
"not supported by this system"); return rv; }
|
sl@0
|
185 |
static gpointer
|
sl@0
|
186 |
_g_module_open (const gchar *file_name,
|
sl@0
|
187 |
gboolean bind_lazy,
|
sl@0
|
188 |
gboolean bind_local)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
return NULL;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
static void
|
sl@0
|
193 |
_g_module_close (gpointer handle,
|
sl@0
|
194 |
gboolean is_unref)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
}
|
sl@0
|
197 |
static gpointer
|
sl@0
|
198 |
_g_module_self (void)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
return NULL;
|
sl@0
|
201 |
}
|
sl@0
|
202 |
static gpointer
|
sl@0
|
203 |
_g_module_symbol (gpointer handle,
|
sl@0
|
204 |
const gchar *symbol_name)
|
sl@0
|
205 |
{
|
sl@0
|
206 |
return NULL;
|
sl@0
|
207 |
}
|
sl@0
|
208 |
static gchar*
|
sl@0
|
209 |
_g_module_build_path (const gchar *directory,
|
sl@0
|
210 |
const gchar *module_name)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
return NULL;
|
sl@0
|
213 |
}
|
sl@0
|
214 |
#endif /* no implementation */
|
sl@0
|
215 |
|
sl@0
|
216 |
/* --- functions --- */
|
sl@0
|
217 |
EXPORT_C gboolean
|
sl@0
|
218 |
g_module_supported (void)
|
sl@0
|
219 |
{
|
sl@0
|
220 |
SUPPORT_OR_RETURN (FALSE);
|
sl@0
|
221 |
|
sl@0
|
222 |
return TRUE;
|
sl@0
|
223 |
}
|
sl@0
|
224 |
|
sl@0
|
225 |
static gchar*
|
sl@0
|
226 |
parse_libtool_archive (const gchar* libtool_name)
|
sl@0
|
227 |
{
|
sl@0
|
228 |
const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
|
sl@0
|
229 |
const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
|
sl@0
|
230 |
const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
|
sl@0
|
231 |
gchar *lt_dlname = NULL;
|
sl@0
|
232 |
gboolean lt_installed = TRUE;
|
sl@0
|
233 |
gchar *lt_libdir = NULL;
|
sl@0
|
234 |
gchar *name;
|
sl@0
|
235 |
GTokenType token;
|
sl@0
|
236 |
GScanner *scanner;
|
sl@0
|
237 |
|
sl@0
|
238 |
int fd = g_open (libtool_name, O_RDONLY, 0);
|
sl@0
|
239 |
if (fd < 0)
|
sl@0
|
240 |
{
|
sl@0
|
241 |
gchar *display_libtool_name = g_filename_display_name (libtool_name);
|
sl@0
|
242 |
g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", display_libtool_name));
|
sl@0
|
243 |
g_free (display_libtool_name);
|
sl@0
|
244 |
return NULL;
|
sl@0
|
245 |
}
|
sl@0
|
246 |
/* search libtool's dlname specification */
|
sl@0
|
247 |
scanner = g_scanner_new (NULL);
|
sl@0
|
248 |
g_scanner_input_file (scanner, fd);
|
sl@0
|
249 |
scanner->config->symbol_2_token = TRUE;
|
sl@0
|
250 |
g_scanner_scope_add_symbol (scanner, 0, "dlname",
|
sl@0
|
251 |
GUINT_TO_POINTER (TOKEN_DLNAME));
|
sl@0
|
252 |
g_scanner_scope_add_symbol (scanner, 0, "installed",
|
sl@0
|
253 |
GUINT_TO_POINTER (TOKEN_INSTALLED));
|
sl@0
|
254 |
g_scanner_scope_add_symbol (scanner, 0, "libdir",
|
sl@0
|
255 |
GUINT_TO_POINTER (TOKEN_LIBDIR));
|
sl@0
|
256 |
while (!g_scanner_eof (scanner))
|
sl@0
|
257 |
{
|
sl@0
|
258 |
token = g_scanner_get_next_token (scanner);
|
sl@0
|
259 |
if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED ||
|
sl@0
|
260 |
token == TOKEN_LIBDIR)
|
sl@0
|
261 |
{
|
sl@0
|
262 |
if (g_scanner_get_next_token (scanner) != '=' ||
|
sl@0
|
263 |
g_scanner_get_next_token (scanner) !=
|
sl@0
|
264 |
(token == TOKEN_INSTALLED ?
|
sl@0
|
265 |
G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
|
sl@0
|
266 |
{
|
sl@0
|
267 |
gchar *display_libtool_name = g_filename_display_name (libtool_name);
|
sl@0
|
268 |
g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", display_libtool_name));
|
sl@0
|
269 |
g_free (display_libtool_name);
|
sl@0
|
270 |
|
sl@0
|
271 |
g_free (lt_dlname);
|
sl@0
|
272 |
g_free (lt_libdir);
|
sl@0
|
273 |
g_scanner_destroy (scanner);
|
sl@0
|
274 |
close (fd);
|
sl@0
|
275 |
|
sl@0
|
276 |
return NULL;
|
sl@0
|
277 |
}
|
sl@0
|
278 |
else
|
sl@0
|
279 |
{
|
sl@0
|
280 |
if (token == TOKEN_DLNAME)
|
sl@0
|
281 |
{
|
sl@0
|
282 |
g_free (lt_dlname);
|
sl@0
|
283 |
lt_dlname = g_strdup (scanner->value.v_string);
|
sl@0
|
284 |
}
|
sl@0
|
285 |
else if (token == TOKEN_INSTALLED)
|
sl@0
|
286 |
lt_installed =
|
sl@0
|
287 |
strcmp (scanner->value.v_identifier, "yes") == 0;
|
sl@0
|
288 |
else /* token == TOKEN_LIBDIR */
|
sl@0
|
289 |
{
|
sl@0
|
290 |
g_free (lt_libdir);
|
sl@0
|
291 |
lt_libdir = g_strdup (scanner->value.v_string);
|
sl@0
|
292 |
}
|
sl@0
|
293 |
}
|
sl@0
|
294 |
}
|
sl@0
|
295 |
}
|
sl@0
|
296 |
|
sl@0
|
297 |
if (!lt_installed)
|
sl@0
|
298 |
{
|
sl@0
|
299 |
gchar *dir = g_path_get_dirname (libtool_name);
|
sl@0
|
300 |
g_free (lt_libdir);
|
sl@0
|
301 |
lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
|
sl@0
|
302 |
g_free (dir);
|
sl@0
|
303 |
}
|
sl@0
|
304 |
|
sl@0
|
305 |
name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
|
sl@0
|
306 |
|
sl@0
|
307 |
g_free (lt_dlname);
|
sl@0
|
308 |
g_free (lt_libdir);
|
sl@0
|
309 |
g_scanner_destroy (scanner);
|
sl@0
|
310 |
close (fd);
|
sl@0
|
311 |
|
sl@0
|
312 |
return name;
|
sl@0
|
313 |
}
|
sl@0
|
314 |
|
sl@0
|
315 |
static inline gboolean
|
sl@0
|
316 |
str_check_suffix (const gchar* string,
|
sl@0
|
317 |
const gchar* suffix)
|
sl@0
|
318 |
{
|
sl@0
|
319 |
gsize string_len = strlen (string);
|
sl@0
|
320 |
gsize suffix_len = strlen (suffix);
|
sl@0
|
321 |
|
sl@0
|
322 |
return string_len >= suffix_len &&
|
sl@0
|
323 |
strcmp (string + string_len - suffix_len, suffix) == 0;
|
sl@0
|
324 |
}
|
sl@0
|
325 |
|
sl@0
|
326 |
enum
|
sl@0
|
327 |
{
|
sl@0
|
328 |
G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0,
|
sl@0
|
329 |
G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1
|
sl@0
|
330 |
};
|
sl@0
|
331 |
|
sl@0
|
332 |
static void
|
sl@0
|
333 |
_g_module_debug_init (void)
|
sl@0
|
334 |
{
|
sl@0
|
335 |
const GDebugKey keys[] = {
|
sl@0
|
336 |
{ "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
|
sl@0
|
337 |
{ "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
|
sl@0
|
338 |
};
|
sl@0
|
339 |
const gchar *env;
|
sl@0
|
340 |
|
sl@0
|
341 |
env = g_getenv ("G_DEBUG");
|
sl@0
|
342 |
|
sl@0
|
343 |
module_debug_flags =
|
sl@0
|
344 |
!env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));
|
sl@0
|
345 |
|
sl@0
|
346 |
module_debug_initialized = TRUE;
|
sl@0
|
347 |
}
|
sl@0
|
348 |
|
sl@0
|
349 |
#if EMULATOR
|
sl@0
|
350 |
PLS(g_module_global_lock,gmodule,GStaticRecMutex)
|
sl@0
|
351 |
#define g_module_global_lock (*FUNCTION_NAME(g_module_global_lock,gmodule)())
|
sl@0
|
352 |
#else
|
sl@0
|
353 |
static GStaticRecMutex g_module_global_lock = G_STATIC_REC_MUTEX_INIT;
|
sl@0
|
354 |
#endif /* EMULATOR */
|
sl@0
|
355 |
|
sl@0
|
356 |
EXPORT_C GModule*
|
sl@0
|
357 |
g_module_open (const gchar *file_name,
|
sl@0
|
358 |
GModuleFlags flags)
|
sl@0
|
359 |
{
|
sl@0
|
360 |
GModule *module;
|
sl@0
|
361 |
gpointer handle = NULL;
|
sl@0
|
362 |
gchar *name = NULL;
|
sl@0
|
363 |
|
sl@0
|
364 |
SUPPORT_OR_RETURN (NULL);
|
sl@0
|
365 |
|
sl@0
|
366 |
g_static_rec_mutex_lock (&g_module_global_lock);
|
sl@0
|
367 |
|
sl@0
|
368 |
if (G_UNLIKELY (!module_debug_initialized))
|
sl@0
|
369 |
_g_module_debug_init ();
|
sl@0
|
370 |
|
sl@0
|
371 |
if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
|
sl@0
|
372 |
flags &= ~G_MODULE_BIND_LAZY;
|
sl@0
|
373 |
|
sl@0
|
374 |
if (!file_name)
|
sl@0
|
375 |
{
|
sl@0
|
376 |
if (!main_module)
|
sl@0
|
377 |
{
|
sl@0
|
378 |
handle = _g_module_self ();
|
sl@0
|
379 |
if (handle)
|
sl@0
|
380 |
{
|
sl@0
|
381 |
main_module = g_new (GModule, 1);
|
sl@0
|
382 |
main_module->file_name = NULL;
|
sl@0
|
383 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
384 |
main_module->cp_file_name = NULL;
|
sl@0
|
385 |
#endif
|
sl@0
|
386 |
main_module->handle = handle;
|
sl@0
|
387 |
main_module->ref_count = 1;
|
sl@0
|
388 |
main_module->is_resident = TRUE;
|
sl@0
|
389 |
main_module->unload = NULL;
|
sl@0
|
390 |
main_module->next = NULL;
|
sl@0
|
391 |
}
|
sl@0
|
392 |
}
|
sl@0
|
393 |
else
|
sl@0
|
394 |
main_module->ref_count++;
|
sl@0
|
395 |
|
sl@0
|
396 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
397 |
return main_module;
|
sl@0
|
398 |
}
|
sl@0
|
399 |
|
sl@0
|
400 |
/* we first search the module list by name */
|
sl@0
|
401 |
module = g_module_find_by_name (file_name);
|
sl@0
|
402 |
if (module)
|
sl@0
|
403 |
{
|
sl@0
|
404 |
module->ref_count++;
|
sl@0
|
405 |
|
sl@0
|
406 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
407 |
return module;
|
sl@0
|
408 |
}
|
sl@0
|
409 |
|
sl@0
|
410 |
/* check whether we have a readable file right away */
|
sl@0
|
411 |
if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
|
sl@0
|
412 |
name = g_strdup (file_name);
|
sl@0
|
413 |
/* try completing file name with standard library suffix */
|
sl@0
|
414 |
if (!name)
|
sl@0
|
415 |
{
|
sl@0
|
416 |
name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
|
sl@0
|
417 |
if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
|
sl@0
|
418 |
{
|
sl@0
|
419 |
g_free (name);
|
sl@0
|
420 |
name = NULL;
|
sl@0
|
421 |
}
|
sl@0
|
422 |
}
|
sl@0
|
423 |
/* try completing by appending libtool suffix */
|
sl@0
|
424 |
if (!name)
|
sl@0
|
425 |
{
|
sl@0
|
426 |
name = g_strconcat (file_name, ".la", NULL);
|
sl@0
|
427 |
if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
|
sl@0
|
428 |
{
|
sl@0
|
429 |
g_free (name);
|
sl@0
|
430 |
name = NULL;
|
sl@0
|
431 |
}
|
sl@0
|
432 |
}
|
sl@0
|
433 |
/* we can't access() the file, lets hope the platform backends finds
|
sl@0
|
434 |
* it via library paths
|
sl@0
|
435 |
*/
|
sl@0
|
436 |
if (!name)
|
sl@0
|
437 |
{
|
sl@0
|
438 |
gchar *dot = strrchr (file_name, '.');
|
sl@0
|
439 |
gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
|
sl@0
|
440 |
|
sl@0
|
441 |
/* make sure the name has a suffix */
|
sl@0
|
442 |
if (!dot || dot < slash)
|
sl@0
|
443 |
name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
|
sl@0
|
444 |
else
|
sl@0
|
445 |
name = g_strdup (file_name);
|
sl@0
|
446 |
}
|
sl@0
|
447 |
|
sl@0
|
448 |
/* ok, try loading the module */
|
sl@0
|
449 |
if (name)
|
sl@0
|
450 |
{
|
sl@0
|
451 |
/* if it's a libtool archive, figure library file to load */
|
sl@0
|
452 |
if (str_check_suffix (name, ".la")) /* libtool archive? */
|
sl@0
|
453 |
{
|
sl@0
|
454 |
gchar *real_name = parse_libtool_archive (name);
|
sl@0
|
455 |
|
sl@0
|
456 |
/* real_name might be NULL, but then module error is already set */
|
sl@0
|
457 |
if (real_name)
|
sl@0
|
458 |
{
|
sl@0
|
459 |
g_free (name);
|
sl@0
|
460 |
name = real_name;
|
sl@0
|
461 |
}
|
sl@0
|
462 |
}
|
sl@0
|
463 |
if (name)
|
sl@0
|
464 |
handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
|
sl@0
|
465 |
(flags & G_MODULE_BIND_LOCAL) != 0);
|
sl@0
|
466 |
}
|
sl@0
|
467 |
else
|
sl@0
|
468 |
{
|
sl@0
|
469 |
gchar *display_file_name = g_filename_display_name (file_name);
|
sl@0
|
470 |
g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", display_file_name));
|
sl@0
|
471 |
g_free (display_file_name);
|
sl@0
|
472 |
}
|
sl@0
|
473 |
g_free (name);
|
sl@0
|
474 |
|
sl@0
|
475 |
if (handle)
|
sl@0
|
476 |
{
|
sl@0
|
477 |
gchar *saved_error;
|
sl@0
|
478 |
GModuleCheckInit check_init;
|
sl@0
|
479 |
const gchar *check_failed = NULL;
|
sl@0
|
480 |
|
sl@0
|
481 |
/* search the module list by handle, since file names are not unique */
|
sl@0
|
482 |
module = g_module_find_by_handle (handle);
|
sl@0
|
483 |
if (module)
|
sl@0
|
484 |
{
|
sl@0
|
485 |
_g_module_close (module->handle, TRUE);
|
sl@0
|
486 |
module->ref_count++;
|
sl@0
|
487 |
g_module_set_error (NULL);
|
sl@0
|
488 |
|
sl@0
|
489 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
490 |
return module;
|
sl@0
|
491 |
}
|
sl@0
|
492 |
|
sl@0
|
493 |
saved_error = g_strdup (g_module_error ());
|
sl@0
|
494 |
g_module_set_error (NULL);
|
sl@0
|
495 |
|
sl@0
|
496 |
module = g_new (GModule, 1);
|
sl@0
|
497 |
module->file_name = g_strdup (file_name);
|
sl@0
|
498 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
499 |
module->cp_file_name = g_locale_from_utf8 (file_name, -1,
|
sl@0
|
500 |
NULL, NULL, NULL);
|
sl@0
|
501 |
#endif
|
sl@0
|
502 |
module->handle = handle;
|
sl@0
|
503 |
module->ref_count = 1;
|
sl@0
|
504 |
module->is_resident = FALSE;
|
sl@0
|
505 |
module->unload = NULL;
|
sl@0
|
506 |
module->next = modules;
|
sl@0
|
507 |
modules = module;
|
sl@0
|
508 |
|
sl@0
|
509 |
/* check initialization */
|
sl@0
|
510 |
if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
|
sl@0
|
511 |
check_failed = check_init (module);
|
sl@0
|
512 |
|
sl@0
|
513 |
/* we don't call unload() if the initialization check failed. */
|
sl@0
|
514 |
if (!check_failed)
|
sl@0
|
515 |
g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
|
sl@0
|
516 |
|
sl@0
|
517 |
if (check_failed)
|
sl@0
|
518 |
{
|
sl@0
|
519 |
gchar *error;
|
sl@0
|
520 |
|
sl@0
|
521 |
error = g_strconcat ("GModule (",
|
sl@0
|
522 |
file_name ? file_name : "NULL",
|
sl@0
|
523 |
") initialization check failed: ",
|
sl@0
|
524 |
check_failed, NULL);
|
sl@0
|
525 |
g_module_close (module);
|
sl@0
|
526 |
module = NULL;
|
sl@0
|
527 |
g_module_set_error (error);
|
sl@0
|
528 |
g_free (error);
|
sl@0
|
529 |
}
|
sl@0
|
530 |
else
|
sl@0
|
531 |
g_module_set_error (saved_error);
|
sl@0
|
532 |
|
sl@0
|
533 |
g_free (saved_error);
|
sl@0
|
534 |
}
|
sl@0
|
535 |
|
sl@0
|
536 |
if (module != NULL &&
|
sl@0
|
537 |
(module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
|
sl@0
|
538 |
g_module_make_resident (module);
|
sl@0
|
539 |
|
sl@0
|
540 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
541 |
return module;
|
sl@0
|
542 |
}
|
sl@0
|
543 |
|
sl@0
|
544 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
545 |
|
sl@0
|
546 |
#undef g_module_open
|
sl@0
|
547 |
|
sl@0
|
548 |
GModule*
|
sl@0
|
549 |
g_module_open (const gchar *file_name,
|
sl@0
|
550 |
GModuleFlags flags)
|
sl@0
|
551 |
{
|
sl@0
|
552 |
gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
|
sl@0
|
553 |
GModule *retval = g_module_open_utf8 (utf8_file_name, flags);
|
sl@0
|
554 |
|
sl@0
|
555 |
g_free (utf8_file_name);
|
sl@0
|
556 |
|
sl@0
|
557 |
return retval;
|
sl@0
|
558 |
}
|
sl@0
|
559 |
|
sl@0
|
560 |
#endif
|
sl@0
|
561 |
|
sl@0
|
562 |
EXPORT_C gboolean
|
sl@0
|
563 |
g_module_close (GModule *module)
|
sl@0
|
564 |
{
|
sl@0
|
565 |
SUPPORT_OR_RETURN (FALSE);
|
sl@0
|
566 |
|
sl@0
|
567 |
g_return_val_if_fail (module != NULL, FALSE);
|
sl@0
|
568 |
g_return_val_if_fail (module->ref_count > 0, FALSE);
|
sl@0
|
569 |
|
sl@0
|
570 |
g_static_rec_mutex_lock (&g_module_global_lock);
|
sl@0
|
571 |
|
sl@0
|
572 |
module->ref_count--;
|
sl@0
|
573 |
|
sl@0
|
574 |
if (!module->ref_count && !module->is_resident && module->unload)
|
sl@0
|
575 |
{
|
sl@0
|
576 |
GModuleUnload unload;
|
sl@0
|
577 |
|
sl@0
|
578 |
unload = module->unload;
|
sl@0
|
579 |
module->unload = NULL;
|
sl@0
|
580 |
unload (module);
|
sl@0
|
581 |
}
|
sl@0
|
582 |
|
sl@0
|
583 |
if (!module->ref_count && !module->is_resident)
|
sl@0
|
584 |
{
|
sl@0
|
585 |
GModule *last;
|
sl@0
|
586 |
GModule *node;
|
sl@0
|
587 |
|
sl@0
|
588 |
last = NULL;
|
sl@0
|
589 |
|
sl@0
|
590 |
node = modules;
|
sl@0
|
591 |
while (node)
|
sl@0
|
592 |
{
|
sl@0
|
593 |
if (node == module)
|
sl@0
|
594 |
{
|
sl@0
|
595 |
if (last)
|
sl@0
|
596 |
last->next = node->next;
|
sl@0
|
597 |
else
|
sl@0
|
598 |
modules = node->next;
|
sl@0
|
599 |
break;
|
sl@0
|
600 |
}
|
sl@0
|
601 |
last = node;
|
sl@0
|
602 |
node = last->next;
|
sl@0
|
603 |
}
|
sl@0
|
604 |
module->next = NULL;
|
sl@0
|
605 |
|
sl@0
|
606 |
_g_module_close (module->handle, FALSE);
|
sl@0
|
607 |
g_free (module->file_name);
|
sl@0
|
608 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
609 |
g_free (module->cp_file_name);
|
sl@0
|
610 |
#endif
|
sl@0
|
611 |
g_free (module);
|
sl@0
|
612 |
}
|
sl@0
|
613 |
|
sl@0
|
614 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
615 |
return g_module_error() == NULL;
|
sl@0
|
616 |
}
|
sl@0
|
617 |
|
sl@0
|
618 |
EXPORT_C void
|
sl@0
|
619 |
g_module_make_resident (GModule *module)
|
sl@0
|
620 |
{
|
sl@0
|
621 |
g_return_if_fail (module != NULL);
|
sl@0
|
622 |
|
sl@0
|
623 |
module->is_resident = TRUE;
|
sl@0
|
624 |
}
|
sl@0
|
625 |
|
sl@0
|
626 |
EXPORT_C G_CONST_RETURN gchar*
|
sl@0
|
627 |
g_module_error (void)
|
sl@0
|
628 |
{
|
sl@0
|
629 |
return g_static_private_get (&module_error_private);
|
sl@0
|
630 |
}
|
sl@0
|
631 |
|
sl@0
|
632 |
EXPORT_C gboolean
|
sl@0
|
633 |
g_module_symbol (GModule *module,
|
sl@0
|
634 |
const gchar *symbol_name,
|
sl@0
|
635 |
gpointer *symbol)
|
sl@0
|
636 |
{
|
sl@0
|
637 |
const gchar *module_error;
|
sl@0
|
638 |
|
sl@0
|
639 |
if (symbol)
|
sl@0
|
640 |
*symbol = NULL;
|
sl@0
|
641 |
SUPPORT_OR_RETURN (FALSE);
|
sl@0
|
642 |
|
sl@0
|
643 |
g_return_val_if_fail (module != NULL, FALSE);
|
sl@0
|
644 |
g_return_val_if_fail (symbol_name != NULL, FALSE);
|
sl@0
|
645 |
g_return_val_if_fail (symbol != NULL, FALSE);
|
sl@0
|
646 |
|
sl@0
|
647 |
g_static_rec_mutex_lock (&g_module_global_lock);
|
sl@0
|
648 |
|
sl@0
|
649 |
#ifdef G_MODULE_NEED_USCORE
|
sl@0
|
650 |
{
|
sl@0
|
651 |
gchar *name;
|
sl@0
|
652 |
|
sl@0
|
653 |
name = g_strconcat ("_", symbol_name, NULL);
|
sl@0
|
654 |
*symbol = _g_module_symbol (module->handle, name);
|
sl@0
|
655 |
g_free (name);
|
sl@0
|
656 |
}
|
sl@0
|
657 |
#else /* !G_MODULE_NEED_USCORE */
|
sl@0
|
658 |
*symbol = _g_module_symbol (module->handle, symbol_name);
|
sl@0
|
659 |
#endif /* !G_MODULE_NEED_USCORE */
|
sl@0
|
660 |
|
sl@0
|
661 |
module_error = g_module_error ();
|
sl@0
|
662 |
if (module_error)
|
sl@0
|
663 |
{
|
sl@0
|
664 |
gchar *error;
|
sl@0
|
665 |
|
sl@0
|
666 |
error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
|
sl@0
|
667 |
g_module_set_error (error);
|
sl@0
|
668 |
g_free (error);
|
sl@0
|
669 |
*symbol = NULL;
|
sl@0
|
670 |
}
|
sl@0
|
671 |
|
sl@0
|
672 |
g_static_rec_mutex_unlock (&g_module_global_lock);
|
sl@0
|
673 |
return !module_error;
|
sl@0
|
674 |
}
|
sl@0
|
675 |
|
sl@0
|
676 |
EXPORT_C G_CONST_RETURN gchar*
|
sl@0
|
677 |
g_module_name (GModule *module)
|
sl@0
|
678 |
{
|
sl@0
|
679 |
g_return_val_if_fail (module != NULL, NULL);
|
sl@0
|
680 |
|
sl@0
|
681 |
if (module == main_module)
|
sl@0
|
682 |
return "main";
|
sl@0
|
683 |
|
sl@0
|
684 |
return module->file_name;
|
sl@0
|
685 |
}
|
sl@0
|
686 |
|
sl@0
|
687 |
#if defined (G_OS_WIN32) && !defined(_WIN64)
|
sl@0
|
688 |
|
sl@0
|
689 |
#undef g_module_name
|
sl@0
|
690 |
|
sl@0
|
691 |
G_CONST_RETURN gchar*
|
sl@0
|
692 |
g_module_name (GModule *module)
|
sl@0
|
693 |
{
|
sl@0
|
694 |
g_return_val_if_fail (module != NULL, NULL);
|
sl@0
|
695 |
|
sl@0
|
696 |
if (module == main_module)
|
sl@0
|
697 |
return "main";
|
sl@0
|
698 |
|
sl@0
|
699 |
return module->cp_file_name;
|
sl@0
|
700 |
}
|
sl@0
|
701 |
|
sl@0
|
702 |
#endif
|
sl@0
|
703 |
|
sl@0
|
704 |
EXPORT_C gchar*
|
sl@0
|
705 |
g_module_build_path (const gchar *directory,
|
sl@0
|
706 |
const gchar *module_name)
|
sl@0
|
707 |
{
|
sl@0
|
708 |
g_return_val_if_fail (module_name != NULL, NULL);
|
sl@0
|
709 |
|
sl@0
|
710 |
return _g_module_build_path (directory, module_name);
|
sl@0
|
711 |
}
|
sl@0
|
712 |
|
sl@0
|
713 |
#define __GMODULE_C__
|