/src/glib/gmodule/gmodule.c
Line | Count | Source |
1 | | /* GMODULE - GLIB wrapper code for dynamic module loading |
2 | | * Copyright (C) 1998 Tim Janik |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | /* |
21 | | * Modified by the GLib Team and others 1997-2000. 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 "config.h" |
32 | | |
33 | | /* For the G_MODULE_SUFFIX macro; since macro deprecation is implemented |
34 | | * in the preprocessor, we need to define this before including glib.h */ |
35 | | #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS |
36 | | #define GLIB_DISABLE_DEPRECATION_WARNINGS |
37 | | #endif |
38 | | |
39 | | #include "glib.h" |
40 | | #include "gmodule.h" |
41 | | |
42 | | #include <errno.h> |
43 | | #include <string.h> |
44 | | #include <sys/types.h> |
45 | | #include <sys/stat.h> |
46 | | #include <fcntl.h> |
47 | | #ifdef G_OS_UNIX |
48 | | #include <unistd.h> |
49 | | #endif |
50 | | #ifdef G_OS_WIN32 |
51 | | #include <io.h> /* For open() and close() prototypes. */ |
52 | | #endif |
53 | | |
54 | | #ifndef O_CLOEXEC |
55 | | #define O_CLOEXEC 0 |
56 | | #endif |
57 | | |
58 | | #include "gmoduleconf.h" |
59 | | #include "gstdio.h" |
60 | | |
61 | | |
62 | | /** |
63 | | * GModule: |
64 | | * |
65 | | * The #GModule struct is an opaque data structure to represent a |
66 | | * [dynamically-loaded module](modules.html#dynamic-loading-of-modules). |
67 | | * It should only be accessed via the following functions. |
68 | | * |
69 | | * To ensure correct lock ordering, these functions must not be called from |
70 | | * global constructors (for example, those using GCC’s |
71 | | * `__attribute__((constructor))` attribute). |
72 | | */ |
73 | | |
74 | | /** |
75 | | * GModuleCheckInit: |
76 | | * @module: the #GModule corresponding to the module which has just been loaded |
77 | | * |
78 | | * Specifies the type of the module initialization function. |
79 | | * If a module contains a function named g_module_check_init() it is called |
80 | | * automatically when the module is loaded. It is passed the #GModule structure |
81 | | * and should return %NULL on success or a string describing the initialization |
82 | | * error. |
83 | | * |
84 | | * Returns: %NULL on success, or a string describing the initialization error |
85 | | */ |
86 | | |
87 | | /** |
88 | | * GModuleUnload: |
89 | | * @module: the #GModule about to be unloaded |
90 | | * |
91 | | * Specifies the type of the module function called when it is unloaded. |
92 | | * If a module contains a function named g_module_unload() it is called |
93 | | * automatically when the module is unloaded. |
94 | | * It is passed the #GModule structure. |
95 | | */ |
96 | | |
97 | | /** |
98 | | * G_MODULE_SUFFIX: |
99 | | * |
100 | | * Expands to a shared library suffix for the current platform without the |
101 | | * leading dot. On Unixes this is "so", and on Windows this is "dll". |
102 | | * |
103 | | * Deprecated: 2.76: Use g_module_open() instead with @module_name as the |
104 | | * basename of the file_name argument. You will get the wrong results using |
105 | | * this macro most of the time: |
106 | | * |
107 | | * 1. The suffix on macOS is usually 'dylib', but it's 'so' when using |
108 | | * Autotools, so there's no way to get the suffix correct using |
109 | | * a pre-processor macro. |
110 | | * 2. Prefixes also vary in a platform-specific way. You may or may not have |
111 | | * a 'lib' prefix for the name on Windows and on Cygwin the prefix is |
112 | | * 'cyg'. |
113 | | * 3. The library name itself can vary per platform. For instance, you may |
114 | | * want to load foo-1.dll on Windows and libfoo.1.dylib on macOS. |
115 | | * |
116 | | * g_module_open() takes care of all this by searching the filesystem for |
117 | | * combinations of possible suffixes and prefixes. |
118 | | */ |
119 | | |
120 | | /** |
121 | | * G_MODULE_EXPORT: |
122 | | * |
123 | | * Used to declare functions exported by libraries or modules. |
124 | | * |
125 | | * When compiling for Windows, it marks the symbol as `dllexport`. |
126 | | * |
127 | | * When compiling for Linux and Unices, it marks the symbol as having `default` |
128 | | * visibility. This is no-op unless the code is being compiled with a |
129 | | * non-default |
130 | | * [visibility flag](https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility-1260) |
131 | | * such as `hidden`. |
132 | | * |
133 | | * This macro must only be used when compiling a shared module. Modules that |
134 | | * support both shared and static linking should define their own macro that |
135 | | * expands to %G_MODULE_EXPORT when compiling the shared module, but is empty |
136 | | * when compiling the static module on Windows. |
137 | | */ |
138 | | |
139 | | /** |
140 | | * G_MODULE_IMPORT: |
141 | | * |
142 | | * Used to declare functions imported from modules. |
143 | | */ |
144 | | |
145 | | /* We maintain a list of modules, so we can reference count them. |
146 | | * That's needed because some platforms don't support references counts on |
147 | | * modules. Also, the module for the program itself is kept separately for |
148 | | * faster access and because it has special semantics. |
149 | | */ |
150 | | |
151 | | |
152 | | /* --- structures --- */ |
153 | | struct _GModule |
154 | | { |
155 | | gchar *file_name; |
156 | | gpointer handle; |
157 | | guint ref_count : 31; |
158 | | guint is_resident : 1; |
159 | | GModuleUnload unload; |
160 | | GModule *next; |
161 | | }; |
162 | | |
163 | | |
164 | | /* --- prototypes --- */ |
165 | | static gpointer _g_module_open (const gchar *file_name, |
166 | | gboolean bind_lazy, |
167 | | gboolean bind_local, |
168 | | GError **error); |
169 | | static void _g_module_close (gpointer handle); |
170 | | static gpointer _g_module_self (void); |
171 | | static gpointer _g_module_symbol (gpointer handle, |
172 | | const gchar *symbol_name); |
173 | | #if (G_MODULE_IMPL != G_MODULE_IMPL_DL) && (G_MODULE_IMPL != G_MODULE_IMPL_AR) |
174 | | static gchar* _g_module_build_path (const gchar *directory, |
175 | | const gchar *module_name); |
176 | | #else |
177 | | /* Implementation is in gmodule-deprecated.c */ |
178 | | gchar* _g_module_build_path (const gchar *directory, |
179 | | const gchar *module_name); |
180 | | #endif |
181 | | static inline void g_module_set_error (const gchar *error); |
182 | | static inline GModule* g_module_find_by_handle (gpointer handle); |
183 | | static inline GModule* g_module_find_by_name (const gchar *name); |
184 | | |
185 | | |
186 | | /* --- variables --- */ |
187 | | static GModule *modules = NULL; |
188 | | static GModule *main_module = NULL; |
189 | | static GPrivate module_error_private = G_PRIVATE_INIT (g_free); |
190 | | static gboolean module_debug_initialized = FALSE; |
191 | | static guint module_debug_flags = 0; |
192 | | |
193 | | |
194 | | /* --- inline functions --- */ |
195 | | static inline GModule* |
196 | | g_module_find_by_handle (gpointer handle) |
197 | 0 | { |
198 | 0 | GModule *module; |
199 | 0 | GModule *retval = NULL; |
200 | | |
201 | 0 | if (main_module && main_module->handle == handle) |
202 | 0 | retval = main_module; |
203 | 0 | else |
204 | 0 | for (module = modules; module; module = module->next) |
205 | 0 | if (handle == module->handle) |
206 | 0 | { |
207 | 0 | retval = module; |
208 | 0 | break; |
209 | 0 | } |
210 | |
|
211 | 0 | return retval; |
212 | 0 | } |
213 | | |
214 | | static inline GModule* |
215 | | g_module_find_by_name (const gchar *name) |
216 | 0 | { |
217 | 0 | GModule *module; |
218 | 0 | GModule *retval = NULL; |
219 | | |
220 | 0 | for (module = modules; module; module = module->next) |
221 | 0 | if (strcmp (name, module->file_name) == 0) |
222 | 0 | { |
223 | 0 | retval = module; |
224 | 0 | break; |
225 | 0 | } |
226 | |
|
227 | 0 | return retval; |
228 | 0 | } |
229 | | |
230 | | static inline void |
231 | | g_module_set_error_unduped (gchar *error) |
232 | 0 | { |
233 | 0 | g_private_replace (&module_error_private, error); |
234 | 0 | errno = 0; |
235 | 0 | } |
236 | | |
237 | | static inline void |
238 | | g_module_set_error (const gchar *error) |
239 | 0 | { |
240 | 0 | g_module_set_error_unduped (g_strdup (error)); |
241 | 0 | } |
242 | | |
243 | | |
244 | | /* --- include platform specific code --- */ |
245 | 0 | #define SUPPORT_OR_RETURN(rv) { g_module_set_error (NULL); } |
246 | | #if (G_MODULE_IMPL == G_MODULE_IMPL_DL) |
247 | | #include "gmodule-dl.c" |
248 | | #elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32) |
249 | | #include "gmodule-win32.c" |
250 | | #elif (G_MODULE_IMPL == G_MODULE_IMPL_AR) |
251 | | #include "gmodule-ar.c" |
252 | | #else |
253 | | #undef SUPPORT_OR_RETURN |
254 | | #define SUPPORT_OR_RETURN(rv) { g_module_set_error ("dynamic modules are " \ |
255 | | "not supported by this system"); return rv; } |
256 | | static gpointer |
257 | | _g_module_open (const gchar *file_name, |
258 | | gboolean bind_lazy, |
259 | | gboolean bind_local, |
260 | | GError **error) |
261 | | { |
262 | | g_module_set_error (NULL); |
263 | | return NULL; |
264 | | } |
265 | | static void |
266 | | _g_module_close (gpointer handle) |
267 | | { |
268 | | } |
269 | | static gpointer |
270 | | _g_module_self (void) |
271 | | { |
272 | | return NULL; |
273 | | } |
274 | | static gpointer |
275 | | _g_module_symbol (gpointer handle, |
276 | | const gchar *symbol_name) |
277 | | { |
278 | | return NULL; |
279 | | } |
280 | | static gchar* |
281 | | _g_module_build_path (const gchar *directory, |
282 | | const gchar *module_name) |
283 | | { |
284 | | return NULL; |
285 | | } |
286 | | #endif /* no implementation */ |
287 | | |
288 | | /** |
289 | | * G_MODULE_ERROR: |
290 | | * |
291 | | * The error domain of the #GModule API. |
292 | | * |
293 | | * Since: 2.70 |
294 | | */ |
295 | | G_DEFINE_QUARK (g-module-error-quark, g_module_error) |
296 | | |
297 | | /* --- functions --- */ |
298 | | |
299 | | /** |
300 | | * g_module_supported: |
301 | | * |
302 | | * Checks if modules are supported on the current platform. |
303 | | * |
304 | | * Returns: %TRUE if modules are supported |
305 | | */ |
306 | | gboolean |
307 | | g_module_supported (void) |
308 | 0 | { |
309 | 0 | SUPPORT_OR_RETURN (FALSE); |
310 | | |
311 | 0 | return TRUE; |
312 | 0 | } |
313 | | |
314 | | static gchar* |
315 | | parse_libtool_archive (const gchar* libtool_name) |
316 | 0 | { |
317 | 0 | const guint TOKEN_DLNAME = G_TOKEN_LAST + 1; |
318 | 0 | const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2; |
319 | 0 | const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3; |
320 | 0 | gchar *lt_dlname = NULL; |
321 | 0 | gboolean lt_installed = TRUE; |
322 | 0 | gchar *lt_libdir = NULL; |
323 | 0 | gchar *name; |
324 | 0 | GTokenType token; |
325 | 0 | GScanner *scanner; |
326 | | |
327 | 0 | int fd = g_open (libtool_name, O_RDONLY | O_CLOEXEC, 0); |
328 | 0 | if (fd < 0) |
329 | 0 | { |
330 | 0 | gchar *display_libtool_name = g_filename_display_name (libtool_name); |
331 | 0 | g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive ‘%s’", display_libtool_name)); |
332 | 0 | g_free (display_libtool_name); |
333 | 0 | return NULL; |
334 | 0 | } |
335 | | /* search libtool's dlname specification */ |
336 | 0 | scanner = g_scanner_new (NULL); |
337 | 0 | g_scanner_input_file (scanner, fd); |
338 | 0 | scanner->config->symbol_2_token = TRUE; |
339 | 0 | g_scanner_scope_add_symbol (scanner, 0, "dlname", |
340 | 0 | GUINT_TO_POINTER (TOKEN_DLNAME)); |
341 | 0 | g_scanner_scope_add_symbol (scanner, 0, "installed", |
342 | 0 | GUINT_TO_POINTER (TOKEN_INSTALLED)); |
343 | 0 | g_scanner_scope_add_symbol (scanner, 0, "libdir", |
344 | 0 | GUINT_TO_POINTER (TOKEN_LIBDIR)); |
345 | 0 | while (!g_scanner_eof (scanner)) |
346 | 0 | { |
347 | 0 | token = g_scanner_get_next_token (scanner); |
348 | 0 | if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || |
349 | 0 | token == TOKEN_LIBDIR) |
350 | 0 | { |
351 | 0 | if (g_scanner_get_next_token (scanner) != '=' || |
352 | 0 | g_scanner_get_next_token (scanner) != |
353 | 0 | (token == TOKEN_INSTALLED ? |
354 | 0 | G_TOKEN_IDENTIFIER : G_TOKEN_STRING)) |
355 | 0 | { |
356 | 0 | gchar *display_libtool_name = g_filename_display_name (libtool_name); |
357 | 0 | g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s’", display_libtool_name)); |
358 | 0 | g_free (display_libtool_name); |
359 | |
|
360 | 0 | g_free (lt_dlname); |
361 | 0 | g_free (lt_libdir); |
362 | 0 | g_scanner_destroy (scanner); |
363 | 0 | close (fd); |
364 | |
|
365 | 0 | return NULL; |
366 | 0 | } |
367 | 0 | else |
368 | 0 | { |
369 | 0 | if (token == TOKEN_DLNAME) |
370 | 0 | { |
371 | 0 | g_free (lt_dlname); |
372 | 0 | lt_dlname = g_strdup (scanner->value.v_string); |
373 | 0 | } |
374 | 0 | else if (token == TOKEN_INSTALLED) |
375 | 0 | lt_installed = |
376 | 0 | strcmp (scanner->value.v_identifier, "yes") == 0; |
377 | 0 | else /* token == TOKEN_LIBDIR */ |
378 | 0 | { |
379 | 0 | g_free (lt_libdir); |
380 | 0 | lt_libdir = g_strdup (scanner->value.v_string); |
381 | 0 | } |
382 | 0 | } |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | 0 | if (!lt_installed) |
387 | 0 | { |
388 | 0 | gchar *dir = g_path_get_dirname (libtool_name); |
389 | 0 | g_free (lt_libdir); |
390 | 0 | lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL); |
391 | 0 | g_free (dir); |
392 | 0 | } |
393 | |
|
394 | 0 | g_clear_pointer (&scanner, g_scanner_destroy); |
395 | 0 | close (g_steal_fd (&fd)); |
396 | |
|
397 | 0 | if (lt_libdir == NULL || lt_dlname == NULL) |
398 | 0 | { |
399 | 0 | gchar *display_libtool_name = g_filename_display_name (libtool_name); |
400 | 0 | g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive ‘%s’", display_libtool_name)); |
401 | 0 | g_free (display_libtool_name); |
402 | |
|
403 | 0 | return NULL; |
404 | 0 | } |
405 | | |
406 | 0 | name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL); |
407 | | |
408 | 0 | g_free (lt_dlname); |
409 | 0 | g_free (lt_libdir); |
410 | |
|
411 | 0 | return name; |
412 | 0 | } |
413 | | |
414 | | enum |
415 | | { |
416 | | G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0, |
417 | | G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1 |
418 | | } G_GNUC_FLAG_ENUM; |
419 | | |
420 | | static void |
421 | | _g_module_debug_init (void) |
422 | 0 | { |
423 | 0 | const GDebugKey keys[] = { |
424 | 0 | { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES }, |
425 | 0 | { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES } |
426 | 0 | }; |
427 | 0 | const gchar *env; |
428 | |
|
429 | 0 | env = g_getenv ("G_DEBUG"); |
430 | |
|
431 | 0 | module_debug_flags = |
432 | 0 | !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys)); |
433 | |
|
434 | 0 | module_debug_initialized = TRUE; |
435 | 0 | } |
436 | | |
437 | | static GRecMutex g_module_global_lock; |
438 | | |
439 | | /** |
440 | | * g_module_open_full: |
441 | | * @file_name: (nullable): the name or path to the file containing the module, |
442 | | * or %NULL to obtain a #GModule representing the main program itself |
443 | | * @flags: the flags used for opening the module. This can be the |
444 | | * logical OR of any of the #GModuleFlags |
445 | | * @error: #GError. |
446 | | * |
447 | | * Opens a module. If the module has already been opened, its reference count |
448 | | * is incremented. If not, the module is searched using @file_name. |
449 | | * |
450 | | * Since 2.76, the search order/behavior is as follows: |
451 | | * |
452 | | * 1. If @file_name exists as a regular file, it is used as-is; else |
453 | | * 2. If @file_name doesn't have the correct suffix and/or prefix for the |
454 | | * platform, then possible suffixes and prefixes will be added to the |
455 | | * basename till a file is found and whatever is found will be used; else |
456 | | * 3. If @file_name doesn't have the ".la"-suffix, ".la" is appended. Either |
457 | | * way, if a matching .la file exists (and is a libtool archive) the |
458 | | * libtool archive is parsed to find the actual file name, and that is |
459 | | * used. |
460 | | * |
461 | | * If, at the end of all this, we have a file path that we can access on disk, |
462 | | * it is opened as a module. If not, @file_name is attempted to be opened as a |
463 | | * module verbatim in the hopes that the system implementation will somehow be |
464 | | * able to access it. If that is not possible, %NULL is returned. |
465 | | * |
466 | | * Note that this behaviour was different prior to 2.76, but there is some |
467 | | * overlap in functionality. If backwards compatibility is an issue, kindly |
468 | | * consult earlier #GModule documentation for the prior search order/behavior |
469 | | * of @file_name. |
470 | | * |
471 | | * Returns: a #GModule on success, or %NULL on failure |
472 | | * |
473 | | * Since: 2.70 |
474 | | */ |
475 | | GModule* |
476 | | g_module_open_full (const gchar *file_name, |
477 | | GModuleFlags flags, |
478 | | GError **error) |
479 | 0 | { |
480 | 0 | GModule *module; |
481 | 0 | gpointer handle = NULL; |
482 | 0 | gchar *name = NULL; |
483 | | |
484 | 0 | SUPPORT_OR_RETURN (NULL); |
485 | |
|
486 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
487 | | |
488 | 0 | g_rec_mutex_lock (&g_module_global_lock); |
489 | |
|
490 | 0 | if (G_UNLIKELY (!module_debug_initialized)) |
491 | 0 | _g_module_debug_init (); |
492 | |
|
493 | 0 | if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES) |
494 | 0 | flags &= (unsigned) ~G_MODULE_BIND_LAZY; |
495 | |
|
496 | 0 | if (!file_name) |
497 | 0 | { |
498 | 0 | if (!main_module) |
499 | 0 | { |
500 | 0 | handle = _g_module_self (); |
501 | | /* On Android 64 bit, RTLD_DEFAULT is (void *)0x0 |
502 | | * so it always fails to create main_module if file_name is NULL */ |
503 | 0 | #if !defined(__ANDROID__) || !defined(__LP64__) |
504 | 0 | if (handle) |
505 | 0 | #endif |
506 | 0 | { |
507 | 0 | main_module = g_new (GModule, 1); |
508 | 0 | main_module->file_name = NULL; |
509 | 0 | main_module->handle = handle; |
510 | 0 | main_module->ref_count = 1; |
511 | 0 | main_module->is_resident = TRUE; |
512 | 0 | main_module->unload = NULL; |
513 | 0 | main_module->next = NULL; |
514 | 0 | } |
515 | 0 | } |
516 | 0 | else |
517 | 0 | main_module->ref_count++; |
518 | |
|
519 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
520 | 0 | return main_module; |
521 | 0 | } |
522 | | |
523 | | /* we first search the module list by name */ |
524 | 0 | module = g_module_find_by_name (file_name); |
525 | 0 | if (module) |
526 | 0 | { |
527 | 0 | module->ref_count++; |
528 | | |
529 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
530 | 0 | return module; |
531 | 0 | } |
532 | | |
533 | | /* check whether we have a readable file right away */ |
534 | 0 | if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR)) |
535 | 0 | name = g_strdup (file_name); |
536 | | /* try completing file name with standard library suffix */ |
537 | 0 | if (!name) |
538 | 0 | { |
539 | 0 | char *basename, *dirname; |
540 | 0 | size_t prefix_idx = 0, suffix_idx = 0; |
541 | 0 | const char *prefixes[2] = {0}, *suffixes[2] = {0}; |
542 | |
|
543 | 0 | basename = g_path_get_basename (file_name); |
544 | 0 | dirname = g_path_get_dirname (file_name); |
545 | | #ifdef G_OS_WIN32 |
546 | | if (!g_str_has_prefix (basename, "lib")) |
547 | | prefixes[prefix_idx++] = "lib"; |
548 | | prefixes[prefix_idx++] = ""; |
549 | | if (!g_str_has_suffix (basename, ".dll")) |
550 | | suffixes[suffix_idx++] = ".dll"; |
551 | | #else |
552 | | #ifdef __CYGWIN__ |
553 | | #ifdef __MSYS__ |
554 | | if (!g_str_has_prefix (basename, "msys-")) |
555 | | prefixes[prefix_idx++] = "msys-"; |
556 | | #else |
557 | | if (!g_str_has_prefix (basename, "cyg")) |
558 | | prefixes[prefix_idx++] = "cyg"; |
559 | | #endif |
560 | | #else |
561 | 0 | if (!g_str_has_prefix (basename, "lib")) |
562 | 0 | prefixes[prefix_idx++] = "lib"; |
563 | 0 | else |
564 | | /* People commonly pass `libfoo` as the file_name and want us to |
565 | | * auto-detect the suffix as .la or .so, etc. We need to also find |
566 | | * .dylib and .dll in those cases. */ |
567 | 0 | prefixes[prefix_idx++] = ""; |
568 | 0 | #endif |
569 | | #ifdef __CYGWIN__ |
570 | | if (!g_str_has_suffix (basename, ".dll")) |
571 | | suffixes[suffix_idx++] = ".dll"; |
572 | | #elif defined (__APPLE__) |
573 | | if (!g_str_has_suffix (basename, ".dylib") && |
574 | | !g_str_has_suffix (basename, ".so")) |
575 | | { |
576 | | suffixes[suffix_idx++] = ".dylib"; |
577 | | suffixes[suffix_idx++] = ".so"; |
578 | | } |
579 | | #else |
580 | 0 | if (!g_str_has_suffix (basename, ".so")) |
581 | 0 | suffixes[suffix_idx++] = ".so"; |
582 | 0 | #endif |
583 | 0 | #endif |
584 | 0 | for (guint i = 0; i < prefix_idx; i++) |
585 | 0 | { |
586 | 0 | for (guint j = 0; j < suffix_idx; j++) |
587 | 0 | { |
588 | 0 | name = g_strconcat (dirname, G_DIR_SEPARATOR_S, prefixes[i], |
589 | 0 | basename, suffixes[j], NULL); |
590 | 0 | if (g_file_test (name, G_FILE_TEST_IS_REGULAR)) |
591 | 0 | goto name_found; |
592 | 0 | g_free (name); |
593 | 0 | name = NULL; |
594 | 0 | } |
595 | 0 | } |
596 | 0 | name_found: |
597 | 0 | g_free (basename); |
598 | 0 | g_free (dirname); |
599 | 0 | } |
600 | | /* try completing by appending libtool suffix */ |
601 | 0 | if (!name) |
602 | 0 | { |
603 | 0 | name = g_strconcat (file_name, ".la", NULL); |
604 | 0 | if (!g_file_test (name, G_FILE_TEST_IS_REGULAR)) |
605 | 0 | { |
606 | 0 | g_free (name); |
607 | 0 | name = NULL; |
608 | 0 | } |
609 | 0 | } |
610 | | /* we can't access() the file, lets hope the platform backends finds |
611 | | * it via library paths |
612 | | */ |
613 | 0 | if (!name) |
614 | 0 | { |
615 | 0 | gchar *dot = strrchr (file_name, '.'); |
616 | 0 | gchar *slash = strrchr (file_name, G_DIR_SEPARATOR); |
617 | | |
618 | | /* we make sure the name has a suffix using the deprecated |
619 | | * G_MODULE_SUFFIX for backward-compat */ |
620 | 0 | if (!dot || dot < slash) |
621 | 0 | name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL); |
622 | 0 | else |
623 | 0 | name = g_strdup (file_name); |
624 | 0 | } |
625 | | |
626 | | /* ok, try loading the module */ |
627 | 0 | g_assert (name != NULL); |
628 | | |
629 | | /* if it's a libtool archive, figure library file to load */ |
630 | 0 | if (g_str_has_suffix (name, ".la")) /* libtool archive? */ |
631 | 0 | { |
632 | 0 | gchar *real_name = parse_libtool_archive (name); |
633 | | |
634 | | /* real_name might be NULL, but then module error is already set */ |
635 | 0 | if (real_name) |
636 | 0 | { |
637 | 0 | g_free (name); |
638 | 0 | name = real_name; |
639 | 0 | } |
640 | 0 | } |
641 | |
|
642 | 0 | handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0, |
643 | 0 | (flags & G_MODULE_BIND_LOCAL) != 0, error); |
644 | 0 | g_free (name); |
645 | |
|
646 | 0 | if (handle) |
647 | 0 | { |
648 | 0 | gchar *saved_error; |
649 | 0 | GModuleCheckInit check_init; |
650 | 0 | const gchar *check_failed = NULL; |
651 | | |
652 | | /* search the module list by handle, since file names are not unique */ |
653 | 0 | module = g_module_find_by_handle (handle); |
654 | 0 | if (module) |
655 | 0 | { |
656 | 0 | _g_module_close (module->handle); |
657 | 0 | module->ref_count++; |
658 | 0 | g_module_set_error (NULL); |
659 | | |
660 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
661 | 0 | return module; |
662 | 0 | } |
663 | | |
664 | 0 | saved_error = g_strdup (g_module_error ()); |
665 | 0 | g_module_set_error (NULL); |
666 | | |
667 | 0 | module = g_new (GModule, 1); |
668 | 0 | module->file_name = g_strdup (file_name); |
669 | 0 | module->handle = handle; |
670 | 0 | module->ref_count = 1; |
671 | 0 | module->is_resident = FALSE; |
672 | 0 | module->unload = NULL; |
673 | 0 | module->next = modules; |
674 | 0 | modules = module; |
675 | | |
676 | | /* check initialization */ |
677 | 0 | if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL) |
678 | 0 | check_failed = check_init (module); |
679 | | |
680 | | /* we don't call unload() if the initialization check failed. */ |
681 | 0 | if (!check_failed) |
682 | 0 | g_module_symbol (module, "g_module_unload", (gpointer) &module->unload); |
683 | | |
684 | 0 | if (check_failed) |
685 | 0 | { |
686 | 0 | gchar *temp_error; |
687 | |
|
688 | 0 | temp_error = g_strconcat ("GModule (", file_name, ") ", |
689 | 0 | "initialization check failed: ", |
690 | 0 | check_failed, NULL); |
691 | 0 | g_module_close (module); |
692 | 0 | module = NULL; |
693 | 0 | g_module_set_error (temp_error); |
694 | 0 | g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_CHECK_FAILED, temp_error); |
695 | 0 | g_free (temp_error); |
696 | 0 | } |
697 | 0 | else |
698 | 0 | g_module_set_error (saved_error); |
699 | |
|
700 | 0 | g_free (saved_error); |
701 | 0 | } |
702 | | |
703 | 0 | if (module != NULL && |
704 | 0 | (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES)) |
705 | 0 | g_module_make_resident (module); |
706 | |
|
707 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
708 | 0 | return module; |
709 | 0 | } |
710 | | |
711 | | /** |
712 | | * g_module_open: |
713 | | * @file_name: (nullable): the name or path to the file containing the module, |
714 | | * or %NULL to obtain a #GModule representing the main program itself |
715 | | * @flags: the flags used for opening the module. This can be the |
716 | | * logical OR of any of the #GModuleFlags. |
717 | | * |
718 | | * A thin wrapper function around g_module_open_full() |
719 | | * |
720 | | * Returns: a #GModule on success, or %NULL on failure |
721 | | */ |
722 | | GModule * |
723 | | g_module_open (const gchar *file_name, |
724 | | GModuleFlags flags) |
725 | 0 | { |
726 | 0 | return g_module_open_full (file_name, flags, NULL); |
727 | 0 | } |
728 | | |
729 | | /** |
730 | | * g_module_close: |
731 | | * @module: a #GModule to close |
732 | | * |
733 | | * Closes a module. |
734 | | * |
735 | | * Returns: %TRUE on success |
736 | | */ |
737 | | gboolean |
738 | | g_module_close (GModule *module) |
739 | 0 | { |
740 | 0 | SUPPORT_OR_RETURN (FALSE); |
741 | | |
742 | 0 | g_return_val_if_fail (module != NULL, FALSE); |
743 | 0 | g_return_val_if_fail (module->ref_count > 0, FALSE); |
744 | | |
745 | 0 | g_rec_mutex_lock (&g_module_global_lock); |
746 | |
|
747 | 0 | module->ref_count--; |
748 | | |
749 | 0 | if (!module->ref_count && !module->is_resident && module->unload) |
750 | 0 | { |
751 | 0 | GModuleUnload unload; |
752 | |
|
753 | 0 | unload = module->unload; |
754 | 0 | module->unload = NULL; |
755 | 0 | unload (module); |
756 | 0 | } |
757 | |
|
758 | 0 | if (!module->ref_count && !module->is_resident) |
759 | 0 | { |
760 | 0 | GModule *last; |
761 | 0 | GModule *node; |
762 | | |
763 | 0 | last = NULL; |
764 | | |
765 | 0 | node = modules; |
766 | 0 | while (node) |
767 | 0 | { |
768 | 0 | if (node == module) |
769 | 0 | { |
770 | 0 | if (last) |
771 | 0 | last->next = node->next; |
772 | 0 | else |
773 | 0 | modules = node->next; |
774 | 0 | break; |
775 | 0 | } |
776 | 0 | last = node; |
777 | 0 | node = last->next; |
778 | 0 | } |
779 | 0 | module->next = NULL; |
780 | | |
781 | 0 | _g_module_close (module->handle); |
782 | 0 | g_free (module->file_name); |
783 | 0 | g_free (module); |
784 | 0 | } |
785 | | |
786 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
787 | 0 | return g_module_error() == NULL; |
788 | 0 | } |
789 | | |
790 | | /** |
791 | | * g_module_make_resident: |
792 | | * @module: a #GModule to make permanently resident |
793 | | * |
794 | | * Ensures that a module will never be unloaded. |
795 | | * Any future g_module_close() calls on the module will be ignored. |
796 | | */ |
797 | | void |
798 | | g_module_make_resident (GModule *module) |
799 | 0 | { |
800 | 0 | g_return_if_fail (module != NULL); |
801 | | |
802 | 0 | module->is_resident = TRUE; |
803 | 0 | } |
804 | | |
805 | | /** |
806 | | * g_module_error: |
807 | | * |
808 | | * Gets a string describing the last module error. |
809 | | * |
810 | | * Returns: a string describing the last module error |
811 | | */ |
812 | | const gchar * |
813 | | g_module_error (void) |
814 | 0 | { |
815 | 0 | return g_private_get (&module_error_private); |
816 | 0 | } |
817 | | |
818 | | /** |
819 | | * g_module_symbol: |
820 | | * @module: a #GModule |
821 | | * @symbol_name: the name of the symbol to find |
822 | | * @symbol: (out): returns the pointer to the symbol value |
823 | | * |
824 | | * Gets a symbol pointer from a module, such as one exported |
825 | | * by %G_MODULE_EXPORT. Note that a valid symbol can be %NULL. |
826 | | * |
827 | | * Returns: %TRUE on success |
828 | | */ |
829 | | gboolean |
830 | | g_module_symbol (GModule *module, |
831 | | const gchar *symbol_name, |
832 | | gpointer *symbol) |
833 | 0 | { |
834 | 0 | const gchar *module_error; |
835 | |
|
836 | 0 | if (symbol) |
837 | 0 | *symbol = NULL; |
838 | 0 | SUPPORT_OR_RETURN (FALSE); |
839 | | |
840 | 0 | g_return_val_if_fail (module != NULL, FALSE); |
841 | 0 | g_return_val_if_fail (symbol_name != NULL, FALSE); |
842 | 0 | g_return_val_if_fail (symbol != NULL, FALSE); |
843 | | |
844 | 0 | g_rec_mutex_lock (&g_module_global_lock); |
845 | |
|
846 | | #ifdef G_MODULE_NEED_USCORE |
847 | | { |
848 | | gchar *name; |
849 | | |
850 | | name = g_strconcat ("_", symbol_name, NULL); |
851 | | *symbol = _g_module_symbol (module->handle, name); |
852 | | g_free (name); |
853 | | } |
854 | | #else /* !G_MODULE_NEED_USCORE */ |
855 | 0 | *symbol = _g_module_symbol (module->handle, symbol_name); |
856 | 0 | #endif /* !G_MODULE_NEED_USCORE */ |
857 | | |
858 | 0 | module_error = g_module_error (); |
859 | 0 | if (module_error) |
860 | 0 | { |
861 | 0 | gchar *error; |
862 | |
|
863 | 0 | error = g_strconcat ("'", symbol_name, "': ", module_error, NULL); |
864 | 0 | g_module_set_error (error); |
865 | 0 | g_free (error); |
866 | 0 | *symbol = NULL; |
867 | 0 | } |
868 | | |
869 | 0 | g_rec_mutex_unlock (&g_module_global_lock); |
870 | 0 | return !module_error; |
871 | 0 | } |
872 | | |
873 | | /** |
874 | | * g_module_name: |
875 | | * @module: a #GModule |
876 | | * |
877 | | * Returns the filename that the module was opened with. |
878 | | * |
879 | | * If @module refers to the application itself, "main" is returned. |
880 | | * |
881 | | * Returns: (transfer none): the filename of the module |
882 | | */ |
883 | | const gchar * |
884 | | g_module_name (GModule *module) |
885 | 0 | { |
886 | 0 | g_return_val_if_fail (module != NULL, NULL); |
887 | | |
888 | 0 | if (module == main_module) |
889 | 0 | return "main"; |
890 | | |
891 | 0 | return module->file_name; |
892 | 0 | } |
893 | | |
894 | | /** |
895 | | * g_module_build_path: |
896 | | * @directory: (nullable): the directory where the module is. This can be |
897 | | * %NULL or the empty string to indicate that the standard platform-specific |
898 | | * directories will be used, though that is not recommended |
899 | | * @module_name: the name of the module |
900 | | * |
901 | | * A portable way to build the filename of a module. The platform-specific |
902 | | * prefix and suffix are added to the filename, if needed, and the result |
903 | | * is added to the directory, using the correct separator character. |
904 | | * |
905 | | * The directory should specify the directory where the module can be found. |
906 | | * It can be %NULL or an empty string to indicate that the module is in a |
907 | | * standard platform-specific directory, though this is not recommended |
908 | | * since the wrong module may be found. |
909 | | * |
910 | | * For example, calling g_module_build_path() on a Linux system with a |
911 | | * @directory of `/lib` and a @module_name of "mylibrary" will return |
912 | | * `/lib/libmylibrary.so`. On a Windows system, using `\Windows` as the |
913 | | * directory it will return `\Windows\mylibrary.dll`. |
914 | | * |
915 | | * Returns: the complete path of the module, including the standard library |
916 | | * prefix and suffix. This should be freed when no longer needed |
917 | | * |
918 | | * Deprecated: 2.76: Use g_module_open() instead with @module_name as the |
919 | | * basename of the file_name argument. See %G_MODULE_SUFFIX for why. |
920 | | */ |
921 | | gchar * |
922 | | g_module_build_path (const gchar *directory, |
923 | | const gchar *module_name) |
924 | 0 | { |
925 | 0 | g_return_val_if_fail (module_name != NULL, NULL); |
926 | | |
927 | 0 | return _g_module_build_path (directory, module_name); |
928 | 0 | } |
929 | | |
930 | | |
931 | | #ifdef G_OS_WIN32 |
932 | | |
933 | | /* Binary compatibility versions. Not for newly compiled code. */ |
934 | | |
935 | | _GMODULE_EXTERN GModule * g_module_open_utf8 (const gchar *file_name, |
936 | | GModuleFlags flags); |
937 | | |
938 | | _GMODULE_EXTERN const gchar *g_module_name_utf8 (GModule *module); |
939 | | |
940 | | GModule* |
941 | | g_module_open_utf8 (const gchar *file_name, |
942 | | GModuleFlags flags) |
943 | | { |
944 | | return g_module_open (file_name, flags); |
945 | | } |
946 | | |
947 | | const gchar * |
948 | | g_module_name_utf8 (GModule *module) |
949 | | { |
950 | | return g_module_name (module); |
951 | | } |
952 | | |
953 | | #endif |