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