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 "glocalfilemonitor.h"
30 #include "glocaldirectorymonitor.h"
31 #include "gnativevolumemonitor.h"
32 #include "gproxyresolver.h"
34 #include "gsettingsbackendinternal.h"
35 #include "gsocks4proxy.h"
36 #include "gsocks4aproxy.h"
37 #include "gsocks5proxy.h"
38 #include "gtlsbackend.h"
41 #include "gregistrysettingsbackend.h"
43 #include <glib/gstdio.h>
45 #undef G_DISABLE_DEPRECATED
48 #include "gdesktopappinfo.h"
53 * @short_description: Loadable GIO Modules
56 * Provides an interface and default functions for loading and unloading
57 * modules. This is used internally to make GIO extensible, but can also
58 * be used by others to implement module loading.
63 * SECTION:extensionpoints
64 * @short_description: Extension Points
66 * @see_also: <link linkend="extending-gio">Extending GIO</link>
68 * #GIOExtensionPoint provides a mechanism for modules to extend the
69 * functionality of the library or application that loaded it in an
72 * An extension point is identified by a name, and it may optionally
73 * require that any implementation must by of a certain type (or derived
74 * thereof). Use g_io_extension_point_register() to register an
75 * extension point, and g_io_extension_point_set_required_type() to
76 * set a required type.
78 * A module can implement an extension point by specifying the #GType
79 * that implements the functionality. Additionally, each implementation
80 * of an extension point has a name, and a priority. Use
81 * g_io_extension_point_implement() to implement an extension point.
84 * GIOExtensionPoint *ep;
86 * /* Register an extension point */
87 * ep = g_io_extension_point_register ("my-extension-point");
88 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
92 * /* Implement an extension point */
93 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
94 * g_io_extension_point_implement ("my-extension-point",
95 * my_example_impl_get_type (),
100 * It is up to the code that registered the extension point how
101 * it uses the implementations that have been associated with it.
102 * Depending on the use case, it may use all implementations, or
103 * only the one with the highest priority, or pick a specific
106 * To avoid opening all modules just to find out what extension
107 * points they implement, GIO makes use of a caching mechanism,
108 * see <link linkend="gio-querymodules">gio-querymodules</link>.
109 * You are expected to run this command after installing a
113 GTypeModule parent_instance;
117 gboolean initialized; /* The module was loaded at least once */
119 void (* load) (GIOModule *module);
120 void (* unload) (GIOModule *module);
123 struct _GIOModuleClass
125 GTypeModuleClass parent_class;
129 static void g_io_module_finalize (GObject *object);
130 static gboolean g_io_module_load_module (GTypeModule *gmodule);
131 static void g_io_module_unload_module (GTypeModule *gmodule);
133 struct _GIOExtension {
139 struct _GIOExtensionPoint {
143 GList *lazy_load_modules;
146 static GHashTable *extension_points = NULL;
147 G_LOCK_DEFINE_STATIC(extension_points);
149 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
152 g_io_module_class_init (GIOModuleClass *class)
154 GObjectClass *object_class = G_OBJECT_CLASS (class);
155 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
157 object_class->finalize = g_io_module_finalize;
159 type_module_class->load = g_io_module_load_module;
160 type_module_class->unload = g_io_module_unload_module;
164 g_io_module_init (GIOModule *module)
169 g_io_module_finalize (GObject *object)
171 GIOModule *module = G_IO_MODULE (object);
173 g_free (module->filename);
175 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
179 g_io_module_load_module (GTypeModule *gmodule)
181 GIOModule *module = G_IO_MODULE (gmodule);
183 if (!module->filename)
185 g_warning ("GIOModule path not set");
189 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
191 if (!module->library)
193 g_printerr ("%s\n", g_module_error ());
197 /* Make sure that the loaded library contains the required methods */
198 if (! g_module_symbol (module->library,
200 (gpointer) &module->load) ||
201 ! g_module_symbol (module->library,
202 "g_io_module_unload",
203 (gpointer) &module->unload))
205 g_printerr ("%s\n", g_module_error ());
206 g_module_close (module->library);
211 /* Initialize the loaded module */
212 module->load (module);
213 module->initialized = TRUE;
219 g_io_module_unload_module (GTypeModule *gmodule)
221 GIOModule *module = G_IO_MODULE (gmodule);
223 module->unload (module);
225 g_module_close (module->library);
226 module->library = NULL;
229 module->unload = NULL;
234 * @filename: filename of the shared library module.
236 * Creates a new GIOModule that will load the specific
237 * shared library when in use.
239 * Returns: a #GIOModule from given @filename,
243 g_io_module_new (const gchar *filename)
247 g_return_val_if_fail (filename != NULL, NULL);
249 module = g_object_new (G_IO_TYPE_MODULE, NULL);
250 module->filename = g_strdup (filename);
256 is_valid_module_name (const gchar *basename)
258 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
260 g_str_has_prefix (basename, "lib") &&
261 g_str_has_suffix (basename, ".so");
263 return g_str_has_suffix (basename, ".dll");
268 * g_io_modules_scan_all_in_directory:
269 * @dirname: pathname for a directory containing modules to scan.
271 * Scans all the modules in the specified directory, ensuring that
272 * any extension point implemented by a module is registered.
274 * This may not actually load and initialize all the types in each
275 * module, some modules may be lazily loaded and initialized when
276 * an extension point it implementes is used with e.g.
277 * g_io_extension_point_get_extensions() or
278 * g_io_extension_point_get_extension_by_name().
280 * If you need to guarantee that all types are loaded in all the modules,
281 * use g_io_modules_load_all_in_directory().
286 g_io_modules_scan_all_in_directory (const char *dirname)
296 if (!g_module_supported ())
299 dir = g_dir_open (dirname, 0, NULL);
303 filename = g_build_filename (dirname, "giomodule.cache", NULL);
305 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
306 g_free, (GDestroyNotify)g_strfreev);
309 if (g_stat (filename, &statbuf) == 0 &&
310 g_file_get_contents (filename, &data, NULL, NULL))
315 /* Cache mtime is the time the cache file was created, any file
316 * that has a ctime before this was created then and not modified
317 * since then (userspace can't change ctime). Its possible to change
318 * the ctime forward without changing the file content, by e.g.
319 * chmoding the file, but this is uncommon and will only cause us
320 * to not use the cache so will not cause bugs.
322 cache_mtime = statbuf.st_mtime;
324 lines = g_strsplit (data, "\n", -1);
327 for (i = 0; lines[i] != NULL; i++)
329 char *line = lines[i];
332 char **extension_points;
337 colon = strchr (line, ':');
338 if (colon == NULL || line == colon)
339 continue; /* Invalid line, ignore */
341 *colon = 0; /* terminate filename */
342 file = g_strdup (line);
343 colon++; /* after colon */
345 while (g_ascii_isspace (*colon))
348 extension_points = g_strsplit (colon, ",", -1);
349 g_hash_table_insert (cache, file, extension_points);
354 while ((name = g_dir_read_name (dir)))
356 if (is_valid_module_name (name))
358 GIOExtensionPoint *extension_point;
361 char **extension_points;
364 path = g_build_filename (dirname, name, NULL);
365 module = g_io_module_new (path);
367 extension_points = g_hash_table_lookup (cache, name);
368 if (extension_points != NULL &&
369 g_stat (path, &statbuf) == 0 &&
370 statbuf.st_ctime <= cache_mtime)
372 /* Lazy load/init the library when first required */
373 for (i = 0; extension_points[i] != NULL; i++)
376 g_io_extension_point_register (extension_points[i]);
377 extension_point->lazy_load_modules =
378 g_list_prepend (extension_point->lazy_load_modules,
384 /* Try to load and init types */
385 if (g_type_module_use (G_TYPE_MODULE (module)))
386 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
388 { /* Failure to load */
389 g_printerr ("Failed to load module: %s\n", path);
390 g_object_unref (module);
402 g_hash_table_destroy (cache);
409 * g_io_modules_load_all_in_directory:
410 * @dirname: pathname for a directory containing modules to load.
412 * Loads all the modules in the specified directory.
414 * If don't require all modules to be initialized (and thus registering
415 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
416 * which allows delayed/lazy loading of modules.
418 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
419 * from the directory,
420 * All the modules are loaded into memory, if you want to
421 * unload them (enabling on-demand loading) you must call
422 * g_type_module_unuse() on all the modules. Free the list
423 * with g_list_free().
426 g_io_modules_load_all_in_directory (const char *dirname)
432 if (!g_module_supported ())
435 dir = g_dir_open (dirname, 0, NULL);
440 while ((name = g_dir_read_name (dir)))
442 if (is_valid_module_name (name))
447 path = g_build_filename (dirname, name, NULL);
448 module = g_io_module_new (path);
450 if (!g_type_module_use (G_TYPE_MODULE (module)))
452 g_printerr ("Failed to load module: %s\n", path);
453 g_object_unref (module);
460 modules = g_list_prepend (modules, module);
469 G_LOCK_DEFINE_STATIC (registered_extensions);
470 G_LOCK_DEFINE_STATIC (loaded_dirs);
472 extern GType _g_fen_directory_monitor_get_type (void);
473 extern GType _g_fen_file_monitor_get_type (void);
474 extern GType _g_inotify_directory_monitor_get_type (void);
475 extern GType _g_inotify_file_monitor_get_type (void);
476 extern GType _g_unix_volume_monitor_get_type (void);
477 extern GType _g_local_vfs_get_type (void);
479 extern GType _g_win32_volume_monitor_get_type (void);
480 extern GType g_win32_directory_monitor_get_type (void);
481 extern GType _g_winhttp_vfs_get_type (void);
483 extern GType _g_dummy_proxy_resolver_get_type (void);
484 extern GType _g_dummy_tls_backend_get_type (void);
486 #ifdef G_PLATFORM_WIN32
490 static HMODULE gio_dll = NULL;
495 DllMain (HINSTANCE hinstDLL,
499 if (fdwReason == DLL_PROCESS_ATTACH)
507 #undef GIO_MODULE_DIR
509 /* GIO_MODULE_DIR is used only in code called just once,
510 * so no problem leaking this
512 #define GIO_MODULE_DIR \
513 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
520 _g_io_modules_ensure_extension_points_registered (void)
522 static gboolean registered_extensions = FALSE;
523 GIOExtensionPoint *ep;
525 G_LOCK (registered_extensions);
527 if (!registered_extensions)
529 registered_extensions = TRUE;
532 #if !GLIB_CHECK_VERSION (3, 0, 0)
533 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
534 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
538 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
539 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
541 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
542 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
544 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
545 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
547 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
548 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
550 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
551 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
553 ep = g_io_extension_point_register ("gsettings-backend");
554 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
556 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
557 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
559 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
560 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
562 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
563 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
566 G_UNLOCK (registered_extensions);
570 _g_io_modules_ensure_loaded (void)
572 static gboolean loaded_dirs = FALSE;
573 const char *module_path;
575 _g_io_modules_ensure_extension_points_registered ();
577 G_LOCK (loaded_dirs);
583 g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
585 module_path = g_getenv ("GIO_EXTRA_MODULES");
592 paths = g_strsplit (module_path, ":", 0);
594 for (i = 0; paths[i] != NULL; i++)
595 g_io_modules_scan_all_in_directory (paths[i]);
600 /* Initialize types from built-in "modules" */
601 g_null_settings_backend_get_type ();
602 g_memory_settings_backend_get_type ();
603 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
604 _g_inotify_directory_monitor_get_type ();
605 _g_inotify_file_monitor_get_type ();
607 #if defined(HAVE_FEN)
608 _g_fen_directory_monitor_get_type ();
609 _g_fen_file_monitor_get_type ();
612 _g_win32_volume_monitor_get_type ();
613 g_win32_directory_monitor_get_type ();
614 g_registry_backend_get_type ();
617 _g_unix_volume_monitor_get_type ();
620 _g_winhttp_vfs_get_type ();
622 _g_local_vfs_get_type ();
623 _g_dummy_proxy_resolver_get_type ();
624 _g_socks4a_proxy_get_type ();
625 _g_socks4_proxy_get_type ();
626 _g_socks5_proxy_get_type ();
627 _g_dummy_tls_backend_get_type ();
630 G_UNLOCK (loaded_dirs);
634 g_io_extension_point_free (GIOExtensionPoint *ep)
641 * g_io_extension_point_register:
642 * @name: The name of the extension point
644 * Registers an extension point.
646 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
647 * and should not be freed
650 g_io_extension_point_register (const char *name)
652 GIOExtensionPoint *ep;
654 G_LOCK (extension_points);
655 if (extension_points == NULL)
656 extension_points = g_hash_table_new_full (g_str_hash,
659 (GDestroyNotify)g_io_extension_point_free);
661 ep = g_hash_table_lookup (extension_points, name);
664 G_UNLOCK (extension_points);
668 ep = g_new0 (GIOExtensionPoint, 1);
669 ep->name = g_strdup (name);
671 g_hash_table_insert (extension_points, ep->name, ep);
673 G_UNLOCK (extension_points);
679 * g_io_extension_point_lookup:
680 * @name: the name of the extension point
682 * Looks up an existing extension point.
684 * Returns: the #GIOExtensionPoint, or %NULL if there is no
685 * registered extension point with the given name
688 g_io_extension_point_lookup (const char *name)
690 GIOExtensionPoint *ep;
692 G_LOCK (extension_points);
694 if (extension_points != NULL)
695 ep = g_hash_table_lookup (extension_points, name);
697 G_UNLOCK (extension_points);
704 * g_io_extension_point_set_required_type:
705 * @extension_point: a #GIOExtensionPoint
706 * @type: the #GType to require
708 * Sets the required type for @extension_point to @type.
709 * All implementations must henceforth have this type.
712 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
715 extension_point->required_type = type;
719 * g_io_extension_point_get_required_type:
720 * @extension_point: a #GIOExtensionPoint
722 * Gets the required type for @extension_point.
724 * Returns: the #GType that all implementations must have,
725 * or #G_TYPE_INVALID if the extension point has no required type
728 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
730 return extension_point->required_type;
734 lazy_load_modules (GIOExtensionPoint *extension_point)
739 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
743 if (!module->initialized)
745 if (g_type_module_use (G_TYPE_MODULE (module)))
746 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
748 g_printerr ("Failed to load module: %s\n",
755 * g_io_extension_point_get_extensions:
756 * @extension_point: a #GIOExtensionPoint
758 * Gets a list of all extensions that implement this extension point.
759 * The list is sorted by priority, beginning with the highest priority.
761 * Returns: (element-type GIOExtension) (transfer none): a #GList of
762 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
766 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
768 lazy_load_modules (extension_point);
769 return extension_point->extensions;
773 * g_io_extension_point_get_extension_by_name:
774 * @extension_point: a #GIOExtensionPoint
775 * @name: the name of the extension to get
777 * Finds a #GIOExtension for an extension point by name.
779 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
780 * given name, or %NULL if there is no extension with that name
783 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
788 lazy_load_modules (extension_point);
789 for (l = extension_point->extensions; l != NULL; l = l->next)
791 GIOExtension *e = l->data;
793 if (e->name != NULL &&
794 strcmp (e->name, name) == 0)
802 extension_prio_compare (gconstpointer a,
805 const GIOExtension *extension_a = a, *extension_b = b;
807 if (extension_a->priority > extension_b->priority)
810 if (extension_b->priority > extension_a->priority)
817 * g_io_extension_point_implement:
818 * @extension_point_name: the name of the extension point
819 * @type: the #GType to register as extension
820 * @extension_name: the name for the extension
821 * @priority: the priority for the extension
823 * Registers @type as extension for the extension point with name
824 * @extension_point_name.
826 * If @type has already been registered as an extension for this
827 * extension point, the existing #GIOExtension object is returned.
829 * Returns: a #GIOExtension object for #GType
832 g_io_extension_point_implement (const char *extension_point_name,
834 const char *extension_name,
837 GIOExtensionPoint *extension_point;
838 GIOExtension *extension;
841 g_return_val_if_fail (extension_point_name != NULL, NULL);
843 extension_point = g_io_extension_point_lookup (extension_point_name);
844 if (extension_point == NULL)
846 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
850 if (extension_point->required_type != 0 &&
851 !g_type_is_a (type, extension_point->required_type))
853 g_warning ("Tried to register an extension of the type %s to extension point %s. "
854 "Expected type is %s.",
856 extension_point_name,
857 g_type_name (extension_point->required_type));
861 /* It's safe to register the same type multiple times */
862 for (l = extension_point->extensions; l != NULL; l = l->next)
865 if (extension->type == type)
869 extension = g_slice_new0 (GIOExtension);
870 extension->type = type;
871 extension->name = g_strdup (extension_name);
872 extension->priority = priority;
874 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
875 extension, extension_prio_compare);
881 * g_io_extension_ref_class:
882 * @extension: a #GIOExtension
884 * Gets a reference to the class for the type that is
885 * associated with @extension.
887 * Returns: (transfer full): the #GTypeClass for the type of @extension
890 g_io_extension_ref_class (GIOExtension *extension)
892 return g_type_class_ref (extension->type);
896 * g_io_extension_get_type:
897 * @extension: a #GIOExtension
899 * Gets the type associated with @extension.
901 * Returns: the type of @extension
904 g_io_extension_get_type (GIOExtension *extension)
906 return extension->type;
910 * g_io_extension_get_name:
911 * @extension: a #GIOExtension
913 * Gets the name under which @extension was registered.
915 * Note that the same type may be registered as extension
916 * for multiple extension points, under different names.
918 * Returns: the name of @extension.
921 g_io_extension_get_name (GIOExtension *extension)
923 return extension->name;
927 * g_io_extension_get_priority:
928 * @extension: a #GIOExtension
930 * Gets the priority with which @extension was registered.
932 * Returns: the priority of @extension
935 g_io_extension_get_priority (GIOExtension *extension)
937 return extension->priority;