Make g_io_modules_load_all_in_directory not unuse loaded modules so that
[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 #include "glocalfilemonitor.h"
28 #include "glocaldirectorymonitor.h"
29 #include "gnativevolumemonitor.h"
30 #include "gvfs.h"
31
32 #include "gioalias.h"
33
34 /**
35  * SECTION:giomodule
36  * @short_description: Loadable GIO Modules
37  * @include: gio.h
38  *
39  * Provides an interface and default functions for loading and unloading 
40  * modules. This is used internally to make gio extensible, but can also
41  * be used by other to implement module loading.
42  * 
43  **/
44
45 struct _GIOModule {
46   GTypeModule parent_instance;
47   
48   gchar       *filename;
49   GModule     *library;
50   
51   void (* load)   (GIOModule *module);
52   void (* unload) (GIOModule *module);
53 };
54
55 struct _GIOModuleClass
56 {
57   GTypeModuleClass parent_class;
58
59 };
60
61 static void      g_io_module_finalize      (GObject      *object);
62 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
63 static void      g_io_module_unload_module (GTypeModule  *gmodule);
64
65 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
66
67 static void
68 g_io_module_class_init (GIOModuleClass *class)
69 {
70   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
71   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
72
73   object_class->finalize     = g_io_module_finalize;
74
75   type_module_class->load    = g_io_module_load_module;
76   type_module_class->unload  = g_io_module_unload_module;
77 }
78
79 static void
80 g_io_module_init (GIOModule *module)
81 {
82 }
83
84 static void
85 g_io_module_finalize (GObject *object)
86 {
87   GIOModule *module = G_IO_MODULE (object);
88
89   g_free (module->filename);
90
91   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
92 }
93
94 static gboolean
95 g_io_module_load_module (GTypeModule *gmodule)
96 {
97   GIOModule *module = G_IO_MODULE (gmodule);
98
99   if (!module->filename)
100     {
101       g_warning ("GIOModule path not set");
102       return FALSE;
103     }
104
105   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
106
107   if (!module->library)
108     {
109       g_printerr ("%s\n", g_module_error ());
110       return FALSE;
111     }
112
113   /* Make sure that the loaded library contains the required methods */
114   if (! g_module_symbol (module->library,
115                          "g_io_module_load",
116                          (gpointer) &module->load) ||
117       ! g_module_symbol (module->library,
118                          "g_io_module_unload",
119                          (gpointer) &module->unload))
120     {
121       g_printerr ("%s\n", g_module_error ());
122       g_module_close (module->library);
123
124       return FALSE;
125     }
126
127   /* Initialize the loaded module */
128   module->load (module);
129
130   return TRUE;
131 }
132
133 static void
134 g_io_module_unload_module (GTypeModule *gmodule)
135 {
136   GIOModule *module = G_IO_MODULE (gmodule);
137
138   module->unload (module);
139
140   g_module_close (module->library);
141   module->library = NULL;
142
143   module->load   = NULL;
144   module->unload = NULL;
145 }
146
147 /**
148  * g_io_module_new:
149  * @filename: filename of the shared library module.
150  * 
151  * Creates a new GIOModule that will load the specific
152  * shared library when in use.
153  * 
154  * Returns: a #GIOModule from given @filename, 
155  * or %NULL on error.
156  **/
157 GIOModule *
158 g_io_module_new (const gchar *filename)
159 {
160   GIOModule *module;
161
162   g_return_val_if_fail (filename != NULL, NULL);
163
164   module = g_object_new (G_IO_TYPE_MODULE, NULL);
165   module->filename = g_strdup (filename);
166
167   return module;
168 }
169
170 static gboolean
171 is_valid_module_name (const gchar *basename)
172 {
173 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
174   return
175     g_str_has_prefix (basename, "lib") &&
176     g_str_has_suffix (basename, ".so");
177 #else
178   return g_str_has_suffix (basename, ".dll");
179 #endif
180 }
181
182 /**
183  * g_io_modules_load_all_in_directory:
184  * @dirname: pathname for a directory containing modules to load.
185  * 
186  * Loads all the modules in the the specified directory. 
187  * 
188  * Returns: a list of #GIOModules loaded from the directory,
189  *      All the modules are loaded into memory, if you want to
190  *      unload them (enabling on-demand loading) you must call
191  *      g_type_module_unuse() on all the modules. Free the list
192  *      with g_list_free().
193  **/
194 GList *
195 g_io_modules_load_all_in_directory (const char *dirname)
196 {
197   const gchar *name;
198   GDir        *dir;
199   GList *modules;
200
201   if (!g_module_supported ())
202     return NULL;
203
204   dir = g_dir_open (dirname, 0, NULL);
205   if (!dir)
206     return NULL;
207
208   modules = NULL;
209   while ((name = g_dir_read_name (dir)))
210     {
211       if (is_valid_module_name (name))
212         {
213           GIOModule *module;
214           gchar     *path;
215
216           path = g_build_filename (dirname, name, NULL);
217           module = g_io_module_new (path);
218
219           if (!g_type_module_use (G_TYPE_MODULE (module)))
220             {
221               g_printerr ("Failed to load module: %s\n", path);
222               g_object_unref (module);
223               g_free (path);
224               continue;
225             }
226           
227           g_free (path);
228
229           modules = g_list_prepend (modules, module);
230         }
231     }
232   
233   g_dir_close (dir);
234
235   return modules;
236 }
237
238 G_LOCK_DEFINE_STATIC (loaded_dirs);
239
240 extern GType _g_inotify_directory_monitor_get_type (void);
241 extern GType _g_inotify_file_monitor_get_type (void);
242 extern GType _g_unix_volume_monitor_get_type (void);
243 extern GType _g_local_vfs_get_type (void);
244
245 void
246 _g_io_modules_ensure_loaded (void)
247 {
248   GList *modules, *l;
249   static gboolean loaded_dirs = FALSE;
250   int i;
251   GType *types;
252   guint n_types;
253   GQuark private_q, name_q;
254
255   G_LOCK (loaded_dirs);
256
257   if (!loaded_dirs)
258     {
259       loaded_dirs = TRUE;
260       modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
261
262       private_q = g_quark_from_static_string ("gio-prio");
263       name_q = g_quark_from_static_string ("gio-name");
264
265       /* Initialize types from built-in "modules" */
266 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
267       _g_inotify_directory_monitor_get_type ();
268       _g_inotify_file_monitor_get_type ();
269 #endif
270 #ifdef G_OS_UNIX
271       _g_unix_volume_monitor_get_type ();
272 #endif
273       _g_local_vfs_get_type ();
274
275       /* Copy over all prios to static gtype data so
276        * we can avoid loading the module again
277        */
278
279       types = g_type_children (G_TYPE_LOCAL_FILE_MONITOR, &n_types);
280       for (i = 0; i < n_types; i++)
281         {
282           GLocalFileMonitorClass *klass = g_type_class_ref (types[i]);
283           g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->prio));
284           g_type_class_unref (klass);
285         }
286       g_free (types);
287
288       types = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR, &n_types);
289       for (i = 0; i < n_types; i++)
290         {
291           GLocalDirectoryMonitorClass *klass = g_type_class_ref (types[i]);
292           g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->prio));
293           g_type_class_unref (klass);
294         }
295       g_free (types);
296
297       types = g_type_children (G_TYPE_NATIVE_VOLUME_MONITOR, &n_types);
298       for (i = 0; i < n_types; i++)
299         {
300           GNativeVolumeMonitorClass *klass = g_type_class_ref (types[i]);
301           g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->priority));
302           g_type_set_qdata (types[i], name_q, g_strdup (klass->name));
303           g_type_class_unref (klass);
304         }
305       g_free (types);
306       
307       types = g_type_children (G_TYPE_VFS, &n_types);
308       for (i = 0; i < n_types; i++)
309         {
310           GVfsClass *klass = g_type_class_ref (types[i]);
311           g_type_set_qdata (types[i], private_q, GINT_TO_POINTER (klass->priority));
312           g_type_set_qdata (types[i], name_q, g_strdup (klass->name));
313           g_type_class_unref (klass);
314         }
315       g_free (types);
316       
317       for (l = modules; l != NULL; l = l->next)
318         g_type_module_unuse (G_TYPE_MODULE (l->data));
319       
320       g_list_free (modules);
321     }
322
323   G_UNLOCK (loaded_dirs);
324 }
325
326 #define __G_IO_MODULE_C__
327 #include "gioaliasdef.c"