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>
46 #include "gdesktopappinfo.h"
51 * @short_description: Loadable GIO Modules
54 * Provides an interface and default functions for loading and unloading
55 * modules. This is used internally to make GIO extensible, but can also
56 * be used by others to implement module loading.
61 * SECTION:extensionpoints
62 * @short_description: Extension Points
64 * @see_also: <link linkend="extending-gio">Extending GIO</link>
66 * #GIOExtensionPoint provides a mechanism for modules to extend the
67 * functionality of the library or application that loaded it in an
70 * An extension point is identified by a name, and it may optionally
71 * require that any implementation must by of a certain type (or derived
72 * thereof). Use g_io_extension_point_register() to register an
73 * extension point, and g_io_extension_point_set_required_type() to
74 * set a required type.
76 * A module can implement an extension point by specifying the #GType
77 * that implements the functionality. Additionally, each implementation
78 * of an extension point has a name, and a priority. Use
79 * g_io_extension_point_implement() to implement an extension point.
82 * GIOExtensionPoint *ep;
84 * /* Register an extension point */
85 * ep = g_io_extension_point_register ("my-extension-point");
86 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
90 * /* Implement an extension point */
91 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
92 * g_io_extension_point_implement ("my-extension-point",
93 * my_example_impl_get_type (),
98 * It is up to the code that registered the extension point how
99 * it uses the implementations that have been associated with it.
100 * Depending on the use case, it may use all implementations, or
101 * only the one with the highest priority, or pick a specific
104 * To avoid opening all modules just to find out what extension
105 * points they implement, GIO makes use of a caching mechanism,
106 * see <link linkend="gio-querymodules">gio-querymodules</link>.
107 * You are expected to run this command after installing a
110 * The <envar>GIO_EXTRA_MODULES</envar> environment variable can be
111 * used to specify additional directories to automatically load modules
112 * from. This environment variable has the same syntax as the
113 * <envar>PATH</envar>. If two modules have the same base name in different
114 * directories, then the latter one will be ignored. If additional
115 * directories are specified GIO will load modules from the built-in
122 * Represents a scope for loading IO modules. A scope can be used for blocking
123 * duplicate modules, or blocking a module you don't want to load.
125 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
126 * or g_io_modules_scan_all_in_directory_with_scope().
130 struct _GIOModuleScope {
131 GIOModuleScopeFlags flags;
132 GHashTable *basenames;
136 * g_io_module_scope_new:
137 * @flags: flags for the new scope
139 * Create a new scope for loading of IO modules. A scope can be used for
140 * blocking duplicate modules, or blocking a module you don't want to load.
142 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
143 * which have the same base name as a module that has already been seen
146 * Returns: (transfer full): the new module scope
151 g_io_module_scope_new (GIOModuleScopeFlags flags)
153 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
154 scope->flags = flags;
155 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
160 * g_io_module_scope_free:
161 * @scope: a module loading scope
163 * Free a module scope.
168 g_io_module_scope_free (GIOModuleScope *scope)
172 g_hash_table_destroy (scope->basenames);
177 * g_io_module_scope_block:
178 * @scope: a module loading scope
179 * @basename: the basename to block
181 * Block modules with the given @basename from being loaded when
182 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
183 * or g_io_modules_load_all_in_directory_with_scope().
188 g_io_module_scope_block (GIOModuleScope *scope,
189 const gchar *basename)
193 g_return_if_fail (scope != NULL);
194 g_return_if_fail (basename != NULL);
196 key = g_strdup (basename);
197 g_hash_table_insert (scope->basenames, key, key);
201 _g_io_module_scope_contains (GIOModuleScope *scope,
202 const gchar *basename)
204 return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
208 GTypeModule parent_instance;
212 gboolean initialized; /* The module was loaded at least once */
214 void (* load) (GIOModule *module);
215 void (* unload) (GIOModule *module);
218 struct _GIOModuleClass
220 GTypeModuleClass parent_class;
224 static void g_io_module_finalize (GObject *object);
225 static gboolean g_io_module_load_module (GTypeModule *gmodule);
226 static void g_io_module_unload_module (GTypeModule *gmodule);
228 struct _GIOExtension {
234 struct _GIOExtensionPoint {
238 GList *lazy_load_modules;
241 static GHashTable *extension_points = NULL;
242 G_LOCK_DEFINE_STATIC(extension_points);
244 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
247 g_io_module_class_init (GIOModuleClass *class)
249 GObjectClass *object_class = G_OBJECT_CLASS (class);
250 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
252 object_class->finalize = g_io_module_finalize;
254 type_module_class->load = g_io_module_load_module;
255 type_module_class->unload = g_io_module_unload_module;
259 g_io_module_init (GIOModule *module)
264 g_io_module_finalize (GObject *object)
266 GIOModule *module = G_IO_MODULE (object);
268 g_free (module->filename);
270 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
274 g_io_module_load_module (GTypeModule *gmodule)
276 GIOModule *module = G_IO_MODULE (gmodule);
278 if (!module->filename)
280 g_warning ("GIOModule path not set");
284 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
286 if (!module->library)
288 g_printerr ("%s\n", g_module_error ());
292 /* Make sure that the loaded library contains the required methods */
293 if (! g_module_symbol (module->library,
295 (gpointer) &module->load) ||
296 ! g_module_symbol (module->library,
297 "g_io_module_unload",
298 (gpointer) &module->unload))
300 g_printerr ("%s\n", g_module_error ());
301 g_module_close (module->library);
306 /* Initialize the loaded module */
307 module->load (module);
308 module->initialized = TRUE;
314 g_io_module_unload_module (GTypeModule *gmodule)
316 GIOModule *module = G_IO_MODULE (gmodule);
318 module->unload (module);
320 g_module_close (module->library);
321 module->library = NULL;
324 module->unload = NULL;
329 * @filename: filename of the shared library module.
331 * Creates a new GIOModule that will load the specific
332 * shared library when in use.
334 * Returns: a #GIOModule from given @filename,
338 g_io_module_new (const gchar *filename)
342 g_return_val_if_fail (filename != NULL, NULL);
344 module = g_object_new (G_IO_TYPE_MODULE, NULL);
345 module->filename = g_strdup (filename);
351 is_valid_module_name (const gchar *basename,
352 GIOModuleScope *scope)
356 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
357 if (!g_str_has_prefix (basename, "lib") ||
358 !g_str_has_suffix (basename, ".so"))
361 if (!g_str_has_suffix (basename, ".dll"))
368 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
369 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
370 g_io_module_scope_block (scope, basename);
378 * g_io_modules_scan_all_in_directory_with_scope:
379 * @dirname: pathname for a directory containing modules to scan.
380 * @scope: a scope to use when scanning the modules
382 * Scans all the modules in the specified directory, ensuring that
383 * any extension point implemented by a module is registered.
385 * This may not actually load and initialize all the types in each
386 * module, some modules may be lazily loaded and initialized when
387 * an extension point it implementes is used with e.g.
388 * g_io_extension_point_get_extensions() or
389 * g_io_extension_point_get_extension_by_name().
391 * If you need to guarantee that all types are loaded in all the modules,
392 * use g_io_modules_load_all_in_directory().
397 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
398 GIOModuleScope *scope)
408 if (!g_module_supported ())
411 dir = g_dir_open (dirname, 0, NULL);
415 filename = g_build_filename (dirname, "giomodule.cache", NULL);
417 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
418 g_free, (GDestroyNotify)g_strfreev);
421 if (g_stat (filename, &statbuf) == 0 &&
422 g_file_get_contents (filename, &data, NULL, NULL))
427 /* Cache mtime is the time the cache file was created, any file
428 * that has a ctime before this was created then and not modified
429 * since then (userspace can't change ctime). Its possible to change
430 * the ctime forward without changing the file content, by e.g.
431 * chmoding the file, but this is uncommon and will only cause us
432 * to not use the cache so will not cause bugs.
434 cache_mtime = statbuf.st_mtime;
436 lines = g_strsplit (data, "\n", -1);
439 for (i = 0; lines[i] != NULL; i++)
441 char *line = lines[i];
444 char **extension_points;
449 colon = strchr (line, ':');
450 if (colon == NULL || line == colon)
451 continue; /* Invalid line, ignore */
453 *colon = 0; /* terminate filename */
454 file = g_strdup (line);
455 colon++; /* after colon */
457 while (g_ascii_isspace (*colon))
460 extension_points = g_strsplit (colon, ",", -1);
461 g_hash_table_insert (cache, file, extension_points);
466 while ((name = g_dir_read_name (dir)))
468 if (is_valid_module_name (name, scope))
470 GIOExtensionPoint *extension_point;
473 char **extension_points;
476 path = g_build_filename (dirname, name, NULL);
477 module = g_io_module_new (path);
479 extension_points = g_hash_table_lookup (cache, name);
480 if (extension_points != NULL &&
481 g_stat (path, &statbuf) == 0 &&
482 statbuf.st_ctime <= cache_mtime)
484 /* Lazy load/init the library when first required */
485 for (i = 0; extension_points[i] != NULL; i++)
488 g_io_extension_point_register (extension_points[i]);
489 extension_point->lazy_load_modules =
490 g_list_prepend (extension_point->lazy_load_modules,
496 /* Try to load and init types */
497 if (g_type_module_use (G_TYPE_MODULE (module)))
498 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
500 { /* Failure to load */
501 g_printerr ("Failed to load module: %s\n", path);
502 g_object_unref (module);
514 g_hash_table_destroy (cache);
520 * g_io_modules_scan_all_in_directory:
521 * @dirname: pathname for a directory containing modules to scan.
523 * Scans all the modules in the specified directory, ensuring that
524 * any extension point implemented by a module is registered.
526 * This may not actually load and initialize all the types in each
527 * module, some modules may be lazily loaded and initialized when
528 * an extension point it implementes is used with e.g.
529 * g_io_extension_point_get_extensions() or
530 * g_io_extension_point_get_extension_by_name().
532 * If you need to guarantee that all types are loaded in all the modules,
533 * use g_io_modules_load_all_in_directory().
538 g_io_modules_scan_all_in_directory (const char *dirname)
540 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
544 * g_io_modules_load_all_in_directory_with_scope:
545 * @dirname: pathname for a directory containing modules to load.
546 * @scope: a scope to use when scanning the modules.
548 * Loads all the modules in the specified directory.
550 * If don't require all modules to be initialized (and thus registering
551 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
552 * which allows delayed/lazy loading of modules.
554 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
555 * from the directory,
556 * All the modules are loaded into memory, if you want to
557 * unload them (enabling on-demand loading) you must call
558 * g_type_module_unuse() on all the modules. Free the list
559 * with g_list_free().
564 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
565 GIOModuleScope *scope)
571 if (!g_module_supported ())
574 dir = g_dir_open (dirname, 0, NULL);
579 while ((name = g_dir_read_name (dir)))
581 if (is_valid_module_name (name, scope))
586 path = g_build_filename (dirname, name, NULL);
587 module = g_io_module_new (path);
589 if (!g_type_module_use (G_TYPE_MODULE (module)))
591 g_printerr ("Failed to load module: %s\n", path);
592 g_object_unref (module);
599 modules = g_list_prepend (modules, module);
609 * g_io_modules_load_all_in_directory:
610 * @dirname: pathname for a directory containing modules to load.
612 * Loads all the modules in the specified directory.
614 * If don't require all modules to be initialized (and thus registering
615 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
616 * which allows delayed/lazy loading of modules.
618 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
619 * from the directory,
620 * All the modules are loaded into memory, if you want to
621 * unload them (enabling on-demand loading) you must call
622 * g_type_module_unuse() on all the modules. Free the list
623 * with g_list_free().
626 g_io_modules_load_all_in_directory (const char *dirname)
628 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
631 GRecMutex default_modules_lock;
632 GHashTable *default_modules;
635 try_implementation (GIOExtension *extension,
636 GIOModuleVerifyFunc verify_func)
638 GType type = g_io_extension_get_type (extension);
641 if (g_type_is_a (type, G_TYPE_INITABLE))
642 return g_initable_new (type, NULL, NULL, NULL);
645 impl = g_object_new (type, NULL);
646 if (!verify_func || verify_func (impl))
649 g_object_unref (impl);
655 * _g_io_module_get_default:
656 * @extension_point: the name of an extension point
657 * @envvar: (allow-none): the name of an environment variable to
658 * override the default implementation.
659 * @verify_func: (allow-none): a function to call to verify that
660 * a given implementation is usable in the current environment.
662 * Retrieves the default object implementing @extension_point.
664 * If @envvar is not %NULL, and the environment variable with that
665 * name is set, then the implementation it specifies will be tried
666 * first. After that, or if @envvar is not set, all other
667 * implementations will be tried in order of decreasing priority.
669 * If an extension point implementation implements #GInitable, then
670 * that implementation will only be used if it initializes
671 * successfully. Otherwise, if @verify_func is not %NULL, then it will
672 * be called on each candidate implementation after construction, to
673 * check if it is actually usable or not.
675 * The result is cached after it is generated the first time, and
676 * the function is thread-safe.
678 * Return value: (transfer none): an object implementing
679 * @extension_point, or %NULL if there are no usable
683 _g_io_module_get_default (const gchar *extension_point,
685 GIOModuleVerifyFunc verify_func)
687 const char *use_this;
689 GIOExtensionPoint *ep;
690 GIOExtension *extension, *preferred;
693 g_rec_mutex_lock (&default_modules_lock);
698 if (g_hash_table_lookup_extended (default_modules, extension_point,
701 g_rec_mutex_unlock (&default_modules_lock);
707 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
710 _g_io_modules_ensure_loaded ();
711 ep = g_io_extension_point_lookup (extension_point);
715 g_warn_if_reached ();
716 g_rec_mutex_unlock (&default_modules_lock);
720 use_this = envvar ? g_getenv (envvar) : NULL;
723 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
726 impl = try_implementation (preferred, verify_func);
731 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
736 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
739 if (extension == preferred)
742 impl = try_implementation (extension, verify_func);
750 g_hash_table_insert (default_modules,
751 g_strdup (extension_point),
752 impl ? g_object_ref (impl) : NULL);
753 g_rec_mutex_unlock (&default_modules_lock);
758 G_LOCK_DEFINE_STATIC (registered_extensions);
759 G_LOCK_DEFINE_STATIC (loaded_dirs);
761 extern GType _g_fen_directory_monitor_get_type (void);
762 extern GType _g_fen_file_monitor_get_type (void);
763 extern GType _g_inotify_directory_monitor_get_type (void);
764 extern GType _g_inotify_file_monitor_get_type (void);
765 extern GType _g_kqueue_directory_monitor_get_type (void);
766 extern GType _g_kqueue_file_monitor_get_type (void);
767 extern GType _g_unix_volume_monitor_get_type (void);
768 extern GType _g_local_vfs_get_type (void);
770 extern GType _g_win32_volume_monitor_get_type (void);
771 extern GType g_win32_directory_monitor_get_type (void);
772 extern GType _g_winhttp_vfs_get_type (void);
774 extern GType _g_dummy_proxy_resolver_get_type (void);
775 extern GType _g_dummy_tls_backend_get_type (void);
776 extern GType g_network_monitor_base_get_type (void);
778 extern GType _g_network_monitor_netlink_get_type (void);
781 #ifdef G_PLATFORM_WIN32
785 static HMODULE gio_dll = NULL;
789 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
794 DllMain (HINSTANCE hinstDLL,
798 if (fdwReason == DLL_PROCESS_ATTACH)
807 _g_io_win32_get_module (void)
810 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
811 (const char *) _g_io_win32_get_module,
816 #undef GIO_MODULE_DIR
818 /* GIO_MODULE_DIR is used only in code called just once,
819 * so no problem leaking this
821 #define GIO_MODULE_DIR \
822 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
829 _g_io_modules_ensure_extension_points_registered (void)
831 static gboolean registered_extensions = FALSE;
832 GIOExtensionPoint *ep;
834 G_LOCK (registered_extensions);
836 if (!registered_extensions)
838 registered_extensions = TRUE;
841 #if !GLIB_CHECK_VERSION (3, 0, 0)
842 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
843 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
844 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
845 G_GNUC_END_IGNORE_DEPRECATIONS
849 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
850 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
852 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
853 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
855 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
856 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
858 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
859 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
861 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
862 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
864 ep = g_io_extension_point_register ("gsettings-backend");
865 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
867 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
868 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
870 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
871 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
873 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
874 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
876 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
877 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
880 G_UNLOCK (registered_extensions);
884 _g_io_modules_ensure_loaded (void)
886 static gboolean loaded_dirs = FALSE;
887 const char *module_path;
888 GIOModuleScope *scope;
890 _g_io_modules_ensure_extension_points_registered ();
892 G_LOCK (loaded_dirs);
897 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
899 /* First load any overrides, extras */
900 module_path = g_getenv ("GIO_EXTRA_MODULES");
906 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
908 for (i = 0; paths[i] != NULL; i++)
910 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
916 /* Then load the compiled in path */
917 g_io_modules_scan_all_in_directory_with_scope (GIO_MODULE_DIR, scope);
919 g_io_module_scope_free (scope);
921 /* Initialize types from built-in "modules" */
922 g_type_ensure (g_null_settings_backend_get_type ());
923 g_type_ensure (g_memory_settings_backend_get_type ());
924 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
925 g_type_ensure (_g_inotify_directory_monitor_get_type ());
926 g_type_ensure (_g_inotify_file_monitor_get_type ());
928 #if defined(HAVE_KQUEUE)
929 g_type_ensure (_g_kqueue_directory_monitor_get_type ());
930 g_type_ensure (_g_kqueue_file_monitor_get_type ());
932 #if defined(HAVE_FEN)
933 g_type_ensure (_g_fen_directory_monitor_get_type ());
934 g_type_ensure (_g_fen_file_monitor_get_type ());
937 g_type_ensure (_g_win32_volume_monitor_get_type ());
938 g_type_ensure (g_win32_directory_monitor_get_type ());
939 g_type_ensure (g_registry_backend_get_type ());
942 g_nextstep_settings_backend_get_type ();
945 g_type_ensure (_g_unix_volume_monitor_get_type ());
948 g_type_ensure (_g_winhttp_vfs_get_type ());
950 g_type_ensure (_g_local_vfs_get_type ());
951 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
952 g_type_ensure (_g_socks4a_proxy_get_type ());
953 g_type_ensure (_g_socks4_proxy_get_type ());
954 g_type_ensure (_g_socks5_proxy_get_type ());
955 g_type_ensure (_g_dummy_tls_backend_get_type ());
956 g_type_ensure (g_network_monitor_base_get_type ());
958 g_type_ensure (_g_network_monitor_netlink_get_type ());
962 G_UNLOCK (loaded_dirs);
966 g_io_extension_point_free (GIOExtensionPoint *ep)
973 * g_io_extension_point_register:
974 * @name: The name of the extension point
976 * Registers an extension point.
978 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
979 * owned by GIO and should not be freed.
982 g_io_extension_point_register (const char *name)
984 GIOExtensionPoint *ep;
986 G_LOCK (extension_points);
987 if (extension_points == NULL)
988 extension_points = g_hash_table_new_full (g_str_hash,
991 (GDestroyNotify)g_io_extension_point_free);
993 ep = g_hash_table_lookup (extension_points, name);
996 G_UNLOCK (extension_points);
1000 ep = g_new0 (GIOExtensionPoint, 1);
1001 ep->name = g_strdup (name);
1003 g_hash_table_insert (extension_points, ep->name, ep);
1005 G_UNLOCK (extension_points);
1011 * g_io_extension_point_lookup:
1012 * @name: the name of the extension point
1014 * Looks up an existing extension point.
1016 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1017 * is no registered extension point with the given name.
1020 g_io_extension_point_lookup (const char *name)
1022 GIOExtensionPoint *ep;
1024 G_LOCK (extension_points);
1026 if (extension_points != NULL)
1027 ep = g_hash_table_lookup (extension_points, name);
1029 G_UNLOCK (extension_points);
1036 * g_io_extension_point_set_required_type:
1037 * @extension_point: a #GIOExtensionPoint
1038 * @type: the #GType to require
1040 * Sets the required type for @extension_point to @type.
1041 * All implementations must henceforth have this type.
1044 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1047 extension_point->required_type = type;
1051 * g_io_extension_point_get_required_type:
1052 * @extension_point: a #GIOExtensionPoint
1054 * Gets the required type for @extension_point.
1056 * Returns: the #GType that all implementations must have,
1057 * or #G_TYPE_INVALID if the extension point has no required type
1060 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1062 return extension_point->required_type;
1066 lazy_load_modules (GIOExtensionPoint *extension_point)
1071 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1075 if (!module->initialized)
1077 if (g_type_module_use (G_TYPE_MODULE (module)))
1078 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1080 g_printerr ("Failed to load module: %s\n",
1087 * g_io_extension_point_get_extensions:
1088 * @extension_point: a #GIOExtensionPoint
1090 * Gets a list of all extensions that implement this extension point.
1091 * The list is sorted by priority, beginning with the highest priority.
1093 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1094 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
1098 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1100 lazy_load_modules (extension_point);
1101 return extension_point->extensions;
1105 * g_io_extension_point_get_extension_by_name:
1106 * @extension_point: a #GIOExtensionPoint
1107 * @name: the name of the extension to get
1109 * Finds a #GIOExtension for an extension point by name.
1111 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1112 * given name, or %NULL if there is no extension with that name
1115 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1120 lazy_load_modules (extension_point);
1121 for (l = extension_point->extensions; l != NULL; l = l->next)
1123 GIOExtension *e = l->data;
1125 if (e->name != NULL &&
1126 strcmp (e->name, name) == 0)
1134 extension_prio_compare (gconstpointer a,
1137 const GIOExtension *extension_a = a, *extension_b = b;
1139 if (extension_a->priority > extension_b->priority)
1142 if (extension_b->priority > extension_a->priority)
1149 * g_io_extension_point_implement:
1150 * @extension_point_name: the name of the extension point
1151 * @type: the #GType to register as extension
1152 * @extension_name: the name for the extension
1153 * @priority: the priority for the extension
1155 * Registers @type as extension for the extension point with name
1156 * @extension_point_name.
1158 * If @type has already been registered as an extension for this
1159 * extension point, the existing #GIOExtension object is returned.
1161 * Returns: (transfer none): a #GIOExtension object for #GType
1164 g_io_extension_point_implement (const char *extension_point_name,
1166 const char *extension_name,
1169 GIOExtensionPoint *extension_point;
1170 GIOExtension *extension;
1173 g_return_val_if_fail (extension_point_name != NULL, NULL);
1175 extension_point = g_io_extension_point_lookup (extension_point_name);
1176 if (extension_point == NULL)
1178 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1182 if (extension_point->required_type != 0 &&
1183 !g_type_is_a (type, extension_point->required_type))
1185 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1186 "Expected type is %s.",
1188 extension_point_name,
1189 g_type_name (extension_point->required_type));
1193 /* It's safe to register the same type multiple times */
1194 for (l = extension_point->extensions; l != NULL; l = l->next)
1196 extension = l->data;
1197 if (extension->type == type)
1201 extension = g_slice_new0 (GIOExtension);
1202 extension->type = type;
1203 extension->name = g_strdup (extension_name);
1204 extension->priority = priority;
1206 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1207 extension, extension_prio_compare);
1213 * g_io_extension_ref_class:
1214 * @extension: a #GIOExtension
1216 * Gets a reference to the class for the type that is
1217 * associated with @extension.
1219 * Returns: (transfer full): the #GTypeClass for the type of @extension
1222 g_io_extension_ref_class (GIOExtension *extension)
1224 return g_type_class_ref (extension->type);
1228 * g_io_extension_get_type:
1229 * @extension: a #GIOExtension
1231 * Gets the type associated with @extension.
1233 * Returns: the type of @extension
1236 g_io_extension_get_type (GIOExtension *extension)
1238 return extension->type;
1242 * g_io_extension_get_name:
1243 * @extension: a #GIOExtension
1245 * Gets the name under which @extension was registered.
1247 * Note that the same type may be registered as extension
1248 * for multiple extension points, under different names.
1250 * Returns: the name of @extension.
1253 g_io_extension_get_name (GIOExtension *extension)
1255 return extension->name;
1259 * g_io_extension_get_priority:
1260 * @extension: a #GIOExtension
1262 * Gets the priority with which @extension was registered.
1264 * Returns: the priority of @extension
1267 g_io_extension_get_priority (GIOExtension *extension)
1269 return extension->priority;