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: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
415 * from the directory,
416 * All the modules are loaded into memory, if you want to
417 * unload them (enabling on-demand loading) you must call
418 * g_type_module_unuse() on all the modules. Free the list
419 * with g_list_free().
422 g_io_modules_load_all_in_directory (const char *dirname)
428 if (!g_module_supported ())
431 dir = g_dir_open (dirname, 0, NULL);
436 while ((name = g_dir_read_name (dir)))
438 if (is_valid_module_name (name))
443 path = g_build_filename (dirname, name, NULL);
444 module = g_io_module_new (path);
446 if (!g_type_module_use (G_TYPE_MODULE (module)))
448 g_printerr ("Failed to load module: %s\n", path);
449 g_object_unref (module);
456 modules = g_list_prepend (modules, module);
465 G_LOCK_DEFINE_STATIC (registered_extensions);
466 G_LOCK_DEFINE_STATIC (loaded_dirs);
468 extern GType _g_fen_directory_monitor_get_type (void);
469 extern GType _g_fen_file_monitor_get_type (void);
470 extern GType _g_inotify_directory_monitor_get_type (void);
471 extern GType _g_inotify_file_monitor_get_type (void);
472 extern GType _g_unix_volume_monitor_get_type (void);
473 extern GType _g_local_vfs_get_type (void);
475 extern GType _g_win32_volume_monitor_get_type (void);
476 extern GType g_win32_directory_monitor_get_type (void);
477 extern GType _g_winhttp_vfs_get_type (void);
479 extern GType _g_dummy_proxy_resolver_get_type (void);
481 #ifdef G_PLATFORM_WIN32
485 static HMODULE gio_dll = NULL;
490 DllMain (HINSTANCE hinstDLL,
494 if (fdwReason == DLL_PROCESS_ATTACH)
502 #undef GIO_MODULE_DIR
504 /* GIO_MODULE_DIR is used only in code called just once,
505 * so no problem leaking this
507 #define GIO_MODULE_DIR \
508 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
515 _g_io_modules_ensure_extension_points_registered (void)
517 static gboolean registered_extensions = FALSE;
518 GIOExtensionPoint *ep;
520 G_LOCK (registered_extensions);
522 if (!registered_extensions)
524 registered_extensions = TRUE;
527 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
528 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
531 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
532 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
534 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
535 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
537 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
538 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
540 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
541 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
543 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
544 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
546 ep = g_io_extension_point_register ("gsettings-backend");
547 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
549 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
550 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
552 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
553 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
556 G_UNLOCK (registered_extensions);
560 _g_io_modules_ensure_loaded (void)
562 static gboolean loaded_dirs = FALSE;
563 const char *module_path;
565 _g_io_modules_ensure_extension_points_registered ();
567 G_LOCK (loaded_dirs);
573 g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
575 module_path = g_getenv ("GIO_EXTRA_MODULES");
582 paths = g_strsplit (module_path, ":", 0);
584 for (i = 0; paths[i] != NULL; i++)
585 g_io_modules_scan_all_in_directory (paths[i]);
590 /* Initialize types from built-in "modules" */
591 g_memory_settings_backend_get_type ();
592 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
593 _g_inotify_directory_monitor_get_type ();
594 _g_inotify_file_monitor_get_type ();
596 #if defined(HAVE_FEN)
597 _g_fen_directory_monitor_get_type ();
598 _g_fen_file_monitor_get_type ();
601 _g_win32_volume_monitor_get_type ();
602 g_win32_directory_monitor_get_type ();
603 g_registry_backend_get_type ();
606 _g_unix_volume_monitor_get_type ();
609 _g_winhttp_vfs_get_type ();
611 _g_local_vfs_get_type ();
612 _g_dummy_proxy_resolver_get_type ();
613 _g_socks4a_proxy_get_type ();
614 _g_socks4_proxy_get_type ();
615 _g_socks5_proxy_get_type ();
618 G_UNLOCK (loaded_dirs);
622 g_io_extension_point_free (GIOExtensionPoint *ep)
629 * g_io_extension_point_register:
630 * @name: The name of the extension point
632 * Registers an extension point.
634 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
635 * and should not be freed
638 g_io_extension_point_register (const char *name)
640 GIOExtensionPoint *ep;
642 G_LOCK (extension_points);
643 if (extension_points == NULL)
644 extension_points = g_hash_table_new_full (g_str_hash,
647 (GDestroyNotify)g_io_extension_point_free);
649 ep = g_hash_table_lookup (extension_points, name);
652 G_UNLOCK (extension_points);
656 ep = g_new0 (GIOExtensionPoint, 1);
657 ep->name = g_strdup (name);
659 g_hash_table_insert (extension_points, ep->name, ep);
661 G_UNLOCK (extension_points);
667 * g_io_extension_point_lookup:
668 * @name: the name of the extension point
670 * Looks up an existing extension point.
672 * Returns: the #GIOExtensionPoint, or %NULL if there is no
673 * registered extension point with the given name
676 g_io_extension_point_lookup (const char *name)
678 GIOExtensionPoint *ep;
680 G_LOCK (extension_points);
682 if (extension_points != NULL)
683 ep = g_hash_table_lookup (extension_points, name);
685 G_UNLOCK (extension_points);
692 * g_io_extension_point_set_required_type:
693 * @extension_point: a #GIOExtensionPoint
694 * @type: the #GType to require
696 * Sets the required type for @extension_point to @type.
697 * All implementations must henceforth have this type.
700 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
703 extension_point->required_type = type;
707 * g_io_extension_point_get_required_type:
708 * @extension_point: a #GIOExtensionPoint
710 * Gets the required type for @extension_point.
712 * Returns: the #GType that all implementations must have,
713 * or #G_TYPE_INVALID if the extension point has no required type
716 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
718 return extension_point->required_type;
722 lazy_load_modules (GIOExtensionPoint *extension_point)
727 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
731 if (!module->initialized)
733 if (g_type_module_use (G_TYPE_MODULE (module)))
734 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
736 g_printerr ("Failed to load module: %s\n",
743 * g_io_extension_point_get_extensions:
744 * @extension_point: a #GIOExtensionPoint
746 * Gets a list of all extensions that implement this extension point.
747 * The list is sorted by priority, beginning with the highest priority.
749 * Returns: (element-type GIOExtension) (transfer none): a #GList of
750 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
754 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
756 lazy_load_modules (extension_point);
757 return extension_point->extensions;
761 * g_io_extension_point_get_extension_by_name:
762 * @extension_point: a #GIOExtensionPoint
763 * @name: the name of the extension to get
765 * Finds a #GIOExtension for an extension point by name.
767 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
768 * given name, or %NULL if there is no extension with that name
771 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
776 lazy_load_modules (extension_point);
777 for (l = extension_point->extensions; l != NULL; l = l->next)
779 GIOExtension *e = l->data;
781 if (e->name != NULL &&
782 strcmp (e->name, name) == 0)
790 extension_prio_compare (gconstpointer a,
793 const GIOExtension *extension_a = a, *extension_b = b;
795 if (extension_a->priority > extension_b->priority)
798 if (extension_b->priority > extension_a->priority)
805 * g_io_extension_point_implement:
806 * @extension_point_name: the name of the extension point
807 * @type: the #GType to register as extension
808 * @extension_name: the name for the extension
809 * @priority: the priority for the extension
811 * Registers @type as extension for the extension point with name
812 * @extension_point_name.
814 * If @type has already been registered as an extension for this
815 * extension point, the existing #GIOExtension object is returned.
817 * Returns: a #GIOExtension object for #GType
820 g_io_extension_point_implement (const char *extension_point_name,
822 const char *extension_name,
825 GIOExtensionPoint *extension_point;
826 GIOExtension *extension;
829 g_return_val_if_fail (extension_point_name != NULL, NULL);
831 extension_point = g_io_extension_point_lookup (extension_point_name);
832 if (extension_point == NULL)
834 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
838 if (extension_point->required_type != 0 &&
839 !g_type_is_a (type, extension_point->required_type))
841 g_warning ("Tried to register an extension of the type %s to extension point %s. "
842 "Expected type is %s.",
844 extension_point_name,
845 g_type_name (extension_point->required_type));
849 /* It's safe to register the same type multiple times */
850 for (l = extension_point->extensions; l != NULL; l = l->next)
853 if (extension->type == type)
857 extension = g_slice_new0 (GIOExtension);
858 extension->type = type;
859 extension->name = g_strdup (extension_name);
860 extension->priority = priority;
862 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
863 extension, extension_prio_compare);
869 * g_io_extension_ref_class:
870 * @extension: a #GIOExtension
872 * Gets a reference to the class for the type that is
873 * associated with @extension.
875 * Returns: (transfer full): the #GTypeClass for the type of @extension
878 g_io_extension_ref_class (GIOExtension *extension)
880 return g_type_class_ref (extension->type);
884 * g_io_extension_get_type:
885 * @extension: a #GIOExtension
887 * Gets the type associated with @extension.
889 * Returns: the type of @extension
892 g_io_extension_get_type (GIOExtension *extension)
894 return extension->type;
898 * g_io_extension_get_name:
899 * @extension: a #GIOExtension
901 * Gets the name under which @extension was registered.
903 * Note that the same type may be registered as extension
904 * for multiple extension points, under different names.
906 * Returns: the name of @extension.
909 g_io_extension_get_name (GIOExtension *extension)
911 return extension->name;
915 * g_io_extension_get_priority:
916 * @extension: a #GIOExtension
918 * Gets the priority with which @extension was registered.
920 * Returns: the priority of @extension
923 g_io_extension_get_priority (GIOExtension *extension)
925 return extension->priority;