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__
82 * Provides an interface and default functions for loading and unloading
83 * modules. This is used internally to make GIO extensible, but can also
84 * be used by others to implement module loading.
90 * `GIOExtensionPoint` provides a mechanism for modules to extend the
91 * functionality of the library or application that loaded it in an
94 * An extension point is identified by a name, and it may optionally
95 * require that any implementation must be of a certain type (or derived
96 * thereof). Use [func@Gio.IOExtensionPoint.register] to register an
97 * extension point, and [method@Gio.IOExtensionPoint.set_required_type] to
98 * set a required type.
100 * A module can implement an extension point by specifying the
101 * [type@GObject.Type] that implements the functionality. Additionally, each
102 * implementation of an extension point has a name, and a priority. Use
103 * [func@Gio.IOExtensionPoint.implement] to implement an extension point.
106 * GIOExtensionPoint *ep;
108 * // Register an extension point
109 * ep = g_io_extension_point_register ("my-extension-point");
110 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
114 * // Implement an extension point
115 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
116 * g_io_extension_point_implement ("my-extension-point",
117 * my_example_impl_get_type (),
122 * It is up to the code that registered the extension point how
123 * it uses the implementations that have been associated with it.
124 * Depending on the use case, it may use all implementations, or
125 * only the one with the highest priority, or pick a specific
128 * To avoid opening all modules just to find out what extension
129 * points they implement, GIO makes use of a caching mechanism,
130 * see [gio-querymodules](gio-querymodules.html).
131 * You are expected to run this command after installing a
134 * The `GIO_EXTRA_MODULES` environment variable can be used to
135 * specify additional directories to automatically load modules
136 * from. This environment variable has the same syntax as the
137 * `PATH`. If two modules have the same base name in different
138 * directories, then the latter one will be ignored. If additional
139 * directories are specified GIO will load modules from the built-in
146 * Represents a scope for loading IO modules. A scope can be used for blocking
147 * duplicate modules, or blocking a module you don't want to load.
149 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
150 * or g_io_modules_scan_all_in_directory_with_scope().
154 struct _GIOModuleScope {
155 GIOModuleScopeFlags flags;
156 GHashTable *basenames;
160 * g_io_module_scope_new:
161 * @flags: flags for the new scope
163 * Create a new scope for loading of IO modules. A scope can be used for
164 * blocking duplicate modules, or blocking a module you don't want to load.
166 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
167 * which have the same base name as a module that has already been seen
170 * Returns: (transfer full): the new module scope
175 g_io_module_scope_new (GIOModuleScopeFlags flags)
177 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
178 scope->flags = flags;
179 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
184 * g_io_module_scope_free:
185 * @scope: a module loading scope
187 * Free a module scope.
192 g_io_module_scope_free (GIOModuleScope *scope)
196 g_hash_table_destroy (scope->basenames);
201 * g_io_module_scope_block:
202 * @scope: a module loading scope
203 * @basename: the basename to block
205 * Block modules with the given @basename from being loaded when
206 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
207 * or g_io_modules_load_all_in_directory_with_scope().
212 g_io_module_scope_block (GIOModuleScope *scope,
213 const gchar *basename)
217 g_return_if_fail (scope != NULL);
218 g_return_if_fail (basename != NULL);
220 key = g_strdup (basename);
221 g_hash_table_add (scope->basenames, key);
225 _g_io_module_scope_contains (GIOModuleScope *scope,
226 const gchar *basename)
228 return g_hash_table_contains (scope->basenames, basename);
232 GTypeModule parent_instance;
236 gboolean initialized; /* The module was loaded at least once */
238 void (* load) (GIOModule *module);
239 void (* unload) (GIOModule *module);
242 struct _GIOModuleClass
244 GTypeModuleClass parent_class;
248 static void g_io_module_finalize (GObject *object);
249 static gboolean g_io_module_load_module (GTypeModule *gmodule);
250 static void g_io_module_unload_module (GTypeModule *gmodule);
255 * #GIOExtension is an opaque data structure and can only be accessed
256 * using the following functions.
258 struct _GIOExtension {
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);
344 GError *error = NULL;
346 if (!module->filename)
348 g_warning ("GIOModule path not set");
352 module->library = g_module_open_full (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
354 if (!module->library)
356 g_printerr ("%s\n", error->message);
357 g_clear_error (&error);
361 /* Make sure that the loaded library contains the required methods */
362 if (!load_symbols (module))
364 g_printerr ("%s\n", g_module_error ());
365 g_module_close (module->library);
370 /* Initialize the loaded module */
371 module->load (module);
372 module->initialized = TRUE;
378 g_io_module_unload_module (GTypeModule *gmodule)
380 GIOModule *module = G_IO_MODULE (gmodule);
382 module->unload (module);
384 g_module_close (module->library);
385 module->library = NULL;
388 module->unload = NULL;
393 * @filename: (type filename): filename of the shared library module.
395 * Creates a new GIOModule that will load the specific
396 * shared library when in use.
398 * Returns: a #GIOModule from given @filename,
402 g_io_module_new (const gchar *filename)
406 g_return_val_if_fail (filename != NULL, NULL);
408 module = g_object_new (G_IO_TYPE_MODULE, NULL);
409 module->filename = g_strdup (filename);
415 is_valid_module_name (const gchar *basename,
416 GIOModuleScope *scope)
420 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
421 #if defined(__APPLE__)
422 if (!g_str_has_prefix (basename, "lib") ||
423 !(g_str_has_suffix (basename, ".so") ||
424 g_str_has_suffix (basename, ".dylib")))
427 if (!g_str_has_prefix (basename, "lib") ||
428 !g_str_has_suffix (basename, ".so"))
432 if (!g_str_has_suffix (basename, ".dll"))
439 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
440 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
441 g_io_module_scope_block (scope, basename);
449 * g_io_modules_scan_all_in_directory_with_scope:
450 * @dirname: (type filename): pathname for a directory containing modules
452 * @scope: a scope to use when scanning the modules
454 * Scans all the modules in the specified directory, ensuring that
455 * any extension point implemented by a module is registered.
457 * This may not actually load and initialize all the types in each
458 * module, some modules may be lazily loaded and initialized when
459 * an extension point it implements is used with e.g.
460 * g_io_extension_point_get_extensions() or
461 * g_io_extension_point_get_extension_by_name().
463 * If you need to guarantee that all types are loaded in all the modules,
464 * use g_io_modules_load_all_in_directory().
469 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
470 GIOModuleScope *scope)
480 if (!g_module_supported ())
483 dir = g_dir_open (dirname, 0, NULL);
487 filename = g_build_filename (dirname, "giomodule.cache", NULL);
491 if (g_stat (filename, &statbuf) == 0 &&
492 g_file_get_contents (filename, &data, NULL, NULL))
497 /* cache_time is the time the cache file was created; we also take
498 * into account the change time because in ostree based systems, all
499 * system file have mtime equal to epoch 0.
501 * Any file that has a ctime before this was created then and not modified
502 * since then (userspace can't change ctime). Its possible to change the
503 * ctime forward without changing the file content, by e.g. chmoding the
504 * file, but this is uncommon and will only cause us to not use the cache
505 * so will not cause bugs.
507 cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
509 lines = g_strsplit (data, "\n", -1);
512 for (i = 0; lines[i] != NULL; i++)
514 char *line = lines[i];
517 char **strv_extension_points;
522 colon = strchr (line, ':');
523 if (colon == NULL || line == colon)
524 continue; /* Invalid line, ignore */
526 *colon = 0; /* terminate filename */
527 file = g_strdup (line);
528 colon++; /* after colon */
530 while (g_ascii_isspace (*colon))
533 if (G_UNLIKELY (!cache))
534 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
535 g_free, (GDestroyNotify)g_strfreev);
537 strv_extension_points = g_strsplit (colon, ",", -1);
538 g_hash_table_insert (cache, file, strv_extension_points);
543 while ((name = g_dir_read_name (dir)))
545 if (is_valid_module_name (name, scope))
547 GIOExtensionPoint *extension_point;
550 char **strv_extension_points = NULL;
553 path = g_build_filename (dirname, name, NULL);
554 module = g_io_module_new (path);
557 strv_extension_points = g_hash_table_lookup (cache, name);
559 if (strv_extension_points != NULL &&
560 g_stat (path, &statbuf) == 0 &&
561 statbuf.st_ctime <= cache_time)
563 /* Lazy load/init the library when first required */
564 for (i = 0; strv_extension_points[i] != NULL; i++)
567 g_io_extension_point_register (strv_extension_points[i]);
568 extension_point->lazy_load_modules =
569 g_list_prepend (extension_point->lazy_load_modules,
575 /* Try to load and init types */
576 if (g_type_module_use (G_TYPE_MODULE (module)))
578 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
579 /* module must remain alive, because the type system keeps weak refs */
580 g_ignore_leak (module);
584 g_printerr ("Failed to load module: %s\n", path);
585 g_object_unref (module);
596 g_hash_table_destroy (cache);
602 * g_io_modules_scan_all_in_directory:
603 * @dirname: (type filename): pathname for a directory containing modules
606 * Scans all the modules in the specified directory, ensuring that
607 * any extension point implemented by a module is registered.
609 * This may not actually load and initialize all the types in each
610 * module, some modules may be lazily loaded and initialized when
611 * an extension point it implements is used with e.g.
612 * g_io_extension_point_get_extensions() or
613 * g_io_extension_point_get_extension_by_name().
615 * If you need to guarantee that all types are loaded in all the modules,
616 * use g_io_modules_load_all_in_directory().
621 g_io_modules_scan_all_in_directory (const char *dirname)
623 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
627 * g_io_modules_load_all_in_directory_with_scope:
628 * @dirname: (type filename): pathname for a directory containing modules
630 * @scope: a scope to use when scanning the modules.
632 * Loads all the modules in the specified directory.
634 * If don't require all modules to be initialized (and thus registering
635 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
636 * which allows delayed/lazy loading of modules.
638 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
639 * from the directory,
640 * All the modules are loaded into memory, if you want to
641 * unload them (enabling on-demand loading) you must call
642 * g_type_module_unuse() on all the modules. Free the list
643 * with g_list_free().
648 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
649 GIOModuleScope *scope)
655 if (!g_module_supported ())
658 dir = g_dir_open (dirname, 0, NULL);
663 while ((name = g_dir_read_name (dir)))
665 if (is_valid_module_name (name, scope))
670 path = g_build_filename (dirname, name, NULL);
671 module = g_io_module_new (path);
673 if (!g_type_module_use (G_TYPE_MODULE (module)))
675 g_printerr ("Failed to load module: %s\n", path);
676 g_object_unref (module);
683 modules = g_list_prepend (modules, module);
693 * g_io_modules_load_all_in_directory:
694 * @dirname: (type filename): pathname for a directory containing modules
697 * Loads all the modules in the specified directory.
699 * If don't require all modules to be initialized (and thus registering
700 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
701 * which allows delayed/lazy loading of modules.
703 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
704 * from the directory,
705 * All the modules are loaded into memory, if you want to
706 * unload them (enabling on-demand loading) you must call
707 * g_type_module_unuse() on all the modules. Free the list
708 * with g_list_free().
711 g_io_modules_load_all_in_directory (const char *dirname)
713 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
717 try_class (GIOExtension *extension,
718 guint is_supported_offset)
720 GType type = g_io_extension_get_type (extension);
721 typedef gboolean (*verify_func) (void);
724 class = g_type_class_ref (type);
725 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
728 g_type_class_unref (class);
733 print_help (const char *envvar,
734 GIOExtensionPoint *ep)
736 g_print ("Supported arguments for %s environment variable:\n", envvar);
738 if (g_io_extension_point_get_extensions (ep) == NULL)
739 g_print (" (none)\n");
743 GIOExtension *extension;
746 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
749 width = MAX (width, strlen (g_io_extension_get_name (extension)));
752 for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
756 g_print (" %*s - %d\n", (int) MIN (width, G_MAXINT),
757 g_io_extension_get_name (extension),
758 g_io_extension_get_priority (extension));
764 * _g_io_module_get_default_type:
765 * @extension_point: the name of an extension point
766 * @envvar: (nullable): the name of an environment variable to
767 * override the default implementation.
768 * @is_supported_offset: a vtable offset, or zero
770 * Retrieves the default class implementing @extension_point.
772 * If @envvar is not %NULL, and the environment variable with that
773 * name is set, then the implementation it specifies will be tried
774 * first. After that, or if @envvar is not set, all other
775 * implementations will be tried in order of decreasing priority.
777 * If @is_supported_offset is non-zero, then it is the offset into the
778 * class vtable at which there is a function that takes no arguments and
779 * returns a boolean. This function will be called on each candidate
780 * implementation to check if it is actually usable or not.
782 * The result is cached after it is generated the first time, and
783 * the function is thread-safe.
785 * Returns: (transfer none): the type to instantiate to implement
786 * @extension_point, or %G_TYPE_INVALID if there are no usable
790 _g_io_module_get_default_type (const gchar *extension_point,
792 guint is_supported_offset)
794 static GRecMutex default_modules_lock;
795 static GHashTable *default_modules;
796 const char *use_this;
798 GIOExtensionPoint *ep;
799 GIOExtension *extension, *preferred;
802 g_rec_mutex_lock (&default_modules_lock);
807 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
809 g_rec_mutex_unlock (&default_modules_lock);
810 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
815 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
818 _g_io_modules_ensure_loaded ();
819 ep = g_io_extension_point_lookup (extension_point);
823 g_warn_if_reached ();
824 g_rec_mutex_unlock (&default_modules_lock);
825 return G_TYPE_INVALID;
828 /* It’s OK to query the environment here, even when running as setuid, because
829 * it only allows a choice between existing already-loaded modules. No new
830 * code is loaded based on the environment variable value. */
831 use_this = envvar ? g_getenv (envvar) : NULL;
832 if (g_strcmp0 (use_this, "help") == 0)
834 print_help (envvar, ep);
840 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
843 impl = try_class (preferred, is_supported_offset);
848 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
853 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
856 if (extension == preferred)
859 impl = try_class (extension, is_supported_offset);
867 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
868 g_rec_mutex_unlock (&default_modules_lock);
870 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
874 try_implementation (const char *extension_point,
875 GIOExtension *extension,
876 GIOModuleVerifyFunc verify_func)
878 GType type = g_io_extension_get_type (extension);
881 if (g_type_is_a (type, G_TYPE_INITABLE))
883 GError *error = NULL;
885 impl = g_initable_new (type, NULL, &error, NULL);
889 g_debug ("Failed to initialize %s (%s) for %s: %s",
890 g_io_extension_get_name (extension),
893 error ? error->message : "");
894 g_clear_error (&error);
899 impl = g_object_new (type, NULL);
900 if (!verify_func || verify_func (impl))
903 g_object_unref (impl);
909 weak_ref_free (GWeakRef *weak_ref)
911 g_weak_ref_clear (weak_ref);
916 * _g_io_module_get_default:
917 * @extension_point: the name of an extension point
918 * @envvar: (nullable): the name of an environment variable to
919 * override the default implementation.
920 * @verify_func: (nullable): a function to call to verify that
921 * a given implementation is usable in the current environment.
923 * Retrieves the default object implementing @extension_point.
925 * If @envvar is not %NULL, and the environment variable with that
926 * name is set, then the implementation it specifies will be tried
927 * first. After that, or if @envvar is not set, all other
928 * implementations will be tried in order of decreasing priority.
930 * If an extension point implementation implements #GInitable, then
931 * that implementation will only be used if it initializes
932 * successfully. Otherwise, if @verify_func is not %NULL, then it will
933 * be called on each candidate implementation after construction, to
934 * check if it is actually usable or not.
936 * The result is cached after it is generated the first time (but the cache does
937 * not keep a strong reference to the object), and
938 * the function is thread-safe.
940 * Returns: (transfer full) (nullable): an object implementing
941 * @extension_point, or %NULL if there are no usable
945 _g_io_module_get_default (const gchar *extension_point,
947 GIOModuleVerifyFunc verify_func)
949 static GRecMutex default_modules_lock;
950 static GHashTable *default_modules;
951 const char *use_this;
953 GIOExtensionPoint *ep;
954 GIOExtension *extension = NULL, *preferred;
955 gpointer impl, value;
956 GWeakRef *impl_weak_ref = NULL;
958 g_rec_mutex_lock (&default_modules_lock);
961 if (g_hash_table_lookup_extended (default_modules, extension_point,
964 /* Don’t debug here, since we’re returning a cached object which was
965 * already printed earlier. */
966 impl_weak_ref = value;
967 impl = g_weak_ref_get (impl_weak_ref);
969 /* If the object has been finalised (impl == NULL), fall through and
970 * instantiate a new one. */
973 g_rec_mutex_unlock (&default_modules_lock);
974 return g_steal_pointer (&impl);
980 default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
981 g_free, (GDestroyNotify) weak_ref_free);
984 _g_io_modules_ensure_loaded ();
985 ep = g_io_extension_point_lookup (extension_point);
989 g_debug ("%s: Failed to find extension point ‘%s’",
990 G_STRFUNC, extension_point);
991 g_warn_if_reached ();
992 g_rec_mutex_unlock (&default_modules_lock);
996 /* It’s OK to query the environment here, even when running as setuid, because
997 * it only allows a choice between existing already-loaded modules. No new
998 * code is loaded based on the environment variable value. */
999 use_this = envvar ? g_getenv (envvar) : NULL;
1000 if (g_strcmp0 (use_this, "help") == 0)
1002 print_help (envvar, ep);
1008 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
1011 impl = try_implementation (extension_point, preferred, verify_func);
1012 extension = preferred;
1017 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
1022 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1024 extension = l->data;
1025 if (extension == preferred)
1028 impl = try_implementation (extension_point, extension, verify_func);
1036 if (impl_weak_ref == NULL)
1038 impl_weak_ref = g_new0 (GWeakRef, 1);
1039 g_weak_ref_init (impl_weak_ref, impl);
1040 g_hash_table_insert (default_modules, g_strdup (extension_point),
1041 g_steal_pointer (&impl_weak_ref));
1045 g_weak_ref_set (impl_weak_ref, impl);
1048 g_rec_mutex_unlock (&default_modules_lock);
1052 g_assert (extension != NULL);
1053 g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
1054 G_STRFUNC, g_io_extension_get_name (extension),
1055 G_OBJECT_TYPE_NAME (impl), extension_point);
1058 g_debug ("%s: Failed to find default implementation for ‘%s’",
1059 G_STRFUNC, extension_point);
1061 return g_steal_pointer (&impl);
1064 extern GType g_inotify_file_monitor_get_type (void);
1065 extern GType g_kqueue_file_monitor_get_type (void);
1066 extern GType g_win32_file_monitor_get_type (void);
1068 extern GType _g_unix_volume_monitor_get_type (void);
1069 extern GType _g_local_vfs_get_type (void);
1071 extern GType _g_win32_volume_monitor_get_type (void);
1072 extern GType _g_winhttp_vfs_get_type (void);
1074 extern GType _g_dummy_proxy_resolver_get_type (void);
1075 extern GType _g_dummy_tls_backend_get_type (void);
1076 extern GType g_network_monitor_base_get_type (void);
1078 extern GType _g_network_monitor_netlink_get_type (void);
1079 extern GType _g_network_monitor_nm_get_type (void);
1082 extern GType g_debug_controller_dbus_get_type (void);
1083 extern GType g_memory_monitor_dbus_get_type (void);
1084 extern GType g_memory_monitor_portal_get_type (void);
1085 extern GType g_memory_monitor_win32_get_type (void);
1086 extern GType g_power_profile_monitor_dbus_get_type (void);
1089 extern GType g_fdo_notification_backend_get_type (void);
1090 extern GType g_gtk_notification_backend_get_type (void);
1091 extern GType g_portal_notification_backend_get_type (void);
1092 extern GType g_proxy_resolver_portal_get_type (void);
1093 extern GType g_network_monitor_portal_get_type (void);
1096 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1097 extern GType g_cocoa_notification_backend_get_type (void);
1100 #ifdef G_PLATFORM_WIN32
1101 extern GType g_win32_notification_backend_get_type (void);
1103 #include <windows.h>
1104 extern GType _g_win32_network_monitor_get_type (void);
1106 static HMODULE gio_dll = NULL;
1108 #ifndef GLIB_STATIC_COMPILATION
1110 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1112 LPVOID lpvReserved);
1115 DllMain (HINSTANCE hinstDLL,
1119 if (fdwReason == DLL_PROCESS_ATTACH)
1122 gio_win32_appinfo_init (FALSE);
1128 #elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
1129 extern void glib_win32_init (void);
1130 extern void gobject_win32_init (void);
1132 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
1133 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(giomodule_init_ctor)
1136 G_DEFINE_CONSTRUCTOR (giomodule_init_ctor)
1139 giomodule_init_ctor (void)
1141 /* When built dynamically, module initialization is done through DllMain
1142 * function which is called when the dynamic library is loaded by the glib
1143 * module AFTER loading gobject. So, in dynamic configuration glib and
1144 * gobject are always initialized BEFORE gio.
1146 * When built statically, initialization mechanism relies on hooking
1147 * functions to the CRT section directly at compilation time. As we don't
1148 * control how each compilation unit will be built and in which order, we
1149 * obtain the same kind of issue as the "static initialization order fiasco".
1150 * In this case, we must ensure explicitly that glib and gobject are always
1151 * well initialized BEFORE gio.
1154 gobject_win32_init ();
1155 gio_win32_appinfo_init (FALSE);
1158 #else /* G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION && !G_HAS_CONSTRUCTORS */
1159 #error Your platform/compiler is missing constructor support
1160 #endif /* GLIB_STATIC_COMPILATION */
1163 _g_io_win32_get_module (void)
1166 GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1167 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1168 (LPCWSTR) _g_io_win32_get_module,
1173 #endif /* G_PLATFORM_WIN32 */
1176 _g_io_modules_ensure_extension_points_registered (void)
1178 static gsize registered_extensions = FALSE;
1179 GIOExtensionPoint *ep;
1181 if (g_once_init_enter (®istered_extensions))
1183 #if defined(G_OS_UNIX) && !defined(__APPLE__)
1184 #if !GLIB_CHECK_VERSION (3, 0, 0)
1185 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1186 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1190 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1191 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1193 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1194 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1196 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1197 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1199 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1200 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1202 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1203 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1205 ep = g_io_extension_point_register ("gsettings-backend");
1206 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1208 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1209 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1211 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1212 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1214 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1215 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1217 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1218 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1220 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1221 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1223 ep = g_io_extension_point_register (G_DEBUG_CONTROLLER_EXTENSION_POINT_NAME);
1224 g_io_extension_point_set_required_type (ep, G_TYPE_DEBUG_CONTROLLER);
1226 ep = g_io_extension_point_register (G_MEMORY_MONITOR_EXTENSION_POINT_NAME);
1227 g_io_extension_point_set_required_type (ep, G_TYPE_MEMORY_MONITOR);
1229 ep = g_io_extension_point_register (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME);
1230 g_io_extension_point_set_required_type (ep, G_TYPE_POWER_PROFILE_MONITOR);
1232 g_once_init_leave (®istered_extensions, TRUE);
1237 get_gio_module_dir (void)
1240 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1242 /* If running as setuid, loading modules from an arbitrary directory
1243 * controlled by the unprivileged user who is running the program could allow
1244 * for execution of arbitrary code (in constructors in modules).
1247 * If a setuid program somehow needs to load additional GIO modules, it should
1248 * explicitly call g_io_modules_scan_all_in_directory(). */
1249 module_dir = !is_setuid ? g_strdup (g_getenv ("GIO_MODULE_DIR")) : NULL;
1250 if (module_dir == NULL)
1255 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1256 module_dir = g_build_filename (install_dir,
1257 "lib", "gio", "modules",
1259 g_free (install_dir);
1261 module_dir = g_strdup (GIO_MODULE_DIR);
1263 #include "TargetConditionals.h"
1264 /* Only auto-relocate on macOS, not watchOS etc; older macOS SDKs only define TARGET_OS_MAC */
1265 #if (defined (TARGET_OS_OSX) && TARGET_OS_OSX) || \
1266 (!defined (TARGET_OS_OSX) && defined (TARGET_OS_MAC) && TARGET_OS_MAC)
1269 g_autofree gchar *path = NULL;
1270 g_autofree gchar *possible_dir = NULL;
1273 if (dladdr (get_gio_module_dir, &info))
1275 /* Gets path to the PREFIX/lib directory */
1276 path = g_path_get_dirname (info.dli_fname);
1277 possible_dir = g_build_filename (path, "gio", "modules", NULL);
1278 if (g_file_test (possible_dir, G_FILE_TEST_IS_DIR))
1280 g_free (module_dir);
1281 module_dir = g_steal_pointer (&possible_dir);
1294 _g_io_modules_ensure_loaded (void)
1296 static gsize loaded_dirs = FALSE;
1297 const char *module_path;
1298 GIOModuleScope *scope;
1300 _g_io_modules_ensure_extension_points_registered ();
1302 if (g_once_init_enter (&loaded_dirs))
1304 gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1307 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1309 /* First load any overrides, extras (but not if running as setuid!) */
1310 module_path = !is_setuid ? g_getenv ("GIO_EXTRA_MODULES") : NULL;
1316 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1318 for (i = 0; paths[i] != NULL; i++)
1320 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1326 /* Then load the compiled in path */
1327 module_dir = get_gio_module_dir ();
1329 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1330 g_free (module_dir);
1332 g_io_module_scope_free (scope);
1334 /* Initialize types from built-in "modules" */
1335 g_type_ensure (g_null_settings_backend_get_type ());
1336 g_type_ensure (g_memory_settings_backend_get_type ());
1337 g_type_ensure (g_keyfile_settings_backend_get_type ());
1338 g_type_ensure (g_power_profile_monitor_dbus_get_type ());
1339 #if defined(HAVE_INOTIFY_INIT1)
1340 g_type_ensure (g_inotify_file_monitor_get_type ());
1342 #if defined(HAVE_KQUEUE)
1343 g_type_ensure (g_kqueue_file_monitor_get_type ());
1346 g_type_ensure (_g_win32_volume_monitor_get_type ());
1347 g_type_ensure (g_win32_file_monitor_get_type ());
1348 g_type_ensure (g_registry_settings_backend_get_type ());
1351 g_type_ensure (g_nextstep_settings_backend_get_type ());
1352 g_type_ensure (g_osx_app_info_get_type ());
1355 g_type_ensure (_g_unix_volume_monitor_get_type ());
1356 g_type_ensure (g_debug_controller_dbus_get_type ());
1357 g_type_ensure (g_fdo_notification_backend_get_type ());
1358 g_type_ensure (g_gtk_notification_backend_get_type ());
1359 g_type_ensure (g_portal_notification_backend_get_type ());
1360 g_type_ensure (g_memory_monitor_dbus_get_type ());
1361 g_type_ensure (g_memory_monitor_portal_get_type ());
1362 g_type_ensure (g_network_monitor_portal_get_type ());
1363 g_type_ensure (g_power_profile_monitor_portal_get_type ());
1364 g_type_ensure (g_proxy_resolver_portal_get_type ());
1366 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1367 g_type_ensure (g_cocoa_notification_backend_get_type ());
1370 g_type_ensure (g_win32_notification_backend_get_type ());
1371 g_type_ensure (_g_winhttp_vfs_get_type ());
1372 g_type_ensure (g_memory_monitor_win32_get_type ());
1374 g_type_ensure (_g_local_vfs_get_type ());
1375 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1376 g_type_ensure (_g_http_proxy_get_type ());
1377 g_type_ensure (_g_https_proxy_get_type ());
1378 g_type_ensure (_g_socks4a_proxy_get_type ());
1379 g_type_ensure (_g_socks4_proxy_get_type ());
1380 g_type_ensure (_g_socks5_proxy_get_type ());
1381 g_type_ensure (_g_dummy_tls_backend_get_type ());
1382 g_type_ensure (g_network_monitor_base_get_type ());
1384 g_type_ensure (_g_network_monitor_netlink_get_type ());
1385 g_type_ensure (_g_network_monitor_nm_get_type ());
1388 g_type_ensure (_g_win32_network_monitor_get_type ());
1391 g_once_init_leave (&loaded_dirs, TRUE);
1396 g_io_extension_point_free (GIOExtensionPoint *ep)
1403 * g_io_extension_point_register:
1404 * @name: The name of the extension point
1406 * Registers an extension point.
1408 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1409 * owned by GIO and should not be freed.
1412 g_io_extension_point_register (const char *name)
1414 GIOExtensionPoint *ep;
1416 G_LOCK (extension_points);
1417 if (extension_points == NULL)
1418 extension_points = g_hash_table_new_full (g_str_hash,
1421 (GDestroyNotify)g_io_extension_point_free);
1423 ep = g_hash_table_lookup (extension_points, name);
1426 G_UNLOCK (extension_points);
1430 ep = g_new0 (GIOExtensionPoint, 1);
1431 ep->name = g_strdup (name);
1433 g_hash_table_insert (extension_points, ep->name, ep);
1435 G_UNLOCK (extension_points);
1441 * g_io_extension_point_lookup:
1442 * @name: the name of the extension point
1444 * Looks up an existing extension point.
1446 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1447 * is no registered extension point with the given name.
1450 g_io_extension_point_lookup (const char *name)
1452 GIOExtensionPoint *ep;
1454 G_LOCK (extension_points);
1456 if (extension_points != NULL)
1457 ep = g_hash_table_lookup (extension_points, name);
1459 G_UNLOCK (extension_points);
1466 * g_io_extension_point_set_required_type:
1467 * @extension_point: a #GIOExtensionPoint
1468 * @type: the #GType to require
1470 * Sets the required type for @extension_point to @type.
1471 * All implementations must henceforth have this type.
1474 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1477 extension_point->required_type = type;
1481 * g_io_extension_point_get_required_type:
1482 * @extension_point: a #GIOExtensionPoint
1484 * Gets the required type for @extension_point.
1486 * Returns: the #GType that all implementations must have,
1487 * or %G_TYPE_INVALID if the extension point has no required type
1490 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1492 return extension_point->required_type;
1496 lazy_load_modules (GIOExtensionPoint *extension_point)
1501 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1505 if (!module->initialized)
1507 if (g_type_module_use (G_TYPE_MODULE (module)))
1508 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1510 g_printerr ("Failed to load module: %s\n",
1517 * g_io_extension_point_get_extensions:
1518 * @extension_point: a #GIOExtensionPoint
1520 * Gets a list of all extensions that implement this extension point.
1521 * The list is sorted by priority, beginning with the highest priority.
1523 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1524 * #GIOExtensions. The list is owned by GIO and should not be
1528 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1530 g_return_val_if_fail (extension_point != NULL, NULL);
1532 lazy_load_modules (extension_point);
1533 return extension_point->extensions;
1537 * g_io_extension_point_get_extension_by_name:
1538 * @extension_point: a #GIOExtensionPoint
1539 * @name: the name of the extension to get
1541 * Finds a #GIOExtension for an extension point by name.
1543 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1544 * given name, or %NULL if there is no extension with that name
1547 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1552 g_return_val_if_fail (name != NULL, NULL);
1554 lazy_load_modules (extension_point);
1555 for (l = extension_point->extensions; l != NULL; l = l->next)
1557 GIOExtension *e = l->data;
1559 if (e->name != NULL &&
1560 strcmp (e->name, name) == 0)
1568 extension_prio_compare (gconstpointer a,
1571 const GIOExtension *extension_a = a, *extension_b = b;
1573 if (extension_a->priority > extension_b->priority)
1576 if (extension_b->priority > extension_a->priority)
1583 * g_io_extension_point_implement:
1584 * @extension_point_name: the name of the extension point
1585 * @type: the #GType to register as extension
1586 * @extension_name: the name for the extension
1587 * @priority: the priority for the extension
1589 * Registers @type as extension for the extension point with name
1590 * @extension_point_name.
1592 * If @type has already been registered as an extension for this
1593 * extension point, the existing #GIOExtension object is returned.
1595 * Returns: (transfer none): a #GIOExtension object for #GType
1598 g_io_extension_point_implement (const char *extension_point_name,
1600 const char *extension_name,
1603 GIOExtensionPoint *extension_point;
1604 GIOExtension *extension;
1607 g_return_val_if_fail (extension_point_name != NULL, NULL);
1609 extension_point = g_io_extension_point_lookup (extension_point_name);
1610 if (extension_point == NULL)
1612 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1616 if (extension_point->required_type != 0 &&
1617 !g_type_is_a (type, extension_point->required_type))
1619 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1620 "Expected type is %s.",
1622 extension_point_name,
1623 g_type_name (extension_point->required_type));
1627 /* It's safe to register the same type multiple times */
1628 for (l = extension_point->extensions; l != NULL; l = l->next)
1630 extension = l->data;
1631 if (extension->type == type)
1635 extension = g_slice_new0 (GIOExtension);
1636 extension->type = type;
1637 extension->name = g_strdup (extension_name);
1638 extension->priority = priority;
1640 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1641 extension, extension_prio_compare);
1647 * g_io_extension_ref_class:
1648 * @extension: a #GIOExtension
1650 * Gets a reference to the class for the type that is
1651 * associated with @extension.
1653 * Returns: (transfer full): the #GTypeClass for the type of @extension
1656 g_io_extension_ref_class (GIOExtension *extension)
1658 return g_type_class_ref (extension->type);
1662 * g_io_extension_get_type:
1663 * @extension: a #GIOExtension
1665 * Gets the type associated with @extension.
1667 * Returns: the type of @extension
1670 g_io_extension_get_type (GIOExtension *extension)
1672 return extension->type;
1676 * g_io_extension_get_name:
1677 * @extension: a #GIOExtension
1679 * Gets the name under which @extension was registered.
1681 * Note that the same type may be registered as extension
1682 * for multiple extension points, under different names.
1684 * Returns: the name of @extension.
1687 g_io_extension_get_name (GIOExtension *extension)
1689 return extension->name;
1693 * g_io_extension_get_priority:
1694 * @extension: a #GIOExtension
1696 * Gets the priority with which @extension was registered.
1698 * Returns: the priority of @extension
1701 g_io_extension_get_priority (GIOExtension *extension)
1703 return extension->priority;