Disconnect service on removal if still connected
[connman] / src / plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <dlfcn.h>
27
28 #include <glib.h>
29
30 #ifdef CONNMAN_PLUGIN_BUILTIN
31 #undef CONNMAN_PLUGIN_BUILTIN
32 #endif
33
34 #include "connman.h"
35
36 static GSList *plugins = NULL;
37
38 struct connman_plugin {
39         void *handle;
40         gboolean active;
41         struct connman_plugin_desc *desc;
42 };
43
44 static gint compare_priority(gconstpointer a, gconstpointer b)
45 {
46         const struct connman_plugin *plugin1 = a;
47         const struct connman_plugin *plugin2 = b;
48
49         return plugin2->desc->priority - plugin1->desc->priority;
50 }
51
52 static gboolean add_plugin(void *handle, struct connman_plugin_desc *desc)
53 {
54         struct connman_plugin *plugin;
55
56         if (desc->init == NULL)
57                 return FALSE;
58
59         if (g_str_equal(desc->version, CONNMAN_VERSION) == FALSE) {
60                 connman_error("Version mismatch for %s", desc->description);
61                 return FALSE;
62         }
63
64         plugin = g_try_new0(struct connman_plugin, 1);
65         if (plugin == NULL)
66                 return FALSE;
67
68         plugin->handle = handle;
69         plugin->active = FALSE;
70         plugin->desc = desc;
71
72         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
73
74         return TRUE;
75 }
76
77 static gboolean check_plugin(struct connman_plugin_desc *desc,
78                                 const char *pattern, const char *exclude)
79 {
80         if (exclude != NULL &&
81                         g_pattern_match_simple(exclude, desc->name) == TRUE) {
82                 connman_info("Excluding %s", desc->description);
83                 return FALSE;
84         }
85
86         if (pattern != NULL &&
87                         g_pattern_match_simple(pattern, desc->name) == FALSE) {
88                 connman_info("Ignoring %s", desc->description);
89                 return FALSE;
90         }
91
92         return TRUE;
93 }
94
95 #include "builtin.h"
96
97 int __connman_plugin_init(const char *pattern, const char *exclude)
98 {
99         GSList *list;
100         GDir *dir;
101         const gchar *file;
102         gchar *filename;
103         unsigned int i;
104
105         DBG("");
106
107         for (i = 0; __connman_builtin[i]; i++) {
108                 if (check_plugin(__connman_builtin[i],
109                                                 pattern, exclude) == FALSE)
110                         continue;
111
112                 add_plugin(NULL, __connman_builtin[i]);
113         }
114
115         dir = g_dir_open(PLUGINDIR, 0, NULL);
116         if (dir != NULL) {
117                 while ((file = g_dir_read_name(dir)) != NULL) {
118                         void *handle;
119                         struct connman_plugin_desc *desc;
120
121                         if (g_str_has_prefix(file, "lib") == TRUE ||
122                                         g_str_has_suffix(file, ".so") == FALSE)
123                                 continue;
124
125                         filename = g_build_filename(PLUGINDIR, file, NULL);
126
127                         handle = dlopen(filename, RTLD_NOW);
128                         if (handle == NULL) {
129                                 connman_error("Can't load %s: %s",
130                                                         filename, dlerror());
131                                 g_free(filename);
132                                 continue;
133                         }
134
135                         g_free(filename);
136
137                         desc = dlsym(handle, "connman_plugin_desc");
138                         if (desc == NULL) {
139                                 connman_error("Can't load symbol: %s",
140                                                                 dlerror());
141                                 dlclose(handle);
142                                 continue;
143                         }
144
145                         if (check_plugin(desc, pattern, exclude) == FALSE) {
146                                 dlclose(handle);
147                                 continue;
148                         }
149
150                         if (add_plugin(handle, desc) == FALSE)
151                                 dlclose(handle);
152                 }
153
154                 g_dir_close(dir);
155         }
156
157         for (list = plugins; list; list = list->next) {
158                 struct connman_plugin *plugin = list->data;
159
160                 if (plugin->desc->init() < 0)
161                         continue;
162
163                 plugin->active = TRUE;
164         }
165
166         return 0;
167 }
168
169 void __connman_plugin_cleanup(void)
170 {
171         GSList *list;
172
173         DBG("");
174
175         for (list = plugins; list; list = list->next) {
176                 struct connman_plugin *plugin = list->data;
177
178                 if (plugin->active == TRUE && plugin->desc->exit)
179                         plugin->desc->exit();
180
181                 if (plugin->handle != NULL)
182                         dlclose(plugin->handle);
183
184                 g_free(plugin);
185         }
186
187         g_slist_free(plugins);
188 }