Add support for Update method via D-Bus
[platform/upstream/connman.git] / src / plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <glib.h>
29 #include <gmodule.h>
30
31 #include <connman/plugin.h>
32
33 #include "connman.h"
34
35 static GSList *plugins = NULL;
36
37 struct connman_plugin {
38         GModule *module;
39         struct connman_plugin_desc *desc;
40 };
41
42 static gboolean add_plugin(GModule *module, struct connman_plugin_desc *desc)
43 {
44         struct connman_plugin *plugin;
45
46         plugin = g_try_new0(struct connman_plugin, 1);
47         if (plugin == NULL)
48                 return FALSE;
49
50         plugin->module = module;
51         plugin->desc = desc;
52
53         plugins = g_slist_append(plugins, plugin);
54
55         desc->init();
56
57         return TRUE;
58 }
59
60 static void load_plugins(const gchar *path)
61 {
62         GDir *dir;
63         const gchar *file;
64         gchar *filename;
65
66         dir = g_dir_open(path, 0, NULL);
67         if (dir != NULL) {
68                 while ((file = g_dir_read_name(dir)) != NULL) {
69                         GModule *module;
70                         struct connman_plugin_desc *desc;
71
72                         if (g_str_has_prefix(file, "lib") == TRUE ||
73                                         g_str_has_suffix(file, ".so") == FALSE)
74                                 continue;
75
76                         filename = g_build_filename(path, file, NULL);
77
78                         module = g_module_open(filename, 0);
79                         if (module == NULL) {
80                                 g_warning("Can't load %s", filename);
81                                 continue;
82                         }
83
84                         g_free(filename);
85
86                         DBG("%s", g_module_name(module));
87
88                         if (g_module_symbol(module, "connman_plugin_desc",
89                                                 (gpointer) &desc) == FALSE) {
90                                 g_warning("Can't load symbol");
91                                 g_module_close(module);
92                                 continue;
93                         }
94
95                         if (desc == NULL || desc->init == NULL) {
96                                 g_module_close(module);
97                                 continue;
98                         }
99
100                         if (add_plugin(module, desc) == FALSE)
101                                 g_module_close(module);
102                 }
103
104                 g_dir_close(dir);
105         }
106 }
107
108 int __connman_plugin_init(void)
109 {
110         DBG("");
111
112         if (g_module_supported() == FALSE) {
113                 g_warning("Modules not supported: %s", g_module_error());
114                 return FALSE;
115         }
116
117         load_plugins(PLUGINDIR);
118
119         return 0;
120 }
121
122 void __connman_plugin_cleanup(void)
123 {
124         GSList *list;
125
126         DBG("");
127
128         for (list = plugins; list; list = list->next) {
129                 struct connman_plugin *plugin = list->data;
130
131                 DBG("%s", g_module_name(plugin->module));
132
133                 if (plugin->desc->exit)
134                         plugin->desc->exit();
135
136                 g_module_close(plugin->module);
137
138                 g_free(plugin);
139         }
140
141         g_slist_free(plugins);
142 }