1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 /* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
26 * in the preprocessor, we need to define this before including glib.h*/
27 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
28 #define GLIB_DISABLE_DEPRECATION_WARNINGS
33 #include "giomodule.h"
34 #include "giomodule-priv.h"
35 #include "glib-private.h"
36 #include "glocalfilemonitor.h"
37 #include "gnativevolumemonitor.h"
38 #include "gproxyresolver.h"
40 #include "gsettingsbackendinternal.h"
41 #include "ghttpproxy.h"
42 #include "gsocks4proxy.h"
43 #include "gsocks4aproxy.h"
44 #include "gsocks5proxy.h"
45 #include "gtlsbackend.h"
47 #include "gnotificationbackend.h"
48 #include "ginitable.h"
49 #include "gnetworkmonitor.h"
50 #include "gdebugcontroller.h"
51 #include "gdebugcontrollerdbus.h"
52 #include "gmemorymonitor.h"
53 #include "gmemorymonitorportal.h"
54 #include "gmemorymonitordbus.h"
55 #include "gpowerprofilemonitor.h"
56 #include "gpowerprofilemonitordbus.h"
57 #include "gpowerprofilemonitorportal.h"
59 #include "gregistrysettingsbackend.h"
60 #include "giowin32-priv.h"
62 #include <glib/gstdio.h>
64 #if defined(G_OS_UNIX) && !defined(__APPLE__)
65 #include "gdesktopappinfo.h"
68 #include "gosxappinfo.h"
72 #include <AvailabilityMacros.h>
75 #define __GLIB_H_INSIDE__
76 #include "gconstructor.h"
77 #undef __GLIB_H_INSIDE__
81 * @short_description: Loadable GIO Modules
84 * Provides an interface and default functions for loading and unloading
85 * modules. This is used internally to make GIO extensible, but can also
86 * be used by others to implement module loading.
91 * SECTION:extensionpoints
92 * @short_description: Extension Points
94 * @see_also: [Extending GIO][extending-gio]
96 * #GIOExtensionPoint provides a mechanism for modules to extend the
97 * functionality of the library or application that loaded it in an
100 * An extension point is identified by a name, and it may optionally
101 * require that any implementation must be of a certain type (or derived
102 * thereof). Use g_io_extension_point_register() to register an
103 * extension point, and g_io_extension_point_set_required_type() to
104 * set a required type.
106 * A module can implement an extension point by specifying the #GType
107 * that implements the functionality. Additionally, each implementation
108 * of an extension point has a name, and a priority. Use
109 * g_io_extension_point_implement() to implement an extension point.
111 * |[<!-- language="C" -->
112 * GIOExtensionPoint *ep;
114 * // Register an extension point
115 * ep = g_io_extension_point_register ("my-extension-point");
116 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
119 * |[<!-- language="C" -->
120 * // Implement an extension point
121 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
122 * g_io_extension_point_implement ("my-extension-point",
123 * my_example_impl_get_type (),
128 * It is up to the code that registered the extension point how
129 * it uses the implementations that have been associated with it.
130 * Depending on the use case, it may use all implementations, or
131 * only the one with the highest priority, or pick a specific
134 * To avoid opening all modules just to find out what extension
135 * points they implement, GIO makes use of a caching mechanism,
136 * see [gio-querymodules][gio-querymodules].
137 * You are expected to run this command after installing a
140 * The `GIO_EXTRA_MODULES` environment variable can be used to
141 * specify additional directories to automatically load modules
142 * from. This environment variable has the same syntax as the
143 * `PATH`. If two modules have the same base name in different
144 * directories, then the latter one will be ignored. If additional
145 * directories are specified GIO will load modules from the built-in
152 * Represents a scope for loading IO modules. A scope can be used for blocking
153 * duplicate modules, or blocking a module you don't want to load.
155 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
156 * or g_io_modules_scan_all_in_directory_with_scope().
160 struct _GIOModuleScope {
161 GIOModuleScopeFlags flags;
162 GHashTable *basenames;
166 * g_io_module_scope_new:
167 * @flags: flags for the new scope
169 * Create a new scope for loading of IO modules. A scope can be used for
170 * blocking duplicate modules, or blocking a module you don't want to load.
172 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
173 * which have the same base name as a module that has already been seen
176 * Returns: (transfer full): the new module scope
181 g_io_module_scope_new (GIOModuleScopeFlags flags)
183 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
184 scope->flags = flags;
185 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
190 * g_io_module_scope_free:
191 * @scope: a module loading scope
193 * Free a module scope.
198 g_io_module_scope_free (GIOModuleScope *scope)
202 g_hash_table_destroy (scope->basenames);
207 * g_io_module_scope_block:
208 * @scope: a module loading scope
209 * @basename: the basename to block
211 * Block modules with the given @basename from being loaded when
212 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
213 * or g_io_modules_load_all_in_directory_with_scope().
218 g_io_module_scope_block (GIOModuleScope *scope,
219 const gchar *basename)
223 g_return_if_fail (scope != NULL);
224 g_return_if_fail (basename != NULL);
226 key = g_strdup (basename);
227 g_hash_table_add (scope->basenames, key);
231 _g_io_module_scope_contains (GIOModuleScope *scope,
232 const gchar *basename)
234 return g_hash_table_contains (scope->basenames, basename);
238 GTypeModule parent_instance;
242 gboolean initialized; /* The module was loaded at least once */
244 void (* load) (GIOModule *module);
245 void (* unload) (GIOModule *module);
248 struct _GIOModuleClass
250 GTypeModuleClass parent_class;
254 static void g_io_module_finalize (GObject *object);
255 static gboolean g_io_module_load_module (GTypeModule *gmodule);
256 static void g_io_module_unload_module (GTypeModule *gmodule);
261 * #GIOExtension is an opaque data structure and can only be accessed
262 * using the following functions.
264 struct _GIOExtension {
273 * #GIOExtensionPoint is an opaque data structure and can only be accessed
274 * using the following functions.
276 struct _GIOExtensionPoint {
280 GList *lazy_load_modules;
283 static GHashTable *extension_points = NULL;
284 G_LOCK_DEFINE_STATIC(extension_points);
286 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
289 g_io_module_class_init (GIOModuleClass *class)
291 GObjectClass *object_class = G_OBJECT_CLASS (class);
292 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
294 object_class->finalize = g_io_module_finalize;
296 type_module_class->load = g_io_module_load_module;
297 type_module_class->unload = g_io_module_unload_module;
301 g_io_module_init (GIOModule *module)
306 g_io_module_finalize (GObject *object)
308 GIOModule *module = G_IO_MODULE (object);
310 g_free (module->filename);
312 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
316 load_symbols (GIOModule *module)
320 gchar *unload_symname;
323 name = _g_io_module_extract_name (module->filename);
324 load_symname = g_strconcat ("g_io_", name, "_load", NULL);
325 unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
327 ret = g_module_symbol (module->library,
329 (gpointer) &module->load) &&
330 g_module_symbol (module->library,
332 (gpointer) &module->unload);
336 /* Fallback to old names */
337 ret = g_module_symbol (module->library,
339 (gpointer) &module->load) &&
340 g_module_symbol (module->library,
341 "g_io_module_unload",
342 (gpointer) &module->unload);
346 g_free (load_symname);
347 g_free (unload_symname);
353 g_io_module_load_module (GTypeModule *gmodule)
355 GIOModule *module = G_IO_MODULE (gmodule);
356 GError *error = NULL;
358 if (!module->filename)
360 g_warning ("GIOModule path not set");
364 module->library = g_module_open_full (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
366 if (!module->library)
368 g_printerr ("%s\n", error->message);
369 g_clear_error (&error);
373 /* Make sure that the loaded library contains the required methods */
374 if (!load_symbols (module))
376 g_printerr ("%s\n", g_module_error ());
377 g_module_close (module->library);
382 /* Initialize the loaded module */
383 module->load (module);
384 module->initialized = TRUE;
390 g_io_module_unload_module (GTypeModule *gmodule)
392 GIOModule *module = G_IO_MODULE (gmodule);
394 module->unload (module);
396 g_module_close (module->library);
397 module->library = NULL;
400 module->unload = NULL;
405 * @filename: (type filename): filename of the shared library module.
407 * Creates a new GIOModule that will load the specific
408 * shared library when in use.
410 * Returns: a #GIOModule from given @filename,
414 g_io_module_new (const gchar *filename)
418 g_return_val_if_fail (filename != NULL, NULL);
420 module = g_object_new (G_IO_TYPE_MODULE, NULL);
421 module->filename = g_strdup (filename);
427 is_valid_module_name (const gchar *basename,
428 GIOModuleScope *scope)
432 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
433 #if defined(__APPLE__)
434 if (!g_str_has_prefix (basename, "lib") ||
435 !(g_str_has_suffix (basename, ".so") ||
436 g_str_has_suffix (basename, ".dylib")))
439 if (!g_str_has_prefix (basename, "lib") ||
440 !g_str_has_suffix (basename, ".so"))
444 if (!g_str_has_suffix (basename, ".dll"))
451 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
452 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
453 g_io_module_scope_block (scope, basename);
461 * g_io_modules_scan_all_in_directory_with_scope:
462 * @dirname: (type filename): pathname for a directory containing modules
464 * @scope: a scope to use when scanning the modules
466 * Scans all the modules in the specified directory, ensuring that
467 * any extension point implemented by a module is registered.
469 * This may not actually load and initialize all the types in each
470 * module, some modules may be lazily loaded and initialized when
471 * an extension point it implements is used with e.g.
472 * g_io_extension_point_get_extensions() or
473 * g_io_extension_point_get_extension_by_name().
475 * If you need to guarantee that all types are loaded in all the modules,
476 * use g_io_modules_load_all_in_directory().
481 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
482 GIOModuleScope *scope)
492 if (!g_module_supported ())
495 dir = g_dir_open (dirname, 0, NULL);
499 filename = g_build_filename (dirname, "giomodule.cache", NULL);
503 if (g_stat (filename, &statbuf) == 0 &&
504 g_file_get_contents (filename, &data, NULL, NULL))
509 /* cache_time is the time the cache file was created; we also take
510 * into account the change time because in ostree based systems, all
511 * system file have mtime equal to epoch 0.
513 * Any file that has a ctime before this was created then and not modified
514 * since then (userspace can't change ctime). Its possible to change the
515 * ctime forward without changing the file content, by e.g. chmoding the
516 * file, but this is uncommon and will only cause us to not use the cache
517 * so will not cause bugs.
519 cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
521 lines = g_strsplit (data, "\n", -1);
524 for (i = 0; lines[i] != NULL; i++)
526 char *line = lines[i];
529 char **strv_extension_points;
534 colon = strchr (line, ':');
535 if (colon == NULL || line == colon)
536 continue; /* Invalid line, ignore */
538 *colon = 0; /* terminate filename */
539 file = g_strdup (line);
540 colon++; /* after colon */
542 while (g_ascii_isspace (*colon))
545 if (G_UNLIKELY (!cache))
546 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
547 g_free, (GDestroyNotify)g_strfreev);
549 strv_extension_points = g_strsplit (colon, ",", -1);
550 g_hash_table_insert (cache, file, strv_extension_points);
555 while ((name = g_dir_read_name (dir)))
557 if (is_valid_module_name (name, scope))
559 GIOExtensionPoint *extension_point;
562 char **strv_extension_points = NULL;
565 path = g_build_filename (dirname, name, NULL);
566 module = g_io_module_new (path);
569 strv_extension_points = g_hash_table_lookup (cache, name);
571 if (strv_extension_points != NULL &&
572 g_stat (path, &statbuf) == 0 &&
573 statbuf.st_ctime <= cache_time)
575 /* Lazy load/init the library when first required */
576 for (i = 0; strv_extension_points[i] != NULL; i++)
579 g_io_extension_point_register (strv_extension_points[i]);
580 extension_point->lazy_load_modules =
581 g_list_prepend (extension_point->lazy_load_modules,
587 /* Try to load and init types */
588 if (g_type_module_use (G_TYPE_MODULE (module)))
590 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
591 /* module must remain alive, because the type system keeps weak refs */
592 g_ignore_leak (module);
596 g_printerr ("Failed to load module: %s\n", path);
597 g_object_unref (module);
608 g_hash_table_destroy (cache);
614 * g_io_modules_scan_all_in_directory:
615 * @dirname: (type filename): pathname for a directory containing modules
618 * Scans all the modules in the specified directory, ensuring that
619 * any extension point implemented by a module is registered.
621 * This may not actually load and initialize all the types in each
622 * module, some modules may be lazily loaded and initialized when
623 * an extension point it implements is used with e.g.
624 * g_io_extension_point_get_extensions() or
625 * g_io_extension_point_get_extension_by_name().
627 * If you need to guarantee that all types are loaded in all the modules,
628 * use g_io_modules_load_all_in_directory().
633 g_io_modules_scan_all_in_directory (const char *dirname)
635 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
639 * g_io_modules_load_all_in_directory_with_scope:
640 * @dirname: (type filename): pathname for a directory containing modules
642 * @scope: a scope to use when scanning the modules.
644 * Loads all the modules in the specified directory.
646 * If don't require all modules to be initialized (and thus registering
647 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
648 * which allows delayed/lazy loading of modules.
650 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
651 * from the directory,
652 * All the modules are loaded into memory, if you want to
653 * unload them (enabling on-demand loading) you must call
654 * g_type_module_unuse() on all the modules. Free the list
655 * with g_list_free().
660 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
661 GIOModuleScope *scope)
667 if (!g_module_supported ())
670 dir = g_dir_open (dirname, 0, NULL);
675 while ((name = g_dir_read_name (dir)))
677 if (is_valid_module_name (name, scope))
682 path = g_build_filename (dirname, name, NULL);
683 module = g_io_module_new (path);
685 if (!g_type_module_use (G_TYPE_MODULE (module)))
687 g_printerr ("Failed to load module: %s\n", path);
688 g_object_unref (module);
695 modules = g_list_prepend (modules, module);
705 * g_io_modules_load_all_in_directory:
706 * @dirname: (type filename): pathname for a directory containing modules
709 * Loads all the modules in the specified directory.
711 * If don't require all modules to be initialized (and thus registering
712 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
713 * which allows delayed/lazy loading of modules.
715 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
716 * from the directory,
717 * All the modules are loaded into memory, if you want to
718 * unload them (enabling on-demand loading) you must call
719 * g_type_module_unuse() on all the modules. Free the list
720 * with g_list_free().
723 g_io_modules_load_all_in_directory (const char *dirname)
725 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
729 try_class (GIOExtension *extension,
730 guint is_supported_offset)
732 GType type = g_io_extension_get_type (extension);
733 typedef gboolean (*verify_func) (void);
736 class = g_type_class_ref (type);
737 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
740 g_type_class_unref (class);
745 print_help (const char *envvar,
746 GIOExtensionPoint *ep)
748 g_print ("Supported arguments for %s environment variable:\n", envvar);
750 if (g_io_extension_point_get_extensions (ep) == NULL)
751 g_print (" (none)\n");
755 GIOExtension *extension;
758 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
761 width = MAX (width, strlen (g_io_extension_get_name (extension)));
764 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
768 g_print (" %*s - %d\n", (int) MIN (width, G_MAXINT),
769 g_io_extension_get_name (extension),
770 g_io_extension_get_priority (extension));
776 * _g_io_module_get_default_type:
777 * @extension_point: the name of an extension point
778 * @envvar: (nullable): the name of an environment variable to
779 * override the default implementation.
780 * @is_supported_offset: a vtable offset, or zero
782 * Retrieves the default class implementing @extension_point.
784 * If @envvar is not %NULL, and the environment variable with that
785 * name is set, then the implementation it specifies will be tried
786 * first. After that, or if @envvar is not set, all other
787 * implementations will be tried in order of decreasing priority.
789 * If @is_supported_offset is non-zero, then it is the offset into the
790 * class vtable at which there is a function that takes no arguments and
791 * returns a boolean. This function will be called on each candidate
792 * implementation to check if it is actually usable or not.
794 * The result is cached after it is generated the first time, and
795 * the function is thread-safe.
797 * Returns: (transfer none): the type to instantiate to implement
798 * @extension_point, or %G_TYPE_INVALID if there are no usable
802 _g_io_module_get_default_type (const gchar *extension_point,
804 guint is_supported_offset)
806 static GRecMutex default_modules_lock;
807 static GHashTable *default_modules;
808 const char *use_this;
810 GIOExtensionPoint *ep;
811 GIOExtension *extension, *preferred;
814 g_rec_mutex_lock (&default_modules_lock);
819 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
821 g_rec_mutex_unlock (&default_modules_lock);
822 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
827 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
830 _g_io_modules_ensure_loaded ();
831 ep = g_io_extension_point_lookup (extension_point);
835 g_warn_if_reached ();
836 g_rec_mutex_unlock (&default_modules_lock);
837 return G_TYPE_INVALID;
840 /* It’s OK to query the environment here, even when running as setuid, because
841 * it only allows a choice between existing already-loaded modules. No new
842 * code is loaded based on the environment variable value. */
843 use_this = envvar ? g_getenv (envvar) : NULL;
844 if (g_strcmp0 (use_this, "help") == 0)
846 print_help (envvar, ep);
852 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
855 impl = try_class (preferred, is_supported_offset);
860 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
865 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
868 if (extension == preferred)
871 impl = try_class (extension, is_supported_offset);
879 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
880 g_rec_mutex_unlock (&default_modules_lock);
882 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
886 try_implementation (const char *extension_point,
887 GIOExtension *extension,
888 GIOModuleVerifyFunc verify_func)
890 GType type = g_io_extension_get_type (extension);
893 if (g_type_is_a (type, G_TYPE_INITABLE))
895 GError *error = NULL;
897 impl = g_initable_new (type, NULL, &error, NULL);
901 g_debug ("Failed to initialize %s (%s) for %s: %s",
902 g_io_extension_get_name (extension),
905 error ? error->message : "");
906 g_clear_error (&error);
911 impl = g_object_new (type, NULL);
912 if (!verify_func || verify_func (impl))
915 g_object_unref (impl);
921 weak_ref_free (GWeakRef *weak_ref)
923 g_weak_ref_clear (weak_ref);
928 * _g_io_module_get_default:
929 * @extension_point: the name of an extension point
930 * @envvar: (nullable): the name of an environment variable to
931 * override the default implementation.
932 * @verify_func: (nullable): a function to call to verify that
933 * a given implementation is usable in the current environment.
935 * Retrieves the default object implementing @extension_point.
937 * If @envvar is not %NULL, and the environment variable with that
938 * name is set, then the implementation it specifies will be tried
939 * first. After that, or if @envvar is not set, all other
940 * implementations will be tried in order of decreasing priority.
942 * If an extension point implementation implements #GInitable, then
943 * that implementation will only be used if it initializes
944 * successfully. Otherwise, if @verify_func is not %NULL, then it will
945 * be called on each candidate implementation after construction, to
946 * check if it is actually usable or not.
948 * The result is cached after it is generated the first time (but the cache does
949 * not keep a strong reference to the object), and
950 * the function is thread-safe.
952 * Returns: (transfer full) (nullable): an object implementing
953 * @extension_point, or %NULL if there are no usable
957 _g_io_module_get_default (const gchar *extension_point,
959 GIOModuleVerifyFunc verify_func)
961 static GRecMutex default_modules_lock;
962 static GHashTable *default_modules;
963 const char *use_this;
965 GIOExtensionPoint *ep;
966 GIOExtension *extension = NULL, *preferred;
967 gpointer impl, value;
968 GWeakRef *impl_weak_ref = NULL;
970 g_rec_mutex_lock (&default_modules_lock);
973 if (g_hash_table_lookup_extended (default_modules, extension_point,
976 /* Don’t debug here, since we’re returning a cached object which was
977 * already printed earlier. */
978 impl_weak_ref = value;
979 impl = g_weak_ref_get (impl_weak_ref);
981 /* If the object has been finalised (impl == NULL), fall through and
982 * instantiate a new one. */
985 g_rec_mutex_unlock (&default_modules_lock);
986 return g_steal_pointer (&impl);
992 default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
993 g_free, (GDestroyNotify) weak_ref_free);
996 _g_io_modules_ensure_loaded ();
997 ep = g_io_extension_point_lookup (extension_point);
1001 g_debug ("%s: Failed to find extension point ‘%s’",
1002 G_STRFUNC, extension_point);
1003 g_warn_if_reached ();
1004 g_rec_mutex_unlock (&default_modules_lock);
1008 /* It’s OK to query the environment here, even when running as setuid, because
1009 * it only allows a choice between existing already-loaded modules. No new
1010 * code is loaded based on the environment variable value. */
1011 use_this = envvar ? g_getenv (envvar) : NULL;
1012 if (g_strcmp0 (use_this, "help") == 0)
1014 print_help (envvar, ep);
1020 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
1023 impl = try_implementation (extension_point, preferred, verify_func);
1024 extension = preferred;
1029 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
1034 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1036 extension = l->data;
1037 if (extension == preferred)
1040 impl = try_implementation (extension_point, extension, verify_func);
1048 if (impl_weak_ref == NULL)
1050 impl_weak_ref = g_new0 (GWeakRef, 1);
1051 g_weak_ref_init (impl_weak_ref, impl);
1052 g_hash_table_insert (default_modules, g_strdup (extension_point),
1053 g_steal_pointer (&impl_weak_ref));
1057 g_weak_ref_set (impl_weak_ref, impl);
1060 g_rec_mutex_unlock (&default_modules_lock);
1064 g_assert (extension != NULL);
1065 g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
1066 G_STRFUNC, g_io_extension_get_name (extension),
1067 G_OBJECT_TYPE_NAME (impl), extension_point);
1070 g_debug ("%s: Failed to find default implementation for ‘%s’",
1071 G_STRFUNC, extension_point);
1073 return g_steal_pointer (&impl);
1076 extern GType g_inotify_file_monitor_get_type (void);
1077 extern GType g_kqueue_file_monitor_get_type (void);
1078 extern GType g_win32_file_monitor_get_type (void);
1080 extern GType _g_unix_volume_monitor_get_type (void);
1081 extern GType _g_local_vfs_get_type (void);
1083 extern GType _g_win32_volume_monitor_get_type (void);
1084 extern GType _g_winhttp_vfs_get_type (void);
1086 extern GType _g_dummy_proxy_resolver_get_type (void);
1087 extern GType _g_dummy_tls_backend_get_type (void);
1088 extern GType g_network_monitor_base_get_type (void);
1090 extern GType _g_network_monitor_netlink_get_type (void);
1091 extern GType _g_network_monitor_nm_get_type (void);
1094 extern GType g_debug_controller_dbus_get_type (void);
1095 extern GType g_memory_monitor_dbus_get_type (void);
1096 extern GType g_memory_monitor_portal_get_type (void);
1097 extern GType g_memory_monitor_win32_get_type (void);
1098 extern GType g_power_profile_monitor_dbus_get_type (void);
1101 extern GType g_fdo_notification_backend_get_type (void);
1102 extern GType g_gtk_notification_backend_get_type (void);
1103 extern GType g_portal_notification_backend_get_type (void);
1104 extern GType g_proxy_resolver_portal_get_type (void);
1105 extern GType g_network_monitor_portal_get_type (void);
1108 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1109 extern GType g_cocoa_notification_backend_get_type (void);
1112 #ifdef G_PLATFORM_WIN32
1113 extern GType g_win32_notification_backend_get_type (void);
1115 #include <windows.h>
1116 extern GType _g_win32_network_monitor_get_type (void);
1118 static HMODULE gio_dll = NULL;
1120 #ifndef GLIB_STATIC_COMPILATION
1122 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1124 LPVOID lpvReserved);
1127 DllMain (HINSTANCE hinstDLL,
1131 if (fdwReason == DLL_PROCESS_ATTACH)
1134 gio_win32_appinfo_init (FALSE);
1140 #elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
1141 extern void glib_win32_init (void);
1142 extern void gobject_win32_init (void);
1144 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
1145 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(giomodule_init_ctor)
1148 G_DEFINE_CONSTRUCTOR (giomodule_init_ctor)
1151 giomodule_init_ctor (void)
1153 /* When built dynamically, module initialization is done through DllMain
1154 * function which is called when the dynamic library is loaded by the glib
1155 * module AFTER loading gobject. So, in dynamic configuration glib and
1156 * gobject are always initialized BEFORE gio.
1158 * When built statically, initialization mechanism relies on hooking
1159 * functions to the CRT section directly at compilation time. As we don't
1160 * control how each compilation unit will be built and in which order, we
1161 * obtain the same kind of issue as the "static initialization order fiasco".
1162 * In this case, we must ensure explicitly that glib and gobject are always
1163 * well initialized BEFORE gio.
1166 gobject_win32_init ();
1167 gio_win32_appinfo_init (FALSE);
1170 #else /* G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION && !G_HAS_CONSTRUCTORS */
1171 #error Your platform/compiler is missing constructor support
1172 #endif /* GLIB_STATIC_COMPILATION */
1175 _g_io_win32_get_module (void)
1178 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1179 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1180 (const char *) _g_io_win32_get_module,
1185 #endif /* G_PLATFORM_WIN32 */
1188 _g_io_modules_ensure_extension_points_registered (void)
1190 static gsize registered_extensions = FALSE;
1191 GIOExtensionPoint *ep;
1193 if (g_once_init_enter (®istered_extensions))
1195 #if defined(G_OS_UNIX) && !defined(__APPLE__)
1196 #if !GLIB_CHECK_VERSION (3, 0, 0)
1197 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1198 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1202 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1203 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1205 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1206 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1208 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1209 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1211 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1212 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1214 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1215 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1217 ep = g_io_extension_point_register ("gsettings-backend");
1218 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1220 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1221 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1223 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1224 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1226 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1227 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1229 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1230 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1232 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1233 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1235 ep = g_io_extension_point_register (G_DEBUG_CONTROLLER_EXTENSION_POINT_NAME);
1236 g_io_extension_point_set_required_type (ep, G_TYPE_DEBUG_CONTROLLER);
1238 ep = g_io_extension_point_register (G_MEMORY_MONITOR_EXTENSION_POINT_NAME);
1239 g_io_extension_point_set_required_type (ep, G_TYPE_MEMORY_MONITOR);
1241 ep = g_io_extension_point_register (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME);
1242 g_io_extension_point_set_required_type (ep, G_TYPE_POWER_PROFILE_MONITOR);
1244 g_once_init_leave (®istered_extensions, TRUE);
1249 get_gio_module_dir (void)
1252 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1254 /* If running as setuid, loading modules from an arbitrary directory
1255 * controlled by the unprivileged user who is running the program could allow
1256 * for execution of arbitrary code (in constructors in modules).
1259 * If a setuid program somehow needs to load additional GIO modules, it should
1260 * explicitly call g_io_modules_scan_all_in_directory(). */
1261 module_dir = !is_setuid ? g_strdup (g_getenv ("GIO_MODULE_DIR")) : NULL;
1262 if (module_dir == NULL)
1267 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1268 module_dir = g_build_filename (install_dir,
1269 "lib", "gio", "modules",
1271 g_free (install_dir);
1273 module_dir = g_strdup (GIO_MODULE_DIR);
1275 #include "TargetConditionals.h"
1276 /* Only auto-relocate on macOS, not watchOS etc; older macOS SDKs only define TARGET_OS_MAC */
1277 #if (defined (TARGET_OS_OSX) && TARGET_OS_OSX) || \
1278 (!defined (TARGET_OS_OSX) && defined (TARGET_OS_MAC) && TARGET_OS_MAC)
1281 g_autofree gchar *path = NULL;
1282 g_autofree gchar *possible_dir = NULL;
1285 if (dladdr (get_gio_module_dir, &info))
1287 /* Gets path to the PREFIX/lib directory */
1288 path = g_path_get_dirname (info.dli_fname);
1289 possible_dir = g_build_filename (path, "gio", "modules", NULL);
1290 if (g_file_test (possible_dir, G_FILE_TEST_IS_DIR))
1292 g_free (module_dir);
1293 module_dir = g_steal_pointer (&possible_dir);
1306 _g_io_modules_ensure_loaded (void)
1308 static gsize loaded_dirs = FALSE;
1309 const char *module_path;
1310 GIOModuleScope *scope;
1312 _g_io_modules_ensure_extension_points_registered ();
1314 if (g_once_init_enter (&loaded_dirs))
1316 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1319 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1321 /* First load any overrides, extras (but not if running as setuid!) */
1322 module_path = !is_setuid ? g_getenv ("GIO_EXTRA_MODULES") : NULL;
1328 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1330 for (i = 0; paths[i] != NULL; i++)
1332 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1338 /* Then load the compiled in path */
1339 module_dir = get_gio_module_dir ();
1341 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1342 g_free (module_dir);
1344 g_io_module_scope_free (scope);
1346 /* Initialize types from built-in "modules" */
1347 g_type_ensure (g_null_settings_backend_get_type ());
1348 g_type_ensure (g_memory_settings_backend_get_type ());
1349 g_type_ensure (g_keyfile_settings_backend_get_type ());
1350 g_type_ensure (g_power_profile_monitor_dbus_get_type ());
1351 #if defined(HAVE_INOTIFY_INIT1)
1352 g_type_ensure (g_inotify_file_monitor_get_type ());
1354 #if defined(HAVE_KQUEUE)
1355 g_type_ensure (g_kqueue_file_monitor_get_type ());
1358 g_type_ensure (_g_win32_volume_monitor_get_type ());
1359 g_type_ensure (g_win32_file_monitor_get_type ());
1360 g_type_ensure (g_registry_settings_backend_get_type ());
1363 g_type_ensure (g_nextstep_settings_backend_get_type ());
1364 g_type_ensure (g_osx_app_info_get_type ());
1367 g_type_ensure (_g_unix_volume_monitor_get_type ());
1368 g_type_ensure (g_debug_controller_dbus_get_type ());
1369 g_type_ensure (g_fdo_notification_backend_get_type ());
1370 g_type_ensure (g_gtk_notification_backend_get_type ());
1371 g_type_ensure (g_portal_notification_backend_get_type ());
1372 g_type_ensure (g_memory_monitor_dbus_get_type ());
1373 g_type_ensure (g_memory_monitor_portal_get_type ());
1374 g_type_ensure (g_network_monitor_portal_get_type ());
1375 g_type_ensure (g_power_profile_monitor_portal_get_type ());
1376 g_type_ensure (g_proxy_resolver_portal_get_type ());
1378 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1379 g_type_ensure (g_cocoa_notification_backend_get_type ());
1382 g_type_ensure (g_win32_notification_backend_get_type ());
1383 g_type_ensure (_g_winhttp_vfs_get_type ());
1384 g_type_ensure (g_memory_monitor_win32_get_type ());
1386 g_type_ensure (_g_local_vfs_get_type ());
1387 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1388 g_type_ensure (_g_http_proxy_get_type ());
1389 g_type_ensure (_g_https_proxy_get_type ());
1390 g_type_ensure (_g_socks4a_proxy_get_type ());
1391 g_type_ensure (_g_socks4_proxy_get_type ());
1392 g_type_ensure (_g_socks5_proxy_get_type ());
1393 g_type_ensure (_g_dummy_tls_backend_get_type ());
1394 g_type_ensure (g_network_monitor_base_get_type ());
1396 g_type_ensure (_g_network_monitor_netlink_get_type ());
1397 g_type_ensure (_g_network_monitor_nm_get_type ());
1400 g_type_ensure (_g_win32_network_monitor_get_type ());
1403 g_once_init_leave (&loaded_dirs, TRUE);
1408 g_io_extension_point_free (GIOExtensionPoint *ep)
1415 * g_io_extension_point_register:
1416 * @name: The name of the extension point
1418 * Registers an extension point.
1420 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1421 * owned by GIO and should not be freed.
1424 g_io_extension_point_register (const char *name)
1426 GIOExtensionPoint *ep;
1428 G_LOCK (extension_points);
1429 if (extension_points == NULL)
1430 extension_points = g_hash_table_new_full (g_str_hash,
1433 (GDestroyNotify)g_io_extension_point_free);
1435 ep = g_hash_table_lookup (extension_points, name);
1438 G_UNLOCK (extension_points);
1442 ep = g_new0 (GIOExtensionPoint, 1);
1443 ep->name = g_strdup (name);
1445 g_hash_table_insert (extension_points, ep->name, ep);
1447 G_UNLOCK (extension_points);
1453 * g_io_extension_point_lookup:
1454 * @name: the name of the extension point
1456 * Looks up an existing extension point.
1458 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1459 * is no registered extension point with the given name.
1462 g_io_extension_point_lookup (const char *name)
1464 GIOExtensionPoint *ep;
1466 G_LOCK (extension_points);
1468 if (extension_points != NULL)
1469 ep = g_hash_table_lookup (extension_points, name);
1471 G_UNLOCK (extension_points);
1478 * g_io_extension_point_set_required_type:
1479 * @extension_point: a #GIOExtensionPoint
1480 * @type: the #GType to require
1482 * Sets the required type for @extension_point to @type.
1483 * All implementations must henceforth have this type.
1486 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1489 extension_point->required_type = type;
1493 * g_io_extension_point_get_required_type:
1494 * @extension_point: a #GIOExtensionPoint
1496 * Gets the required type for @extension_point.
1498 * Returns: the #GType that all implementations must have,
1499 * or %G_TYPE_INVALID if the extension point has no required type
1502 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1504 return extension_point->required_type;
1508 lazy_load_modules (GIOExtensionPoint *extension_point)
1513 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1517 if (!module->initialized)
1519 if (g_type_module_use (G_TYPE_MODULE (module)))
1520 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1522 g_printerr ("Failed to load module: %s\n",
1529 * g_io_extension_point_get_extensions:
1530 * @extension_point: a #GIOExtensionPoint
1532 * Gets a list of all extensions that implement this extension point.
1533 * The list is sorted by priority, beginning with the highest priority.
1535 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1536 * #GIOExtensions. The list is owned by GIO and should not be
1540 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1542 g_return_val_if_fail (extension_point != NULL, NULL);
1544 lazy_load_modules (extension_point);
1545 return extension_point->extensions;
1549 * g_io_extension_point_get_extension_by_name:
1550 * @extension_point: a #GIOExtensionPoint
1551 * @name: the name of the extension to get
1553 * Finds a #GIOExtension for an extension point by name.
1555 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1556 * given name, or %NULL if there is no extension with that name
1559 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1564 g_return_val_if_fail (name != NULL, NULL);
1566 lazy_load_modules (extension_point);
1567 for (l = extension_point->extensions; l != NULL; l = l->next)
1569 GIOExtension *e = l->data;
1571 if (e->name != NULL &&
1572 strcmp (e->name, name) == 0)
1580 extension_prio_compare (gconstpointer a,
1583 const GIOExtension *extension_a = a, *extension_b = b;
1585 if (extension_a->priority > extension_b->priority)
1588 if (extension_b->priority > extension_a->priority)
1595 * g_io_extension_point_implement:
1596 * @extension_point_name: the name of the extension point
1597 * @type: the #GType to register as extension
1598 * @extension_name: the name for the extension
1599 * @priority: the priority for the extension
1601 * Registers @type as extension for the extension point with name
1602 * @extension_point_name.
1604 * If @type has already been registered as an extension for this
1605 * extension point, the existing #GIOExtension object is returned.
1607 * Returns: (transfer none): a #GIOExtension object for #GType
1610 g_io_extension_point_implement (const char *extension_point_name,
1612 const char *extension_name,
1615 GIOExtensionPoint *extension_point;
1616 GIOExtension *extension;
1619 g_return_val_if_fail (extension_point_name != NULL, NULL);
1621 extension_point = g_io_extension_point_lookup (extension_point_name);
1622 if (extension_point == NULL)
1624 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1628 if (extension_point->required_type != 0 &&
1629 !g_type_is_a (type, extension_point->required_type))
1631 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1632 "Expected type is %s.",
1634 extension_point_name,
1635 g_type_name (extension_point->required_type));
1639 /* It's safe to register the same type multiple times */
1640 for (l = extension_point->extensions; l != NULL; l = l->next)
1642 extension = l->data;
1643 if (extension->type == type)
1647 extension = g_slice_new0 (GIOExtension);
1648 extension->type = type;
1649 extension->name = g_strdup (extension_name);
1650 extension->priority = priority;
1652 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1653 extension, extension_prio_compare);
1659 * g_io_extension_ref_class:
1660 * @extension: a #GIOExtension
1662 * Gets a reference to the class for the type that is
1663 * associated with @extension.
1665 * Returns: (transfer full): the #GTypeClass for the type of @extension
1668 g_io_extension_ref_class (GIOExtension *extension)
1670 return g_type_class_ref (extension->type);
1674 * g_io_extension_get_type:
1675 * @extension: a #GIOExtension
1677 * Gets the type associated with @extension.
1679 * Returns: the type of @extension
1682 g_io_extension_get_type (GIOExtension *extension)
1684 return extension->type;
1688 * g_io_extension_get_name:
1689 * @extension: a #GIOExtension
1691 * Gets the name under which @extension was registered.
1693 * Note that the same type may be registered as extension
1694 * for multiple extension points, under different names.
1696 * Returns: the name of @extension.
1699 g_io_extension_get_name (GIOExtension *extension)
1701 return extension->name;
1705 * g_io_extension_get_priority:
1706 * @extension: a #GIOExtension
1708 * Gets the priority with which @extension was registered.
1710 * Returns: the priority of @extension
1713 g_io_extension_get_priority (GIOExtension *extension)
1715 return extension->priority;