Coverage Report

Created: 2026-02-26 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.76.5/gio/giomodule.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
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.
11
 *
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.
16
 *
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/>.
19
 *
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
 */
22
23
#include "config.h"
24
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
29
#endif
30
31
#include <string.h>
32
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"
39
#include "gproxy.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"
46
#include "gvfs.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"
58
#ifdef G_OS_WIN32
59
#include "gregistrysettingsbackend.h"
60
#include "giowin32-priv.h"
61
#endif
62
#include <glib/gstdio.h>
63
64
#if defined(G_OS_UNIX) && !defined(__APPLE__)
65
#include "gdesktopappinfo.h"
66
#endif
67
#ifdef HAVE_COCOA
68
#include "gosxappinfo.h"
69
#endif
70
71
#ifdef __APPLE__
72
#include <AvailabilityMacros.h>
73
#endif
74
75
#define __GLIB_H_INSIDE__
76
#include "gconstructor.h"
77
#undef __GLIB_H_INSIDE__
78
79
/**
80
 * SECTION:giomodule
81
 * @short_description: Loadable GIO Modules
82
 * @include: gio/gio.h
83
 *
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.
87
 * 
88
 **/
89
90
/**
91
 * SECTION:extensionpoints
92
 * @short_description: Extension Points
93
 * @include: gio.h
94
 * @see_also: [Extending GIO][extending-gio]
95
 *
96
 * #GIOExtensionPoint provides a mechanism for modules to extend the
97
 * functionality of the library or application that loaded it in an 
98
 * organized fashion.  
99
 *
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.
105
 *
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.
110
 * 
111
 *  |[<!-- language="C" -->
112
 *  GIOExtensionPoint *ep;
113
 *
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);
117
 *  ]|
118
 *
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 (),
124
 *                                  "my-example",
125
 *                                  10);
126
 *  ]|
127
 *
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
132
 *  one by name.
133
 *
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
138
 *  GIO module.
139
 *
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
146
 *  directory last.
147
 */
148
149
/**
150
 * GIOModuleScope:
151
 *
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.
154
 *
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().
157
 *
158
 * Since: 2.30
159
 */
160
struct _GIOModuleScope {
161
  GIOModuleScopeFlags flags;
162
  GHashTable *basenames;
163
};
164
165
/**
166
 * g_io_module_scope_new:
167
 * @flags: flags for the new scope
168
 *
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.
171
 *
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
174
 * in this scope.
175
 *
176
 * Returns: (transfer full): the new module scope
177
 *
178
 * Since: 2.30
179
 */
180
GIOModuleScope *
181
g_io_module_scope_new (GIOModuleScopeFlags flags)
182
1
{
183
1
  GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
184
1
  scope->flags = flags;
185
1
  scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
186
1
  return scope;
187
1
}
188
189
/**
190
 * g_io_module_scope_free:
191
 * @scope: a module loading scope
192
 *
193
 * Free a module scope.
194
 *
195
 * Since: 2.30
196
 */
197
void
198
g_io_module_scope_free (GIOModuleScope *scope)
199
1
{
200
1
  if (!scope)
201
0
    return;
202
1
  g_hash_table_destroy (scope->basenames);
203
1
  g_free (scope);
204
1
}
205
206
/**
207
 * g_io_module_scope_block:
208
 * @scope: a module loading scope
209
 * @basename: the basename to block
210
 *
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().
214
 *
215
 * Since: 2.30
216
 */
217
void
218
g_io_module_scope_block (GIOModuleScope *scope,
219
                         const gchar    *basename)
220
0
{
221
0
  gchar *key;
222
223
0
  g_return_if_fail (scope != NULL);
224
0
  g_return_if_fail (basename != NULL);
225
226
0
  key = g_strdup (basename);
227
0
  g_hash_table_add (scope->basenames, key);
228
0
}
229
230
static gboolean
231
_g_io_module_scope_contains (GIOModuleScope *scope,
232
                             const gchar    *basename)
233
0
{
234
0
  return g_hash_table_contains (scope->basenames, basename);
235
0
}
236
237
struct _GIOModule {
238
  GTypeModule parent_instance;
239
240
  gchar       *filename;
241
  GModule     *library;
242
  gboolean     initialized; /* The module was loaded at least once */
243
244
  void (* load)   (GIOModule *module);
245
  void (* unload) (GIOModule *module);
246
};
247
248
struct _GIOModuleClass
249
{
250
  GTypeModuleClass parent_class;
251
252
};
253
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);
257
258
/**
259
 * GIOExtension:
260
 *
261
 * #GIOExtension is an opaque data structure and can only be accessed
262
 * using the following functions.
263
 */
264
struct _GIOExtension {
265
  char *name;
266
  GType type;
267
  gint priority;
268
};
269
270
/**
271
 * GIOExtensionPoint:
272
 *
273
 * #GIOExtensionPoint is an opaque data structure and can only be accessed
274
 * using the following functions.
275
 */
276
struct _GIOExtensionPoint {
277
  GType required_type;
278
  char *name;
279
  GList *extensions;
280
  GList *lazy_load_modules;
281
};
282
283
static GHashTable *extension_points = NULL;
284
G_LOCK_DEFINE_STATIC(extension_points);
285
286
0
G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
287
0
288
0
static void
289
0
g_io_module_class_init (GIOModuleClass *class)
290
0
{
291
0
  GObjectClass     *object_class      = G_OBJECT_CLASS (class);
292
0
  GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
293
294
0
  object_class->finalize     = g_io_module_finalize;
295
296
0
  type_module_class->load    = g_io_module_load_module;
297
0
  type_module_class->unload  = g_io_module_unload_module;
298
0
}
299
300
static void
301
g_io_module_init (GIOModule *module)
302
0
{
303
0
}
304
305
static void
306
g_io_module_finalize (GObject *object)
307
0
{
308
0
  GIOModule *module = G_IO_MODULE (object);
309
310
0
  g_free (module->filename);
311
312
0
  G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
313
0
}
314
315
static gboolean
316
load_symbols (GIOModule *module)
317
0
{
318
0
  gchar *name;
319
0
  gchar *load_symname;
320
0
  gchar *unload_symname;
321
0
  gboolean ret;
322
323
0
  name = _g_io_module_extract_name (module->filename);
324
0
  load_symname = g_strconcat ("g_io_", name, "_load", NULL);
325
0
  unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
326
327
0
  ret = g_module_symbol (module->library,
328
0
                         load_symname,
329
0
                         (gpointer) &module->load) &&
330
0
        g_module_symbol (module->library,
331
0
                         unload_symname,
332
0
                         (gpointer) &module->unload);
333
334
0
  if (!ret)
335
0
    {
336
      /* Fallback to old names */
337
0
      ret = g_module_symbol (module->library,
338
0
                             "g_io_module_load",
339
0
                             (gpointer) &module->load) &&
340
0
            g_module_symbol (module->library,
341
0
                             "g_io_module_unload",
342
0
                             (gpointer) &module->unload);
343
0
    }
344
345
0
  g_free (name);
346
0
  g_free (load_symname);
347
0
  g_free (unload_symname);
348
349
0
  return ret;
350
0
}
351
352
static gboolean
353
g_io_module_load_module (GTypeModule *gmodule)
354
0
{
355
0
  GIOModule *module = G_IO_MODULE (gmodule);
356
0
  GError *error = NULL;
357
358
0
  if (!module->filename)
359
0
    {
360
0
      g_warning ("GIOModule path not set");
361
0
      return FALSE;
362
0
    }
363
364
0
  module->library = g_module_open_full (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL, &error);
365
366
0
  if (!module->library)
367
0
    {
368
0
      g_printerr ("%s\n", error->message);
369
0
      g_clear_error (&error);
370
0
      return FALSE;
371
0
    }
372
373
  /* Make sure that the loaded library contains the required methods */
374
0
  if (!load_symbols (module))
375
0
    {
376
0
      g_printerr ("%s\n", g_module_error ());
377
0
      g_module_close (module->library);
378
379
0
      return FALSE;
380
0
    }
381
382
  /* Initialize the loaded module */
383
0
  module->load (module);
384
0
  module->initialized = TRUE;
385
386
0
  return TRUE;
387
0
}
388
389
static void
390
g_io_module_unload_module (GTypeModule *gmodule)
391
0
{
392
0
  GIOModule *module = G_IO_MODULE (gmodule);
393
394
0
  module->unload (module);
395
396
0
  g_module_close (module->library);
397
0
  module->library = NULL;
398
399
0
  module->load   = NULL;
400
0
  module->unload = NULL;
401
0
}
402
403
/**
404
 * g_io_module_new:
405
 * @filename: (type filename): filename of the shared library module.
406
 * 
407
 * Creates a new GIOModule that will load the specific
408
 * shared library when in use.
409
 * 
410
 * Returns: a #GIOModule from given @filename, 
411
 * or %NULL on error.
412
 **/
413
GIOModule *
414
g_io_module_new (const gchar *filename)
415
0
{
416
0
  GIOModule *module;
417
418
0
  g_return_val_if_fail (filename != NULL, NULL);
419
420
0
  module = g_object_new (G_IO_TYPE_MODULE, NULL);
421
0
  module->filename = g_strdup (filename);
422
423
0
  return module;
424
0
}
425
426
static gboolean
427
is_valid_module_name (const gchar        *basename,
428
                      GIOModuleScope     *scope)
429
0
{
430
0
  gboolean result;
431
432
0
#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")))
437
    return FALSE;
438
  #else
439
0
  if (!g_str_has_prefix (basename, "lib") ||
440
0
      !g_str_has_suffix (basename, ".so"))
441
0
    return FALSE;
442
0
  #endif
443
#else
444
  if (!g_str_has_suffix (basename, ".dll"))
445
    return FALSE;
446
#endif
447
448
0
  result = TRUE;
449
0
  if (scope)
450
0
    {
451
0
      result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
452
0
      if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
453
0
        g_io_module_scope_block (scope, basename);
454
0
    }
455
456
0
  return result;
457
0
}
458
459
460
/**
461
 * g_io_modules_scan_all_in_directory_with_scope:
462
 * @dirname: (type filename): pathname for a directory containing modules
463
 *     to scan.
464
 * @scope: a scope to use when scanning the modules
465
 *
466
 * Scans all the modules in the specified directory, ensuring that
467
 * any extension point implemented by a module is registered.
468
 *
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().
474
 *
475
 * If you need to guarantee that all types are loaded in all the modules,
476
 * use g_io_modules_load_all_in_directory().
477
 *
478
 * Since: 2.30
479
 **/
480
void
481
g_io_modules_scan_all_in_directory_with_scope (const char     *dirname,
482
                                               GIOModuleScope *scope)
483
1
{
484
1
  const gchar *name;
485
1
  char *filename;
486
1
  GDir *dir;
487
1
  GStatBuf statbuf;
488
1
  char *data;
489
1
  time_t cache_time;
490
1
  GHashTable *cache;
491
492
1
  if (!g_module_supported ())
493
0
    return;
494
495
1
  dir = g_dir_open (dirname, 0, NULL);
496
1
  if (!dir)
497
1
    return;
498
499
0
  filename = g_build_filename (dirname, "giomodule.cache", NULL);
500
501
0
  cache = NULL;
502
0
  cache_time = 0;
503
0
  if (g_stat (filename, &statbuf) == 0 &&
504
0
      g_file_get_contents (filename, &data, NULL, NULL))
505
0
    {
506
0
      char **lines;
507
0
      int i;
508
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.
512
       *
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.
518
       */
519
0
      cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
520
521
0
      lines = g_strsplit (data, "\n", -1);
522
0
      g_free (data);
523
524
0
      for (i = 0;  lines[i] != NULL; i++)
525
0
  {
526
0
    char *line = lines[i];
527
0
    char *file;
528
0
    char *colon;
529
0
    char **strv_extension_points;
530
531
0
    if (line[0] == '#')
532
0
      continue;
533
534
0
    colon = strchr (line, ':');
535
0
    if (colon == NULL || line == colon)
536
0
      continue; /* Invalid line, ignore */
537
538
0
    *colon = 0; /* terminate filename */
539
0
    file = g_strdup (line);
540
0
    colon++; /* after colon */
541
542
0
    while (g_ascii_isspace (*colon))
543
0
      colon++;
544
545
0
          if (G_UNLIKELY (!cache))
546
0
            cache = g_hash_table_new_full (g_str_hash, g_str_equal,
547
0
                                           g_free, (GDestroyNotify)g_strfreev);
548
549
0
    strv_extension_points = g_strsplit (colon, ",", -1);
550
0
    g_hash_table_insert (cache, file, strv_extension_points);
551
0
  }
552
0
      g_strfreev (lines);
553
0
    }
554
555
0
  while ((name = g_dir_read_name (dir)))
556
0
    {
557
0
      if (is_valid_module_name (name, scope))
558
0
  {
559
0
    GIOExtensionPoint *extension_point;
560
0
    GIOModule *module;
561
0
    gchar *path;
562
0
    char **strv_extension_points = NULL;
563
0
    int i;
564
565
0
    path = g_build_filename (dirname, name, NULL);
566
0
    module = g_io_module_new (path);
567
568
0
          if (cache)
569
0
            strv_extension_points = g_hash_table_lookup (cache, name);
570
571
0
    if (strv_extension_points != NULL &&
572
0
        g_stat (path, &statbuf) == 0 &&
573
0
        statbuf.st_ctime <= cache_time)
574
0
      {
575
        /* Lazy load/init the library when first required */
576
0
        for (i = 0; strv_extension_points[i] != NULL; i++)
577
0
    {
578
0
      extension_point =
579
0
        g_io_extension_point_register (strv_extension_points[i]);
580
0
      extension_point->lazy_load_modules =
581
0
        g_list_prepend (extension_point->lazy_load_modules,
582
0
            module);
583
0
    }
584
0
      }
585
0
    else
586
0
      {
587
        /* Try to load and init types */
588
0
        if (g_type_module_use (G_TYPE_MODULE (module)))
589
0
    {
590
0
      g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
591
      /* module must remain alive, because the type system keeps weak refs */
592
0
      g_ignore_leak (module);
593
0
    }
594
0
        else
595
0
    {
596
0
      g_printerr ("Failed to load module: %s\n", path);
597
0
      g_object_unref (module);
598
0
    }
599
0
      }
600
601
0
    g_free (path);
602
0
  }
603
0
    }
604
605
0
  g_dir_close (dir);
606
607
0
  if (cache)
608
0
    g_hash_table_destroy (cache);
609
610
0
  g_free (filename);
611
0
}
612
613
/**
614
 * g_io_modules_scan_all_in_directory:
615
 * @dirname: (type filename): pathname for a directory containing modules
616
 *     to scan.
617
 *
618
 * Scans all the modules in the specified directory, ensuring that
619
 * any extension point implemented by a module is registered.
620
 *
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().
626
 *
627
 * If you need to guarantee that all types are loaded in all the modules,
628
 * use g_io_modules_load_all_in_directory().
629
 *
630
 * Since: 2.24
631
 **/
632
void
633
g_io_modules_scan_all_in_directory (const char *dirname)
634
0
{
635
0
  g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
636
0
}
637
638
/**
639
 * g_io_modules_load_all_in_directory_with_scope:
640
 * @dirname: (type filename): pathname for a directory containing modules
641
 *     to load.
642
 * @scope: a scope to use when scanning the modules.
643
 *
644
 * Loads all the modules in the specified directory.
645
 *
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.
649
 *
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().
656
 *
657
 * Since: 2.30
658
 **/
659
GList *
660
g_io_modules_load_all_in_directory_with_scope (const char     *dirname,
661
                                               GIOModuleScope *scope)
662
0
{
663
0
  const gchar *name;
664
0
  GDir        *dir;
665
0
  GList *modules;
666
667
0
  if (!g_module_supported ())
668
0
    return NULL;
669
670
0
  dir = g_dir_open (dirname, 0, NULL);
671
0
  if (!dir)
672
0
    return NULL;
673
674
0
  modules = NULL;
675
0
  while ((name = g_dir_read_name (dir)))
676
0
    {
677
0
      if (is_valid_module_name (name, scope))
678
0
        {
679
0
          GIOModule *module;
680
0
          gchar     *path;
681
682
0
          path = g_build_filename (dirname, name, NULL);
683
0
          module = g_io_module_new (path);
684
685
0
          if (!g_type_module_use (G_TYPE_MODULE (module)))
686
0
            {
687
0
              g_printerr ("Failed to load module: %s\n", path);
688
0
              g_object_unref (module);
689
0
              g_free (path);
690
0
              continue;
691
0
            }
692
    
693
0
          g_free (path);
694
695
0
          modules = g_list_prepend (modules, module);
696
0
        }
697
0
    }
698
  
699
0
  g_dir_close (dir);
700
701
0
  return modules;
702
0
}
703
704
/**
705
 * g_io_modules_load_all_in_directory:
706
 * @dirname: (type filename): pathname for a directory containing modules
707
 *     to load.
708
 *
709
 * Loads all the modules in the specified directory.
710
 *
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.
714
 *
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().
721
 **/
722
GList *
723
g_io_modules_load_all_in_directory (const char *dirname)
724
0
{
725
0
  return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
726
0
}
727
728
static gpointer
729
try_class (GIOExtension *extension,
730
           guint         is_supported_offset)
731
0
{
732
0
  GType type = g_io_extension_get_type (extension);
733
0
  typedef gboolean (*verify_func) (void);
734
0
  gpointer class;
735
736
0
  class = g_type_class_ref (type);
737
0
  if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
738
0
    return class;
739
740
0
  g_type_class_unref (class);
741
0
  return NULL;
742
0
}
743
744
static void
745
print_help (const char        *envvar,
746
            GIOExtensionPoint *ep)
747
0
{
748
0
  g_print ("Supported arguments for %s environment variable:\n", envvar);
749
750
0
  if (g_io_extension_point_get_extensions (ep) == NULL)
751
0
    g_print (" (none)\n");
752
0
  else
753
0
    {
754
0
      GList *l;
755
0
      GIOExtension *extension;
756
0
      gsize width = 0;
757
758
0
      for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
759
0
        {
760
0
          extension = l->data;
761
0
          width = MAX (width, strlen (g_io_extension_get_name (extension)));
762
0
        }
763
764
0
      for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
765
0
        {
766
0
          extension = l->data;
767
768
0
          g_print (" %*s - %d\n", (int) MIN (width, G_MAXINT),
769
0
                   g_io_extension_get_name (extension),
770
0
                   g_io_extension_get_priority (extension));
771
0
        }
772
0
    }
773
0
}
774
775
/**
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
781
 *
782
 * Retrieves the default class implementing @extension_point.
783
 *
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.
788
 *
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.
793
 *
794
 * The result is cached after it is generated the first time, and
795
 * the function is thread-safe.
796
 *
797
 * Returns: (transfer none): the type to instantiate to implement
798
 *     @extension_point, or %G_TYPE_INVALID if there are no usable
799
 *     implementations.
800
 */
801
GType
802
_g_io_module_get_default_type (const gchar *extension_point,
803
                               const gchar *envvar,
804
                               guint        is_supported_offset)
805
0
{
806
0
  static GRecMutex default_modules_lock;
807
0
  static GHashTable *default_modules;
808
0
  const char *use_this;
809
0
  GList *l;
810
0
  GIOExtensionPoint *ep;
811
0
  GIOExtension *extension, *preferred;
812
0
  gpointer impl;
813
814
0
  g_rec_mutex_lock (&default_modules_lock);
815
0
  if (default_modules)
816
0
    {
817
0
      gpointer key;
818
819
0
      if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
820
0
        {
821
0
          g_rec_mutex_unlock (&default_modules_lock);
822
0
          return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
823
0
        }
824
0
    }
825
0
  else
826
0
    {
827
0
      default_modules = g_hash_table_new (g_str_hash, g_str_equal);
828
0
    }
829
830
0
  _g_io_modules_ensure_loaded ();
831
0
  ep = g_io_extension_point_lookup (extension_point);
832
833
0
  if (!ep)
834
0
    {
835
0
      g_warn_if_reached ();
836
0
      g_rec_mutex_unlock (&default_modules_lock);
837
0
      return G_TYPE_INVALID;
838
0
    }
839
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
0
  use_this = envvar ? g_getenv (envvar) : NULL;
844
0
  if (g_strcmp0 (use_this, "help") == 0)
845
0
    {
846
0
      print_help (envvar, ep);
847
0
      use_this = NULL;
848
0
    }
849
850
0
  if (use_this)
851
0
    {
852
0
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
853
0
      if (preferred)
854
0
        {
855
0
          impl = try_class (preferred, is_supported_offset);
856
0
          if (impl)
857
0
            goto done;
858
0
        }
859
0
      else
860
0
        g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
861
0
    }
862
0
  else
863
0
    preferred = NULL;
864
865
0
  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
866
0
    {
867
0
      extension = l->data;
868
0
      if (extension == preferred)
869
0
        continue;
870
871
0
      impl = try_class (extension, is_supported_offset);
872
0
      if (impl)
873
0
        goto done;
874
0
    }
875
876
0
  impl = NULL;
877
878
0
 done:
879
0
  g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
880
0
  g_rec_mutex_unlock (&default_modules_lock);
881
882
0
  return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
883
0
}
884
885
static gpointer
886
try_implementation (const char           *extension_point,
887
                    GIOExtension         *extension,
888
        GIOModuleVerifyFunc   verify_func)
889
1
{
890
1
  GType type = g_io_extension_get_type (extension);
891
1
  gpointer impl;
892
893
1
  if (g_type_is_a (type, G_TYPE_INITABLE))
894
0
    {
895
0
      GError *error = NULL;
896
897
0
      impl = g_initable_new (type, NULL, &error, NULL);
898
0
      if (impl)
899
0
        return impl;
900
901
0
      g_debug ("Failed to initialize %s (%s) for %s: %s",
902
0
               g_io_extension_get_name (extension),
903
0
               g_type_name (type),
904
0
               extension_point,
905
0
               error ? error->message : "");
906
0
      g_clear_error (&error);
907
0
      return NULL;
908
0
    }
909
1
  else
910
1
    {
911
1
      impl = g_object_new (type, NULL);
912
1
      if (!verify_func || verify_func (impl))
913
1
  return impl;
914
915
0
      g_object_unref (impl);
916
0
      return NULL;
917
1
    }
918
1
}
919
920
static void
921
weak_ref_free (GWeakRef *weak_ref)
922
0
{
923
0
  g_weak_ref_clear (weak_ref);
924
0
  g_free (weak_ref);
925
0
}
926
927
/**
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.
934
 *
935
 * Retrieves the default object implementing @extension_point.
936
 *
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.
941
 *
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.
947
 *
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.
951
 *
952
 * Returns: (transfer full) (nullable): an object implementing
953
 *     @extension_point, or %NULL if there are no usable
954
 *     implementations.
955
 */
956
gpointer
957
_g_io_module_get_default (const gchar         *extension_point,
958
        const gchar         *envvar,
959
        GIOModuleVerifyFunc  verify_func)
960
1
{
961
1
  static GRecMutex default_modules_lock;
962
1
  static GHashTable *default_modules;
963
1
  const char *use_this;
964
1
  GList *l;
965
1
  GIOExtensionPoint *ep;
966
1
  GIOExtension *extension = NULL, *preferred;
967
1
  gpointer impl, value;
968
1
  GWeakRef *impl_weak_ref = NULL;
969
970
1
  g_rec_mutex_lock (&default_modules_lock);
971
1
  if (default_modules)
972
0
    {
973
0
      if (g_hash_table_lookup_extended (default_modules, extension_point,
974
0
                                        NULL, &value))
975
0
        {
976
          /* Don’t debug here, since we’re returning a cached object which was
977
           * already printed earlier. */
978
0
          impl_weak_ref = value;
979
0
          impl = g_weak_ref_get (impl_weak_ref);
980
981
          /* If the object has been finalised (impl == NULL), fall through and
982
           * instantiate a new one. */
983
0
          if (impl != NULL)
984
0
            {
985
0
              g_rec_mutex_unlock (&default_modules_lock);
986
0
              return g_steal_pointer (&impl);
987
0
            }
988
0
        }
989
0
    }
990
1
  else
991
1
    {
992
1
      default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
993
1
                                               g_free, (GDestroyNotify) weak_ref_free);
994
1
    }
995
996
1
  _g_io_modules_ensure_loaded ();
997
1
  ep = g_io_extension_point_lookup (extension_point);
998
999
1
  if (!ep)
1000
0
    {
1001
0
      g_debug ("%s: Failed to find extension point ‘%s’",
1002
0
               G_STRFUNC, extension_point);
1003
0
      g_warn_if_reached ();
1004
0
      g_rec_mutex_unlock (&default_modules_lock);
1005
0
      return NULL;
1006
0
    }
1007
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
1
  use_this = envvar ? g_getenv (envvar) : NULL;
1012
1
  if (g_strcmp0 (use_this, "help") == 0)
1013
0
    {
1014
0
      print_help (envvar, ep);
1015
0
      use_this = NULL;
1016
0
    }
1017
1018
1
  if (use_this)
1019
0
    {
1020
0
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
1021
0
      if (preferred)
1022
0
  {
1023
0
    impl = try_implementation (extension_point, preferred, verify_func);
1024
0
    extension = preferred;
1025
0
    if (impl)
1026
0
      goto done;
1027
0
  }
1028
0
      else
1029
0
        g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
1030
0
    }
1031
1
  else
1032
1
    preferred = NULL;
1033
1034
1
  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1035
1
    {
1036
1
      extension = l->data;
1037
1
      if (extension == preferred)
1038
0
  continue;
1039
1040
1
      impl = try_implementation (extension_point, extension, verify_func);
1041
1
      if (impl)
1042
1
  goto done;
1043
1
    }
1044
1045
0
  impl = NULL;
1046
1047
1
 done:
1048
1
  if (impl_weak_ref == NULL)
1049
1
    {
1050
1
      impl_weak_ref = g_new0 (GWeakRef, 1);
1051
1
      g_weak_ref_init (impl_weak_ref, impl);
1052
1
      g_hash_table_insert (default_modules, g_strdup (extension_point),
1053
1
                           g_steal_pointer (&impl_weak_ref));
1054
1
    }
1055
0
  else
1056
0
    {
1057
0
      g_weak_ref_set (impl_weak_ref, impl);
1058
0
    }
1059
1060
1
  g_rec_mutex_unlock (&default_modules_lock);
1061
1062
1
  if (impl != NULL)
1063
1
    {
1064
1
      g_assert (extension != NULL);
1065
1
      g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
1066
1
               G_STRFUNC, g_io_extension_get_name (extension),
1067
1
               G_OBJECT_TYPE_NAME (impl), extension_point);
1068
1
    }
1069
0
  else
1070
0
    g_debug ("%s: Failed to find default implementation for ‘%s’",
1071
1
             G_STRFUNC, extension_point);
1072
1073
1
  return g_steal_pointer (&impl);
1074
1
}
1075
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);
1079
1080
extern GType _g_unix_volume_monitor_get_type (void);
1081
extern GType _g_local_vfs_get_type (void);
1082
1083
extern GType _g_win32_volume_monitor_get_type (void);
1084
extern GType _g_winhttp_vfs_get_type (void);
1085
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);
1089
#ifdef HAVE_NETLINK
1090
extern GType _g_network_monitor_netlink_get_type (void);
1091
extern GType _g_network_monitor_nm_get_type (void);
1092
#endif
1093
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);
1099
1100
#ifdef G_OS_UNIX
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);
1106
#endif
1107
1108
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1109
extern GType g_cocoa_notification_backend_get_type (void);
1110
#endif
1111
1112
#ifdef G_PLATFORM_WIN32
1113
extern GType g_win32_notification_backend_get_type (void);
1114
1115
#include <windows.h>
1116
extern GType _g_win32_network_monitor_get_type (void);
1117
1118
static HMODULE gio_dll = NULL;
1119
1120
#ifndef GLIB_STATIC_COMPILATION
1121
1122
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1123
                     DWORD     fdwReason,
1124
                     LPVOID    lpvReserved);
1125
1126
BOOL WINAPI
1127
DllMain (HINSTANCE hinstDLL,
1128
   DWORD     fdwReason,
1129
   LPVOID    lpvReserved)
1130
{
1131
  if (fdwReason == DLL_PROCESS_ATTACH)
1132
    {
1133
      gio_dll = hinstDLL;
1134
      gio_win32_appinfo_init (FALSE);
1135
    }
1136
1137
  return TRUE;
1138
}
1139
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);
1143
1144
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
1145
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(giomodule_init_ctor)
1146
#endif
1147
1148
G_DEFINE_CONSTRUCTOR (giomodule_init_ctor)
1149
1150
static void
1151
giomodule_init_ctor (void)
1152
{
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.
1157
   *
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.
1164
   */
1165
  glib_win32_init ();
1166
  gobject_win32_init ();
1167
  gio_win32_appinfo_init (FALSE);
1168
}
1169
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 */
1173
1174
void *
1175
_g_io_win32_get_module (void)
1176
{
1177
  if (!gio_dll)
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,
1181
                        &gio_dll);
1182
  return gio_dll;
1183
}
1184
1185
#endif /* G_PLATFORM_WIN32 */
1186
1187
void
1188
_g_io_modules_ensure_extension_points_registered (void)
1189
25
{
1190
25
  static gsize registered_extensions = FALSE;
1191
25
  GIOExtensionPoint *ep;
1192
1193
25
  if (g_once_init_enter (&registered_extensions))
1194
1
    {
1195
1
#if defined(G_OS_UNIX) && !defined(__APPLE__)
1196
1
#if !GLIB_CHECK_VERSION (3, 0, 0)
1197
1
      ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1198
1
      g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1199
1
#endif
1200
1
#endif
1201
1202
1
      ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1203
1
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1204
1205
1
      ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1206
1
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1207
1208
1
      ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1209
1
      g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1210
      
1211
1
      ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1212
1
      g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1213
      
1214
1
      ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1215
1
      g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1216
1217
1
      ep = g_io_extension_point_register ("gsettings-backend");
1218
1
      g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1219
1220
1
      ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1221
1
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1222
1223
1
      ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1224
1
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1225
1226
1
      ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1227
1
      g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1228
1229
1
      ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1230
1
      g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1231
1232
1
      ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1233
1
      g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1234
1235
1
      ep = g_io_extension_point_register (G_DEBUG_CONTROLLER_EXTENSION_POINT_NAME);
1236
1
      g_io_extension_point_set_required_type (ep, G_TYPE_DEBUG_CONTROLLER);
1237
1238
1
      ep = g_io_extension_point_register (G_MEMORY_MONITOR_EXTENSION_POINT_NAME);
1239
1
      g_io_extension_point_set_required_type (ep, G_TYPE_MEMORY_MONITOR);
1240
1241
1
      ep = g_io_extension_point_register (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME);
1242
1
      g_io_extension_point_set_required_type (ep, G_TYPE_POWER_PROFILE_MONITOR);
1243
1244
1
      g_once_init_leave (&registered_extensions, TRUE);
1245
1
    }
1246
25
}
1247
1248
static gchar *
1249
get_gio_module_dir (void)
1250
1
{
1251
1
  gchar *module_dir;
1252
1
  gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1253
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).
1257
   * Don’t allow it.
1258
   *
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
1
  module_dir = !is_setuid ? g_strdup (g_getenv ("GIO_MODULE_DIR")) : NULL;
1262
1
  if (module_dir == NULL)
1263
1
    {
1264
#ifdef G_OS_WIN32
1265
      gchar *install_dir;
1266
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",
1270
                                     NULL);
1271
      g_free (install_dir);
1272
#else
1273
1
      module_dir = g_strdup (GIO_MODULE_DIR);
1274
#ifdef __APPLE__
1275
#include "TargetConditionals.h"
1276
#if TARGET_OS_OSX
1277
#include <dlfcn.h>
1278
      {
1279
        g_autofree gchar *path = NULL;
1280
        g_autofree gchar *possible_dir = NULL;
1281
        Dl_info info;
1282
1283
        if (dladdr (get_gio_module_dir, &info))
1284
          {
1285
            /* Gets path to the PREFIX/lib directory */
1286
            path = g_path_get_dirname (info.dli_fname);
1287
            possible_dir = g_build_filename (path, "gio", "modules", NULL);
1288
            if (g_file_test (possible_dir, G_FILE_TEST_IS_DIR))
1289
              {
1290
                g_free (module_dir);
1291
                module_dir = g_steal_pointer (&possible_dir);
1292
              }
1293
          }
1294
      }
1295
#endif
1296
#endif
1297
1
#endif
1298
1
    }
1299
1300
1
  return module_dir;
1301
1
}
1302
1303
void
1304
_g_io_modules_ensure_loaded (void)
1305
1
{
1306
1
  static gsize loaded_dirs = FALSE;
1307
1
  const char *module_path;
1308
1
  GIOModuleScope *scope;
1309
1310
1
  _g_io_modules_ensure_extension_points_registered ();
1311
1312
1
  if (g_once_init_enter (&loaded_dirs))
1313
1
    {
1314
1
      gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1315
1
      gchar *module_dir;
1316
1317
1
      scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1318
1319
      /* First load any overrides, extras (but not if running as setuid!) */
1320
1
      module_path = !is_setuid ? g_getenv ("GIO_EXTRA_MODULES") : NULL;
1321
1
      if (module_path)
1322
0
  {
1323
0
    gchar **paths;
1324
0
    int i;
1325
1326
0
    paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1327
1328
0
    for (i = 0; paths[i] != NULL; i++)
1329
0
      {
1330
0
        g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1331
0
      }
1332
1333
0
    g_strfreev (paths);
1334
0
  }
1335
1336
      /* Then load the compiled in path */
1337
1
      module_dir = get_gio_module_dir ();
1338
1339
1
      g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1340
1
      g_free (module_dir);
1341
1342
1
      g_io_module_scope_free (scope);
1343
1344
      /* Initialize types from built-in "modules" */
1345
1
      g_type_ensure (g_null_settings_backend_get_type ());
1346
1
      g_type_ensure (g_memory_settings_backend_get_type ());
1347
1
      g_type_ensure (g_keyfile_settings_backend_get_type ());
1348
1
      g_type_ensure (g_power_profile_monitor_dbus_get_type ());
1349
1
#if defined(HAVE_INOTIFY_INIT1)
1350
1
      g_type_ensure (g_inotify_file_monitor_get_type ());
1351
1
#endif
1352
#if defined(HAVE_KQUEUE)
1353
      g_type_ensure (g_kqueue_file_monitor_get_type ());
1354
#endif
1355
#ifdef G_OS_WIN32
1356
      g_type_ensure (_g_win32_volume_monitor_get_type ());
1357
      g_type_ensure (g_win32_file_monitor_get_type ());
1358
      g_type_ensure (g_registry_backend_get_type ());
1359
#endif
1360
#ifdef HAVE_COCOA
1361
      g_type_ensure (g_nextstep_settings_backend_get_type ());
1362
      g_type_ensure (g_osx_app_info_get_type ());
1363
#endif
1364
1
#ifdef G_OS_UNIX
1365
1
      g_type_ensure (_g_unix_volume_monitor_get_type ());
1366
1
      g_type_ensure (g_debug_controller_dbus_get_type ());
1367
1
      g_type_ensure (g_fdo_notification_backend_get_type ());
1368
1
      g_type_ensure (g_gtk_notification_backend_get_type ());
1369
1
      g_type_ensure (g_portal_notification_backend_get_type ());
1370
1
      g_type_ensure (g_memory_monitor_dbus_get_type ());
1371
1
      g_type_ensure (g_memory_monitor_portal_get_type ());
1372
1
      g_type_ensure (g_network_monitor_portal_get_type ());
1373
1
      g_type_ensure (g_power_profile_monitor_portal_get_type ());
1374
1
      g_type_ensure (g_proxy_resolver_portal_get_type ());
1375
1
#endif
1376
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1377
      g_type_ensure (g_cocoa_notification_backend_get_type ());
1378
#endif
1379
#ifdef G_OS_WIN32
1380
      g_type_ensure (g_win32_notification_backend_get_type ());
1381
      g_type_ensure (_g_winhttp_vfs_get_type ());
1382
      g_type_ensure (g_memory_monitor_win32_get_type ());
1383
#endif
1384
1
      g_type_ensure (_g_local_vfs_get_type ());
1385
1
      g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1386
1
      g_type_ensure (_g_http_proxy_get_type ());
1387
1
      g_type_ensure (_g_https_proxy_get_type ());
1388
1
      g_type_ensure (_g_socks4a_proxy_get_type ());
1389
1
      g_type_ensure (_g_socks4_proxy_get_type ());
1390
1
      g_type_ensure (_g_socks5_proxy_get_type ());
1391
1
      g_type_ensure (_g_dummy_tls_backend_get_type ());
1392
1
      g_type_ensure (g_network_monitor_base_get_type ());
1393
1
#ifdef HAVE_NETLINK
1394
1
      g_type_ensure (_g_network_monitor_netlink_get_type ());
1395
1
      g_type_ensure (_g_network_monitor_nm_get_type ());
1396
1
#endif
1397
#ifdef G_OS_WIN32
1398
      g_type_ensure (_g_win32_network_monitor_get_type ());
1399
#endif
1400
1401
1
      g_once_init_leave (&loaded_dirs, TRUE);
1402
1
    }
1403
1
}
1404
1405
static void
1406
g_io_extension_point_free (GIOExtensionPoint *ep)
1407
0
{
1408
0
  g_free (ep->name);
1409
0
  g_free (ep);
1410
0
}
1411
1412
/**
1413
 * g_io_extension_point_register:
1414
 * @name: The name of the extension point
1415
 *
1416
 * Registers an extension point.
1417
 *
1418
 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1419
 *    owned by GIO and should not be freed.
1420
 */
1421
GIOExtensionPoint *
1422
g_io_extension_point_register (const char *name)
1423
15
{
1424
15
  GIOExtensionPoint *ep;
1425
  
1426
15
  G_LOCK (extension_points);
1427
15
  if (extension_points == NULL)
1428
1
    extension_points = g_hash_table_new_full (g_str_hash,
1429
1
                g_str_equal,
1430
1
                NULL,
1431
1
                (GDestroyNotify)g_io_extension_point_free);
1432
1433
15
  ep = g_hash_table_lookup (extension_points, name);
1434
15
  if (ep != NULL)
1435
0
    {
1436
0
      G_UNLOCK (extension_points);
1437
0
      return ep;
1438
0
    }
1439
1440
15
  ep = g_new0 (GIOExtensionPoint, 1);
1441
15
  ep->name = g_strdup (name);
1442
  
1443
15
  g_hash_table_insert (extension_points, ep->name, ep);
1444
  
1445
15
  G_UNLOCK (extension_points);
1446
1447
15
  return ep;
1448
15
}
1449
1450
/**
1451
 * g_io_extension_point_lookup:
1452
 * @name: the name of the extension point
1453
 *
1454
 * Looks up an existing extension point.
1455
 *
1456
 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1457
 *    is no registered extension point with the given name.
1458
 */
1459
GIOExtensionPoint *
1460
g_io_extension_point_lookup (const char *name)
1461
27
{
1462
27
  GIOExtensionPoint *ep;
1463
1464
27
  G_LOCK (extension_points);
1465
27
  ep = NULL;
1466
27
  if (extension_points != NULL)
1467
27
    ep = g_hash_table_lookup (extension_points, name);
1468
  
1469
27
  G_UNLOCK (extension_points);
1470
1471
27
  return ep;
1472
  
1473
27
}
1474
1475
/**
1476
 * g_io_extension_point_set_required_type:
1477
 * @extension_point: a #GIOExtensionPoint
1478
 * @type: the #GType to require
1479
 *
1480
 * Sets the required type for @extension_point to @type. 
1481
 * All implementations must henceforth have this type.
1482
 */
1483
void
1484
g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1485
          GType              type)
1486
15
{
1487
15
  extension_point->required_type = type;
1488
15
}
1489
1490
/**
1491
 * g_io_extension_point_get_required_type:
1492
 * @extension_point: a #GIOExtensionPoint
1493
 *
1494
 * Gets the required type for @extension_point.
1495
 *
1496
 * Returns: the #GType that all implementations must have, 
1497
 *   or %G_TYPE_INVALID if the extension point has no required type
1498
 */
1499
GType
1500
g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1501
0
{
1502
0
  return extension_point->required_type;
1503
0
}
1504
1505
static void
1506
lazy_load_modules (GIOExtensionPoint *extension_point)
1507
1
{
1508
1
  GIOModule *module;
1509
1
  GList *l;
1510
1511
1
  for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1512
0
    {
1513
0
      module = l->data;
1514
1515
0
      if (!module->initialized)
1516
0
  {
1517
0
    if (g_type_module_use (G_TYPE_MODULE (module)))
1518
0
      g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1519
0
    else
1520
0
      g_printerr ("Failed to load module: %s\n",
1521
0
      module->filename);
1522
0
  }
1523
0
    }
1524
1
}
1525
1526
/**
1527
 * g_io_extension_point_get_extensions:
1528
 * @extension_point: a #GIOExtensionPoint
1529
 *
1530
 * Gets a list of all extensions that implement this extension point.
1531
 * The list is sorted by priority, beginning with the highest priority.
1532
 *
1533
 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1534
 *     #GIOExtensions. The list is owned by GIO and should not be
1535
 *     modified.
1536
 */
1537
GList *
1538
g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1539
1
{
1540
1
  g_return_val_if_fail (extension_point != NULL, NULL);
1541
1542
1
  lazy_load_modules (extension_point);
1543
1
  return extension_point->extensions;
1544
1
}
1545
1546
/**
1547
 * g_io_extension_point_get_extension_by_name:
1548
 * @extension_point: a #GIOExtensionPoint
1549
 * @name: the name of the extension to get
1550
 *
1551
 * Finds a #GIOExtension for an extension point by name.
1552
 *
1553
 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1554
 *    given name, or %NULL if there is no extension with that name
1555
 */
1556
GIOExtension *
1557
g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1558
              const char        *name)
1559
0
{
1560
0
  GList *l;
1561
1562
0
  g_return_val_if_fail (name != NULL, NULL);
1563
1564
0
  lazy_load_modules (extension_point);
1565
0
  for (l = extension_point->extensions; l != NULL; l = l->next)
1566
0
    {
1567
0
      GIOExtension *e = l->data;
1568
1569
0
      if (e->name != NULL &&
1570
0
    strcmp (e->name, name) == 0)
1571
0
  return e;
1572
0
    }
1573
  
1574
0
  return NULL;
1575
0
}
1576
1577
static gint
1578
extension_prio_compare (gconstpointer  a,
1579
      gconstpointer  b)
1580
16
{
1581
16
  const GIOExtension *extension_a = a, *extension_b = b;
1582
1583
16
  if (extension_a->priority > extension_b->priority)
1584
7
    return -1;
1585
1586
9
  if (extension_b->priority > extension_a->priority)
1587
3
    return 1;
1588
1589
6
  return 0;
1590
9
}
1591
1592
/**
1593
 * g_io_extension_point_implement:
1594
 * @extension_point_name: the name of the extension point
1595
 * @type: the #GType to register as extension 
1596
 * @extension_name: the name for the extension
1597
 * @priority: the priority for the extension
1598
 *
1599
 * Registers @type as extension for the extension point with name
1600
 * @extension_point_name. 
1601
 *
1602
 * If @type has already been registered as an extension for this 
1603
 * extension point, the existing #GIOExtension object is returned.
1604
 *
1605
 * Returns: (transfer none): a #GIOExtension object for #GType
1606
 */
1607
GIOExtension *
1608
g_io_extension_point_implement (const char *extension_point_name,
1609
        GType       type,
1610
        const char *extension_name,
1611
        gint        priority)
1612
26
{
1613
26
  GIOExtensionPoint *extension_point;
1614
26
  GIOExtension *extension;
1615
26
  GList *l;
1616
1617
26
  g_return_val_if_fail (extension_point_name != NULL, NULL);
1618
1619
26
  extension_point = g_io_extension_point_lookup (extension_point_name);
1620
26
  if (extension_point == NULL)
1621
0
    {
1622
0
      g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1623
0
      return NULL;
1624
0
    }
1625
  
1626
26
  if (extension_point->required_type != 0 &&
1627
26
      !g_type_is_a (type, extension_point->required_type))
1628
0
    {
1629
0
      g_warning ("Tried to register an extension of the type %s to extension point %s. "
1630
0
     "Expected type is %s.",
1631
0
     g_type_name (type),
1632
0
     extension_point_name, 
1633
0
     g_type_name (extension_point->required_type));
1634
0
      return NULL;
1635
0
    }      
1636
1637
  /* It's safe to register the same type multiple times */
1638
51
  for (l = extension_point->extensions; l != NULL; l = l->next)
1639
25
    {
1640
25
      extension = l->data;
1641
25
      if (extension->type == type)
1642
0
  return extension;
1643
25
    }
1644
  
1645
26
  extension = g_slice_new0 (GIOExtension);
1646
26
  extension->type = type;
1647
26
  extension->name = g_strdup (extension_name);
1648
26
  extension->priority = priority;
1649
  
1650
26
  extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1651
26
                  extension, extension_prio_compare);
1652
  
1653
26
  return extension;
1654
26
}
1655
1656
/**
1657
 * g_io_extension_ref_class:
1658
 * @extension: a #GIOExtension
1659
 *
1660
 * Gets a reference to the class for the type that is 
1661
 * associated with @extension.
1662
 *
1663
 * Returns: (transfer full): the #GTypeClass for the type of @extension
1664
 */
1665
GTypeClass *
1666
g_io_extension_ref_class (GIOExtension *extension)
1667
0
{
1668
0
  return g_type_class_ref (extension->type);
1669
0
}
1670
1671
/**
1672
 * g_io_extension_get_type:
1673
 * @extension: a #GIOExtension
1674
 *
1675
 * Gets the type associated with @extension.
1676
 *
1677
 * Returns: the type of @extension
1678
 */
1679
GType
1680
g_io_extension_get_type (GIOExtension *extension)
1681
1
{
1682
1
  return extension->type;
1683
1
}
1684
1685
/**
1686
 * g_io_extension_get_name:
1687
 * @extension: a #GIOExtension
1688
 *
1689
 * Gets the name under which @extension was registered.
1690
 *
1691
 * Note that the same type may be registered as extension
1692
 * for multiple extension points, under different names.
1693
 *
1694
 * Returns: the name of @extension.
1695
 */
1696
const char *
1697
g_io_extension_get_name (GIOExtension *extension)
1698
1
{
1699
1
  return extension->name;
1700
1
}
1701
1702
/**
1703
 * g_io_extension_get_priority:
1704
 * @extension: a #GIOExtension
1705
 *
1706
 * Gets the priority with which @extension was registered.
1707
 *
1708
 * Returns: the priority of @extension
1709
 */
1710
gint
1711
g_io_extension_get_priority (GIOExtension *extension)
1712
0
{
1713
0
  return extension->priority;
1714
0
}