1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
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/.
35 #include "gmoduleconf.h"
38 #include <sys/types.h>
44 #if defined (G_OS_WIN32)
45 # include <io.h> /* For open() and close() prototypes. */
48 /* We maintain a list of modules, so we can reference count them.
49 * That's needed because some platforms don't support refernce counts on
50 * modules e.g. the shl_* implementation of HP-UX
51 * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
52 * Also, the module for the program itself is kept seperatedly for
53 * faster access and because it has special semantics.
57 /* --- structures --- */
63 guint is_resident : 1;
69 /* --- prototypes --- */
70 static gpointer _g_module_open (const gchar *file_name,
72 static void _g_module_close (gpointer handle,
74 static gpointer _g_module_self (void);
75 static gpointer _g_module_symbol (gpointer handle,
76 const gchar *symbol_name);
77 static gchar* _g_module_build_path (const gchar *directory,
78 const gchar *module_name);
79 static inline void g_module_set_error (const gchar *error);
80 static inline GModule* g_module_find_by_handle (gpointer handle);
81 static inline GModule* g_module_find_by_name (const gchar *name);
84 /* --- variables --- */
85 static GModule *modules = NULL;
86 static GModule *main_module = NULL;
87 static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
90 /* --- inline functions --- */
91 static inline GModule*
92 g_module_find_by_handle (gpointer handle)
95 GModule *retval = NULL;
97 if (main_module && main_module->handle == handle)
100 for (module = modules; module; module = module->next)
101 if (handle == module->handle)
110 static inline GModule*
111 g_module_find_by_name (const gchar *name)
114 GModule *retval = NULL;
116 for (module = modules; module; module = module->next)
117 if (strcmp (name, module->file_name) == 0)
127 g_module_set_error (const gchar *error)
129 g_static_private_set (&module_error_private, g_strdup (error), g_free);
134 /* --- include platform specifc code --- */
135 #define SUPPORT_OR_RETURN(rv) { g_module_set_error (NULL); }
136 #if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
137 #include "gmodule-dl.c"
138 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
139 #include "gmodule-dld.c"
140 #elif (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
141 #include "gmodule-win32.c"
143 #undef SUPPORT_OR_RETURN
144 #define SUPPORT_OR_RETURN(rv) { g_module_set_error ("dynamic modules are " \
145 "not supported by this system"); return rv; }
147 _g_module_open (const gchar *file_name,
153 _g_module_close (gpointer handle,
158 _g_module_self (void)
163 _g_module_symbol (gpointer handle,
164 const gchar *symbol_name)
169 _g_module_build_path (const gchar *directory,
170 const gchar *module_name)
174 #endif /* no implementation */
176 /* --- functions --- */
178 g_module_supported (void)
180 SUPPORT_OR_RETURN (FALSE);
186 parse_libtool_archive (const gchar* libtool_name)
188 const gint TOKEN_DLNAME = G_TOKEN_LAST + 1;
189 const gint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
190 const gint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
191 gchar *lt_dlname = NULL;
192 gboolean lt_installed = TRUE;
193 gchar *lt_libdir = NULL;
198 int fd = open (libtool_name, O_RDONLY, 0);
201 g_module_set_error ("couldn't open libtool archive");
204 /* search libtool's dlname specification */
205 scanner = g_scanner_new (NULL);
206 g_scanner_input_file (scanner, fd);
207 scanner->config->symbol_2_token = TRUE;
208 g_scanner_scope_add_symbol (scanner, 0, "dlname",
209 GUINT_TO_POINTER (TOKEN_DLNAME));
210 g_scanner_scope_add_symbol (scanner, 0, "installed",
211 GUINT_TO_POINTER (TOKEN_INSTALLED));
212 g_scanner_scope_add_symbol (scanner, 0, "libdir",
213 GUINT_TO_POINTER (TOKEN_LIBDIR));
214 while (!g_scanner_eof (scanner))
216 token = g_scanner_get_next_token (scanner);
217 if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED ||
218 token == TOKEN_LIBDIR)
220 if (g_scanner_get_next_token (scanner) != '=' ||
221 g_scanner_get_next_token (scanner) !=
222 (token == TOKEN_INSTALLED ?
223 G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
225 g_module_set_error ("libtool archive has unknown format");
229 g_scanner_destroy (scanner);
236 if (token == TOKEN_DLNAME)
239 lt_dlname = g_strdup (scanner->value.v_string);
241 else if (token == TOKEN_INSTALLED)
243 strcmp (scanner->value.v_identifier, "yes") == 0;
244 else /* token == TOKEN_LIBDIR */
247 lt_libdir = g_strdup (scanner->value.v_string);
255 gchar *dir = g_path_get_dirname (libtool_name);
257 lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
260 name = g_module_build_path (lt_libdir, lt_dlname);
264 g_scanner_destroy (scanner);
270 static inline gboolean
271 g_str_check_suffix (const gchar* string, const gchar* suffix)
273 guint string_len = strlen (string);
274 guint suffix_len = strlen (suffix);
276 return string_len >= suffix_len &&
277 strcmp (string + string_len - suffix_len, suffix) == 0;
280 static GStaticRecMutex g_module_global_lock = G_STATIC_REC_MUTEX_INIT;
283 g_module_open (const gchar *file_name,
289 SUPPORT_OR_RETURN (NULL);
291 g_static_rec_mutex_lock (&g_module_global_lock);
296 handle = _g_module_self ();
299 main_module = g_new (GModule, 1);
300 main_module->file_name = NULL;
301 main_module->handle = handle;
302 main_module->ref_count = 1;
303 main_module->is_resident = TRUE;
304 main_module->unload = NULL;
305 main_module->next = NULL;
309 g_static_rec_mutex_unlock (&g_module_global_lock);
313 /* we first search the module list by name */
314 module = g_module_find_by_name (file_name);
319 g_static_rec_mutex_unlock (&g_module_global_lock);
323 /* First we try to open the module as provided */
324 handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
326 /* If not found, we check, if it is a libtool archive */
327 if (!handle && g_str_check_suffix (file_name, ".la"))
329 gchar *name = parse_libtool_archive (file_name);
332 handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0);
337 /* If still not found, we check, if it is a library name without suffix */
338 if (!handle && !g_str_check_suffix (file_name, "." G_MODULE_SUFFIX))
340 gchar *name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
341 handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0);
345 /* If still not found, we check, if it is a libtool archive name
347 if (!handle && !g_str_check_suffix (file_name, ".la"))
349 gchar *la_name = g_strconcat (file_name, ".la", NULL);
350 gchar *name = parse_libtool_archive (la_name);
353 handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0);
362 GModuleCheckInit check_init;
363 const gchar *check_failed = NULL;
365 /* search the module list by handle, since file names are not unique */
366 module = g_module_find_by_handle (handle);
369 _g_module_close (module->handle, TRUE);
371 g_module_set_error (NULL);
373 g_static_rec_mutex_unlock (&g_module_global_lock);
377 saved_error = g_strdup (g_module_error ());
378 g_module_set_error (NULL);
380 module = g_new (GModule, 1);
381 module->file_name = g_strdup (file_name);
382 module->handle = handle;
383 module->ref_count = 1;
384 module->is_resident = FALSE;
385 module->unload = NULL;
386 module->next = modules;
389 /* check initialization */
390 if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init))
391 check_failed = check_init (module);
393 /* we don't call unload() if the initialization check failed. */
395 g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
401 error = g_strconcat ("GModule initialization check failed: ", check_failed, NULL);
402 g_module_close (module);
404 g_module_set_error (error);
408 g_module_set_error (saved_error);
410 g_free (saved_error);
413 g_static_rec_mutex_unlock (&g_module_global_lock);
418 g_module_close (GModule *module)
420 SUPPORT_OR_RETURN (FALSE);
422 g_return_val_if_fail (module != NULL, FALSE);
423 g_return_val_if_fail (module->ref_count > 0, FALSE);
425 g_static_rec_mutex_lock (&g_module_global_lock);
429 if (!module->ref_count && !module->is_resident && module->unload)
431 GModuleUnload unload;
433 unload = module->unload;
434 module->unload = NULL;
438 if (!module->ref_count && !module->is_resident)
451 last->next = node->next;
453 modules = node->next;
461 _g_module_close (module->handle, FALSE);
462 g_free (module->file_name);
467 g_static_rec_mutex_unlock (&g_module_global_lock);
468 return g_module_error() == NULL;
472 g_module_make_resident (GModule *module)
474 g_return_if_fail (module != NULL);
476 module->is_resident = TRUE;
479 G_CONST_RETURN gchar*
480 g_module_error (void)
482 return g_static_private_get (&module_error_private);
486 g_module_symbol (GModule *module,
487 const gchar *symbol_name,
490 const gchar *module_error;
494 SUPPORT_OR_RETURN (FALSE);
496 g_return_val_if_fail (module != NULL, FALSE);
497 g_return_val_if_fail (symbol_name != NULL, FALSE);
498 g_return_val_if_fail (symbol != NULL, FALSE);
500 g_static_rec_mutex_lock (&g_module_global_lock);
502 #ifdef G_MODULE_NEED_USCORE
506 name = g_strconcat ("_", symbol_name, NULL);
507 *symbol = _g_module_symbol (module->handle, name);
510 #else /* !G_MODULE_NEED_USCORE */
511 *symbol = _g_module_symbol (module->handle, symbol_name);
512 #endif /* !G_MODULE_NEED_USCORE */
514 module_error = g_module_error ();
519 error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
520 g_module_set_error (error);
525 g_static_rec_mutex_unlock (&g_module_global_lock);
526 return !module_error;
529 G_CONST_RETURN gchar*
530 g_module_name (GModule *module)
532 g_return_val_if_fail (module != NULL, NULL);
534 if (module == main_module)
537 return module->file_name;
541 g_module_build_path (const gchar *directory,
542 const gchar *module_name)
544 g_return_val_if_fail (module_name != NULL, NULL);
546 return _g_module_build_path (directory, module_name);