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
34 * Provides an interface and default functions for loading and unloading
35 * modules. This is used internally to make gio extensible, but can also
36 * be used by other to implement module loading.
41 GTypeModule parent_instance;
46 void (* load) (GIOModule *module);
47 void (* unload) (GIOModule *module);
50 struct _GIOModuleClass
52 GTypeModuleClass parent_class;
56 static void g_io_module_finalize (GObject *object);
57 static gboolean g_io_module_load_module (GTypeModule *gmodule);
58 static void g_io_module_unload_module (GTypeModule *gmodule);
60 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
63 g_io_module_class_init (GIOModuleClass *class)
65 GObjectClass *object_class = G_OBJECT_CLASS (class);
66 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
68 object_class->finalize = g_io_module_finalize;
70 type_module_class->load = g_io_module_load_module;
71 type_module_class->unload = g_io_module_unload_module;
75 g_io_module_init (GIOModule *module)
80 g_io_module_finalize (GObject *object)
82 GIOModule *module = G_IO_MODULE (object);
84 g_free (module->filename);
86 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
90 g_io_module_load_module (GTypeModule *gmodule)
92 GIOModule *module = G_IO_MODULE (gmodule);
94 if (!module->filename)
96 g_warning ("GIOModule path not set");
100 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
102 if (!module->library)
104 g_printerr ("%s\n", g_module_error ());
108 /* Make sure that the loaded library contains the required methods */
109 if (! g_module_symbol (module->library,
111 (gpointer *) &module->load) ||
112 ! g_module_symbol (module->library,
113 "g_io_module_unload",
114 (gpointer *) &module->unload))
116 g_printerr ("%s\n", g_module_error ());
117 g_module_close (module->library);
122 /* Initialize the loaded module */
123 module->load (module);
129 g_io_module_unload_module (GTypeModule *gmodule)
131 GIOModule *module = G_IO_MODULE (gmodule);
133 module->unload (module);
135 g_module_close (module->library);
136 module->library = NULL;
139 module->unload = NULL;
144 * @filename: filename of the shared library module.
146 * Creates a new GIOModule that will load the specific
147 * shared library when in use.
149 * Returns: a #GIOModule from given @filename,
153 g_io_module_new (const gchar *filename)
157 g_return_val_if_fail (filename != NULL, NULL);
159 module = g_object_new (G_IO_TYPE_MODULE, NULL);
160 module->filename = g_strdup (filename);
166 is_valid_module_name (const gchar *basename)
168 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
170 g_str_has_prefix (basename, "lib") &&
171 g_str_has_suffix (basename, ".so");
173 return g_str_has_suffix (basename, ".dll");
178 * g_io_modules_load_all_in_directory:
179 * @dirname: pathname for a directory containing modules to load.
181 * Loads all the modules in the the specified directory.
183 * Returns: a list of #GIOModules loaded from the directory
186 g_io_modules_load_all_in_directory (const char *dirname)
192 if (!g_module_supported ())
195 dir = g_dir_open (dirname, 0, NULL);
200 while ((name = g_dir_read_name (dir)))
202 if (is_valid_module_name (name))
207 path = g_build_filename (dirname, name, NULL);
208 module = g_io_module_new (path);
210 if (!g_type_module_use (G_TYPE_MODULE (module)))
212 g_printerr ("Failed to load module: %s\n", path);
213 g_object_unref (module);
220 g_type_module_unuse (G_TYPE_MODULE (module));
222 modules = g_list_prepend (modules, module);
231 G_LOCK_DEFINE_STATIC (loaded_dirs);
234 _g_io_modules_ensure_loaded (void)
237 const char *directory;
238 static gboolean loaded_dirs = FALSE;
241 G_LOCK (loaded_dirs);
246 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
249 G_UNLOCK (loaded_dirs);
252 #define __G_IO_MODULE_C__
253 #include "gioaliasdef.c"