nexmon – Blame information for rev 1

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