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 "gsocks5proxy.h"
38 #include "gdesktopappinfo.h"
40 #include <glib/gstdio.h>
44 * @short_description: Loadable GIO Modules
47 * Provides an interface and default functions for loading and unloading
48 * modules. This is used internally to make GIO extensible, but can also
49 * be used by others to implement module loading.
54 * SECTION:extensionpoints
55 * @short_description: Extension Points
57 * @see_also: <link linkend="extending-gio">Extending GIO</link>
59 * #GIOExtensionPoint provides a mechanism for modules to extend the
60 * functionality of the library or application that loaded it in an
63 * An extension point is identified by a name, and it may optionally
64 * require that any implementation must by of a certain type (or derived
65 * thereof). Use g_io_extension_point_register() to register an
66 * extension point, and g_io_extension_point_set_required_type() to
67 * set a required type.
69 * A module can implement an extension point by specifying the #GType
70 * that implements the functionality. Additionally, each implementation
71 * of an extension point has a name, and a priority. Use
72 * g_io_extension_point_implement() to implement an extension point.
75 * GIOExtensionPoint *ep;
77 * /* Register an extension point */
78 * ep = g_io_extension_point_register ("my-extension-point");
79 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
83 * /* Implement an extension point */
84 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
85 * g_io_extension_point_implement ("my-extension-point",
86 * my_example_impl_get_type (),
91 * It is up to the code that registered the extension point how
92 * it uses the implementations that have been associated with it.
93 * Depending on the use case, it may use all implementations, or
94 * only the one with the highest priority, or pick a specific
97 * To avoid opening all modules just to find out what extension
98 * points they implement, GIO makes use of a caching mechanism,
99 * see <link linkend="gio-querymodules">gio-querymodules</link>.
100 * You are expected to run this command after installing a
104 GTypeModule parent_instance;
108 gboolean initialized; /* The module was loaded at least once */
110 void (* load) (GIOModule *module);
111 void (* unload) (GIOModule *module);
114 struct _GIOModuleClass
116 GTypeModuleClass parent_class;
120 static void g_io_module_finalize (GObject *object);
121 static gboolean g_io_module_load_module (GTypeModule *gmodule);
122 static void g_io_module_unload_module (GTypeModule *gmodule);
124 struct _GIOExtension {
130 struct _GIOExtensionPoint {
134 GList *lazy_load_modules;
137 static GHashTable *extension_points = NULL;
138 G_LOCK_DEFINE_STATIC(extension_points);
140 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
143 g_io_module_class_init (GIOModuleClass *class)
145 GObjectClass *object_class = G_OBJECT_CLASS (class);
146 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
148 object_class->finalize = g_io_module_finalize;
150 type_module_class->load = g_io_module_load_module;
151 type_module_class->unload = g_io_module_unload_module;
155 g_io_module_init (GIOModule *module)
160 g_io_module_finalize (GObject *object)
162 GIOModule *module = G_IO_MODULE (object);
164 g_free (module->filename);
166 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
170 g_io_module_load_module (GTypeModule *gmodule)
172 GIOModule *module = G_IO_MODULE (gmodule);
174 if (!module->filename)
176 g_warning ("GIOModule path not set");
180 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
182 if (!module->library)
184 g_printerr ("%s\n", g_module_error ());
188 /* Make sure that the loaded library contains the required methods */
189 if (! g_module_symbol (module->library,
191 (gpointer) &module->load) ||
192 ! g_module_symbol (module->library,
193 "g_io_module_unload",
194 (gpointer) &module->unload))
196 g_printerr ("%s\n", g_module_error ());
197 g_module_close (module->library);
202 /* Initialize the loaded module */
203 module->load (module);
204 module->initialized = TRUE;
210 g_io_module_unload_module (GTypeModule *gmodule)
212 GIOModule *module = G_IO_MODULE (gmodule);
214 module->unload (module);
216 g_module_close (module->library);
217 module->library = NULL;
220 module->unload = NULL;
225 * @filename: filename of the shared library module.
227 * Creates a new GIOModule that will load the specific
228 * shared library when in use.
230 * Returns: a #GIOModule from given @filename,
234 g_io_module_new (const gchar *filename)
238 g_return_val_if_fail (filename != NULL, NULL);
240 module = g_object_new (G_IO_TYPE_MODULE, NULL);
241 module->filename = g_strdup (filename);
247 is_valid_module_name (const gchar *basename)
249 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
251 g_str_has_prefix (basename, "lib") &&
252 g_str_has_suffix (basename, ".so");
254 return g_str_has_suffix (basename, ".dll");
259 * g_io_modules_scan_all_in_directory:
260 * @dirname: pathname for a directory containing modules to scan.
262 * Scans all the modules in the specified directory, ensuring that
263 * any extension point implemented by a module is registered.
265 * This may not actually load and initialize all the types in each
266 * module, some modules may be lazily loaded and initialized when
267 * an extension point it implementes is used with e.g.
268 * g_io_extension_point_get_extensions() or
269 * g_io_extension_point_get_extension_by_name().
271 * If you need to guarantee that all types are loaded in all the modules,
272 * use g_io_modules_scan_all_in_directory().
277 g_io_modules_scan_all_in_directory (const char *dirname)
287 if (!g_module_supported ())
290 dir = g_dir_open (dirname, 0, NULL);
294 filename = g_build_filename (dirname, "giomodule.cache", NULL);
296 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
297 g_free, (GDestroyNotify)g_strfreev);
300 if (g_stat (filename, &statbuf) == 0 &&
301 g_file_get_contents (filename, &data, NULL, NULL))
306 /* Cache mtime is the time the cache file was created, any file
307 * that has a ctime before this was created then and not modified
308 * since then (userspace can't change ctime). Its possible to change
309 * the ctime forward without changing the file content, by e.g.
310 * chmoding the file, but this is uncommon and will only cause us
311 * to not use the cache so will not cause bugs.
313 cache_mtime = statbuf.st_mtime;
315 lines = g_strsplit (data, "\n", -1);
318 for (i = 0; lines[i] != NULL; i++)
320 char *line = lines[i];
323 char **extension_points;
328 colon = strchr (line, ':');
329 if (colon == NULL || line == colon)
330 continue; /* Invalid line, ignore */
332 *colon = 0; /* terminate filename */
333 file = g_strdup (line);
334 colon++; /* after colon */
336 while (g_ascii_isspace (*colon))
339 extension_points = g_strsplit (colon, ",", -1);
340 g_hash_table_insert (cache, file, extension_points);
345 while ((name = g_dir_read_name (dir)))
347 if (is_valid_module_name (name))
349 GIOExtensionPoint *extension_point;
352 char **extension_points;
355 path = g_build_filename (dirname, name, NULL);
356 module = g_io_module_new (path);
358 extension_points = g_hash_table_lookup (cache, name);
359 if (extension_points != NULL &&
360 g_stat (path, &statbuf) == 0 &&
361 statbuf.st_ctime <= cache_mtime)
363 /* Lazy load/init the library when first required */
364 for (i = 0; extension_points[i] != NULL; i++)
367 g_io_extension_point_register (extension_points[i]);
368 extension_point->lazy_load_modules =
369 g_list_prepend (extension_point->lazy_load_modules,
375 /* Try to load and init types */
376 if (g_type_module_use (G_TYPE_MODULE (module)))
377 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
379 { /* Failure to load */
380 g_printerr ("Failed to load module: %s\n", path);
381 g_object_unref (module);
393 g_hash_table_destroy (cache);
400 * g_io_modules_load_all_in_directory:
401 * @dirname: pathname for a directory containing modules to load.
403 * Loads all the modules in the specified directory.
405 * If don't require all modules to be initialized (and thus registering
406 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
407 * which allows delayed/lazy loading of modules.
409 * Returns: a list of #GIOModules loaded from the directory,
410 * All the modules are loaded into memory, if you want to
411 * unload them (enabling on-demand loading) you must call
412 * g_type_module_unuse() on all the modules. Free the list
413 * with g_list_free().
416 g_io_modules_load_all_in_directory (const char *dirname)
422 if (!g_module_supported ())
425 dir = g_dir_open (dirname, 0, NULL);
430 while ((name = g_dir_read_name (dir)))
432 if (is_valid_module_name (name))
437 path = g_build_filename (dirname, name, NULL);
438 module = g_io_module_new (path);
440 if (!g_type_module_use (G_TYPE_MODULE (module)))
442 g_printerr ("Failed to load module: %s\n", path);
443 g_object_unref (module);
450 modules = g_list_prepend (modules, module);
459 G_LOCK_DEFINE_STATIC (registered_extensions);
460 G_LOCK_DEFINE_STATIC (loaded_dirs);
462 extern GType _g_fen_directory_monitor_get_type (void);
463 extern GType _g_fen_file_monitor_get_type (void);
464 extern GType _g_inotify_directory_monitor_get_type (void);
465 extern GType _g_inotify_file_monitor_get_type (void);
466 extern GType _g_unix_volume_monitor_get_type (void);
467 extern GType _g_local_vfs_get_type (void);
469 extern GType _g_win32_volume_monitor_get_type (void);
470 extern GType g_win32_directory_monitor_get_type (void);
471 extern GType _g_winhttp_vfs_get_type (void);
473 extern GType _g_dummy_proxy_resolver_get_type (void);
475 #ifdef G_PLATFORM_WIN32
479 static HMODULE gio_dll = NULL;
484 DllMain (HINSTANCE hinstDLL,
488 if (fdwReason == DLL_PROCESS_ATTACH)
496 #undef GIO_MODULE_DIR
498 /* GIO_MODULE_DIR is used only in code called just once,
499 * so no problem leaking this
501 #define GIO_MODULE_DIR \
502 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
509 _g_io_modules_ensure_extension_points_registered (void)
511 static gboolean registered_extensions = FALSE;
512 GIOExtensionPoint *ep;
514 G_LOCK (registered_extensions);
516 if (!registered_extensions)
518 registered_extensions = TRUE;
521 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
522 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
525 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
526 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
528 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
529 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
531 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
532 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
534 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
535 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
537 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
538 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
540 ep = g_io_extension_point_register ("gsettings-backend");
541 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
543 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
544 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
546 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
547 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
550 G_UNLOCK (registered_extensions);
554 _g_io_modules_ensure_loaded (void)
556 static gboolean loaded_dirs = FALSE;
557 const char *module_path;
559 _g_io_modules_ensure_extension_points_registered ();
561 G_LOCK (loaded_dirs);
567 g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
569 module_path = g_getenv ("GIO_EXTRA_MODULES");
576 paths = g_strsplit (module_path, ":", 0);
578 for (i = 0; paths[i] != NULL; i++)
579 g_io_modules_scan_all_in_directory (paths[i]);
584 /* Initialize types from built-in "modules" */
585 g_memory_settings_backend_get_type ();
586 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
587 _g_inotify_directory_monitor_get_type ();
588 _g_inotify_file_monitor_get_type ();
590 #if defined(HAVE_FEN)
591 _g_fen_directory_monitor_get_type ();
592 _g_fen_file_monitor_get_type ();
595 _g_win32_volume_monitor_get_type ();
596 g_win32_directory_monitor_get_type ();
599 _g_unix_volume_monitor_get_type ();
602 _g_winhttp_vfs_get_type ();
604 _g_local_vfs_get_type ();
605 _g_dummy_proxy_resolver_get_type ();
606 _g_socks5_proxy_get_type ();
609 G_UNLOCK (loaded_dirs);
613 g_io_extension_point_free (GIOExtensionPoint *ep)
620 * g_io_extension_point_register:
621 * @name: The name of the extension point
623 * Registers an extension point.
625 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
626 * and should not be freed
629 g_io_extension_point_register (const char *name)
631 GIOExtensionPoint *ep;
633 G_LOCK (extension_points);
634 if (extension_points == NULL)
635 extension_points = g_hash_table_new_full (g_str_hash,
638 (GDestroyNotify)g_io_extension_point_free);
640 ep = g_hash_table_lookup (extension_points, name);
643 G_UNLOCK (extension_points);
647 ep = g_new0 (GIOExtensionPoint, 1);
648 ep->name = g_strdup (name);
650 g_hash_table_insert (extension_points, ep->name, ep);
652 G_UNLOCK (extension_points);
658 * g_io_extension_point_lookup:
659 * @name: the name of the extension point
661 * Looks up an existing extension point.
663 * Returns: the #GIOExtensionPoint, or %NULL if there is no
664 * registered extension point with the given name
667 g_io_extension_point_lookup (const char *name)
669 GIOExtensionPoint *ep;
671 G_LOCK (extension_points);
673 if (extension_points != NULL)
674 ep = g_hash_table_lookup (extension_points, name);
676 G_UNLOCK (extension_points);
683 * g_io_extension_point_set_required_type:
684 * @extension_point: a #GIOExtensionPoint
685 * @type: the #GType to require
687 * Sets the required type for @extension_point to @type.
688 * All implementations must henceforth have this type.
691 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
694 extension_point->required_type = type;
698 * g_io_extension_point_get_required_type:
699 * @extension_point: a #GIOExtensionPoint
701 * Gets the required type for @extension_point.
703 * Returns: the #GType that all implementations must have,
704 * or #G_TYPE_INVALID if the extension point has no required type
707 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
709 return extension_point->required_type;
713 lazy_load_modules (GIOExtensionPoint *extension_point)
718 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
722 if (!module->initialized)
724 if (g_type_module_use (G_TYPE_MODULE (module)))
725 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
727 g_printerr ("Failed to load module: %s\n",
734 * g_io_extension_point_get_extensions:
735 * @extension_point: a #GIOExtensionPoint
737 * Gets a list of all extensions that implement this extension point.
738 * The list is sorted by priority, beginning with the highest priority.
740 * Returns: (element-type GIOExtension) (transfer none): a #GList of
741 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
745 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
747 lazy_load_modules (extension_point);
748 return extension_point->extensions;
752 * g_io_extension_point_get_extension_by_name:
753 * @extension_point: a #GIOExtensionPoint
754 * @name: the name of the extension to get
756 * Finds a #GIOExtension for an extension point by name.
758 * Returns: the #GIOExtension for @extension_point that has the
759 * given name, or %NULL if there is no extension with that name
762 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
767 lazy_load_modules (extension_point);
768 for (l = extension_point->extensions; l != NULL; l = l->next)
770 GIOExtension *e = l->data;
772 if (e->name != NULL &&
773 strcmp (e->name, name) == 0)
781 extension_prio_compare (gconstpointer a,
784 const GIOExtension *extension_a = a, *extension_b = b;
786 return extension_b->priority - extension_a->priority;
790 * g_io_extension_point_implement:
791 * @extension_point_name: the name of the extension point
792 * @type: the #GType to register as extension
793 * @extension_name: the name for the extension
794 * @priority: the priority for the extension
796 * Registers @type as extension for the extension point with name
797 * @extension_point_name.
799 * If @type has already been registered as an extension for this
800 * extension point, the existing #GIOExtension object is returned.
802 * Returns: a #GIOExtension object for #GType
805 g_io_extension_point_implement (const char *extension_point_name,
807 const char *extension_name,
810 GIOExtensionPoint *extension_point;
811 GIOExtension *extension;
814 g_return_val_if_fail (extension_point_name != NULL, NULL);
816 extension_point = g_io_extension_point_lookup (extension_point_name);
817 if (extension_point == NULL)
819 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
823 if (extension_point->required_type != 0 &&
824 !g_type_is_a (type, extension_point->required_type))
826 g_warning ("Tried to register an extension of the type %s to extension point %s. "
827 "Expected type is %s.",
829 extension_point_name,
830 g_type_name (extension_point->required_type));
834 /* It's safe to register the same type multiple times */
835 for (l = extension_point->extensions; l != NULL; l = l->next)
838 if (extension->type == type)
842 extension = g_slice_new0 (GIOExtension);
843 extension->type = type;
844 extension->name = g_strdup (extension_name);
845 extension->priority = priority;
847 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
848 extension, extension_prio_compare);
854 * g_io_extension_ref_class:
855 * @extension: a #GIOExtension
857 * Gets a reference to the class for the type that is
858 * associated with @extension.
860 * Returns: the #GTypeClass for the type of @extension
863 g_io_extension_ref_class (GIOExtension *extension)
865 return g_type_class_ref (extension->type);
869 * g_io_extension_get_type:
870 * @extension: a #GIOExtension
872 * Gets the type associated with @extension.
874 * Returns: the type of @extension
877 g_io_extension_get_type (GIOExtension *extension)
879 return extension->type;
883 * g_io_extension_get_name:
884 * @extension: a #GIOExtension
886 * Gets the name under which @extension was registered.
888 * Note that the same type may be registered as extension
889 * for multiple extension points, under different names.
891 * Returns: the name of @extension.
894 g_io_extension_get_name (GIOExtension *extension)
896 return extension->name;
900 * g_io_extension_get_priority:
901 * @extension: a #GIOExtension
903 * Gets the priority with which @extension was registered.
905 * Returns: the priority of @extension
908 g_io_extension_get_priority (GIOExtension *extension)
910 return extension->priority;