Fix AKS -> ASK typo
[platform/upstream/glib.git] / gio / giomodule.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:giomodule
32  * @short_description: Loadable GIO Modules
33  *
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.
37  * 
38  **/
39
40 struct _GIOModule {
41   GTypeModule parent_instance;
42   
43   gchar       *filename;
44   GModule     *library;
45   
46   void (* load)   (GIOModule *module);
47   void (* unload) (GIOModule *module);
48 };
49
50 struct _GIOModuleClass
51 {
52   GTypeModuleClass parent_class;
53
54 };
55
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);
59
60 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
61
62 static void
63 g_io_module_class_init (GIOModuleClass *class)
64 {
65   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
66   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
67
68   object_class->finalize     = g_io_module_finalize;
69
70   type_module_class->load    = g_io_module_load_module;
71   type_module_class->unload  = g_io_module_unload_module;
72 }
73
74 static void
75 g_io_module_init (GIOModule *module)
76 {
77 }
78
79 static void
80 g_io_module_finalize (GObject *object)
81 {
82   GIOModule *module = G_IO_MODULE (object);
83
84   g_free (module->filename);
85
86   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
87 }
88
89 static gboolean
90 g_io_module_load_module (GTypeModule *gmodule)
91 {
92   GIOModule *module = G_IO_MODULE (gmodule);
93
94   if (!module->filename)
95     {
96       g_warning ("GIOModule path not set");
97       return FALSE;
98     }
99
100   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
101
102   if (!module->library)
103     {
104       g_printerr ("%s\n", g_module_error ());
105       return FALSE;
106     }
107
108   /* Make sure that the loaded library contains the required methods */
109   if (! g_module_symbol (module->library,
110                          "g_io_module_load",
111                          (gpointer *) &module->load) ||
112       ! g_module_symbol (module->library,
113                          "g_io_module_unload",
114                          (gpointer *) &module->unload))
115     {
116       g_printerr ("%s\n", g_module_error ());
117       g_module_close (module->library);
118
119       return FALSE;
120     }
121
122   /* Initialize the loaded module */
123   module->load (module);
124
125   return TRUE;
126 }
127
128 static void
129 g_io_module_unload_module (GTypeModule *gmodule)
130 {
131   GIOModule *module = G_IO_MODULE (gmodule);
132
133   module->unload (module);
134
135   g_module_close (module->library);
136   module->library = NULL;
137
138   module->load   = NULL;
139   module->unload = NULL;
140 }
141
142 /**
143  * g_io_module_new:
144  * @filename: filename of the shared library module.
145  * 
146  * Creates a new GIOModule that will load the specific
147  * shared library when in use.
148  * 
149  * Returns: a #GIOModule from given @filename, 
150  * or %NULL on error.
151  **/
152 GIOModule *
153 g_io_module_new (const gchar *filename)
154 {
155   GIOModule *module;
156
157   g_return_val_if_fail (filename != NULL, NULL);
158
159   module = g_object_new (G_IO_TYPE_MODULE, NULL);
160   module->filename = g_strdup (filename);
161
162   return module;
163 }
164
165 static gboolean
166 is_valid_module_name (const gchar *basename)
167 {
168 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
169   return
170     g_str_has_prefix (basename, "lib") &&
171     g_str_has_suffix (basename, ".so");
172 #else
173   return g_str_has_suffix (basename, ".dll");
174 #endif
175 }
176
177 /**
178  * g_io_modules_load_all_in_directory:
179  * @dirname: pathname for a directory containing modules to load.
180  * 
181  * Loads all the modules in the the specified directory.
182  * 
183  * Returns: a list of #GIOModules loaded from the directory
184  **/
185 GList *
186 g_io_modules_load_all_in_directory (const char *dirname)
187 {
188   const gchar *name;
189   GDir        *dir;
190   GList *modules;
191
192   if (!g_module_supported ())
193     return NULL;
194
195   dir = g_dir_open (dirname, 0, NULL);
196   if (!dir)
197     return NULL;
198
199   modules = NULL;
200   while ((name = g_dir_read_name (dir)))
201     {
202       if (is_valid_module_name (name))
203         {
204           GIOModule *module;
205           gchar     *path;
206
207           path = g_build_filename (dirname, name, NULL);
208           module = g_io_module_new (path);
209
210           if (!g_type_module_use (G_TYPE_MODULE (module)))
211             {
212               g_printerr ("Failed to load module: %s\n", path);
213               g_object_unref (module);
214               g_free (path);
215               continue;
216             }
217           
218           g_free (path);
219
220           g_type_module_unuse (G_TYPE_MODULE (module));
221           
222           modules = g_list_prepend (modules, module);
223         }
224     }
225   
226   g_dir_close (dir);
227
228   return modules;
229 }
230
231 G_LOCK_DEFINE_STATIC (loaded_dirs);
232
233 void
234 _g_io_modules_ensure_loaded (void)
235 {
236   GList *modules;
237   const char *directory;
238   static gboolean loaded_dirs = FALSE;
239
240   
241   G_LOCK (loaded_dirs);
242
243   if (!loaded_dirs)
244     {
245       loaded_dirs = TRUE;
246       modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
247     }
248   
249   G_UNLOCK (loaded_dirs);
250 }
251
252 #define __G_IO_MODULE_C__
253 #include "gioaliasdef.c"