Release 0.0.3
[profile/ivi/gsignond.git] / src / gplugind / gsignond-plugin-loader.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012-2014 Intel Corporation.
7  *
8  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26
27 #include <gmodule.h>
28
29 #include "config.h"
30
31 #include "gsignond/gsignond-log.h"
32 #include "gsignond-plugin-loader.h"
33
34 GSignondPlugin *
35 gsignond_load_plugin (
36         GSignondConfig* config,
37         const gchar *plugin_type)
38 {
39     const gchar *plugin_path = GSIGNOND_GPLUGINS_DIR;
40     gchar *plugin_filename;
41     GSignondPlugin *plugin;
42
43 #   ifdef ENABLE_DEBUG
44     const gchar *env_val = g_getenv("SSO_GPLUGINS_DIR");
45     if (env_val)
46         plugin_path = env_val;
47 #   endif
48     plugin_filename = g_module_build_path (plugin_path, plugin_type);
49     plugin = gsignond_load_plugin_with_filename (plugin_type,
50                                                  plugin_filename);
51     g_free(plugin_filename);
52     return plugin;
53 }
54
55 GSignondPlugin *
56 gsignond_load_plugin_with_filename(
57         const gchar *plugin_type,
58         const gchar *plugin_filename)
59 {
60     DBG("Loading plugin %s", plugin_filename);
61     GModule* plugin_module = g_module_open (plugin_filename, 
62             G_MODULE_BIND_LOCAL);
63     if (plugin_module == NULL) {
64         DBG("Plugin couldn't be opened: %s", g_module_error());
65         return NULL;
66     }
67
68     gchar* plugin_get_type = g_strdup_printf("gsignond_%s_plugin_get_type",
69         plugin_type);
70     gpointer p;
71
72     DBG("Resolving symbol %s", plugin_get_type);
73     gboolean symfound = g_module_symbol (plugin_module,
74         plugin_get_type, &p);
75     g_free(plugin_get_type);
76     if (!symfound) {
77         DBG("Symbol couldn't be resolved");
78         g_module_close (plugin_module);
79         return NULL;
80     }
81     
82     DBG("Creating plugin object");
83     GType (*plugin_get_type_f)(void) = p;
84     GSignondPlugin* plugin = g_object_new(plugin_get_type_f(), NULL);
85     if (plugin == NULL) {
86         DBG("Plugin couldn't be created");
87         g_module_close (plugin_module);
88         return NULL;
89     }
90     g_module_make_resident (plugin_module);
91     return plugin;
92 }