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"
35 #include "gdesktopappinfo.h"
38 #include <glib/gstdio.h>
42 * @short_description: Loadable GIO Modules
45 * Provides an interface and default functions for loading and unloading
46 * modules. This is used internally to make GIO extensible, but can also
47 * be used by others to implement module loading.
52 * SECTION:extensionpoints
53 * @short_description: Extension Points
55 * @see_also: <link linkend="extending-gio">Extending GIO</link>
57 * #GIOExtensionPoint provides a mechanism for modules to extend the
58 * functionality of the library or application that loaded it in an
61 * An extension point is identified by a name, and it may optionally
62 * require that any implementation must by of a certain type (or derived
63 * thereof). Use g_io_extension_point_register() to register an
64 * extension point, and g_io_extension_point_set_required_type() to
65 * set a required type.
67 * A module can implement an extension point by specifying the #GType
68 * that implements the functionality. Additionally, each implementation
69 * of an extension point has a name, and a priority. Use
70 * g_io_extension_point_implement() to implement an extension point.
73 * GIOExtensionPoint *ep;
75 * /* Register an extension point */
76 * ep = g_io_extension_point_register ("my-extension-point");
77 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
81 * /* Implement an extension point */
82 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
83 * g_io_extension_point_implement ("my-extension-point",
84 * my_example_impl_get_type (),
89 * It is up to the code that registered the extension point how
90 * it uses the implementations that have been associated with it.
91 * Depending on the use case, it may use all implementations, or
92 * only the one with the highest priority, or pick a specific
95 * To avoid opening all modules just to find out what extension
96 * points they implement, GIO makes use of a caching mechanism,
97 * see <link linkend="gio-querymodules">gio-querymodules</link>.
98 * You are expected to run this command after installing a
102 GTypeModule parent_instance;
106 gboolean initialized; /* The module was loaded at least once */
108 void (* load) (GIOModule *module);
109 void (* unload) (GIOModule *module);
112 struct _GIOModuleClass
114 GTypeModuleClass parent_class;
118 static void g_io_module_finalize (GObject *object);
119 static gboolean g_io_module_load_module (GTypeModule *gmodule);
120 static void g_io_module_unload_module (GTypeModule *gmodule);
122 struct _GIOExtension {
128 struct _GIOExtensionPoint {
132 GList *lazy_load_modules;
135 static GHashTable *extension_points = NULL;
136 G_LOCK_DEFINE_STATIC(extension_points);
138 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
141 g_io_module_class_init (GIOModuleClass *class)
143 GObjectClass *object_class = G_OBJECT_CLASS (class);
144 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
146 object_class->finalize = g_io_module_finalize;
148 type_module_class->load = g_io_module_load_module;
149 type_module_class->unload = g_io_module_unload_module;
153 g_io_module_init (GIOModule *module)
158 g_io_module_finalize (GObject *object)
160 GIOModule *module = G_IO_MODULE (object);
162 g_free (module->filename);
164 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
168 g_io_module_load_module (GTypeModule *gmodule)
170 GIOModule *module = G_IO_MODULE (gmodule);
172 if (!module->filename)
174 g_warning ("GIOModule path not set");
178 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
180 if (!module->library)
182 g_printerr ("%s\n", g_module_error ());
186 /* Make sure that the loaded library contains the required methods */
187 if (! g_module_symbol (module->library,
189 (gpointer) &module->load) ||
190 ! g_module_symbol (module->library,
191 "g_io_module_unload",
192 (gpointer) &module->unload))
194 g_printerr ("%s\n", g_module_error ());
195 g_module_close (module->library);
200 /* Initialize the loaded module */
201 module->load (module);
202 module->initialized = TRUE;
208 g_io_module_unload_module (GTypeModule *gmodule)
210 GIOModule *module = G_IO_MODULE (gmodule);
212 module->unload (module);
214 g_module_close (module->library);
215 module->library = NULL;
218 module->unload = NULL;
223 * @filename: filename of the shared library module.
225 * Creates a new GIOModule that will load the specific
226 * shared library when in use.
228 * Returns: a #GIOModule from given @filename,
232 g_io_module_new (const gchar *filename)
236 g_return_val_if_fail (filename != NULL, NULL);
238 module = g_object_new (G_IO_TYPE_MODULE, NULL);
239 module->filename = g_strdup (filename);
245 is_valid_module_name (const gchar *basename)
247 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
249 g_str_has_prefix (basename, "lib") &&
250 g_str_has_suffix (basename, ".so");
252 return g_str_has_suffix (basename, ".dll");
257 * g_io_modules_scan_all_in_directory:
258 * @dirname: pathname for a directory containing modules to scan.
260 * Scans all the modules in the specified directory, ensuring that
261 * any extension point implemented by a module is registered.
263 * This may not actually load and initialize all the types in each
264 * module, some modules may be lazily loaded and initialized when
265 * an extension point it implementes is used with e.g.
266 * g_io_extension_point_get_extensions() or
267 * g_io_extension_point_get_extension_by_name().
269 * If you need to guarantee that all types are loaded in all the modules,
270 * use g_io_modules_scan_all_in_directory().
275 g_io_modules_scan_all_in_directory (const char *dirname)
285 if (!g_module_supported ())
288 dir = g_dir_open (dirname, 0, NULL);
292 filename = g_build_filename (dirname, "giomodule.cache", NULL);
294 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
295 g_free, (GDestroyNotify)g_strfreev);
298 if (g_stat (filename, &statbuf) == 0 &&
299 g_file_get_contents (filename, &data, NULL, NULL))
304 /* Cache mtime is the time the cache file was created, any file
305 * that has a ctime before this was created then and not modified
306 * since then (userspace can't change ctime). Its possible to change
307 * the ctime forward without changing the file content, by e.g.
308 * chmoding the file, but this is uncommon and will only cause us
309 * to not use the cache so will not cause bugs.
311 cache_mtime = statbuf.st_mtime;
313 lines = g_strsplit (data, "\n", -1);
316 for (i = 0; lines[i] != NULL; i++)
318 char *line = lines[i];
321 char **extension_points;
326 colon = strchr (line, ':');
327 if (colon == NULL || line == colon)
328 continue; /* Invalid line, ignore */
330 *colon = 0; /* terminate filename */
331 file = g_strdup (line);
332 colon++; /* after colon */
334 while (g_ascii_isspace (*colon))
337 extension_points = g_strsplit (colon, ",", -1);
338 g_hash_table_insert (cache, file, extension_points);
343 while ((name = g_dir_read_name (dir)))
345 if (is_valid_module_name (name))
347 GIOExtensionPoint *extension_point;
350 char **extension_points;
353 path = g_build_filename (dirname, name, NULL);
354 module = g_io_module_new (path);
356 extension_points = g_hash_table_lookup (cache, name);
357 if (extension_points != NULL &&
358 g_stat (path, &statbuf) == 0 &&
359 statbuf.st_ctime <= cache_mtime)
361 /* Lazy load/init the library when first required */
362 for (i = 0; extension_points[i] != NULL; i++)
365 g_io_extension_point_register (extension_points[i]);
366 extension_point->lazy_load_modules =
367 g_list_prepend (extension_point->lazy_load_modules,
373 /* Try to load and init types */
374 if (g_type_module_use (G_TYPE_MODULE (module)))
375 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
377 { /* Failure to load */
378 g_printerr ("Failed to load module: %s\n", path);
379 g_object_unref (module);
391 g_hash_table_destroy (cache);
398 * g_io_modules_load_all_in_directory:
399 * @dirname: pathname for a directory containing modules to load.
401 * Loads all the modules in the specified directory.
403 * If don't require all modules to be initialized (and thus registering
404 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
405 * which allows delayed/lazy loading of modules.
407 * Returns: a list of #GIOModules loaded from the directory,
408 * All the modules are loaded into memory, if you want to
409 * unload them (enabling on-demand loading) you must call
410 * g_type_module_unuse() on all the modules. Free the list
411 * with g_list_free().
414 g_io_modules_load_all_in_directory (const char *dirname)
420 if (!g_module_supported ())
423 dir = g_dir_open (dirname, 0, NULL);
428 while ((name = g_dir_read_name (dir)))
430 if (is_valid_module_name (name))
435 path = g_build_filename (dirname, name, NULL);
436 module = g_io_module_new (path);
438 if (!g_type_module_use (G_TYPE_MODULE (module)))
440 g_printerr ("Failed to load module: %s\n", path);
441 g_object_unref (module);
448 modules = g_list_prepend (modules, module);
457 G_LOCK_DEFINE_STATIC (registered_extensions);
458 G_LOCK_DEFINE_STATIC (loaded_dirs);
460 extern GType _g_fen_directory_monitor_get_type (void);
461 extern GType _g_fen_file_monitor_get_type (void);
462 extern GType _g_inotify_directory_monitor_get_type (void);
463 extern GType _g_inotify_file_monitor_get_type (void);
464 extern GType _g_unix_volume_monitor_get_type (void);
465 extern GType _g_local_vfs_get_type (void);
467 extern GType _g_win32_volume_monitor_get_type (void);
468 extern GType g_win32_directory_monitor_get_type (void);
469 extern GType _g_winhttp_vfs_get_type (void);
471 #ifdef G_PLATFORM_WIN32
475 static HMODULE gio_dll = NULL;
480 DllMain (HINSTANCE hinstDLL,
484 if (fdwReason == DLL_PROCESS_ATTACH)
492 #undef GIO_MODULE_DIR
494 /* GIO_MODULE_DIR is used only in code called just once,
495 * so no problem leaking this
497 #define GIO_MODULE_DIR \
498 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
505 _g_io_modules_ensure_extension_points_registered (void)
507 static gboolean registered_extensions = FALSE;
508 GIOExtensionPoint *ep;
510 G_LOCK (registered_extensions);
512 if (!registered_extensions)
514 registered_extensions = TRUE;
517 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
518 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
521 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
522 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
524 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
525 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
527 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
528 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
530 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
531 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
533 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
534 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
536 ep = g_io_extension_point_register ("gsettings-backend");
537 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
540 G_UNLOCK (registered_extensions);
544 _g_io_modules_ensure_loaded (void)
546 static gboolean loaded_dirs = FALSE;
547 const char *module_path;
549 _g_io_modules_ensure_extension_points_registered ();
551 G_LOCK (loaded_dirs);
557 g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
559 module_path = g_getenv ("GIO_EXTRA_MODULES");
566 paths = g_strsplit (module_path, ":", 0);
568 for (i = 0; paths[i] != NULL; i++)
569 g_io_modules_scan_all_in_directory (paths[i]);
574 /* Initialize types from built-in "modules" */
575 g_memory_settings_backend_get_type ();
576 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
577 _g_inotify_directory_monitor_get_type ();
578 _g_inotify_file_monitor_get_type ();
580 #if defined(HAVE_FEN)
581 _g_fen_directory_monitor_get_type ();
582 _g_fen_file_monitor_get_type ();
585 _g_win32_volume_monitor_get_type ();
586 g_win32_directory_monitor_get_type ();
589 _g_unix_volume_monitor_get_type ();
592 _g_winhttp_vfs_get_type ();
594 _g_local_vfs_get_type ();
597 G_UNLOCK (loaded_dirs);
601 g_io_extension_point_free (GIOExtensionPoint *ep)
608 * g_io_extension_point_register:
609 * @name: The name of the extension point
611 * Registers an extension point.
613 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
614 * and should not be freed
617 g_io_extension_point_register (const char *name)
619 GIOExtensionPoint *ep;
621 G_LOCK (extension_points);
622 if (extension_points == NULL)
623 extension_points = g_hash_table_new_full (g_str_hash,
626 (GDestroyNotify)g_io_extension_point_free);
628 ep = g_hash_table_lookup (extension_points, name);
631 G_UNLOCK (extension_points);
635 ep = g_new0 (GIOExtensionPoint, 1);
636 ep->name = g_strdup (name);
638 g_hash_table_insert (extension_points, ep->name, ep);
640 G_UNLOCK (extension_points);
646 * g_io_extension_point_lookup:
647 * @name: the name of the extension point
649 * Looks up an existing extension point.
651 * Returns: the #GIOExtensionPoint, or %NULL if there is no
652 * registered extension point with the given name
655 g_io_extension_point_lookup (const char *name)
657 GIOExtensionPoint *ep;
659 G_LOCK (extension_points);
661 if (extension_points != NULL)
662 ep = g_hash_table_lookup (extension_points, name);
664 G_UNLOCK (extension_points);
671 * g_io_extension_point_set_required_type:
672 * @extension_point: a #GIOExtensionPoint
673 * @type: the #GType to require
675 * Sets the required type for @extension_point to @type.
676 * All implementations must henceforth have this type.
679 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
682 extension_point->required_type = type;
686 * g_io_extension_point_get_required_type:
687 * @extension_point: a #GIOExtensionPoint
689 * Gets the required type for @extension_point.
691 * Returns: the #GType that all implementations must have,
692 * or #G_TYPE_INVALID if the extension point has no required type
695 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
697 return extension_point->required_type;
701 lazy_load_modules (GIOExtensionPoint *extension_point)
706 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
710 if (!module->initialized)
712 if (g_type_module_use (G_TYPE_MODULE (module)))
713 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
715 g_printerr ("Failed to load module: %s\n",
722 * g_io_extension_point_get_extensions:
723 * @extension_point: a #GIOExtensionPoint
725 * Gets a list of all extensions that implement this extension point.
726 * The list is sorted by priority, beginning with the highest priority.
728 * Returns: a #GList of #GIOExtension<!-- -->s. The list is owned by
729 * GIO and should not be modified
732 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
734 lazy_load_modules (extension_point);
735 return extension_point->extensions;
739 * g_io_extension_point_get_extension_by_name:
740 * @extension_point: a #GIOExtensionPoint
741 * @name: the name of the extension to get
743 * Finds a #GIOExtension for an extension point by name.
745 * Returns: the #GIOExtension for @extension_point that has the
746 * given name, or %NULL if there is no extension with that name
749 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
754 lazy_load_modules (extension_point);
755 for (l = extension_point->extensions; l != NULL; l = l->next)
757 GIOExtension *e = l->data;
759 if (e->name != NULL &&
760 strcmp (e->name, name) == 0)
768 extension_prio_compare (gconstpointer a,
771 const GIOExtension *extension_a = a, *extension_b = b;
773 return extension_b->priority - extension_a->priority;
777 * g_io_extension_point_implement:
778 * @extension_point_name: the name of the extension point
779 * @type: the #GType to register as extension
780 * @extension_name: the name for the extension
781 * @priority: the priority for the extension
783 * Registers @type as extension for the extension point with name
784 * @extension_point_name.
786 * If @type has already been registered as an extension for this
787 * extension point, the existing #GIOExtension object is returned.
789 * Returns: a #GIOExtension object for #GType
792 g_io_extension_point_implement (const char *extension_point_name,
794 const char *extension_name,
797 GIOExtensionPoint *extension_point;
798 GIOExtension *extension;
801 g_return_val_if_fail (extension_point_name != NULL, NULL);
803 extension_point = g_io_extension_point_lookup (extension_point_name);
804 if (extension_point == NULL)
806 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
810 if (extension_point->required_type != 0 &&
811 !g_type_is_a (type, extension_point->required_type))
813 g_warning ("Tried to register an extension of the type %s to extension point %s. "
814 "Expected type is %s.",
816 extension_point_name,
817 g_type_name (extension_point->required_type));
821 /* It's safe to register the same type multiple times */
822 for (l = extension_point->extensions; l != NULL; l = l->next)
825 if (extension->type == type)
829 extension = g_slice_new0 (GIOExtension);
830 extension->type = type;
831 extension->name = g_strdup (extension_name);
832 extension->priority = priority;
834 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
835 extension, extension_prio_compare);
841 * g_io_extension_ref_class:
842 * @extension: a #GIOExtension
844 * Gets a reference to the class for the type that is
845 * associated with @extension.
847 * Returns: the #GTypeClass for the type of @extension
850 g_io_extension_ref_class (GIOExtension *extension)
852 return g_type_class_ref (extension->type);
856 * g_io_extension_get_type:
857 * @extension: a #GIOExtension
859 * Gets the type associated with @extension.
861 * Returns: the type of @extension
864 g_io_extension_get_type (GIOExtension *extension)
866 return extension->type;
870 * g_io_extension_get_name:
871 * @extension: a #GIOExtension
873 * Gets the name under which @extension was registered.
875 * Note that the same type may be registered as extension
876 * for multiple extension points, under different names.
878 * Returns: the name of @extension.
881 g_io_extension_get_name (GIOExtension *extension)
883 return extension->name;
887 * g_io_extension_get_priority:
888 * @extension: a #GIOExtension
890 * Gets the priority with which @extension was registered.
892 * Returns: the priority of @extension
895 g_io_extension_get_priority (GIOExtension *extension)
897 return extension->priority;
900 #define __G_IO_MODULE_C__
901 #include "gioaliasdef.c"