Add properties for WiFi services
[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 #include "connman.h"
31
32 static GSList *plugins = NULL;
33
34 struct connman_plugin {
35         void *handle;
36         gboolean active;
37         struct connman_plugin_desc *desc;
38 };
39
40 static gint compare_priority(gconstpointer a, gconstpointer b)
41 {
42         const struct connman_plugin *plugin1 = a;
43         const struct connman_plugin *plugin2 = b;
44
45         return plugin2->desc->priority - plugin1->desc->priority;
46 }
47
48 static gboolean add_plugin(void *handle, struct connman_plugin_desc *desc)
49 {
50         struct connman_plugin *plugin;
51
52         if (desc->init == NULL)
53                 return FALSE;
54
55         if (g_str_equal(desc->version, CONNMAN_VERSION) == FALSE) {
56                 DBG("version mismatch for %s", desc->description);
57                 return FALSE;
58         }
59
60         plugin = g_try_new0(struct connman_plugin, 1);
61         if (plugin == NULL)
62                 return FALSE;
63
64         plugin->handle = handle;
65         plugin->active = FALSE;
66         plugin->desc = desc;
67
68         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
69
70         return TRUE;
71 }
72
73 int __connman_plugin_init(const char *pattern, const char *exclude)
74 {
75         GSList *list;
76         GDir *dir;
77         const gchar *file;
78         gchar *filename;
79
80         DBG("");
81
82         dir = g_dir_open(PLUGINDIR, 0, NULL);
83         if (dir != NULL) {
84                 while ((file = g_dir_read_name(dir)) != NULL) {
85                         void *handle;
86                         struct connman_plugin_desc *desc;
87
88                         if (g_str_has_prefix(file, "lib") == TRUE ||
89                                         g_str_has_suffix(file, ".so") == FALSE)
90                                 continue;
91
92                         filename = g_build_filename(PLUGINDIR, file, NULL);
93
94                         handle = dlopen(filename, RTLD_NOW);
95                         if (handle == NULL) {
96                                 g_warning("Can't load %s: %s", filename,
97                                                                 dlerror());
98                                 g_free(filename);
99                                 continue;
100                         }
101
102                         g_free(filename);
103
104                         desc = dlsym(handle, "connman_plugin_desc");
105                         if (desc == NULL) {
106                                 g_warning("Can't load symbol: %s", dlerror());
107                                 dlclose(handle);
108                                 continue;
109                         }
110
111                         if (exclude != NULL && g_pattern_match_simple(exclude,
112                                                         desc->name) == TRUE) {
113                                 DBG("excluding %s", desc->description);
114                                 dlclose(handle);
115                                 continue;
116                         }
117
118                         if (pattern != NULL && g_pattern_match_simple(pattern,
119                                                         desc->name) == FALSE) {
120                                 DBG("ignoring %s", desc->description);
121                                 dlclose(handle);
122                                 continue;
123                         }
124
125                         if (add_plugin(handle, desc) == FALSE)
126                                 dlclose(handle);
127                 }
128
129                 g_dir_close(dir);
130         }
131
132         for (list = plugins; list; list = list->next) {
133                 struct connman_plugin *plugin = list->data;
134
135                 if (plugin->desc->init() < 0)
136                         continue;
137
138                 plugin->active = TRUE;
139         }
140
141         return 0;
142 }
143
144 void __connman_plugin_cleanup(void)
145 {
146         GSList *list;
147
148         DBG("");
149
150         for (list = plugins; list; list = list->next) {
151                 struct connman_plugin *plugin = list->data;
152
153                 if (plugin->active == TRUE && plugin->desc->exit)
154                         plugin->desc->exit();
155
156                 dlclose(plugin->handle);
157
158                 g_free(plugin);
159         }
160
161         g_slist_free(plugins);
162 }