Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/giomodule.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Author: Alexander Larsson <alexl@redhat.com>
19
 */
20
21
#include "config.h"
22
23
/* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
24
 * in the preprocessor, we need to define this before including glib.h*/
25
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
26
#define GLIB_DISABLE_DEPRECATION_WARNINGS
27
#endif
28
29
#include <string.h>
30
31
#include "giomodule.h"
32
#include "giomodule-priv.h"
33
#include "glib-private.h"
34
#include "glocalfilemonitor.h"
35
#include "gnativevolumemonitor.h"
36
#include "gproxyresolver.h"
37
#include "gproxy.h"
38
#include "gsettingsbackendinternal.h"
39
#include "ghttpproxy.h"
40
#include "gsocks4proxy.h"
41
#include "gsocks4aproxy.h"
42
#include "gsocks5proxy.h"
43
#include "gtlsbackend.h"
44
#include "gvfs.h"
45
#include "gnotificationbackend.h"
46
#include "ginitable.h"
47
#include "gnetworkmonitor.h"
48
#include "gmemorymonitor.h"
49
#include "gmemorymonitorportal.h"
50
#include "gmemorymonitordbus.h"
51
#ifdef G_OS_WIN32
52
#include "gregistrysettingsbackend.h"
53
#include "giowin32-priv.h"
54
#endif
55
#include <glib/gstdio.h>
56
57
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
58
#include "gdesktopappinfo.h"
59
#endif
60
#ifdef HAVE_COCOA
61
#include "gosxappinfo.h"
62
#endif
63
64
#ifdef HAVE_COCOA
65
#include <AvailabilityMacros.h>
66
#endif
67
68
/**
69
 * SECTION:giomodule
70
 * @short_description: Loadable GIO Modules
71
 * @include: gio/gio.h
72
 *
73
 * Provides an interface and default functions for loading and unloading 
74
 * modules. This is used internally to make GIO extensible, but can also
75
 * be used by others to implement module loading.
76
 * 
77
 **/
78
79
/**
80
 * SECTION:extensionpoints
81
 * @short_description: Extension Points
82
 * @include: gio.h
83
 * @see_also: [Extending GIO][extending-gio]
84
 *
85
 * #GIOExtensionPoint provides a mechanism for modules to extend the
86
 * functionality of the library or application that loaded it in an 
87
 * organized fashion.  
88
 *
89
 * An extension point is identified by a name, and it may optionally
90
 * require that any implementation must be of a certain type (or derived
91
 * thereof). Use g_io_extension_point_register() to register an
92
 * extension point, and g_io_extension_point_set_required_type() to
93
 * set a required type.
94
 *
95
 * A module can implement an extension point by specifying the #GType 
96
 * that implements the functionality. Additionally, each implementation
97
 * of an extension point has a name, and a priority. Use
98
 * g_io_extension_point_implement() to implement an extension point.
99
 * 
100
 *  |[<!-- language="C" -->
101
 *  GIOExtensionPoint *ep;
102
 *
103
 *  // Register an extension point
104
 *  ep = g_io_extension_point_register ("my-extension-point");
105
 *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
106
 *  ]|
107
 *
108
 *  |[<!-- language="C" -->
109
 *  // Implement an extension point
110
 *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
111
 *  g_io_extension_point_implement ("my-extension-point",
112
 *                                  my_example_impl_get_type (),
113
 *                                  "my-example",
114
 *                                  10);
115
 *  ]|
116
 *
117
 *  It is up to the code that registered the extension point how
118
 *  it uses the implementations that have been associated with it.
119
 *  Depending on the use case, it may use all implementations, or
120
 *  only the one with the highest priority, or pick a specific
121
 *  one by name.
122
 *
123
 *  To avoid opening all modules just to find out what extension
124
 *  points they implement, GIO makes use of a caching mechanism,
125
 *  see [gio-querymodules][gio-querymodules].
126
 *  You are expected to run this command after installing a
127
 *  GIO module.
128
 *
129
 *  The `GIO_EXTRA_MODULES` environment variable can be used to
130
 *  specify additional directories to automatically load modules
131
 *  from. This environment variable has the same syntax as the
132
 *  `PATH`. If two modules have the same base name in different
133
 *  directories, then the latter one will be ignored. If additional
134
 *  directories are specified GIO will load modules from the built-in
135
 *  directory last.
136
 */
137
138
/**
139
 * GIOModuleScope:
140
 *
141
 * Represents a scope for loading IO modules. A scope can be used for blocking
142
 * duplicate modules, or blocking a module you don't want to load.
143
 *
144
 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
145
 * or g_io_modules_scan_all_in_directory_with_scope().
146
 *
147
 * Since: 2.30
148
 */
149
struct _GIOModuleScope {
150
  GIOModuleScopeFlags flags;
151
  GHashTable *basenames;
152
};
153
154
/**
155
 * g_io_module_scope_new:
156
 * @flags: flags for the new scope
157
 *
158
 * Create a new scope for loading of IO modules. A scope can be used for
159
 * blocking duplicate modules, or blocking a module you don't want to load.
160
 *
161
 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
162
 * which have the same base name as a module that has already been seen
163
 * in this scope.
164
 *
165
 * Returns: (transfer full): the new module scope
166
 *
167
 * Since: 2.30
168
 */
169
GIOModuleScope *
170
g_io_module_scope_new (GIOModuleScopeFlags flags)
171
0
{
172
0
  GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
173
0
  scope->flags = flags;
174
0
  scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
175
0
  return scope;
176
0
}
177
178
/**
179
 * g_io_module_scope_free:
180
 * @scope: a module loading scope
181
 *
182
 * Free a module scope.
183
 *
184
 * Since: 2.30
185
 */
186
void
187
g_io_module_scope_free (GIOModuleScope *scope)
188
0
{
189
0
  if (!scope)
190
0
    return;
191
0
  g_hash_table_destroy (scope->basenames);
192
0
  g_free (scope);
193
0
}
194
195
/**
196
 * g_io_module_scope_block:
197
 * @scope: a module loading scope
198
 * @basename: the basename to block
199
 *
200
 * Block modules with the given @basename from being loaded when
201
 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
202
 * or g_io_modules_load_all_in_directory_with_scope().
203
 *
204
 * Since: 2.30
205
 */
206
void
207
g_io_module_scope_block (GIOModuleScope *scope,
208
                         const gchar    *basename)
209
0
{
210
0
  gchar *key;
211
212
0
  g_return_if_fail (scope != NULL);
213
0
  g_return_if_fail (basename != NULL);
214
215
0
  key = g_strdup (basename);
216
0
  g_hash_table_add (scope->basenames, key);
217
0
}
218
219
static gboolean
220
_g_io_module_scope_contains (GIOModuleScope *scope,
221
                             const gchar    *basename)
222
0
{
223
0
  return g_hash_table_contains (scope->basenames, basename);
224
0
}
225
226
struct _GIOModule {
227
  GTypeModule parent_instance;
228
229
  gchar       *filename;
230
  GModule     *library;
231
  gboolean     initialized; /* The module was loaded at least once */
232
233
  void (* load)   (GIOModule *module);
234
  void (* unload) (GIOModule *module);
235
};
236
237
struct _GIOModuleClass
238
{
239
  GTypeModuleClass parent_class;
240
241
};
242
243
static void      g_io_module_finalize      (GObject      *object);
244
static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
245
static void      g_io_module_unload_module (GTypeModule  *gmodule);
246
247
/**
248
 * GIOExtension:
249
 *
250
 * #GIOExtension is an opaque data structure and can only be accessed
251
 * using the following functions.
252
 */
253
struct _GIOExtension {
254
  char *name;
255
  GType type;
256
  gint priority;
257
};
258
259
/**
260
 * GIOExtensionPoint:
261
 *
262
 * #GIOExtensionPoint is an opaque data structure and can only be accessed
263
 * using the following functions.
264
 */
265
struct _GIOExtensionPoint {
266
  GType required_type;
267
  char *name;
268
  GList *extensions;
269
  GList *lazy_load_modules;
270
};
271
272
static GHashTable *extension_points = NULL;
273
G_LOCK_DEFINE_STATIC(extension_points);
274
275
G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
276
277
static void
278
g_io_module_class_init (GIOModuleClass *class)
279
0
{
280
0
  GObjectClass     *object_class      = G_OBJECT_CLASS (class);
281
0
  GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
282
283
0
  object_class->finalize     = g_io_module_finalize;
284
285
0
  type_module_class->load    = g_io_module_load_module;
286
0
  type_module_class->unload  = g_io_module_unload_module;
287
0
}
288
289
static void
290
g_io_module_init (GIOModule *module)
291
0
{
292
0
}
293
294
static void
295
g_io_module_finalize (GObject *object)
296
0
{
297
0
  GIOModule *module = G_IO_MODULE (object);
298
299
0
  g_free (module->filename);
300
301
0
  G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
302
0
}
303
304
static gboolean
305
load_symbols (GIOModule *module)
306
0
{
307
0
  gchar *name;
308
0
  gchar *load_symname;
309
0
  gchar *unload_symname;
310
0
  gboolean ret;
311
312
0
  name = _g_io_module_extract_name (module->filename);
313
0
  load_symname = g_strconcat ("g_io_", name, "_load", NULL);
314
0
  unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
315
316
0
  ret = g_module_symbol (module->library,
317
0
                         load_symname,
318
0
                         (gpointer) &module->load) &&
319
0
        g_module_symbol (module->library,
320
0
                         unload_symname,
321
0
                         (gpointer) &module->unload);
322
323
0
  if (!ret)
324
0
    {
325
      /* Fallback to old names */
326
0
      ret = g_module_symbol (module->library,
327
0
                             "g_io_module_load",
328
0
                             (gpointer) &module->load) &&
329
0
            g_module_symbol (module->library,
330
0
                             "g_io_module_unload",
331
0
                             (gpointer) &module->unload);
332
0
    }
333
334
0
  g_free (name);
335
0
  g_free (load_symname);
336
0
  g_free (unload_symname);
337
338
0
  return ret;
339
0
}
340
341
static gboolean
342
g_io_module_load_module (GTypeModule *gmodule)
343
0
{
344
0
  GIOModule *module = G_IO_MODULE (gmodule);
345
346
0
  if (!module->filename)
347
0
    {
348
0
      g_warning ("GIOModule path not set");
349
0
      return FALSE;
350
0
    }
351
352
0
  module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
353
354
0
  if (!module->library)
355
0
    {
356
0
      g_printerr ("%s\n", g_module_error ());
357
0
      return FALSE;
358
0
    }
359
360
  /* Make sure that the loaded library contains the required methods */
361
0
  if (!load_symbols (module))
362
0
    {
363
0
      g_printerr ("%s\n", g_module_error ());
364
0
      g_module_close (module->library);
365
366
0
      return FALSE;
367
0
    }
368
369
  /* Initialize the loaded module */
370
0
  module->load (module);
371
0
  module->initialized = TRUE;
372
373
0
  return TRUE;
374
0
}
375
376
static void
377
g_io_module_unload_module (GTypeModule *gmodule)
378
0
{
379
0
  GIOModule *module = G_IO_MODULE (gmodule);
380
381
0
  module->unload (module);
382
383
0
  g_module_close (module->library);
384
0
  module->library = NULL;
385
386
0
  module->load   = NULL;
387
0
  module->unload = NULL;
388
0
}
389
390
/**
391
 * g_io_module_new:
392
 * @filename: (type filename): filename of the shared library module.
393
 * 
394
 * Creates a new GIOModule that will load the specific
395
 * shared library when in use.
396
 * 
397
 * Returns: a #GIOModule from given @filename, 
398
 * or %NULL on error.
399
 **/
400
GIOModule *
401
g_io_module_new (const gchar *filename)
402
0
{
403
0
  GIOModule *module;
404
405
0
  g_return_val_if_fail (filename != NULL, NULL);
406
407
0
  module = g_object_new (G_IO_TYPE_MODULE, NULL);
408
0
  module->filename = g_strdup (filename);
409
410
0
  return module;
411
0
}
412
413
static gboolean
414
is_valid_module_name (const gchar        *basename,
415
                      GIOModuleScope     *scope)
416
0
{
417
0
  gboolean result;
418
419
0
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
420
0
  if (!g_str_has_prefix (basename, "lib") ||
421
0
      !g_str_has_suffix (basename, ".so"))
422
0
    return FALSE;
423
#else
424
  if (!g_str_has_suffix (basename, ".dll"))
425
    return FALSE;
426
#endif
427
428
0
  result = TRUE;
429
0
  if (scope)
430
0
    {
431
0
      result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
432
0
      if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
433
0
        g_io_module_scope_block (scope, basename);
434
0
    }
435
436
0
  return result;
437
0
}
438
439
440
/**
441
 * g_io_modules_scan_all_in_directory_with_scope:
442
 * @dirname: (type filename): pathname for a directory containing modules
443
 *     to scan.
444
 * @scope: a scope to use when scanning the modules
445
 *
446
 * Scans all the modules in the specified directory, ensuring that
447
 * any extension point implemented by a module is registered.
448
 *
449
 * This may not actually load and initialize all the types in each
450
 * module, some modules may be lazily loaded and initialized when
451
 * an extension point it implements is used with e.g.
452
 * g_io_extension_point_get_extensions() or
453
 * g_io_extension_point_get_extension_by_name().
454
 *
455
 * If you need to guarantee that all types are loaded in all the modules,
456
 * use g_io_modules_load_all_in_directory().
457
 *
458
 * Since: 2.30
459
 **/
460
void
461
g_io_modules_scan_all_in_directory_with_scope (const char     *dirname,
462
                                               GIOModuleScope *scope)
463
0
{
464
0
  const gchar *name;
465
0
  char *filename;
466
0
  GDir *dir;
467
0
  GStatBuf statbuf;
468
0
  char *data;
469
0
  time_t cache_time;
470
0
  GHashTable *cache;
471
472
0
  if (!g_module_supported ())
473
0
    return;
474
475
0
  dir = g_dir_open (dirname, 0, NULL);
476
0
  if (!dir)
477
0
    return;
478
479
0
  filename = g_build_filename (dirname, "giomodule.cache", NULL);
480
481
0
  cache = NULL;
482
0
  cache_time = 0;
483
0
  if (g_stat (filename, &statbuf) == 0 &&
484
0
      g_file_get_contents (filename, &data, NULL, NULL))
485
0
    {
486
0
      char **lines;
487
0
      int i;
488
489
      /* cache_time is the time the cache file was created; we also take
490
       * into account the change time because in ostree based systems, all
491
       * system file have mtime equal to epoch 0.
492
       *
493
       * Any file that has a ctime before this was created then and not modified
494
       * since then (userspace can't change ctime). Its possible to change the
495
       * ctime forward without changing the file content, by e.g.  chmoding the
496
       * file, but this is uncommon and will only cause us to not use the cache
497
       * so will not cause bugs.
498
       */
499
0
      cache_time = MAX(statbuf.st_mtime, statbuf.st_ctime);
500
501
0
      lines = g_strsplit (data, "\n", -1);
502
0
      g_free (data);
503
504
0
      for (i = 0;  lines[i] != NULL; i++)
505
0
  {
506
0
    char *line = lines[i];
507
0
    char *file;
508
0
    char *colon;
509
0
    char **extension_points;
510
511
0
    if (line[0] == '#')
512
0
      continue;
513
514
0
    colon = strchr (line, ':');
515
0
    if (colon == NULL || line == colon)
516
0
      continue; /* Invalid line, ignore */
517
518
0
    *colon = 0; /* terminate filename */
519
0
    file = g_strdup (line);
520
0
    colon++; /* after colon */
521
522
0
    while (g_ascii_isspace (*colon))
523
0
      colon++;
524
525
0
          if (G_UNLIKELY (!cache))
526
0
            cache = g_hash_table_new_full (g_str_hash, g_str_equal,
527
0
                                           g_free, (GDestroyNotify)g_strfreev);
528
529
0
    extension_points = g_strsplit (colon, ",", -1);
530
0
    g_hash_table_insert (cache, file, extension_points);
531
0
  }
532
0
      g_strfreev (lines);
533
0
    }
534
535
0
  while ((name = g_dir_read_name (dir)))
536
0
    {
537
0
      if (is_valid_module_name (name, scope))
538
0
  {
539
0
    GIOExtensionPoint *extension_point;
540
0
    GIOModule *module;
541
0
    gchar *path;
542
0
    char **extension_points = NULL;
543
0
    int i;
544
545
0
    path = g_build_filename (dirname, name, NULL);
546
0
    module = g_io_module_new (path);
547
548
0
          if (cache)
549
0
            extension_points = g_hash_table_lookup (cache, name);
550
551
0
    if (extension_points != NULL &&
552
0
        g_stat (path, &statbuf) == 0 &&
553
0
        statbuf.st_ctime <= cache_time)
554
0
      {
555
        /* Lazy load/init the library when first required */
556
0
        for (i = 0; extension_points[i] != NULL; i++)
557
0
    {
558
0
      extension_point =
559
0
        g_io_extension_point_register (extension_points[i]);
560
0
      extension_point->lazy_load_modules =
561
0
        g_list_prepend (extension_point->lazy_load_modules,
562
0
            module);
563
0
    }
564
0
      }
565
0
    else
566
0
      {
567
        /* Try to load and init types */
568
0
        if (g_type_module_use (G_TYPE_MODULE (module)))
569
0
    g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
570
0
        else
571
0
    { /* Failure to load */
572
0
      g_printerr ("Failed to load module: %s\n", path);
573
0
      g_object_unref (module);
574
0
      g_free (path);
575
0
      continue;
576
0
    }
577
0
      }
578
579
0
    g_free (path);
580
0
  }
581
0
    }
582
583
0
  g_dir_close (dir);
584
585
0
  if (cache)
586
0
    g_hash_table_destroy (cache);
587
588
0
  g_free (filename);
589
0
}
590
591
/**
592
 * g_io_modules_scan_all_in_directory:
593
 * @dirname: (type filename): pathname for a directory containing modules
594
 *     to scan.
595
 *
596
 * Scans all the modules in the specified directory, ensuring that
597
 * any extension point implemented by a module is registered.
598
 *
599
 * This may not actually load and initialize all the types in each
600
 * module, some modules may be lazily loaded and initialized when
601
 * an extension point it implements is used with e.g.
602
 * g_io_extension_point_get_extensions() or
603
 * g_io_extension_point_get_extension_by_name().
604
 *
605
 * If you need to guarantee that all types are loaded in all the modules,
606
 * use g_io_modules_load_all_in_directory().
607
 *
608
 * Since: 2.24
609
 **/
610
void
611
g_io_modules_scan_all_in_directory (const char *dirname)
612
0
{
613
0
  g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
614
0
}
615
616
/**
617
 * g_io_modules_load_all_in_directory_with_scope:
618
 * @dirname: (type filename): pathname for a directory containing modules
619
 *     to load.
620
 * @scope: a scope to use when scanning the modules.
621
 *
622
 * Loads all the modules in the specified directory.
623
 *
624
 * If don't require all modules to be initialized (and thus registering
625
 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
626
 * which allows delayed/lazy loading of modules.
627
 *
628
 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
629
 *      from the directory,
630
 *      All the modules are loaded into memory, if you want to
631
 *      unload them (enabling on-demand loading) you must call
632
 *      g_type_module_unuse() on all the modules. Free the list
633
 *      with g_list_free().
634
 *
635
 * Since: 2.30
636
 **/
637
GList *
638
g_io_modules_load_all_in_directory_with_scope (const char     *dirname,
639
                                               GIOModuleScope *scope)
640
0
{
641
0
  const gchar *name;
642
0
  GDir        *dir;
643
0
  GList *modules;
644
645
0
  if (!g_module_supported ())
646
0
    return NULL;
647
648
0
  dir = g_dir_open (dirname, 0, NULL);
649
0
  if (!dir)
650
0
    return NULL;
651
652
0
  modules = NULL;
653
0
  while ((name = g_dir_read_name (dir)))
654
0
    {
655
0
      if (is_valid_module_name (name, scope))
656
0
        {
657
0
          GIOModule *module;
658
0
          gchar     *path;
659
660
0
          path = g_build_filename (dirname, name, NULL);
661
0
          module = g_io_module_new (path);
662
663
0
          if (!g_type_module_use (G_TYPE_MODULE (module)))
664
0
            {
665
0
              g_printerr ("Failed to load module: %s\n", path);
666
0
              g_object_unref (module);
667
0
              g_free (path);
668
0
              continue;
669
0
            }
670
    
671
0
          g_free (path);
672
673
0
          modules = g_list_prepend (modules, module);
674
0
        }
675
0
    }
676
  
677
0
  g_dir_close (dir);
678
679
0
  return modules;
680
0
}
681
682
/**
683
 * g_io_modules_load_all_in_directory:
684
 * @dirname: (type filename): pathname for a directory containing modules
685
 *     to load.
686
 *
687
 * Loads all the modules in the specified directory.
688
 *
689
 * If don't require all modules to be initialized (and thus registering
690
 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
691
 * which allows delayed/lazy loading of modules.
692
 *
693
 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
694
 *      from the directory,
695
 *      All the modules are loaded into memory, if you want to
696
 *      unload them (enabling on-demand loading) you must call
697
 *      g_type_module_unuse() on all the modules. Free the list
698
 *      with g_list_free().
699
 **/
700
GList *
701
g_io_modules_load_all_in_directory (const char *dirname)
702
0
{
703
0
  return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
704
0
}
705
706
static gpointer
707
try_class (GIOExtension *extension,
708
           guint         is_supported_offset)
709
0
{
710
0
  GType type = g_io_extension_get_type (extension);
711
0
  typedef gboolean (*verify_func) (void);
712
0
  gpointer class;
713
714
0
  class = g_type_class_ref (type);
715
0
  if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
716
0
    return class;
717
718
0
  g_type_class_unref (class);
719
0
  return NULL;
720
0
}
721
722
static void
723
print_help (const char        *envvar,
724
            GIOExtensionPoint *ep)
725
0
{
726
0
  g_print ("Supported arguments for %s environment variable:\n", envvar);
727
728
0
  if (g_io_extension_point_get_extensions (ep) == NULL)
729
0
    g_print (" (none)\n");
730
0
  else
731
0
    {
732
0
      GList *l;
733
0
      GIOExtension *extension;
734
0
      int width = 0;
735
736
0
      for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
737
0
        {
738
0
          extension = l->data;
739
0
          width = MAX (width, strlen (g_io_extension_get_name (extension)));
740
0
        }
741
742
0
      for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
743
0
        {
744
0
          extension = l->data;
745
746
0
          g_print (" %*s - %d\n", width, g_io_extension_get_name (extension), g_io_extension_get_priority (extension));
747
0
        }
748
0
    }
749
0
}
750
751
/**
752
 * _g_io_module_get_default_type:
753
 * @extension_point: the name of an extension point
754
 * @envvar: (nullable): the name of an environment variable to
755
 *     override the default implementation.
756
 * @is_supported_offset: a vtable offset, or zero
757
 *
758
 * Retrieves the default class implementing @extension_point.
759
 *
760
 * If @envvar is not %NULL, and the environment variable with that
761
 * name is set, then the implementation it specifies will be tried
762
 * first. After that, or if @envvar is not set, all other
763
 * implementations will be tried in order of decreasing priority.
764
 *
765
 * If @is_supported_offset is non-zero, then it is the offset into the
766
 * class vtable at which there is a function that takes no arguments and
767
 * returns a boolean.  This function will be called on each candidate
768
 * implementation to check if it is actually usable or not.
769
 *
770
 * The result is cached after it is generated the first time, and
771
 * the function is thread-safe.
772
 *
773
 * Returns: (transfer none): the type to instantiate to implement
774
 *     @extension_point, or %G_TYPE_INVALID if there are no usable
775
 *     implementations.
776
 */
777
GType
778
_g_io_module_get_default_type (const gchar *extension_point,
779
                               const gchar *envvar,
780
                               guint        is_supported_offset)
781
0
{
782
0
  static GRecMutex default_modules_lock;
783
0
  static GHashTable *default_modules;
784
0
  const char *use_this;
785
0
  GList *l;
786
0
  GIOExtensionPoint *ep;
787
0
  GIOExtension *extension, *preferred;
788
0
  gpointer impl;
789
790
0
  g_rec_mutex_lock (&default_modules_lock);
791
0
  if (default_modules)
792
0
    {
793
0
      gpointer key;
794
795
0
      if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
796
0
        {
797
0
          g_rec_mutex_unlock (&default_modules_lock);
798
0
          return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
799
0
        }
800
0
    }
801
0
  else
802
0
    {
803
0
      default_modules = g_hash_table_new (g_str_hash, g_str_equal);
804
0
    }
805
806
0
  _g_io_modules_ensure_loaded ();
807
0
  ep = g_io_extension_point_lookup (extension_point);
808
809
0
  if (!ep)
810
0
    {
811
0
      g_warn_if_reached ();
812
0
      g_rec_mutex_unlock (&default_modules_lock);
813
0
      return G_TYPE_INVALID;
814
0
    }
815
816
  /* It’s OK to query the environment here, even when running as setuid, because
817
   * it only allows a choice between existing already-loaded modules. No new
818
   * code is loaded based on the environment variable value. */
819
0
  use_this = envvar ? g_getenv (envvar) : NULL;
820
0
  if (g_strcmp0 (use_this, "help") == 0)
821
0
    {
822
0
      print_help (envvar, ep);
823
0
      use_this = NULL;
824
0
    }
825
826
0
  if (use_this)
827
0
    {
828
0
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
829
0
      if (preferred)
830
0
        {
831
0
          impl = try_class (preferred, is_supported_offset);
832
0
          if (impl)
833
0
            goto done;
834
0
        }
835
0
      else
836
0
        g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
837
0
    }
838
0
  else
839
0
    preferred = NULL;
840
841
0
  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
842
0
    {
843
0
      extension = l->data;
844
0
      if (extension == preferred)
845
0
        continue;
846
847
0
      impl = try_class (extension, is_supported_offset);
848
0
      if (impl)
849
0
        goto done;
850
0
    }
851
852
0
  impl = NULL;
853
854
0
 done:
855
0
  g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
856
0
  g_rec_mutex_unlock (&default_modules_lock);
857
858
0
  return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
859
0
}
860
861
static gpointer
862
try_implementation (const char           *extension_point,
863
                    GIOExtension         *extension,
864
        GIOModuleVerifyFunc   verify_func)
865
0
{
866
0
  GType type = g_io_extension_get_type (extension);
867
0
  gpointer impl;
868
869
0
  if (g_type_is_a (type, G_TYPE_INITABLE))
870
0
    {
871
0
      GError *error = NULL;
872
873
0
      impl = g_initable_new (type, NULL, &error, NULL);
874
0
      if (impl)
875
0
        return impl;
876
877
0
      g_debug ("Failed to initialize %s (%s) for %s: %s",
878
0
               g_io_extension_get_name (extension),
879
0
               g_type_name (type),
880
0
               extension_point,
881
0
               error ? error->message : "");
882
0
      g_clear_error (&error);
883
0
      return NULL;
884
0
    }
885
0
  else
886
0
    {
887
0
      impl = g_object_new (type, NULL);
888
0
      if (!verify_func || verify_func (impl))
889
0
  return impl;
890
891
0
      g_object_unref (impl);
892
0
      return NULL;
893
0
    }
894
0
}
895
896
static void
897
weak_ref_free (GWeakRef *weak_ref)
898
0
{
899
0
  g_weak_ref_clear (weak_ref);
900
0
  g_free (weak_ref);
901
0
}
902
903
/**
904
 * _g_io_module_get_default:
905
 * @extension_point: the name of an extension point
906
 * @envvar: (nullable): the name of an environment variable to
907
 *     override the default implementation.
908
 * @verify_func: (nullable): a function to call to verify that
909
 *     a given implementation is usable in the current environment.
910
 *
911
 * Retrieves the default object implementing @extension_point.
912
 *
913
 * If @envvar is not %NULL, and the environment variable with that
914
 * name is set, then the implementation it specifies will be tried
915
 * first. After that, or if @envvar is not set, all other
916
 * implementations will be tried in order of decreasing priority.
917
 *
918
 * If an extension point implementation implements #GInitable, then
919
 * that implementation will only be used if it initializes
920
 * successfully. Otherwise, if @verify_func is not %NULL, then it will
921
 * be called on each candidate implementation after construction, to
922
 * check if it is actually usable or not.
923
 *
924
 * The result is cached after it is generated the first time (but the cache does
925
 * not keep a strong reference to the object), and
926
 * the function is thread-safe.
927
 *
928
 * Returns: (transfer full) (nullable): an object implementing
929
 *     @extension_point, or %NULL if there are no usable
930
 *     implementations.
931
 */
932
gpointer
933
_g_io_module_get_default (const gchar         *extension_point,
934
        const gchar         *envvar,
935
        GIOModuleVerifyFunc  verify_func)
936
0
{
937
0
  static GRecMutex default_modules_lock;
938
0
  static GHashTable *default_modules;
939
0
  const char *use_this;
940
0
  GList *l;
941
0
  GIOExtensionPoint *ep;
942
0
  GIOExtension *extension = NULL, *preferred;
943
0
  gpointer impl, value;
944
0
  GWeakRef *impl_weak_ref = NULL;
945
946
0
  g_rec_mutex_lock (&default_modules_lock);
947
0
  if (default_modules)
948
0
    {
949
0
      if (g_hash_table_lookup_extended (default_modules, extension_point,
950
0
                                        NULL, &value))
951
0
        {
952
          /* Don’t debug here, since we’re returning a cached object which was
953
           * already printed earlier. */
954
0
          impl_weak_ref = value;
955
0
          impl = g_weak_ref_get (impl_weak_ref);
956
957
          /* If the object has been finalised (impl == NULL), fall through and
958
           * instantiate a new one. */
959
0
          if (impl != NULL)
960
0
            {
961
0
              g_rec_mutex_unlock (&default_modules_lock);
962
0
              return g_steal_pointer (&impl);
963
0
            }
964
0
        }
965
0
    }
966
0
  else
967
0
    {
968
0
      default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
969
0
                                               g_free, (GDestroyNotify) weak_ref_free);
970
0
    }
971
972
0
  _g_io_modules_ensure_loaded ();
973
0
  ep = g_io_extension_point_lookup (extension_point);
974
975
0
  if (!ep)
976
0
    {
977
0
      g_debug ("%s: Failed to find extension point ‘%s’",
978
0
               G_STRFUNC, extension_point);
979
0
      g_warn_if_reached ();
980
0
      g_rec_mutex_unlock (&default_modules_lock);
981
0
      return NULL;
982
0
    }
983
984
  /* It’s OK to query the environment here, even when running as setuid, because
985
   * it only allows a choice between existing already-loaded modules. No new
986
   * code is loaded based on the environment variable value. */
987
0
  use_this = envvar ? g_getenv (envvar) : NULL;
988
0
  if (g_strcmp0 (use_this, "help") == 0)
989
0
    {
990
0
      print_help (envvar, ep);
991
0
      use_this = NULL;
992
0
    }
993
994
0
  if (use_this)
995
0
    {
996
0
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
997
0
      if (preferred)
998
0
  {
999
0
    impl = try_implementation (extension_point, preferred, verify_func);
1000
0
    extension = preferred;
1001
0
    if (impl)
1002
0
      goto done;
1003
0
  }
1004
0
      else
1005
0
        g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
1006
0
    }
1007
0
  else
1008
0
    preferred = NULL;
1009
1010
0
  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1011
0
    {
1012
0
      extension = l->data;
1013
0
      if (extension == preferred)
1014
0
  continue;
1015
1016
0
      impl = try_implementation (extension_point, extension, verify_func);
1017
0
      if (impl)
1018
0
  goto done;
1019
0
    }
1020
1021
0
  impl = NULL;
1022
1023
0
 done:
1024
0
  if (impl_weak_ref == NULL)
1025
0
    {
1026
0
      impl_weak_ref = g_new0 (GWeakRef, 1);
1027
0
      g_weak_ref_init (impl_weak_ref, impl);
1028
0
      g_hash_table_insert (default_modules, g_strdup (extension_point),
1029
0
                           g_steal_pointer (&impl_weak_ref));
1030
0
    }
1031
0
  else
1032
0
    {
1033
0
      g_weak_ref_set (impl_weak_ref, impl);
1034
0
    }
1035
1036
0
  g_rec_mutex_unlock (&default_modules_lock);
1037
1038
0
  if (impl != NULL)
1039
0
    {
1040
0
      g_assert (extension != NULL);
1041
0
      g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
1042
0
               G_STRFUNC, g_io_extension_get_name (extension),
1043
0
               G_OBJECT_TYPE_NAME (impl), extension_point);
1044
0
    }
1045
0
  else
1046
0
    g_debug ("%s: Failed to find default implementation for ‘%s’",
1047
0
             G_STRFUNC, extension_point);
1048
1049
0
  return g_steal_pointer (&impl);
1050
0
}
1051
1052
G_LOCK_DEFINE_STATIC (registered_extensions);
1053
G_LOCK_DEFINE_STATIC (loaded_dirs);
1054
1055
extern GType g_fen_file_monitor_get_type (void);
1056
extern GType g_inotify_file_monitor_get_type (void);
1057
extern GType g_kqueue_file_monitor_get_type (void);
1058
extern GType g_win32_file_monitor_get_type (void);
1059
1060
extern GType _g_unix_volume_monitor_get_type (void);
1061
extern GType _g_local_vfs_get_type (void);
1062
1063
extern GType _g_win32_volume_monitor_get_type (void);
1064
extern GType _g_winhttp_vfs_get_type (void);
1065
1066
extern GType _g_dummy_proxy_resolver_get_type (void);
1067
extern GType _g_dummy_tls_backend_get_type (void);
1068
extern GType g_network_monitor_base_get_type (void);
1069
#ifdef HAVE_NETLINK
1070
extern GType _g_network_monitor_netlink_get_type (void);
1071
extern GType _g_network_monitor_nm_get_type (void);
1072
#endif
1073
1074
extern GType g_memory_monitor_dbus_get_type (void);
1075
extern GType g_memory_monitor_portal_get_type (void);
1076
1077
#ifdef G_OS_UNIX
1078
extern GType g_fdo_notification_backend_get_type (void);
1079
extern GType g_gtk_notification_backend_get_type (void);
1080
extern GType g_portal_notification_backend_get_type (void);
1081
extern GType g_proxy_resolver_portal_get_type (void);
1082
extern GType g_network_monitor_portal_get_type (void);
1083
#endif
1084
1085
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1086
extern GType g_cocoa_notification_backend_get_type (void);
1087
#endif
1088
1089
#ifdef G_PLATFORM_WIN32
1090
extern GType g_win32_notification_backend_get_type (void);
1091
1092
#include <windows.h>
1093
extern GType _g_win32_network_monitor_get_type (void);
1094
1095
static HMODULE gio_dll = NULL;
1096
1097
#ifdef DLL_EXPORT
1098
1099
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1100
                     DWORD     fdwReason,
1101
                     LPVOID    lpvReserved);
1102
1103
BOOL WINAPI
1104
DllMain (HINSTANCE hinstDLL,
1105
   DWORD     fdwReason,
1106
   LPVOID    lpvReserved)
1107
{
1108
  if (fdwReason == DLL_PROCESS_ATTACH)
1109
    {
1110
      gio_dll = hinstDLL;
1111
      gio_win32_appinfo_init (FALSE);
1112
    }
1113
1114
  return TRUE;
1115
}
1116
1117
#endif
1118
1119
void *
1120
_g_io_win32_get_module (void)
1121
{
1122
  if (!gio_dll)
1123
    GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1124
                        GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1125
                        (const char *) _g_io_win32_get_module,
1126
                        &gio_dll);
1127
  return gio_dll;
1128
}
1129
1130
#endif
1131
1132
void
1133
_g_io_modules_ensure_extension_points_registered (void)
1134
0
{
1135
0
  static gboolean registered_extensions = FALSE;
1136
0
  GIOExtensionPoint *ep;
1137
1138
0
  G_LOCK (registered_extensions);
1139
  
1140
0
  if (!registered_extensions)
1141
0
    {
1142
0
      registered_extensions = TRUE;
1143
      
1144
0
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
1145
0
#if !GLIB_CHECK_VERSION (3, 0, 0)
1146
0
      ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1147
0
      g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1148
0
#endif
1149
0
#endif
1150
1151
0
      ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1152
0
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1153
1154
0
      ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1155
0
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1156
1157
0
      ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1158
0
      g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1159
      
1160
0
      ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1161
0
      g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1162
      
1163
0
      ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1164
0
      g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1165
1166
0
      ep = g_io_extension_point_register ("gsettings-backend");
1167
0
      g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1168
1169
0
      ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1170
0
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1171
1172
0
      ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1173
0
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1174
1175
0
      ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1176
0
      g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1177
1178
0
      ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1179
0
      g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1180
1181
0
      ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1182
0
      g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1183
1184
0
      ep = g_io_extension_point_register (G_MEMORY_MONITOR_EXTENSION_POINT_NAME);
1185
0
      g_io_extension_point_set_required_type (ep, G_TYPE_MEMORY_MONITOR);
1186
0
    }
1187
  
1188
0
  G_UNLOCK (registered_extensions);
1189
0
}
1190
1191
static gchar *
1192
get_gio_module_dir (void)
1193
0
{
1194
0
  gchar *module_dir;
1195
0
  gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1196
1197
  /* If running as setuid, loading modules from an arbitrary directory
1198
   * controlled by the unprivileged user who is running the program could allow
1199
   * for execution of arbitrary code (in constructors in modules).
1200
   * Don’t allow it.
1201
   *
1202
   * If a setuid program somehow needs to load additional GIO modules, it should
1203
   * explicitly call g_io_modules_scan_all_in_directory(). */
1204
0
  module_dir = !is_setuid ? g_strdup (g_getenv ("GIO_MODULE_DIR")) : NULL;
1205
0
  if (module_dir == NULL)
1206
0
    {
1207
#ifdef G_OS_WIN32
1208
      gchar *install_dir;
1209
1210
      install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1211
      module_dir = g_build_filename (install_dir,
1212
                                     "lib", "gio", "modules",
1213
                                     NULL);
1214
      g_free (install_dir);
1215
#else
1216
0
      module_dir = g_strdup (GIO_MODULE_DIR);
1217
0
#endif
1218
0
    }
1219
1220
0
  return module_dir;
1221
0
}
1222
1223
void
1224
_g_io_modules_ensure_loaded (void)
1225
0
{
1226
0
  static gboolean loaded_dirs = FALSE;
1227
0
  const char *module_path;
1228
0
  GIOModuleScope *scope;
1229
1230
0
  _g_io_modules_ensure_extension_points_registered ();
1231
  
1232
0
  G_LOCK (loaded_dirs);
1233
1234
0
  if (!loaded_dirs)
1235
0
    {
1236
0
      gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
1237
0
      gchar *module_dir;
1238
1239
0
      loaded_dirs = TRUE;
1240
0
      scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1241
1242
      /* First load any overrides, extras (but not if running as setuid!) */
1243
0
      module_path = !is_setuid ? g_getenv ("GIO_EXTRA_MODULES") : NULL;
1244
0
      if (module_path)
1245
0
  {
1246
0
    gchar **paths;
1247
0
    int i;
1248
1249
0
    paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1250
1251
0
    for (i = 0; paths[i] != NULL; i++)
1252
0
      {
1253
0
        g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1254
0
      }
1255
1256
0
    g_strfreev (paths);
1257
0
  }
1258
1259
      /* Then load the compiled in path */
1260
0
      module_dir = get_gio_module_dir ();
1261
1262
0
      g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1263
0
      g_free (module_dir);
1264
1265
0
      g_io_module_scope_free (scope);
1266
1267
      /* Initialize types from built-in "modules" */
1268
0
      g_type_ensure (g_null_settings_backend_get_type ());
1269
0
      g_type_ensure (g_memory_settings_backend_get_type ());
1270
0
      g_type_ensure (g_keyfile_settings_backend_get_type ());
1271
0
#if defined(HAVE_INOTIFY_INIT1)
1272
0
      g_type_ensure (g_inotify_file_monitor_get_type ());
1273
0
#endif
1274
#if defined(HAVE_KQUEUE)
1275
      g_type_ensure (g_kqueue_file_monitor_get_type ());
1276
#endif
1277
#if defined(HAVE_FEN)
1278
      g_type_ensure (g_fen_file_monitor_get_type ());
1279
#endif
1280
#ifdef G_OS_WIN32
1281
      g_type_ensure (_g_win32_volume_monitor_get_type ());
1282
      g_type_ensure (g_win32_file_monitor_get_type ());
1283
      g_type_ensure (g_registry_backend_get_type ());
1284
#endif
1285
#ifdef HAVE_COCOA
1286
      g_type_ensure (g_nextstep_settings_backend_get_type ());
1287
      g_type_ensure (g_osx_app_info_get_type ());
1288
#endif
1289
0
#ifdef G_OS_UNIX
1290
0
      g_type_ensure (_g_unix_volume_monitor_get_type ());
1291
0
      g_type_ensure (g_fdo_notification_backend_get_type ());
1292
0
      g_type_ensure (g_gtk_notification_backend_get_type ());
1293
0
      g_type_ensure (g_portal_notification_backend_get_type ());
1294
0
      g_type_ensure (g_memory_monitor_dbus_get_type ());
1295
0
      g_type_ensure (g_memory_monitor_portal_get_type ());
1296
0
      g_type_ensure (g_network_monitor_portal_get_type ());
1297
0
      g_type_ensure (g_proxy_resolver_portal_get_type ());
1298
0
#endif
1299
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1300
      g_type_ensure (g_cocoa_notification_backend_get_type ());
1301
#endif
1302
#ifdef G_OS_WIN32
1303
      g_type_ensure (g_win32_notification_backend_get_type ());
1304
      g_type_ensure (_g_winhttp_vfs_get_type ());
1305
#endif
1306
0
      g_type_ensure (_g_local_vfs_get_type ());
1307
0
      g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1308
0
      g_type_ensure (_g_http_proxy_get_type ());
1309
0
      g_type_ensure (_g_https_proxy_get_type ());
1310
0
      g_type_ensure (_g_socks4a_proxy_get_type ());
1311
0
      g_type_ensure (_g_socks4_proxy_get_type ());
1312
0
      g_type_ensure (_g_socks5_proxy_get_type ());
1313
0
      g_type_ensure (_g_dummy_tls_backend_get_type ());
1314
0
      g_type_ensure (g_network_monitor_base_get_type ());
1315
0
#ifdef HAVE_NETLINK
1316
0
      g_type_ensure (_g_network_monitor_netlink_get_type ());
1317
0
      g_type_ensure (_g_network_monitor_nm_get_type ());
1318
0
#endif
1319
#ifdef G_OS_WIN32
1320
      g_type_ensure (_g_win32_network_monitor_get_type ());
1321
#endif
1322
0
    }
1323
1324
0
  G_UNLOCK (loaded_dirs);
1325
0
}
1326
1327
static void
1328
g_io_extension_point_free (GIOExtensionPoint *ep)
1329
0
{
1330
0
  g_free (ep->name);
1331
0
  g_free (ep);
1332
0
}
1333
1334
/**
1335
 * g_io_extension_point_register:
1336
 * @name: The name of the extension point
1337
 *
1338
 * Registers an extension point.
1339
 *
1340
 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1341
 *    owned by GIO and should not be freed.
1342
 */
1343
GIOExtensionPoint *
1344
g_io_extension_point_register (const char *name)
1345
0
{
1346
0
  GIOExtensionPoint *ep;
1347
  
1348
0
  G_LOCK (extension_points);
1349
0
  if (extension_points == NULL)
1350
0
    extension_points = g_hash_table_new_full (g_str_hash,
1351
0
                g_str_equal,
1352
0
                NULL,
1353
0
                (GDestroyNotify)g_io_extension_point_free);
1354
1355
0
  ep = g_hash_table_lookup (extension_points, name);
1356
0
  if (ep != NULL)
1357
0
    {
1358
0
      G_UNLOCK (extension_points);
1359
0
      return ep;
1360
0
    }
1361
1362
0
  ep = g_new0 (GIOExtensionPoint, 1);
1363
0
  ep->name = g_strdup (name);
1364
  
1365
0
  g_hash_table_insert (extension_points, ep->name, ep);
1366
  
1367
0
  G_UNLOCK (extension_points);
1368
1369
0
  return ep;
1370
0
}
1371
1372
/**
1373
 * g_io_extension_point_lookup:
1374
 * @name: the name of the extension point
1375
 *
1376
 * Looks up an existing extension point.
1377
 *
1378
 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1379
 *    is no registered extension point with the given name.
1380
 */
1381
GIOExtensionPoint *
1382
g_io_extension_point_lookup (const char *name)
1383
0
{
1384
0
  GIOExtensionPoint *ep;
1385
1386
0
  G_LOCK (extension_points);
1387
0
  ep = NULL;
1388
0
  if (extension_points != NULL)
1389
0
    ep = g_hash_table_lookup (extension_points, name);
1390
  
1391
0
  G_UNLOCK (extension_points);
1392
1393
0
  return ep;
1394
  
1395
0
}
1396
1397
/**
1398
 * g_io_extension_point_set_required_type:
1399
 * @extension_point: a #GIOExtensionPoint
1400
 * @type: the #GType to require
1401
 *
1402
 * Sets the required type for @extension_point to @type. 
1403
 * All implementations must henceforth have this type.
1404
 */
1405
void
1406
g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1407
          GType              type)
1408
0
{
1409
0
  extension_point->required_type = type;
1410
0
}
1411
1412
/**
1413
 * g_io_extension_point_get_required_type:
1414
 * @extension_point: a #GIOExtensionPoint
1415
 *
1416
 * Gets the required type for @extension_point.
1417
 *
1418
 * Returns: the #GType that all implementations must have, 
1419
 *     or #G_TYPE_INVALID if the extension point has no required type
1420
 */
1421
GType
1422
g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1423
0
{
1424
0
  return extension_point->required_type;
1425
0
}
1426
1427
static void
1428
lazy_load_modules (GIOExtensionPoint *extension_point)
1429
0
{
1430
0
  GIOModule *module;
1431
0
  GList *l;
1432
1433
0
  for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1434
0
    {
1435
0
      module = l->data;
1436
1437
0
      if (!module->initialized)
1438
0
  {
1439
0
    if (g_type_module_use (G_TYPE_MODULE (module)))
1440
0
      g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1441
0
    else
1442
0
      g_printerr ("Failed to load module: %s\n",
1443
0
      module->filename);
1444
0
  }
1445
0
    }
1446
0
}
1447
1448
/**
1449
 * g_io_extension_point_get_extensions:
1450
 * @extension_point: a #GIOExtensionPoint
1451
 *
1452
 * Gets a list of all extensions that implement this extension point.
1453
 * The list is sorted by priority, beginning with the highest priority.
1454
 *
1455
 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1456
 *     #GIOExtensions. The list is owned by GIO and should not be
1457
 *     modified.
1458
 */
1459
GList *
1460
g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1461
0
{
1462
0
  g_return_val_if_fail (extension_point != NULL, NULL);
1463
1464
0
  lazy_load_modules (extension_point);
1465
0
  return extension_point->extensions;
1466
0
}
1467
1468
/**
1469
 * g_io_extension_point_get_extension_by_name:
1470
 * @extension_point: a #GIOExtensionPoint
1471
 * @name: the name of the extension to get
1472
 *
1473
 * Finds a #GIOExtension for an extension point by name.
1474
 *
1475
 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1476
 *    given name, or %NULL if there is no extension with that name
1477
 */
1478
GIOExtension *
1479
g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1480
              const char        *name)
1481
0
{
1482
0
  GList *l;
1483
1484
0
  g_return_val_if_fail (name != NULL, NULL);
1485
1486
0
  lazy_load_modules (extension_point);
1487
0
  for (l = extension_point->extensions; l != NULL; l = l->next)
1488
0
    {
1489
0
      GIOExtension *e = l->data;
1490
1491
0
      if (e->name != NULL &&
1492
0
    strcmp (e->name, name) == 0)
1493
0
  return e;
1494
0
    }
1495
  
1496
0
  return NULL;
1497
0
}
1498
1499
static gint
1500
extension_prio_compare (gconstpointer  a,
1501
      gconstpointer  b)
1502
0
{
1503
0
  const GIOExtension *extension_a = a, *extension_b = b;
1504
1505
0
  if (extension_a->priority > extension_b->priority)
1506
0
    return -1;
1507
1508
0
  if (extension_b->priority > extension_a->priority)
1509
0
    return 1;
1510
1511
0
  return 0;
1512
0
}
1513
1514
/**
1515
 * g_io_extension_point_implement:
1516
 * @extension_point_name: the name of the extension point
1517
 * @type: the #GType to register as extension 
1518
 * @extension_name: the name for the extension
1519
 * @priority: the priority for the extension
1520
 *
1521
 * Registers @type as extension for the extension point with name
1522
 * @extension_point_name. 
1523
 *
1524
 * If @type has already been registered as an extension for this 
1525
 * extension point, the existing #GIOExtension object is returned.
1526
 *
1527
 * Returns: (transfer none): a #GIOExtension object for #GType
1528
 */
1529
GIOExtension *
1530
g_io_extension_point_implement (const char *extension_point_name,
1531
        GType       type,
1532
        const char *extension_name,
1533
        gint        priority)
1534
0
{
1535
0
  GIOExtensionPoint *extension_point;
1536
0
  GIOExtension *extension;
1537
0
  GList *l;
1538
1539
0
  g_return_val_if_fail (extension_point_name != NULL, NULL);
1540
1541
0
  extension_point = g_io_extension_point_lookup (extension_point_name);
1542
0
  if (extension_point == NULL)
1543
0
    {
1544
0
      g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1545
0
      return NULL;
1546
0
    }
1547
  
1548
0
  if (extension_point->required_type != 0 &&
1549
0
      !g_type_is_a (type, extension_point->required_type))
1550
0
    {
1551
0
      g_warning ("Tried to register an extension of the type %s to extension point %s. "
1552
0
     "Expected type is %s.",
1553
0
     g_type_name (type),
1554
0
     extension_point_name, 
1555
0
     g_type_name (extension_point->required_type));
1556
0
      return NULL;
1557
0
    }      
1558
1559
  /* It's safe to register the same type multiple times */
1560
0
  for (l = extension_point->extensions; l != NULL; l = l->next)
1561
0
    {
1562
0
      extension = l->data;
1563
0
      if (extension->type == type)
1564
0
  return extension;
1565
0
    }
1566
  
1567
0
  extension = g_slice_new0 (GIOExtension);
1568
0
  extension->type = type;
1569
0
  extension->name = g_strdup (extension_name);
1570
0
  extension->priority = priority;
1571
  
1572
0
  extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1573
0
                  extension, extension_prio_compare);
1574
  
1575
0
  return extension;
1576
0
}
1577
1578
/**
1579
 * g_io_extension_ref_class:
1580
 * @extension: a #GIOExtension
1581
 *
1582
 * Gets a reference to the class for the type that is 
1583
 * associated with @extension.
1584
 *
1585
 * Returns: (transfer full): the #GTypeClass for the type of @extension
1586
 */
1587
GTypeClass *
1588
g_io_extension_ref_class (GIOExtension *extension)
1589
0
{
1590
0
  return g_type_class_ref (extension->type);
1591
0
}
1592
1593
/**
1594
 * g_io_extension_get_type:
1595
 * @extension: a #GIOExtension
1596
 *
1597
 * Gets the type associated with @extension.
1598
 *
1599
 * Returns: the type of @extension
1600
 */
1601
GType
1602
g_io_extension_get_type (GIOExtension *extension)
1603
0
{
1604
0
  return extension->type;
1605
0
}
1606
1607
/**
1608
 * g_io_extension_get_name:
1609
 * @extension: a #GIOExtension
1610
 *
1611
 * Gets the name under which @extension was registered.
1612
 *
1613
 * Note that the same type may be registered as extension
1614
 * for multiple extension points, under different names.
1615
 *
1616
 * Returns: the name of @extension.
1617
 */
1618
const char *
1619
g_io_extension_get_name (GIOExtension *extension)
1620
0
{
1621
0
  return extension->name;
1622
0
}
1623
1624
/**
1625
 * g_io_extension_get_priority:
1626
 * @extension: a #GIOExtension
1627
 *
1628
 * Gets the priority with which @extension was registered.
1629
 *
1630
 * Returns: the priority of @extension
1631
 */
1632
gint
1633
g_io_extension_get_priority (GIOExtension *extension)
1634
0
{
1635
0
  return extension->priority;
1636
0
}