-
Notifications
You must be signed in to change notification settings - Fork 532
/
giomodule.c
1704 lines (1473 loc) · 48.3 KB
/
giomodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <[email protected]>
*/
#include "config.h"
/* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
* in the preprocessor, we need to define this before including glib.h*/
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif
#include <string.h>
#include "giomodule.h"
#include "giomodule-priv.h"
#include "glib-private.h"
#include "glocalfilemonitor.h"
#include "gnativevolumemonitor.h"
#include "gproxyresolver.h"
#include "gproxy.h"
#include "gsettingsbackendinternal.h"
#include "ghttpproxy.h"
#include "gsocks4proxy.h"
#include "gsocks4aproxy.h"
#include "gsocks5proxy.h"
#include "gtlsbackend.h"
#include "gvfs.h"
#include "gnotificationbackend.h"
#include "ginitable.h"
#include "gnetworkmonitor.h"
#include "gdebugcontroller.h"
#include "gdebugcontrollerdbus.h"
#include "gmemorymonitor.h"
#include "gmemorymonitorportal.h"
#include "gmemorymonitordbus.h"
#include "gpowerprofilemonitor.h"
#include "gpowerprofilemonitordbus.h"
#include "gpowerprofilemonitorportal.h"
#ifdef G_OS_WIN32
#include "gregistrysettingsbackend.h"
#include "giowin32-priv.h"
#endif
#include <glib/gstdio.h>
#if defined(G_OS_UNIX) && !defined(__APPLE__)
#include "gdesktopappinfo.h"
#endif
#ifdef HAVE_COCOA
#include "gosxappinfo.h"
#endif
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#endif
#define __GLIB_H_INSIDE__
#include "gconstructor.h"
#undef __GLIB_H_INSIDE__
/**
* GIOModule:
*
* Provides an interface and default functions for loading and unloading
* modules. This is used internally to make GIO extensible, but can also
* be used by others to implement module loading.
*/
/**
* GIOExtensionPoint:
*
* `GIOExtensionPoint` provides a mechanism for modules to extend the
* functionality of the library or application that loaded it in an
* organized fashion.
*
* An extension point is identified by a name, and it may optionally
* require that any implementation must be of a certain type (or derived
* thereof). Use [[email protected]] to register an
* extension point, and [[email protected]_required_type] to
* set a required type.
*
* A module can implement an extension point by specifying the
* [[email protected]] that implements the functionality. Additionally, each
* implementation of an extension point has a name, and a priority. Use
* [[email protected]] to implement an extension point.
*
* ```c
* GIOExtensionPoint *ep;
*
* // Register an extension point
* ep = g_io_extension_point_register ("my-extension-point");
* g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
* ```
*
* ```c
* // Implement an extension point
* G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
* g_io_extension_point_implement ("my-extension-point",
* my_example_impl_get_type (),
* "my-example",
* 10);
* ```
*
* It is up to the code that registered the extension point how
* it uses the implementations that have been associated with it.
* Depending on the use case, it may use all implementations, or
* only the one with the highest priority, or pick a specific
* one by name.
*
* To avoid opening all modules just to find out what extension
* points they implement, GIO makes use of a caching mechanism,
* see [gio-querymodules](gio-querymodules.html).
* You are expected to run this command after installing a
* GIO module.
*
* The `GIO_EXTRA_MODULES` environment variable can be used to
* specify additional directories to automatically load modules
* from. This environment variable has the same syntax as the
* `PATH`. If two modules have the same base name in different
* directories, then the latter one will be ignored. If additional
* directories are specified GIO will load modules from the built-in
* directory last.
*/
/**
* GIOModuleScope:
*
* Represents a scope for loading IO modules. A scope can be used for blocking
* duplicate modules, or blocking a module you don't want to load.
*
* The scope can be used with g_io_modules_load_all_in_directory_with_scope()
* or g_io_modules_scan_all_in_directory_with_scope().
*
* Since: 2.30
*/
struct _GIOModuleScope {
GIOModuleScopeFlags flags;
GHashTable *basenames;
};
/**
* g_io_module_scope_new:
* @flags: flags for the new scope
*
* Create a new scope for loading of IO modules. A scope can be used for
* blocking duplicate modules, or blocking a module you don't want to load.
*
* Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
* which have the same base name as a module that has already been seen
* in this scope.
*
* Returns: (transfer full): the new module scope
*
* Since: 2.30
*/
GIOModuleScope *
g_io_module_scope_new (GIOModuleScopeFlags flags)
{
GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
scope->flags = flags;
scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
return scope;
}
/**
* g_io_module_scope_free:
* @scope: a module loading scope
*
* Free a module scope.
*
* Since: 2.30
*/
void
g_io_module_scope_free (GIOModuleScope *scope)
{
if (!scope)
return;
g_hash_table_destroy (scope->basenames);
g_free (scope);
}
/**
* g_io_module_scope_block:
* @scope: a module loading scope
* @basename: the basename to block
*
* Block modules with the given @basename from being loaded when
* this scope is used with g_io_modules_scan_all_in_directory_with_scope()
* or g_io_modules_load_all_in_directory_with_scope().
*
* Since: 2.30
*/
void
g_io_module_scope_block (GIOModuleScope *scope,
const gchar *basename)
{
gchar *key;
g_return_if_fail (scope != NULL);
g_return_if_fail (basename != NULL);
key = g_strdup (basename);
g_hash_table_add (scope->basenames, key);
}
static gboolean
_g_io_module_scope_contains (GIOModuleScope *scope,
const gchar *basename)
{
return g_hash_table_contains (scope->basenames, basename);
}
struct _GIOModule {
GTypeModule parent_instance;
gchar *filename;
GModule *library;
gboolean initialized; /* The module was loaded at least once */
void (* load) (GIOModule *module);
void (* unload) (GIOModule *module);
};
struct _GIOModuleClass
{
GTypeModuleClass parent_class;
};
static void g_io_module_finalize (GObject *object);
static gboolean g_io_module_load_module (GTypeModule *gmodule);
static void g_io_module_unload_module (GTypeModule *gmodule);
/**
* GIOExtension:
*
* #GIOExtension is an opaque data structure and can only be accessed
* using the following functions.
*/
struct _GIOExtension {
char *name;
GType type;
gint priority;
};
struct _GIOExtensionPoint {
GType required_type;
char *name;
GList *extensions;
GList *lazy_load_modules;
};
static GHashTable *extension_points = NULL;
G_LOCK_DEFINE_STATIC(extension_points);
G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
static void
g_io_module_class_init (GIOModuleClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
object_class->finalize = g_io_module_finalize;
type_module_class->load = g_io_module_load_module;
type_module_class->unload = g_io_module_unload_module;
}
static void
g_io_module_init (GIOModule *module)
{
}
static void
g_io_module_finalize (GObject *object)
{
GIOModule *module = G_IO_MODULE (object);
g_free (module->filename);
G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
}
static gboolean
load_symbols (GIOModule *module)
{
gchar *name;
gchar *load_symname;
gchar *unload_symname;
gboolean ret;
name = _g_io_module_extract_name (module->filename);
load_symname = g_strconcat ("g_io_", name, "_load", NULL);
unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
ret = g_module_symbol (module->library,
load_symname,
(gpointer) &module->load) &&
g_module_symbol (module->library,
unload_symname,
(gpointer) &module->unload);
if (!ret)
{
/* Fallback to old names */
ret = g_module_symbol (module->library,
"g_io_module_load",
(gpointer) &module->load) &&
g_module_symbol (module->library,
"g_io_module_unload",
(gpointer) &module->unload);
}
g_free (name);
g_free (load_symname);
g_free (unload_symname);
return ret;
}
static gboolean
g_io_module_load_module (GTypeModule *gmodule)
{
GIOModule *module = G_IO_MODULE (gmodule);
GError *error = NULL;
if (!module->filename)
{
g_warning ("GIOModule path not set");
return FALSE;
}
module->library = g_module_open_full (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
if (!module->library)
{
g_printerr ("%s\n", error->message);
g_clear_error (&error);
return FALSE;
}
/* Make sure that the loaded library contains the required methods */
if (!load_symbols (module))
{
g_printerr ("%s\n", g_module_error ());
g_module_close (module->library);
return FALSE;
}
/* Initialize the loaded module */
module->load (module);
module->initialized = TRUE;
return TRUE;
}
static void
g_io_module_unload_module (GTypeModule *gmodule)
{
GIOModule *module = G_IO_MODULE (gmodule);
module->unload (module);
g_module_close (module->library);
module->library = NULL;
module->load = NULL;
module->unload = NULL;
}
/**
* g_io_module_new:
* @filename: (type filename): filename of the shared library module.
*
* Creates a new GIOModule that will load the specific
* shared library when in use.
*
* Returns: a #GIOModule from given @filename,
* or %NULL on error.
**/
GIOModule *
g_io_module_new (const gchar *filename)
{
GIOModule *module;
g_return_val_if_fail (filename != NULL, NULL);
module = g_object_new (G_IO_TYPE_MODULE, NULL);
module->filename = g_strdup (filename);
return module;
}
static gboolean
is_valid_module_name (const gchar *basename,
GIOModuleScope *scope)
{
gboolean result;
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
#if defined(__APPLE__)
if (!g_str_has_prefix (basename, "lib") ||
!(g_str_has_suffix (basename, ".so") ||
g_str_has_suffix (basename, ".dylib")))
return FALSE;
#else
if (!g_str_has_prefix (basename, "lib") ||
!g_str_has_suffix (basename, ".so"))
return FALSE;
#endif
#else
if (!g_str_has_suffix (basename, ".dll"))
return FALSE;
#endif
result = TRUE;
if (scope)
{
result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
g_io_module_scope_block (scope, basename);
}
return result;
}
/**
* g_io_modules_scan_all_in_directory_with_scope:
* @dirname: (type filename): pathname for a directory containing modules
* to scan.
* @scope: a scope to use when scanning the modules
*
* Scans all the modules in the specified directory, ensuring that
* any extension point implemented by a module is registered.
*
* This may not actually load and initialize all the types in each
* module, some modules may be lazily loaded and initialized when
* an extension point it implements is used with e.g.
* g_io_extension_point_get_extensions() or
* g_io_extension_point_get_extension_by_name().
*
* If you need to guarantee that all types are loaded in all the modules,
* use g_io_modules_load_all_in_directory().
*
* Since: 2.30
**/
void
g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
GIOModuleScope *scope)
{
const gchar *name;
char *filename;
GDir *dir;
GStatBuf statbuf;
char *data;
time_t cache_time;
GHashTable *cache;
if (!g_module_supported ())
return;
dir = g_dir_open (dirname, 0, NULL);
if (!dir)
return;
filename = g_build_filename (dirname, "giomodule.cache", NULL);
cache = NULL;
cache_time = 0;
if (g_stat (filename, &statbuf) == 0 &&
g_file_get_contents (filename, &data, NULL, NULL))
{
char **lines;
int i;
/* cache_time is the time the cache file was created; we also take
* into account the change time because in ostree based systems, all
* system file have mtime equal to epoch 0.
*
* Any file that has a ctime before this was created then and not modified
* since then (userspace can't change ctime). Its possible to change the
* ctime forward without changing the file content, by e.g. chmoding the
* file, but this is uncommon and will only cause us to not use the cache
* so will not cause bugs.
*/
cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
lines = g_strsplit (data, "\n", -1);
g_free (data);
for (i = 0; lines[i] != NULL; i++)
{
char *line = lines[i];
char *file;
char *colon;
char **strv_extension_points;
if (line[0] == '#')
continue;
colon = strchr (line, ':');
if (colon == NULL || line == colon)
continue; /* Invalid line, ignore */
*colon = 0; /* terminate filename */
file = g_strdup (line);
colon++; /* after colon */
while (g_ascii_isspace (*colon))
colon++;
if (G_UNLIKELY (!cache))
cache = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify)g_strfreev);
strv_extension_points = g_strsplit (colon, ",", -1);
g_hash_table_insert (cache, file, strv_extension_points);
}
g_strfreev (lines);
}
while ((name = g_dir_read_name (dir)))
{
if (is_valid_module_name (name, scope))
{
GIOExtensionPoint *extension_point;
GIOModule *module;
gchar *path;
char **strv_extension_points = NULL;
int i;
path = g_build_filename (dirname, name, NULL);
module = g_io_module_new (path);
if (cache)
strv_extension_points = g_hash_table_lookup (cache, name);
if (strv_extension_points != NULL &&
g_stat (path, &statbuf) == 0 &&
statbuf.st_ctime <= cache_time)
{
/* Lazy load/init the library when first required */
for (i = 0; strv_extension_points[i] != NULL; i++)
{
extension_point =
g_io_extension_point_register (strv_extension_points[i]);
extension_point->lazy_load_modules =
g_list_prepend (extension_point->lazy_load_modules,
module);
}
}
else
{
/* Try to load and init types */
if (g_type_module_use (G_TYPE_MODULE (module)))
{
g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
/* module must remain alive, because the type system keeps weak refs */
g_ignore_leak (module);
}
else
{
g_printerr ("Failed to load module: %s\n", path);
g_object_unref (module);
}
}
g_free (path);
}
}
g_dir_close (dir);
if (cache)
g_hash_table_destroy (cache);
g_free (filename);
}
/**
* g_io_modules_scan_all_in_directory:
* @dirname: (type filename): pathname for a directory containing modules
* to scan.
*
* Scans all the modules in the specified directory, ensuring that
* any extension point implemented by a module is registered.
*
* This may not actually load and initialize all the types in each
* module, some modules may be lazily loaded and initialized when
* an extension point it implements is used with e.g.
* g_io_extension_point_get_extensions() or
* g_io_extension_point_get_extension_by_name().
*
* If you need to guarantee that all types are loaded in all the modules,
* use g_io_modules_load_all_in_directory().
*
* Since: 2.24
**/
void
g_io_modules_scan_all_in_directory (const char *dirname)
{
g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
}
/**
* g_io_modules_load_all_in_directory_with_scope:
* @dirname: (type filename): pathname for a directory containing modules
* to load.
* @scope: a scope to use when scanning the modules.
*
* Loads all the modules in the specified directory.
*
* If don't require all modules to be initialized (and thus registering
* all gtypes) then you can use g_io_modules_scan_all_in_directory()
* which allows delayed/lazy loading of modules.
*
* Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
* from the directory,
* All the modules are loaded into memory, if you want to
* unload them (enabling on-demand loading) you must call
* g_type_module_unuse() on all the modules. Free the list
* with g_list_free().
*
* Since: 2.30
**/
GList *
g_io_modules_load_all_in_directory_with_scope (const char *dirname,
GIOModuleScope *scope)
{
const gchar *name;
GDir *dir;
GList *modules;
if (!g_module_supported ())
return NULL;
dir = g_dir_open (dirname, 0, NULL);
if (!dir)
return NULL;
modules = NULL;
while ((name = g_dir_read_name (dir)))
{
if (is_valid_module_name (name, scope))
{
GIOModule *module;
gchar *path;
path = g_build_filename (dirname, name, NULL);
module = g_io_module_new (path);
if (!g_type_module_use (G_TYPE_MODULE (module)))
{
g_printerr ("Failed to load module: %s\n", path);
g_object_unref (module);
g_free (path);
continue;
}
g_free (path);
modules = g_list_prepend (modules, module);
}
}
g_dir_close (dir);
return modules;
}
/**
* g_io_modules_load_all_in_directory:
* @dirname: (type filename): pathname for a directory containing modules
* to load.
*
* Loads all the modules in the specified directory.
*
* If don't require all modules to be initialized (and thus registering
* all gtypes) then you can use g_io_modules_scan_all_in_directory()
* which allows delayed/lazy loading of modules.
*
* Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
* from the directory,
* All the modules are loaded into memory, if you want to
* unload them (enabling on-demand loading) you must call
* g_type_module_unuse() on all the modules. Free the list
* with g_list_free().
**/
GList *
g_io_modules_load_all_in_directory (const char *dirname)
{
return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
}
static gpointer
try_class (GIOExtension *extension,
guint is_supported_offset)
{
GType type = g_io_extension_get_type (extension);
typedef gboolean (*verify_func) (void);
gpointer class;
class = g_type_class_ref (type);
if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
return class;
g_type_class_unref (class);
return NULL;
}
static void
print_help (const char *envvar,
GIOExtensionPoint *ep)
{
g_print ("Supported arguments for %s environment variable:\n", envvar);
if (g_io_extension_point_get_extensions (ep) == NULL)
g_print (" (none)\n");
else
{
GList *l;
GIOExtension *extension;
gsize width = 0;
for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
{
extension = l->data;
width = MAX (width, strlen (g_io_extension_get_name (extension)));
}
for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
{
extension = l->data;
g_print (" %*s - %d\n", (int) MIN (width, G_MAXINT),
g_io_extension_get_name (extension),
g_io_extension_get_priority (extension));
}
}
}
/**
* _g_io_module_get_default_type:
* @extension_point: the name of an extension point
* @envvar: (nullable): the name of an environment variable to
* override the default implementation.
* @is_supported_offset: a vtable offset, or zero
*
* Retrieves the default class implementing @extension_point.
*
* If @envvar is not %NULL, and the environment variable with that
* name is set, then the implementation it specifies will be tried
* first. After that, or if @envvar is not set, all other
* implementations will be tried in order of decreasing priority.
*
* If @is_supported_offset is non-zero, then it is the offset into the
* class vtable at which there is a function that takes no arguments and
* returns a boolean. This function will be called on each candidate
* implementation to check if it is actually usable or not.
*
* The result is cached after it is generated the first time, and
* the function is thread-safe.
*
* Returns: (transfer none): the type to instantiate to implement
* @extension_point, or %G_TYPE_INVALID if there are no usable
* implementations.
*/
GType
_g_io_module_get_default_type (const gchar *extension_point,
const gchar *envvar,
guint is_supported_offset)
{
static GRecMutex default_modules_lock;
static GHashTable *default_modules;
const char *use_this;
GList *l;
GIOExtensionPoint *ep;
GIOExtension *extension, *preferred;
gpointer impl;
g_rec_mutex_lock (&default_modules_lock);
if (default_modules)
{
gpointer key;
if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
{
g_rec_mutex_unlock (&default_modules_lock);
return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
}
}
else
{
default_modules = g_hash_table_new (g_str_hash, g_str_equal);
}
_g_io_modules_ensure_loaded ();
ep = g_io_extension_point_lookup (extension_point);
if (!ep)
{
g_warn_if_reached ();
g_rec_mutex_unlock (&default_modules_lock);
return G_TYPE_INVALID;
}
/* It’s OK to query the environment here, even when running as setuid, because
* it only allows a choice between existing already-loaded modules. No new
* code is loaded based on the environment variable value. */
use_this = envvar ? g_getenv (envvar) : NULL;
if (g_strcmp0 (use_this, "help") == 0)
{
print_help (envvar, ep);
use_this = NULL;
}
if (use_this)
{
preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
if (preferred)
{
impl = try_class (preferred, is_supported_offset);
if (impl)
goto done;
}
else
g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
}
else
preferred = NULL;
for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
{
extension = l->data;
if (extension == preferred)
continue;
impl = try_class (extension, is_supported_offset);
if (impl)
goto done;
}
impl = NULL;
done:
g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
g_rec_mutex_unlock (&default_modules_lock);
return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
}
static gpointer
try_implementation (const char *extension_point,
GIOExtension *extension,
GIOModuleVerifyFunc verify_func)
{
GType type = g_io_extension_get_type (extension);
gpointer impl;
if (g_type_is_a (type, G_TYPE_INITABLE))
{
GError *error = NULL;
impl = g_initable_new (type, NULL, &error, NULL);
if (impl)
return impl;
g_debug ("Failed to initialize %s (%s) for %s: %s",
g_io_extension_get_name (extension),
g_type_name (type),
extension_point,
error ? error->message : "");
g_clear_error (&error);
return NULL;
}
else
{
impl = g_object_new (type, NULL);
if (!verify_func || verify_func (impl))
return impl;
g_object_unref (impl);
return NULL;
}
}
static void
weak_ref_free (GWeakRef *weak_ref)
{
g_weak_ref_clear (weak_ref);
g_free (weak_ref);
}
/**
* _g_io_module_get_default:
* @extension_point: the name of an extension point
* @envvar: (nullable): the name of an environment variable to
* override the default implementation.
* @verify_func: (nullable): a function to call to verify that
* a given implementation is usable in the current environment.
*
* Retrieves the default object implementing @extension_point.
*
* If @envvar is not %NULL, and the environment variable with that
* name is set, then the implementation it specifies will be tried
* first. After that, or if @envvar is not set, all other
* implementations will be tried in order of decreasing priority.
*
* If an extension point implementation implements #GInitable, then
* that implementation will only be used if it initializes
* successfully. Otherwise, if @verify_func is not %NULL, then it will
* be called on each candidate implementation after construction, to
* check if it is actually usable or not.
*
* The result is cached after it is generated the first time (but the cache does
* not keep a strong reference to the object), and
* the function is thread-safe.
*
* Returns: (transfer full) (nullable): an object implementing
* @extension_point, or %NULL if there are no usable
* implementations.
*/
gpointer
_g_io_module_get_default (const gchar *extension_point,
const gchar *envvar,
GIOModuleVerifyFunc verify_func)
{
static GRecMutex default_modules_lock;
static GHashTable *default_modules;
const char *use_this;
GList *l;
GIOExtensionPoint *ep;
GIOExtension *extension = NULL, *preferred;
gpointer impl, value;
GWeakRef *impl_weak_ref = NULL;
g_rec_mutex_lock (&default_modules_lock);
if (default_modules)
{
if (g_hash_table_lookup_extended (default_modules, extension_point,
NULL, &value))
{
/* Don’t debug here, since we’re returning a cached object which was
* already printed earlier. */
impl_weak_ref = value;
impl = g_weak_ref_get (impl_weak_ref);
/* If the object has been finalised (impl == NULL), fall through and
* instantiate a new one. */
if (impl != NULL)
{
g_rec_mutex_unlock (&default_modules_lock);
return g_steal_pointer (&impl);
}
}
}
else
{
default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify) weak_ref_free);
}
_g_io_modules_ensure_loaded ();
ep = g_io_extension_point_lookup (extension_point);
if (!ep)
{
g_debug ("%s: Failed to find extension point ‘%s’",
G_STRFUNC, extension_point);
g_warn_if_reached ();
g_rec_mutex_unlock (&default_modules_lock);
return NULL;
}
/* It’s OK to query the environment here, even when running as setuid, because
* it only allows a choice between existing already-loaded modules. No new
* code is loaded based on the environment variable value. */
use_this = envvar ? g_getenv (envvar) : NULL;
if (g_strcmp0 (use_this, "help") == 0)