1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "gmemorysettingsbackend.h"
30 #include "glocalfilemonitor.h"
31 #include "glocaldirectorymonitor.h"
32 #include "gnativevolumemonitor.h"
33 #include "gproxyresolver.h"
35 #include "gsocks4proxy.h"
36 #include "gsocks4aproxy.h"
37 #include "gsocks5proxy.h"
40 #include "gdesktopappinfo.h"
43 #include "gregistrysettingsbackend.h"
45 #include <glib/gstdio.h>
49 * @short_description: Loadable GIO Modules
52 * Provides an interface and default functions for loading and unloading
53 * modules. This is used internally to make GIO extensible, but can also
54 * be used by others to implement module loading.
59 * SECTION:extensionpoints
60 * @short_description: Extension Points
62 * @see_also: <link linkend="extending-gio">Extending GIO</link>
64 * #GIOExtensionPoint provides a mechanism for modules to extend the
65 * functionality of the library or application that loaded it in an
68 * An extension point is identified by a name, and it may optionally
69 * require that any implementation must by of a certain type (or derived
70 * thereof). Use g_io_extension_point_register() to register an
71 * extension point, and g_io_extension_point_set_required_type() to
72 * set a required type.
74 * A module can implement an extension point by specifying the #GType
75 * that implements the functionality. Additionally, each implementation
76 * of an extension point has a name, and a priority. Use
77 * g_io_extension_point_implement() to implement an extension point.
80 * GIOExtensionPoint *ep;
82 * /* Register an extension point */
83 * ep = g_io_extension_point_register ("my-extension-point");
84 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
88 * /* Implement an extension point */
89 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
90 * g_io_extension_point_implement ("my-extension-point",
91 * my_example_impl_get_type (),
96 * It is up to the code that registered the extension point how
97 * it uses the implementations that have been associated with it.
98 * Depending on the use case, it may use all implementations, or
99 * only the one with the highest priority, or pick a specific
102 * To avoid opening all modules just to find out what extension
103 * points they implement, GIO makes use of a caching mechanism,
104 * see <link linkend="gio-querymodules">gio-querymodules</link>.
105 * You are expected to run this command after installing a
109 GTypeModule parent_instance;
113 gboolean initialized; /* The module was loaded at least once */
115 void (* load) (GIOModule *module);
116 void (* unload) (GIOModule *module);
119 struct _GIOModuleClass
121 GTypeModuleClass parent_class;
125 static void g_io_module_finalize (GObject *object);
126 static gboolean g_io_module_load_module (GTypeModule *gmodule);
127 static void g_io_module_unload_module (GTypeModule *gmodule);
129 struct _GIOExtension {
135 struct _GIOExtensionPoint {
139 GList *lazy_load_modules;
142 static GHashTable *extension_points = NULL;
143 G_LOCK_DEFINE_STATIC(extension_points);
145 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
148 g_io_module_class_init (GIOModuleClass *class)
150 GObjectClass *object_class = G_OBJECT_CLASS (class);
151 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
153 object_class->finalize = g_io_module_finalize;
155 type_module_class->load = g_io_module_load_module;
156 type_module_class->unload = g_io_module_unload_module;
160 g_io_module_init (GIOModule *module)
165 g_io_module_finalize (GObject *object)
167 GIOModule *module = G_IO_MODULE (object);
169 g_free (module->filename);
171 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
175 g_io_module_load_module (GTypeModule *gmodule)
177 GIOModule *module = G_IO_MODULE (gmodule);
179 if (!module->filename)
181 g_warning ("GIOModule path not set");
185 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
187 if (!module->library)
189 g_printerr ("%s\n", g_module_error ());
193 /* Make sure that the loaded library contains the required methods */
194 if (! g_module_symbol (module->library,
196 (gpointer) &module->load) ||
197 ! g_module_symbol (module->library,
198 "g_io_module_unload",
199 (gpointer) &module->unload))
201 g_printerr ("%s\n", g_module_error ());
202 g_module_close (module->library);
207 /* Initialize the loaded module */
208 module->load (module);
209 module->initialized = TRUE;
215 g_io_module_unload_module (GTypeModule *gmodule)
217 GIOModule *module = G_IO_MODULE (gmodule);
219 module->unload (module);
221 g_module_close (module->library);
222 module->library = NULL;
225 module->unload = NULL;
230 * @filename: filename of the shared library module.
232 * Creates a new GIOModule that will load the specific
233 * shared library when in use.
235 * Returns: a #GIOModule from given @filename,
239 g_io_module_new (const gchar *filename)
243 g_return_val_if_fail (filename != NULL, NULL);
245 module = g_object_new (G_IO_TYPE_MODULE, NULL);
246 module->filename = g_strdup (filename);
252 is_valid_module_name (const gchar *basename)
254 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
256 g_str_has_prefix (basename, "lib") &&
257 g_str_has_suffix (basename, ".so");
259 return g_str_has_suffix (basename, ".dll");
264 * g_io_modules_scan_all_in_directory:
265 * @dirname: pathname for a directory containing modules to scan.
267 * Scans all the modules in the specified directory, ensuring that
268 * any extension point implemented by a module is registered.
270 * This may not actually load and initialize all the types in each
271 * module, some modules may be lazily loaded and initialized when
272 * an extension point it implementes is used with e.g.
273 * g_io_extension_point_get_extensions() or
274 * g_io_extension_point_get_extension_by_name().
276 * If you need to guarantee that all types are loaded in all the modules,
277 * use g_io_modules_scan_all_in_directory().
282 g_io_modules_scan_all_in_directory (const char *dirname)
292 if (!g_module_supported ())
295 dir = g_dir_open (dirname, 0, NULL);
299 filename = g_build_filename (dirname, "giomodule.cache", NULL);
301 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
302 g_free, (GDestroyNotify)g_strfreev);
305 if (g_stat (filename, &statbuf) == 0 &&
306 g_file_get_contents (filename, &data, NULL, NULL))
311 /* Cache mtime is the time the cache file was created, any file
312 * that has a ctime before this was created then and not modified
313 * since then (userspace can't change ctime). Its possible to change
314 * the ctime forward without changing the file content, by e.g.
315 * chmoding the file, but this is uncommon and will only cause us
316 * to not use the cache so will not cause bugs.
318 cache_mtime = statbuf.st_mtime;
320 lines = g_strsplit (data, "\n", -1);
323 for (i = 0; lines[i] != NULL; i++)
325 char *line = lines[i];
328 char **extension_points;
333 colon = strchr (line, ':');
334 if (colon == NULL || line == colon)
335 continue; /* Invalid line, ignore */
337 *colon = 0; /* terminate filename */
338 file = g_strdup (line);
339 colon++; /* after colon */
341 while (g_ascii_isspace (*colon))
344 extension_points = g_strsplit (colon, ",", -1);
345 g_hash_table_insert (cache, file, extension_points);
350 while ((name = g_dir_read_name (dir)))
352 if (is_valid_module_name (name))
354 GIOExtensionPoint *extension_point;
357 char **extension_points;
360 path = g_build_filename (dirname, name, NULL);
361 module = g_io_module_new (path);
363 extension_points = g_hash_table_lookup (cache, name);
364 if (extension_points != NULL &&
365 g_stat (path, &statbuf) == 0 &&
366 statbuf.st_ctime <= cache_mtime)
368 /* Lazy load/init the library when first required */
369 for (i = 0; extension_points[i] != NULL; i++)
372 g_io_extension_point_register (extension_points[i]);
373 extension_point->lazy_load_modules =
374 g_list_prepend (extension_point->lazy_load_modules,
380 /* Try to load and init types */
381 if (g_type_module_use (G_TYPE_MODULE (module)))
382 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
384 { /* Failure to load */
385 g_printerr ("Failed to load module: %s\n", path);
386 g_object_unref (module);
398 g_hash_table_destroy (cache);
405 * g_io_modules_load_all_in_directory:
406 * @dirname: pathname for a directory containing modules to load.
408 * Loads all the modules in the specified directory.
410 * If don't require all modules to be initialized (and thus registering
411 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
412 * which allows delayed/lazy loading of modules.
414 * Returns: a list of #GIOModules loaded from the directory,
415 * All the modules are loaded into memory, if you want to
416 * unload them (enabling on-demand loading) you must call
417 * g_type_module_unuse() on all the modules. Free the list
418 * with g_list_free().
421 g_io_modules_load_all_in_directory (const char *dirname)
427 if (!g_module_supported ())
430 dir = g_dir_open (dirname, 0, NULL);
435 while ((name = g_dir_read_name (dir)))
437 if (is_valid_module_name (name))
442 path = g_build_filename (dirname, name, NULL);
443 module = g_io_module_new (path);
445 if (!g_type_module_use (G_TYPE_MODULE (module)))
447 g_printerr ("Failed to load module: %s\n", path);
448 g_object_unref (module);
455 modules = g_list_prepend (modules, module);
464 G_LOCK_DEFINE_STATIC (registered_extensions);
465 G_LOCK_DEFINE_STATIC (loaded_dirs);
467 extern GType _g_fen_directory_monitor_get_type (void);
468 extern GType _g_fen_file_monitor_get_type (void);
469 extern GType _g_inotify_directory_monitor_get_type (void);
470 extern GType _g_inotify_file_monitor_get_type (void);
471 extern GType _g_unix_volume_monitor_get_type (void);
472 extern GType _g_local_vfs_get_type (void);
474 extern GType _g_win32_volume_monitor_get_type (void);
475 extern GType g_win32_directory_monitor_get_type (void);
476 extern GType _g_winhttp_vfs_get_type (void);
478 extern GType _g_dummy_proxy_resolver_get_type (void);
480 #ifdef G_PLATFORM_WIN32
484 static HMODULE gio_dll = NULL;
489 DllMain (HINSTANCE hinstDLL,
493 if (fdwReason == DLL_PROCESS_ATTACH)
501 #undef GIO_MODULE_DIR
503 /* GIO_MODULE_DIR is used only in code called just once,
504 * so no problem leaking this
506 #define GIO_MODULE_DIR \
507 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
514 _g_io_modules_ensure_extension_points_registered (void)
516 static gboolean registered_extensions = FALSE;
517 GIOExtensionPoint *ep;
519 G_LOCK (registered_extensions);
521 if (!registered_extensions)
523 registered_extensions = TRUE;
526 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
527 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
530 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
531 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
533 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
534 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
536 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
537 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
539 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
540 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
542 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
543 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
545 ep = g_io_extension_point_register ("gsettings-backend");
546 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
548 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
549 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
551 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
552 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
555 G_UNLOCK (registered_extensions);
559 _g_io_modules_ensure_loaded (void)
561 static gboolean loaded_dirs = FALSE;
562 const char *module_path;
564 _g_io_modules_ensure_extension_points_registered ();
566 G_LOCK (loaded_dirs);
572 g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
574 module_path = g_getenv ("GIO_EXTRA_MODULES");
581 paths = g_strsplit (module_path, ":", 0);
583 for (i = 0; paths[i] != NULL; i++)
584 g_io_modules_scan_all_in_directory (paths[i]);
589 /* Initialize types from built-in "modules" */
590 g_memory_settings_backend_get_type ();
591 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
592 _g_inotify_directory_monitor_get_type ();
593 _g_inotify_file_monitor_get_type ();
595 #if defined(HAVE_FEN)
596 _g_fen_directory_monitor_get_type ();
597 _g_fen_file_monitor_get_type ();
600 _g_win32_volume_monitor_get_type ();
601 g_win32_directory_monitor_get_type ();
602 g_registry_backend_get_type ();
605 _g_unix_volume_monitor_get_type ();
608 _g_winhttp_vfs_get_type ();
610 _g_local_vfs_get_type ();
611 _g_dummy_proxy_resolver_get_type ();
612 _g_socks4a_proxy_get_type ();
613 _g_socks4_proxy_get_type ();
614 _g_socks5_proxy_get_type ();
617 G_UNLOCK (loaded_dirs);
621 g_io_extension_point_free (GIOExtensionPoint *ep)
628 * g_io_extension_point_register:
629 * @name: The name of the extension point
631 * Registers an extension point.
633 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
634 * and should not be freed
637 g_io_extension_point_register (const char *name)
639 GIOExtensionPoint *ep;
641 G_LOCK (extension_points);
642 if (extension_points == NULL)
643 extension_points = g_hash_table_new_full (g_str_hash,
646 (GDestroyNotify)g_io_extension_point_free);
648 ep = g_hash_table_lookup (extension_points, name);
651 G_UNLOCK (extension_points);
655 ep = g_new0 (GIOExtensionPoint, 1);
656 ep->name = g_strdup (name);
658 g_hash_table_insert (extension_points, ep->name, ep);
660 G_UNLOCK (extension_points);
666 * g_io_extension_point_lookup:
667 * @name: the name of the extension point
669 * Looks up an existing extension point.
671 * Returns: the #GIOExtensionPoint, or %NULL if there is no
672 * registered extension point with the given name
675 g_io_extension_point_lookup (const char *name)
677 GIOExtensionPoint *ep;
679 G_LOCK (extension_points);
681 if (extension_points != NULL)
682 ep = g_hash_table_lookup (extension_points, name);
684 G_UNLOCK (extension_points);
691 * g_io_extension_point_set_required_type:
692 * @extension_point: a #GIOExtensionPoint
693 * @type: the #GType to require
695 * Sets the required type for @extension_point to @type.
696 * All implementations must henceforth have this type.
699 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
702 extension_point->required_type = type;
706 * g_io_extension_point_get_required_type:
707 * @extension_point: a #GIOExtensionPoint
709 * Gets the required type for @extension_point.
711 * Returns: the #GType that all implementations must have,
712 * or #G_TYPE_INVALID if the extension point has no required type
715 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
717 return extension_point->required_type;
721 lazy_load_modules (GIOExtensionPoint *extension_point)
726 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
730 if (!module->initialized)
732 if (g_type_module_use (G_TYPE_MODULE (module)))
733 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
735 g_printerr ("Failed to load module: %s\n",
742 * g_io_extension_point_get_extensions:
743 * @extension_point: a #GIOExtensionPoint
745 * Gets a list of all extensions that implement this extension point.
746 * The list is sorted by priority, beginning with the highest priority.
748 * Returns: (element-type GIOExtension) (transfer none): a #GList of
749 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
753 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
755 lazy_load_modules (extension_point);
756 return extension_point->extensions;
760 * g_io_extension_point_get_extension_by_name:
761 * @extension_point: a #GIOExtensionPoint
762 * @name: the name of the extension to get
764 * Finds a #GIOExtension for an extension point by name.
766 * Returns: the #GIOExtension for @extension_point that has the
767 * given name, or %NULL if there is no extension with that name
770 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
775 lazy_load_modules (extension_point);
776 for (l = extension_point->extensions; l != NULL; l = l->next)
778 GIOExtension *e = l->data;
780 if (e->name != NULL &&
781 strcmp (e->name, name) == 0)
789 extension_prio_compare (gconstpointer a,
792 const GIOExtension *extension_a = a, *extension_b = b;
794 if (extension_a->priority > extension_b->priority)
797 if (extension_b->priority > extension_a->priority)
804 * g_io_extension_point_implement:
805 * @extension_point_name: the name of the extension point
806 * @type: the #GType to register as extension
807 * @extension_name: the name for the extension
808 * @priority: the priority for the extension
810 * Registers @type as extension for the extension point with name
811 * @extension_point_name.
813 * If @type has already been registered as an extension for this
814 * extension point, the existing #GIOExtension object is returned.
816 * Returns: a #GIOExtension object for #GType
819 g_io_extension_point_implement (const char *extension_point_name,
821 const char *extension_name,
824 GIOExtensionPoint *extension_point;
825 GIOExtension *extension;
828 g_return_val_if_fail (extension_point_name != NULL, NULL);
830 extension_point = g_io_extension_point_lookup (extension_point_name);
831 if (extension_point == NULL)
833 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
837 if (extension_point->required_type != 0 &&
838 !g_type_is_a (type, extension_point->required_type))
840 g_warning ("Tried to register an extension of the type %s to extension point %s. "
841 "Expected type is %s.",
843 extension_point_name,
844 g_type_name (extension_point->required_type));
848 /* It's safe to register the same type multiple times */
849 for (l = extension_point->extensions; l != NULL; l = l->next)
852 if (extension->type == type)
856 extension = g_slice_new0 (GIOExtension);
857 extension->type = type;
858 extension->name = g_strdup (extension_name);
859 extension->priority = priority;
861 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
862 extension, extension_prio_compare);
868 * g_io_extension_ref_class:
869 * @extension: a #GIOExtension
871 * Gets a reference to the class for the type that is
872 * associated with @extension.
874 * Returns: the #GTypeClass for the type of @extension
877 g_io_extension_ref_class (GIOExtension *extension)
879 return g_type_class_ref (extension->type);
883 * g_io_extension_get_type:
884 * @extension: a #GIOExtension
886 * Gets the type associated with @extension.
888 * Returns: the type of @extension
891 g_io_extension_get_type (GIOExtension *extension)
893 return extension->type;
897 * g_io_extension_get_name:
898 * @extension: a #GIOExtension
900 * Gets the name under which @extension was registered.
902 * Note that the same type may be registered as extension
903 * for multiple extension points, under different names.
905 * Returns: the name of @extension.
908 g_io_extension_get_name (GIOExtension *extension)
910 return extension->name;
914 * g_io_extension_get_priority:
915 * @extension: a #GIOExtension
917 * Gets the priority with which @extension was registered.
919 * Returns: the priority of @extension
922 g_io_extension_get_priority (GIOExtension *extension)
924 return extension->priority;