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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 /* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
24 * in the preprocessor, we need to define this before including glib.h*/
25 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 #include "giomodule.h"
32 #include "giomodule-priv.h"
33 #include "glib-private.h"
34 #include "glocalfilemonitor.h"
35 #include "gnativevolumemonitor.h"
36 #include "gproxyresolver.h"
38 #include "gsettingsbackendinternal.h"
39 #include "ghttpproxy.h"
40 #include "gsocks4proxy.h"
41 #include "gsocks4aproxy.h"
42 #include "gsocks5proxy.h"
43 #include "gtlsbackend.h"
45 #include "gnotificationbackend.h"
46 #include "ginitable.h"
47 #include "gnetworkmonitor.h"
48 #include "gmemorymonitor.h"
49 #include "gmemorymonitorportal.h"
50 #include "gmemorymonitordbus.h"
52 #include "gregistrysettingsbackend.h"
54 #include <glib/gstdio.h>
56 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
57 #include "gdesktopappinfo.h"
60 #include "gosxappinfo.h"
64 #include <AvailabilityMacros.h>
69 * @short_description: Loadable GIO Modules
72 * Provides an interface and default functions for loading and unloading
73 * modules. This is used internally to make GIO extensible, but can also
74 * be used by others to implement module loading.
79 * SECTION:extensionpoints
80 * @short_description: Extension Points
82 * @see_also: [Extending GIO][extending-gio]
84 * #GIOExtensionPoint provides a mechanism for modules to extend the
85 * functionality of the library or application that loaded it in an
88 * An extension point is identified by a name, and it may optionally
89 * require that any implementation must be of a certain type (or derived
90 * thereof). Use g_io_extension_point_register() to register an
91 * extension point, and g_io_extension_point_set_required_type() to
92 * set a required type.
94 * A module can implement an extension point by specifying the #GType
95 * that implements the functionality. Additionally, each implementation
96 * of an extension point has a name, and a priority. Use
97 * g_io_extension_point_implement() to implement an extension point.
99 * |[<!-- language="C" -->
100 * GIOExtensionPoint *ep;
102 * // Register an extension point
103 * ep = g_io_extension_point_register ("my-extension-point");
104 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
107 * |[<!-- language="C" -->
108 * // Implement an extension point
109 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
110 * g_io_extension_point_implement ("my-extension-point",
111 * my_example_impl_get_type (),
116 * It is up to the code that registered the extension point how
117 * it uses the implementations that have been associated with it.
118 * Depending on the use case, it may use all implementations, or
119 * only the one with the highest priority, or pick a specific
122 * To avoid opening all modules just to find out what extension
123 * points they implement, GIO makes use of a caching mechanism,
124 * see [gio-querymodules][gio-querymodules].
125 * You are expected to run this command after installing a
128 * The `GIO_EXTRA_MODULES` environment variable can be used to
129 * specify additional directories to automatically load modules
130 * from. This environment variable has the same syntax as the
131 * `PATH`. If two modules have the same base name in different
132 * directories, then the latter one will be ignored. If additional
133 * directories are specified GIO will load modules from the built-in
140 * Represents a scope for loading IO modules. A scope can be used for blocking
141 * duplicate modules, or blocking a module you don't want to load.
143 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
144 * or g_io_modules_scan_all_in_directory_with_scope().
148 struct _GIOModuleScope {
149 GIOModuleScopeFlags flags;
150 GHashTable *basenames;
154 * g_io_module_scope_new:
155 * @flags: flags for the new scope
157 * Create a new scope for loading of IO modules. A scope can be used for
158 * blocking duplicate modules, or blocking a module you don't want to load.
160 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
161 * which have the same base name as a module that has already been seen
164 * Returns: (transfer full): the new module scope
169 g_io_module_scope_new (GIOModuleScopeFlags flags)
171 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
172 scope->flags = flags;
173 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
178 * g_io_module_scope_free:
179 * @scope: a module loading scope
181 * Free a module scope.
186 g_io_module_scope_free (GIOModuleScope *scope)
190 g_hash_table_destroy (scope->basenames);
195 * g_io_module_scope_block:
196 * @scope: a module loading scope
197 * @basename: the basename to block
199 * Block modules with the given @basename from being loaded when
200 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
201 * or g_io_modules_load_all_in_directory_with_scope().
206 g_io_module_scope_block (GIOModuleScope *scope,
207 const gchar *basename)
211 g_return_if_fail (scope != NULL);
212 g_return_if_fail (basename != NULL);
214 key = g_strdup (basename);
215 g_hash_table_add (scope->basenames, key);
219 _g_io_module_scope_contains (GIOModuleScope *scope,
220 const gchar *basename)
222 return g_hash_table_contains (scope->basenames, basename);
226 GTypeModule parent_instance;
230 gboolean initialized; /* The module was loaded at least once */
232 void (* load) (GIOModule *module);
233 void (* unload) (GIOModule *module);
236 struct _GIOModuleClass
238 GTypeModuleClass parent_class;
242 static void g_io_module_finalize (GObject *object);
243 static gboolean g_io_module_load_module (GTypeModule *gmodule);
244 static void g_io_module_unload_module (GTypeModule *gmodule);
249 * #GIOExtension is an opaque data structure and can only be accessed
250 * using the following functions.
252 struct _GIOExtension {
261 * #GIOExtensionPoint is an opaque data structure and can only be accessed
262 * using the following functions.
264 struct _GIOExtensionPoint {
268 GList *lazy_load_modules;
271 static GHashTable *extension_points = NULL;
272 G_LOCK_DEFINE_STATIC(extension_points);
274 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
277 g_io_module_class_init (GIOModuleClass *class)
279 GObjectClass *object_class = G_OBJECT_CLASS (class);
280 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
282 object_class->finalize = g_io_module_finalize;
284 type_module_class->load = g_io_module_load_module;
285 type_module_class->unload = g_io_module_unload_module;
289 g_io_module_init (GIOModule *module)
294 g_io_module_finalize (GObject *object)
296 GIOModule *module = G_IO_MODULE (object);
298 g_free (module->filename);
300 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
304 load_symbols (GIOModule *module)
308 gchar *unload_symname;
311 name = _g_io_module_extract_name (module->filename);
312 load_symname = g_strconcat ("g_io_", name, "_load", NULL);
313 unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
315 ret = g_module_symbol (module->library,
317 (gpointer) &module->load) &&
318 g_module_symbol (module->library,
320 (gpointer) &module->unload);
324 /* Fallback to old names */
325 ret = g_module_symbol (module->library,
327 (gpointer) &module->load) &&
328 g_module_symbol (module->library,
329 "g_io_module_unload",
330 (gpointer) &module->unload);
334 g_free (load_symname);
335 g_free (unload_symname);
341 g_io_module_load_module (GTypeModule *gmodule)
343 GIOModule *module = G_IO_MODULE (gmodule);
345 if (!module->filename)
347 g_warning ("GIOModule path not set");
351 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
353 if (!module->library)
355 g_printerr ("%s\n", g_module_error ());
359 /* Make sure that the loaded library contains the required methods */
360 if (!load_symbols (module))
362 g_printerr ("%s\n", g_module_error ());
363 g_module_close (module->library);
368 /* Initialize the loaded module */
369 module->load (module);
370 module->initialized = TRUE;
376 g_io_module_unload_module (GTypeModule *gmodule)
378 GIOModule *module = G_IO_MODULE (gmodule);
380 module->unload (module);
382 g_module_close (module->library);
383 module->library = NULL;
386 module->unload = NULL;
391 * @filename: (type filename): filename of the shared library module.
393 * Creates a new GIOModule that will load the specific
394 * shared library when in use.
396 * Returns: a #GIOModule from given @filename,
400 g_io_module_new (const gchar *filename)
404 g_return_val_if_fail (filename != NULL, NULL);
406 module = g_object_new (G_IO_TYPE_MODULE, NULL);
407 module->filename = g_strdup (filename);
413 is_valid_module_name (const gchar *basename,
414 GIOModuleScope *scope)
418 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
419 if (!g_str_has_prefix (basename, "lib") ||
420 !g_str_has_suffix (basename, ".so"))
423 if (!g_str_has_suffix (basename, ".dll"))
430 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
431 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
432 g_io_module_scope_block (scope, basename);
440 * g_io_modules_scan_all_in_directory_with_scope:
441 * @dirname: (type filename): pathname for a directory containing modules
443 * @scope: a scope to use when scanning the modules
445 * Scans all the modules in the specified directory, ensuring that
446 * any extension point implemented by a module is registered.
448 * This may not actually load and initialize all the types in each
449 * module, some modules may be lazily loaded and initialized when
450 * an extension point it implements is used with e.g.
451 * g_io_extension_point_get_extensions() or
452 * g_io_extension_point_get_extension_by_name().
454 * If you need to guarantee that all types are loaded in all the modules,
455 * use g_io_modules_load_all_in_directory().
460 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
461 GIOModuleScope *scope)
471 if (!g_module_supported ())
474 dir = g_dir_open (dirname, 0, NULL);
478 filename = g_build_filename (dirname, "giomodule.cache", NULL);
480 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
481 g_free, (GDestroyNotify)g_strfreev);
484 if (g_stat (filename, &statbuf) == 0 &&
485 g_file_get_contents (filename, &data, NULL, NULL))
490 /* cache_time is the time the cache file was created; we also take
491 * into account the change time because in ostree based systems, all
492 * system file have mtime equal to epoch 0.
494 * Any file that has a ctime before this was created then and not modified
495 * since then (userspace can't change ctime). Its possible to change the
496 * ctime forward without changing the file content, by e.g. chmoding the
497 * file, but this is uncommon and will only cause us to not use the cache
498 * so will not cause bugs.
500 cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
502 lines = g_strsplit (data, "\n", -1);
505 for (i = 0; lines[i] != NULL; i++)
507 char *line = lines[i];
510 char **extension_points;
515 colon = strchr (line, ':');
516 if (colon == NULL || line == colon)
517 continue; /* Invalid line, ignore */
519 *colon = 0; /* terminate filename */
520 file = g_strdup (line);
521 colon++; /* after colon */
523 while (g_ascii_isspace (*colon))
526 extension_points = g_strsplit (colon, ",", -1);
527 g_hash_table_insert (cache, file, extension_points);
532 while ((name = g_dir_read_name (dir)))
534 if (is_valid_module_name (name, scope))
536 GIOExtensionPoint *extension_point;
539 char **extension_points;
542 path = g_build_filename (dirname, name, NULL);
543 module = g_io_module_new (path);
545 extension_points = g_hash_table_lookup (cache, name);
546 if (extension_points != NULL &&
547 g_stat (path, &statbuf) == 0 &&
548 statbuf.st_ctime <= cache_time)
550 /* Lazy load/init the library when first required */
551 for (i = 0; extension_points[i] != NULL; i++)
554 g_io_extension_point_register (extension_points[i]);
555 extension_point->lazy_load_modules =
556 g_list_prepend (extension_point->lazy_load_modules,
562 /* Try to load and init types */
563 if (g_type_module_use (G_TYPE_MODULE (module)))
564 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
566 { /* Failure to load */
567 g_printerr ("Failed to load module: %s\n", path);
568 g_object_unref (module);
580 g_hash_table_destroy (cache);
586 * g_io_modules_scan_all_in_directory:
587 * @dirname: (type filename): pathname for a directory containing modules
590 * Scans all the modules in the specified directory, ensuring that
591 * any extension point implemented by a module is registered.
593 * This may not actually load and initialize all the types in each
594 * module, some modules may be lazily loaded and initialized when
595 * an extension point it implements is used with e.g.
596 * g_io_extension_point_get_extensions() or
597 * g_io_extension_point_get_extension_by_name().
599 * If you need to guarantee that all types are loaded in all the modules,
600 * use g_io_modules_load_all_in_directory().
605 g_io_modules_scan_all_in_directory (const char *dirname)
607 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
611 * g_io_modules_load_all_in_directory_with_scope:
612 * @dirname: (type filename): pathname for a directory containing modules
614 * @scope: a scope to use when scanning the modules.
616 * Loads all the modules in the specified directory.
618 * If don't require all modules to be initialized (and thus registering
619 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
620 * which allows delayed/lazy loading of modules.
622 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
623 * from the directory,
624 * All the modules are loaded into memory, if you want to
625 * unload them (enabling on-demand loading) you must call
626 * g_type_module_unuse() on all the modules. Free the list
627 * with g_list_free().
632 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
633 GIOModuleScope *scope)
639 if (!g_module_supported ())
642 dir = g_dir_open (dirname, 0, NULL);
647 while ((name = g_dir_read_name (dir)))
649 if (is_valid_module_name (name, scope))
654 path = g_build_filename (dirname, name, NULL);
655 module = g_io_module_new (path);
657 if (!g_type_module_use (G_TYPE_MODULE (module)))
659 g_printerr ("Failed to load module: %s\n", path);
660 g_object_unref (module);
667 modules = g_list_prepend (modules, module);
677 * g_io_modules_load_all_in_directory:
678 * @dirname: (type filename): pathname for a directory containing modules
681 * Loads all the modules in the specified directory.
683 * If don't require all modules to be initialized (and thus registering
684 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
685 * which allows delayed/lazy loading of modules.
687 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
688 * from the directory,
689 * All the modules are loaded into memory, if you want to
690 * unload them (enabling on-demand loading) you must call
691 * g_type_module_unuse() on all the modules. Free the list
692 * with g_list_free().
695 g_io_modules_load_all_in_directory (const char *dirname)
697 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
701 try_class (GIOExtension *extension,
702 guint is_supported_offset)
704 GType type = g_io_extension_get_type (extension);
705 typedef gboolean (*verify_func) (void);
708 class = g_type_class_ref (type);
709 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
712 g_type_class_unref (class);
717 print_help (const char *envvar,
718 GIOExtensionPoint *ep)
720 g_print ("Supported arguments for %s environment variable:\n", envvar);
722 if (g_io_extension_point_get_extensions (ep) == NULL)
723 g_print (" (none)\n");
727 GIOExtension *extension;
730 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
733 width = MAX (width, strlen (g_io_extension_get_name (extension)));
736 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
740 g_print (" %*s - %d\n", width, g_io_extension_get_name (extension), g_io_extension_get_priority (extension));
746 * _g_io_module_get_default_type:
747 * @extension_point: the name of an extension point
748 * @envvar: (nullable): the name of an environment variable to
749 * override the default implementation.
750 * @is_supported_offset: a vtable offset, or zero
752 * Retrieves the default class implementing @extension_point.
754 * If @envvar is not %NULL, and the environment variable with that
755 * name is set, then the implementation it specifies will be tried
756 * first. After that, or if @envvar is not set, all other
757 * implementations will be tried in order of decreasing priority.
759 * If @is_supported_offset is non-zero, then it is the offset into the
760 * class vtable at which there is a function that takes no arguments and
761 * returns a boolean. This function will be called on each candidate
762 * implementation to check if it is actually usable or not.
764 * The result is cached after it is generated the first time, and
765 * the function is thread-safe.
767 * Returns: (transfer none): the type to instantiate to implement
768 * @extension_point, or %G_TYPE_INVALID if there are no usable
772 _g_io_module_get_default_type (const gchar *extension_point,
774 guint is_supported_offset)
776 static GRecMutex default_modules_lock;
777 static GHashTable *default_modules;
778 const char *use_this;
780 GIOExtensionPoint *ep;
781 GIOExtension *extension, *preferred;
784 g_rec_mutex_lock (&default_modules_lock);
789 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
791 g_rec_mutex_unlock (&default_modules_lock);
792 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
797 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
800 _g_io_modules_ensure_loaded ();
801 ep = g_io_extension_point_lookup (extension_point);
805 g_warn_if_reached ();
806 g_rec_mutex_unlock (&default_modules_lock);
807 return G_TYPE_INVALID;
810 /* It’s OK to query the environment here, even when running as setuid, because
811 * it only allows a choice between existing already-loaded modules. No new
812 * code is loaded based on the environment variable value. */
813 use_this = envvar ? g_getenv (envvar) : NULL;
814 if (g_strcmp0 (use_this, "help") == 0)
816 print_help (envvar, ep);
822 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
825 impl = try_class (preferred, is_supported_offset);
830 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
835 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
838 if (extension == preferred)
841 impl = try_class (extension, is_supported_offset);
849 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
850 g_rec_mutex_unlock (&default_modules_lock);
852 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
856 try_implementation (const char *extension_point,
857 GIOExtension *extension,
858 GIOModuleVerifyFunc verify_func)
860 GType type = g_io_extension_get_type (extension);
863 if (g_type_is_a (type, G_TYPE_INITABLE))
865 GError *error = NULL;
867 impl = g_initable_new (type, NULL, &error, NULL);
871 g_debug ("Failed to initialize %s (%s) for %s: %s",
872 g_io_extension_get_name (extension),
875 error ? error->message : "");
876 g_clear_error (&error);
881 impl = g_object_new (type, NULL);
882 if (!verify_func || verify_func (impl))
885 g_object_unref (impl);
891 * _g_io_module_get_default:
892 * @extension_point: the name of an extension point
893 * @envvar: (nullable): the name of an environment variable to
894 * override the default implementation.
895 * @verify_func: (nullable): a function to call to verify that
896 * a given implementation is usable in the current environment.
898 * Retrieves the default object implementing @extension_point.
900 * If @envvar is not %NULL, and the environment variable with that
901 * name is set, then the implementation it specifies will be tried
902 * first. After that, or if @envvar is not set, all other
903 * implementations will be tried in order of decreasing priority.
905 * If an extension point implementation implements #GInitable, then
906 * that implementation will only be used if it initializes
907 * successfully. Otherwise, if @verify_func is not %NULL, then it will
908 * be called on each candidate implementation after construction, to
909 * check if it is actually usable or not.
911 * The result is cached after it is generated the first time, and
912 * the function is thread-safe.
914 * Returns: (transfer none): an object implementing
915 * @extension_point, or %NULL if there are no usable
919 _g_io_module_get_default (const gchar *extension_point,
921 GIOModuleVerifyFunc verify_func)
923 static GRecMutex default_modules_lock;
924 static GHashTable *default_modules;
925 const char *use_this;
927 GIOExtensionPoint *ep;
928 GIOExtension *extension = NULL, *preferred;
931 g_rec_mutex_lock (&default_modules_lock);
936 if (g_hash_table_lookup_extended (default_modules, extension_point,
939 /* Don’t debug here, since we’re returning a cached object which was
940 * already printed earlier. */
941 g_rec_mutex_unlock (&default_modules_lock);
947 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
950 _g_io_modules_ensure_loaded ();
951 ep = g_io_extension_point_lookup (extension_point);
955 g_debug ("%s: Failed to find extension point ‘%s’",
956 G_STRFUNC, extension_point);
957 g_warn_if_reached ();
958 g_rec_mutex_unlock (&default_modules_lock);
962 /* It’s OK to query the environment here, even when running as setuid, because
963 * it only allows a choice between existing already-loaded modules. No new
964 * code is loaded based on the environment variable value. */
965 use_this = envvar ? g_getenv (envvar) : NULL;
966 if (g_strcmp0 (use_this, "help") == 0)
968 print_help (envvar, ep);
974 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
977 impl = try_implementation (extension_point, preferred, verify_func);
978 extension = preferred;
983 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
988 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
991 if (extension == preferred)
994 impl = try_implementation (extension_point, extension, verify_func);
1002 g_hash_table_insert (default_modules,
1003 g_strdup (extension_point),
1004 impl ? g_object_ref (impl) : NULL);
1005 g_rec_mutex_unlock (&default_modules_lock);
1009 g_assert (extension != NULL);
1010 g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
1011 G_STRFUNC, g_io_extension_get_name (extension),
1012 G_OBJECT_TYPE_NAME (impl), extension_point);
1015 g_debug ("%s: Failed to find default implementation for ‘%s’",
1016 G_STRFUNC, extension_point);
1021 G_LOCK_DEFINE_STATIC (registered_extensions);
1022 G_LOCK_DEFINE_STATIC (loaded_dirs);
1024 extern GType g_fen_file_monitor_get_type (void);
1025 extern GType g_inotify_file_monitor_get_type (void);
1026 extern GType g_kqueue_file_monitor_get_type (void);
1027 extern GType g_win32_file_monitor_get_type (void);
1029 extern GType _g_unix_volume_monitor_get_type (void);
1030 extern GType _g_local_vfs_get_type (void);
1032 extern GType _g_win32_volume_monitor_get_type (void);
1033 extern GType _g_winhttp_vfs_get_type (void);
1035 extern GType _g_dummy_proxy_resolver_get_type (void);
1036 extern GType _g_dummy_tls_backend_get_type (void);
1037 extern GType g_network_monitor_base_get_type (void);
1039 extern GType _g_network_monitor_netlink_get_type (void);
1040 extern GType _g_network_monitor_nm_get_type (void);
1043 extern GType g_memory_monitor_dbus_get_type (void);
1044 extern GType g_memory_monitor_portal_get_type (void);
1047 extern GType g_fdo_notification_backend_get_type (void);
1048 extern GType g_gtk_notification_backend_get_type (void);
1049 extern GType g_portal_notification_backend_get_type (void);
1050 extern GType g_proxy_resolver_portal_get_type (void);
1051 extern GType g_network_monitor_portal_get_type (void);
1054 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1055 extern GType g_cocoa_notification_backend_get_type (void);
1058 #ifdef G_PLATFORM_WIN32
1059 extern GType g_win32_notification_backend_get_type (void);
1061 #include <windows.h>
1062 extern GType _g_win32_network_monitor_get_type (void);
1064 static HMODULE gio_dll = NULL;
1068 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1070 LPVOID lpvReserved);
1073 DllMain (HINSTANCE hinstDLL,
1077 if (fdwReason == DLL_PROCESS_ATTACH)
1086 _g_io_win32_get_module (void)
1089 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
1090 (const char *) _g_io_win32_get_module,
1098 _g_io_modules_ensure_extension_points_registered (void)
1100 static gboolean registered_extensions = FALSE;
1101 GIOExtensionPoint *ep;
1103 G_LOCK (registered_extensions);
1105 if (!registered_extensions)
1107 registered_extensions = TRUE;
1109 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
1110 #if !GLIB_CHECK_VERSION (3, 0, 0)
1111 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1112 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1116 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1117 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1119 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1120 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1122 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1123 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1125 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1126 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1128 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1129 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1131 ep = g_io_extension_point_register ("gsettings-backend");
1132 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1134 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1135 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1137 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1138 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1140 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1141 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1143 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1144 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1146 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1147 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1149 ep = g_io_extension_point_register (G_MEMORY_MONITOR_EXTENSION_POINT_NAME);
1150 g_io_extension_point_set_required_type (ep, G_TYPE_MEMORY_MONITOR);
1153 G_UNLOCK (registered_extensions);
1157 get_gio_module_dir (void)
1160 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1162 /* If running as setuid, loading modules from an arbitrary directory
1163 * controlled by the unprivileged user who is running the program could allow
1164 * for execution of arbitrary code (in constructors in modules).
1167 * If a setuid program somehow needs to load additional GIO modules, it should
1168 * explicitly call g_io_modules_scan_all_in_directory(). */
1169 module_dir = !is_setuid ? g_strdup (g_getenv ("GIO_MODULE_DIR")) : NULL;
1170 if (module_dir == NULL)
1175 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1176 module_dir = g_build_filename (install_dir,
1177 "lib", "gio", "modules",
1179 g_free (install_dir);
1181 module_dir = g_strdup (GIO_MODULE_DIR);
1189 _g_io_modules_ensure_loaded (void)
1191 static gboolean loaded_dirs = FALSE;
1192 const char *module_path;
1193 GIOModuleScope *scope;
1195 _g_io_modules_ensure_extension_points_registered ();
1197 G_LOCK (loaded_dirs);
1201 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1205 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1207 /* First load any overrides, extras (but not if running as setuid!) */
1208 module_path = !is_setuid ? g_getenv ("GIO_EXTRA_MODULES") : NULL;
1214 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1216 for (i = 0; paths[i] != NULL; i++)
1218 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1224 /* Then load the compiled in path */
1225 module_dir = get_gio_module_dir ();
1227 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1228 g_free (module_dir);
1230 g_io_module_scope_free (scope);
1232 /* Initialize types from built-in "modules" */
1233 g_type_ensure (g_null_settings_backend_get_type ());
1234 g_type_ensure (g_memory_settings_backend_get_type ());
1235 g_type_ensure (g_keyfile_settings_backend_get_type ());
1236 #if defined(HAVE_INOTIFY_INIT1)
1237 g_type_ensure (g_inotify_file_monitor_get_type ());
1239 #if defined(HAVE_KQUEUE)
1240 g_type_ensure (g_kqueue_file_monitor_get_type ());
1242 #if defined(HAVE_FEN)
1243 g_type_ensure (g_fen_file_monitor_get_type ());
1246 g_type_ensure (_g_win32_volume_monitor_get_type ());
1247 g_type_ensure (g_win32_file_monitor_get_type ());
1248 g_type_ensure (g_registry_backend_get_type ());
1251 g_type_ensure (g_nextstep_settings_backend_get_type ());
1252 g_type_ensure (g_osx_app_info_get_type ());
1255 g_type_ensure (_g_unix_volume_monitor_get_type ());
1256 g_type_ensure (g_fdo_notification_backend_get_type ());
1257 g_type_ensure (g_gtk_notification_backend_get_type ());
1258 g_type_ensure (g_portal_notification_backend_get_type ());
1259 g_type_ensure (g_memory_monitor_dbus_get_type ());
1260 g_type_ensure (g_memory_monitor_portal_get_type ());
1261 g_type_ensure (g_network_monitor_portal_get_type ());
1262 g_type_ensure (g_proxy_resolver_portal_get_type ());
1264 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1265 g_type_ensure (g_cocoa_notification_backend_get_type ());
1268 g_type_ensure (g_win32_notification_backend_get_type ());
1269 g_type_ensure (_g_winhttp_vfs_get_type ());
1271 g_type_ensure (_g_local_vfs_get_type ());
1272 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1273 g_type_ensure (_g_http_proxy_get_type ());
1274 g_type_ensure (_g_https_proxy_get_type ());
1275 g_type_ensure (_g_socks4a_proxy_get_type ());
1276 g_type_ensure (_g_socks4_proxy_get_type ());
1277 g_type_ensure (_g_socks5_proxy_get_type ());
1278 g_type_ensure (_g_dummy_tls_backend_get_type ());
1279 g_type_ensure (g_network_monitor_base_get_type ());
1281 g_type_ensure (_g_network_monitor_netlink_get_type ());
1282 g_type_ensure (_g_network_monitor_nm_get_type ());
1285 g_type_ensure (_g_win32_network_monitor_get_type ());
1289 G_UNLOCK (loaded_dirs);
1293 g_io_extension_point_free (GIOExtensionPoint *ep)
1300 * g_io_extension_point_register:
1301 * @name: The name of the extension point
1303 * Registers an extension point.
1305 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1306 * owned by GIO and should not be freed.
1309 g_io_extension_point_register (const char *name)
1311 GIOExtensionPoint *ep;
1313 G_LOCK (extension_points);
1314 if (extension_points == NULL)
1315 extension_points = g_hash_table_new_full (g_str_hash,
1318 (GDestroyNotify)g_io_extension_point_free);
1320 ep = g_hash_table_lookup (extension_points, name);
1323 G_UNLOCK (extension_points);
1327 ep = g_new0 (GIOExtensionPoint, 1);
1328 ep->name = g_strdup (name);
1330 g_hash_table_insert (extension_points, ep->name, ep);
1332 G_UNLOCK (extension_points);
1338 * g_io_extension_point_lookup:
1339 * @name: the name of the extension point
1341 * Looks up an existing extension point.
1343 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1344 * is no registered extension point with the given name.
1347 g_io_extension_point_lookup (const char *name)
1349 GIOExtensionPoint *ep;
1351 G_LOCK (extension_points);
1353 if (extension_points != NULL)
1354 ep = g_hash_table_lookup (extension_points, name);
1356 G_UNLOCK (extension_points);
1363 * g_io_extension_point_set_required_type:
1364 * @extension_point: a #GIOExtensionPoint
1365 * @type: the #GType to require
1367 * Sets the required type for @extension_point to @type.
1368 * All implementations must henceforth have this type.
1371 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1374 extension_point->required_type = type;
1378 * g_io_extension_point_get_required_type:
1379 * @extension_point: a #GIOExtensionPoint
1381 * Gets the required type for @extension_point.
1383 * Returns: the #GType that all implementations must have,
1384 * or #G_TYPE_INVALID if the extension point has no required type
1387 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1389 return extension_point->required_type;
1393 lazy_load_modules (GIOExtensionPoint *extension_point)
1398 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1402 if (!module->initialized)
1404 if (g_type_module_use (G_TYPE_MODULE (module)))
1405 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1407 g_printerr ("Failed to load module: %s\n",
1414 * g_io_extension_point_get_extensions:
1415 * @extension_point: a #GIOExtensionPoint
1417 * Gets a list of all extensions that implement this extension point.
1418 * The list is sorted by priority, beginning with the highest priority.
1420 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1421 * #GIOExtensions. The list is owned by GIO and should not be
1425 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1427 g_return_val_if_fail (extension_point != NULL, NULL);
1429 lazy_load_modules (extension_point);
1430 return extension_point->extensions;
1434 * g_io_extension_point_get_extension_by_name:
1435 * @extension_point: a #GIOExtensionPoint
1436 * @name: the name of the extension to get
1438 * Finds a #GIOExtension for an extension point by name.
1440 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1441 * given name, or %NULL if there is no extension with that name
1444 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1449 g_return_val_if_fail (name != NULL, NULL);
1451 lazy_load_modules (extension_point);
1452 for (l = extension_point->extensions; l != NULL; l = l->next)
1454 GIOExtension *e = l->data;
1456 if (e->name != NULL &&
1457 strcmp (e->name, name) == 0)
1465 extension_prio_compare (gconstpointer a,
1468 const GIOExtension *extension_a = a, *extension_b = b;
1470 if (extension_a->priority > extension_b->priority)
1473 if (extension_b->priority > extension_a->priority)
1480 * g_io_extension_point_implement:
1481 * @extension_point_name: the name of the extension point
1482 * @type: the #GType to register as extension
1483 * @extension_name: the name for the extension
1484 * @priority: the priority for the extension
1486 * Registers @type as extension for the extension point with name
1487 * @extension_point_name.
1489 * If @type has already been registered as an extension for this
1490 * extension point, the existing #GIOExtension object is returned.
1492 * Returns: (transfer none): a #GIOExtension object for #GType
1495 g_io_extension_point_implement (const char *extension_point_name,
1497 const char *extension_name,
1500 GIOExtensionPoint *extension_point;
1501 GIOExtension *extension;
1504 g_return_val_if_fail (extension_point_name != NULL, NULL);
1506 extension_point = g_io_extension_point_lookup (extension_point_name);
1507 if (extension_point == NULL)
1509 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1513 if (extension_point->required_type != 0 &&
1514 !g_type_is_a (type, extension_point->required_type))
1516 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1517 "Expected type is %s.",
1519 extension_point_name,
1520 g_type_name (extension_point->required_type));
1524 /* It's safe to register the same type multiple times */
1525 for (l = extension_point->extensions; l != NULL; l = l->next)
1527 extension = l->data;
1528 if (extension->type == type)
1532 extension = g_slice_new0 (GIOExtension);
1533 extension->type = type;
1534 extension->name = g_strdup (extension_name);
1535 extension->priority = priority;
1537 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1538 extension, extension_prio_compare);
1544 * g_io_extension_ref_class:
1545 * @extension: a #GIOExtension
1547 * Gets a reference to the class for the type that is
1548 * associated with @extension.
1550 * Returns: (transfer full): the #GTypeClass for the type of @extension
1553 g_io_extension_ref_class (GIOExtension *extension)
1555 return g_type_class_ref (extension->type);
1559 * g_io_extension_get_type:
1560 * @extension: a #GIOExtension
1562 * Gets the type associated with @extension.
1564 * Returns: the type of @extension
1567 g_io_extension_get_type (GIOExtension *extension)
1569 return extension->type;
1573 * g_io_extension_get_name:
1574 * @extension: a #GIOExtension
1576 * Gets the name under which @extension was registered.
1578 * Note that the same type may be registered as extension
1579 * for multiple extension points, under different names.
1581 * Returns: the name of @extension.
1584 g_io_extension_get_name (GIOExtension *extension)
1586 return extension->name;
1590 * g_io_extension_get_priority:
1591 * @extension: a #GIOExtension
1593 * Gets the priority with which @extension was registered.
1595 * Returns: the priority of @extension
1598 g_io_extension_get_priority (GIOExtension *extension)
1600 return extension->priority;