1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  *  Copyright (C) 2006 Kouhei Sutou <kou@cozmixng.org>
4  *
5  *  This library is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Lesser General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (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
13  *  GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  *  $Id: cut-module.c 1206 2007-04-20 10:29:28Z ktou $
19  */
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <gmodule.h>
25 
26 #include "cut-module.h"
27 #include "cut-module-impl.h"
28 
29 #define CUT_MODULE_GET_PRIVATE(obj) \
30   (G_TYPE_INSTANCE_GET_PRIVATE((obj), CUT_TYPE_MODULE, CutModulePrivate))
31 
32 typedef struct _CutModulePrivate  CutModulePrivate;
33 struct _CutModulePrivate
34 {
35     GModule      *library;
36     gchar        *mod_path;
37     GList        *registered_types;
38 
39     CutModuleInitFunc         init;
40     CutModuleExitFunc         exit;
41     CutModuleInstantiateFunc  instantiate;
42 };
43 
44 G_DEFINE_TYPE(CutModule, cut_module, G_TYPE_TYPE_MODULE)
45 
46 static void     finalize        (GObject     *object);
47 static gboolean load            (GTypeModule *module);
48 static void     unload          (GTypeModule *module);
49 
50 static void     _cut_module_show_error   (GModule     *module);
51 static GModule *_cut_module_open         (const gchar *mod_path);
52 static void     _cut_module_close        (GModule     *module);
53 static gboolean _cut_module_load_func    (GModule     *module,
54                                           const gchar *func_name,
55                                           gpointer    *symbol);
56 static gboolean _cut_module_match_name   (const gchar *mod_path,
57                                           const gchar *name);
58 
59 static void
cut_module_class_init(CutModuleClass * klass)60 cut_module_class_init (CutModuleClass *klass)
61 {
62     GObjectClass *gobject_class;
63     GTypeModuleClass *type_module_class;
64 
65     gobject_class = G_OBJECT_CLASS(klass);
66     gobject_class->finalize     = finalize;
67 
68     type_module_class = G_TYPE_MODULE_CLASS(klass);
69     type_module_class->load     = load;
70     type_module_class->unload   = unload;
71 
72     g_type_class_add_private(gobject_class, sizeof(CutModulePrivate));
73 }
74 
75 static void
cut_module_init(CutModule * module)76 cut_module_init (CutModule *module)
77 {
78     CutModulePrivate *priv = CUT_MODULE_GET_PRIVATE(module);
79 
80     priv->library          = NULL;
81     priv->mod_path         = NULL;
82     priv->registered_types = NULL;
83 }
84 
85 static void
finalize(GObject * object)86 finalize (GObject *object)
87 {
88     CutModulePrivate *priv = CUT_MODULE_GET_PRIVATE(object);
89 
90     g_free(priv->mod_path);
91     priv->mod_path = NULL;
92     g_list_free(priv->registered_types);
93     priv->registered_types = NULL;
94 
95     G_OBJECT_CLASS(cut_module_parent_class)->finalize(object);
96 }
97 
98 static gboolean
load(GTypeModule * module)99 load (GTypeModule *module)
100 {
101     CutModulePrivate *priv = CUT_MODULE_GET_PRIVATE(module);
102 
103     priv->library = _cut_module_open(priv->mod_path);
104     if (!priv->library)
105         return FALSE;
106 
107     if (!_cut_module_load_func(priv->library,
108                                G_STRINGIFY(CUT_MODULE_IMPL_INIT),
109                                (gpointer)&priv->init) ||
110         !_cut_module_load_func(priv->library,
111                                G_STRINGIFY(CUT_MODULE_IMPL_EXIT),
112                                (gpointer)&priv->exit) ||
113         !_cut_module_load_func(priv->library,
114                                G_STRINGIFY(CUT_MODULE_IMPL_INSTANTIATE),
115                                (gpointer)&priv->instantiate)) {
116         _cut_module_close(priv->library);
117         priv->library = NULL;
118         return FALSE;
119     }
120 
121     g_list_free(priv->registered_types);
122     priv->registered_types = priv->init(module);
123 
124     return TRUE;
125 }
126 
127 static void
unload(GTypeModule * module)128 unload (GTypeModule *module)
129 {
130     CutModulePrivate *priv = CUT_MODULE_GET_PRIVATE(module);
131 
132     priv->exit();
133 
134     _cut_module_close(priv->library);
135     priv->library  = NULL;
136 
137     priv->init = NULL;
138     priv->exit = NULL;
139     priv->instantiate = NULL;
140 
141     g_list_free(priv->registered_types);
142     priv->registered_types = NULL;
143 }
144 
145 GList *
cut_module_collect_registered_types(GList * modules)146 cut_module_collect_registered_types (GList *modules)
147 {
148     GList *results = NULL;
149     GList *node;
150 
151     for (node = modules; node; node = g_list_next(node)) {
152         CutModule *module = node->data;
153         GTypeModule *g_type_module;
154 
155         g_type_module = G_TYPE_MODULE(module);
156         if (g_type_module_use(g_type_module)) {
157             CutModulePrivate *priv;
158             GList *node;
159 
160             priv = CUT_MODULE_GET_PRIVATE(module);
161             for (node = priv->registered_types;
162                  node;
163                  node = g_list_next(node)) {
164                 results = g_list_prepend(results, node->data);
165             }
166 
167             g_type_module_unuse(g_type_module);
168         }
169     }
170 
171     return results;
172 }
173 
174 GList *
cut_module_collect_names(GList * modules)175 cut_module_collect_names (GList *modules)
176 {
177     GList *results = NULL;
178     GList *node;
179 
180     for (node = modules; node; node = g_list_next(node)) {
181         CutModule *module;
182 
183         module = node->data;
184         results = g_list_prepend(results, G_TYPE_MODULE(module)->name);
185     }
186 
187     return results;
188 }
189 
190 static void
_cut_module_show_error(GModule * module)191 _cut_module_show_error (GModule *module)
192 {
193     const gchar *module_error_message;
194 
195     module_error_message = g_module_error();
196     if (!module_error_message)
197         return;
198 
199     if (module) {
200         g_warning("%s: %s", g_module_name(module), module_error_message);
201     } else {
202         g_warning("%s", module_error_message);
203     }
204 }
205 
206 CutModule *
cut_module_find(GList * modules,const gchar * name)207 cut_module_find (GList *modules, const gchar *name)
208 {
209     GList *node;
210 
211     for (node = modules; node; node = g_list_next(node)) {
212         CutModule *module = node->data;
213         CutModulePrivate *priv;
214 
215         priv = CUT_MODULE_GET_PRIVATE(module);
216         if (_cut_module_match_name(priv->mod_path, name))
217             return module;
218     }
219 
220     return NULL;
221 }
222 
223 GObject *
cut_module_instantiate(CutModule * module,const gchar * first_property,va_list var_args)224 cut_module_instantiate (CutModule *module,
225                         const gchar *first_property, va_list var_args)
226 {
227     GObject *object = NULL;
228     CutModulePrivate *priv;
229 
230     priv = CUT_MODULE_GET_PRIVATE(module);
231     if (g_type_module_use(G_TYPE_MODULE(module))) {
232         object = priv->instantiate(first_property, var_args);
233         g_type_module_unuse(G_TYPE_MODULE(module));
234     }
235 
236     return object;
237 }
238 
239 
240 static GModule *
_cut_module_open(const gchar * mod_path)241 _cut_module_open (const gchar *mod_path)
242 {
243     GModule *module;
244 
245     module = g_module_open(mod_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
246     if (!module) {
247         _cut_module_show_error(NULL);
248     }
249 
250     return module;
251 }
252 
253 static void
_cut_module_close(GModule * module)254 _cut_module_close (GModule *module)
255 {
256     if (module && g_module_close(module)) {
257         _cut_module_show_error(NULL);
258     }
259 }
260 
261 static gchar *
_cut_module_module_file_name(const gchar * name)262 _cut_module_module_file_name (const gchar *name)
263 {
264     return g_strconcat(name, "." G_MODULE_SUFFIX, NULL);
265 }
266 
267 static gboolean
_cut_module_load_func(GModule * module,const gchar * func_name,gpointer * symbol)268 _cut_module_load_func (GModule *module, const gchar *func_name,
269                        gpointer *symbol)
270 {
271     g_return_val_if_fail(module, FALSE);
272 
273     if (g_module_symbol(module, func_name, symbol)) {
274         return TRUE;
275     } else {
276         _cut_module_show_error(module);
277         return FALSE;
278     }
279 }
280 
281 CutModule *
cut_module_load_module(const gchar * base_dir,const gchar * name)282 cut_module_load_module (const gchar *base_dir, const gchar *name)
283 {
284     gchar *mod_base_name, *mod_path;
285     CutModule *module = NULL;
286 
287     mod_base_name = g_build_filename(base_dir, name, NULL);
288     if (g_str_has_suffix(mod_base_name, G_MODULE_SUFFIX)) {
289         mod_path = mod_base_name;
290     } else {
291         mod_path = _cut_module_module_file_name(mod_base_name);
292         g_free(mod_base_name);
293     }
294 
295     if (g_file_test(mod_path, G_FILE_TEST_EXISTS)) {
296         CutModulePrivate *priv;
297         gchar *mod_name;
298 
299         module = g_object_new(CUT_TYPE_MODULE, NULL);
300         priv = CUT_MODULE_GET_PRIVATE(module);
301         priv->mod_path = g_strdup(mod_path);
302 
303         mod_name = g_strdup(name);
304         if (g_str_has_suffix(mod_name, "."G_MODULE_SUFFIX)) {
305             guint last_index;
306             last_index = strlen(mod_name) - strlen("."G_MODULE_SUFFIX);
307             mod_name[last_index] = '\0';
308         }
309         g_type_module_set_name(G_TYPE_MODULE(module), mod_name);
310         g_free(mod_name);
311     }
312     g_free(mod_path);
313 
314     return module;
315 }
316 
317 GList *
cut_module_load_modules(const gchar * base_dir)318 cut_module_load_modules (const gchar *base_dir)
319 {
320     return cut_module_load_modules_unique(base_dir, NULL);
321 }
322 
323 GList *
cut_module_load_modules_unique(const gchar * base_dir,GList * exist_modules)324 cut_module_load_modules_unique (const gchar *base_dir, GList *exist_modules)
325 {
326     GDir *dir;
327     GSList *sorted_entries = NULL;
328     GSList *node = NULL;
329     GList *modules = NULL;
330     const gchar *entry;
331 
332     dir = g_dir_open(base_dir, 0, NULL);
333     if (!dir)
334         return exist_modules;
335 
336     while ((entry = g_dir_read_name(dir))) {
337         sorted_entries = g_slist_prepend(sorted_entries, g_strdup(entry));
338     }
339     sorted_entries = g_slist_sort(sorted_entries, (GCompareFunc)strcmp);
340     for (node = sorted_entries; node; node = g_slist_next(node)) {
341         CutModule *module;
342         GTypeModule *g_module;
343 
344         entry = node->data;
345         module = cut_module_load_module(base_dir, entry);
346         if (!module)
347             continue;
348 
349         g_module = G_TYPE_MODULE(module);
350         if (cut_module_find(exist_modules, g_module->name))
351             cut_module_unload(module);
352         else
353             modules = g_list_prepend(modules, module);
354     }
355     g_slist_foreach(sorted_entries, (GFunc)g_free, NULL);
356     g_slist_free(sorted_entries);
357     g_dir_close(dir);
358 
359     return g_list_concat(modules, exist_modules);
360 }
361 
362 static gboolean
_cut_module_match_name(const gchar * mod_path,const gchar * name)363 _cut_module_match_name (const gchar *mod_path, const gchar *name)
364 {
365     gboolean matched;
366     gchar *module_base_name, *normalized_matched_name;
367 
368     module_base_name = g_path_get_basename(mod_path);
369     normalized_matched_name = g_strconcat(name, "." G_MODULE_SUFFIX, NULL);
370 
371     matched = (0 == strcmp(module_base_name, normalized_matched_name));
372 
373     g_free(module_base_name);
374     g_free(normalized_matched_name);
375 
376     return matched;
377 }
378 
379 void
cut_module_unload(CutModule * module)380 cut_module_unload (CutModule *module)
381 {
382     GTypeModule *type_module;
383 
384     g_return_if_fail(CUT_IS_MODULE(module));
385 
386     type_module = G_TYPE_MODULE(module);
387 
388     if (type_module->type_infos || type_module->interface_infos)
389         return;
390 
391     g_object_unref(module);
392 }
393 
394 /*
395 vi:ts=4:nowrap:ai:expandtab:sw=4
396 */
397