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