1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
32 * @short_description: Loadable GIO Modules
35 * Provides an interface and default functions for loading and unloading
36 * modules. This is used internally to make gio extensible, but can also
37 * be used by other to implement module loading.
42 GTypeModule parent_instance;
47 void (* load) (GIOModule *module);
48 void (* unload) (GIOModule *module);
51 struct _GIOModuleClass
53 GTypeModuleClass parent_class;
57 static void g_io_module_finalize (GObject *object);
58 static gboolean g_io_module_load_module (GTypeModule *gmodule);
59 static void g_io_module_unload_module (GTypeModule *gmodule);
61 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
64 g_io_module_class_init (GIOModuleClass *class)
66 GObjectClass *object_class = G_OBJECT_CLASS (class);
67 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
69 object_class->finalize = g_io_module_finalize;
71 type_module_class->load = g_io_module_load_module;
72 type_module_class->unload = g_io_module_unload_module;
76 g_io_module_init (GIOModule *module)
81 g_io_module_finalize (GObject *object)
83 GIOModule *module = G_IO_MODULE (object);
85 g_free (module->filename);
87 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
91 g_io_module_load_module (GTypeModule *gmodule)
93 GIOModule *module = G_IO_MODULE (gmodule);
95 if (!module->filename)
97 g_warning ("GIOModule path not set");
101 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
103 if (!module->library)
105 g_printerr ("%s\n", g_module_error ());
109 /* Make sure that the loaded library contains the required methods */
110 if (! g_module_symbol (module->library,
112 (gpointer) &module->load) ||
113 ! g_module_symbol (module->library,
114 "g_io_module_unload",
115 (gpointer) &module->unload))
117 g_printerr ("%s\n", g_module_error ());
118 g_module_close (module->library);
123 /* Initialize the loaded module */
124 module->load (module);
130 g_io_module_unload_module (GTypeModule *gmodule)
132 GIOModule *module = G_IO_MODULE (gmodule);
134 module->unload (module);
136 g_module_close (module->library);
137 module->library = NULL;
140 module->unload = NULL;
145 * @filename: filename of the shared library module.
147 * Creates a new GIOModule that will load the specific
148 * shared library when in use.
150 * Returns: a #GIOModule from given @filename,
154 g_io_module_new (const gchar *filename)
158 g_return_val_if_fail (filename != NULL, NULL);
160 module = g_object_new (G_IO_TYPE_MODULE, NULL);
161 module->filename = g_strdup (filename);
167 is_valid_module_name (const gchar *basename)
169 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
171 g_str_has_prefix (basename, "lib") &&
172 g_str_has_suffix (basename, ".so");
174 return g_str_has_suffix (basename, ".dll");
179 * g_io_modules_load_all_in_directory:
180 * @dirname: pathname for a directory containing modules to load.
182 * Loads all the modules in the the specified directory.
184 * Returns: a list of #GIOModules loaded from the directory
187 g_io_modules_load_all_in_directory (const char *dirname)
193 if (!g_module_supported ())
196 dir = g_dir_open (dirname, 0, NULL);
201 while ((name = g_dir_read_name (dir)))
203 if (is_valid_module_name (name))
208 path = g_build_filename (dirname, name, NULL);
209 module = g_io_module_new (path);
211 if (!g_type_module_use (G_TYPE_MODULE (module)))
213 g_printerr ("Failed to load module: %s\n", path);
214 g_object_unref (module);
221 g_type_module_unuse (G_TYPE_MODULE (module));
223 modules = g_list_prepend (modules, module);
232 G_LOCK_DEFINE_STATIC (loaded_dirs);
235 _g_io_modules_ensure_loaded (void)
238 static gboolean loaded_dirs = FALSE;
240 G_LOCK (loaded_dirs);
245 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
248 G_UNLOCK (loaded_dirs);
251 #define __G_IO_MODULE_C__
252 #include "gioaliasdef.c"