Force symbol resolving and fix memory leak
[connman] / 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 <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         plugin = g_try_new0(struct connman_plugin, 1);
44         if (plugin == NULL)
45                 return FALSE;
46
47         plugin->handle = handle;
48         plugin->desc = desc;
49
50         plugins = g_slist_append(plugins, plugin);
51
52         desc->init();
53
54         return TRUE;
55 }
56
57 int __connman_plugin_init(void)
58 {
59         GDir *dir;
60         const gchar *file;
61         gchar *filename;
62
63         DBG("");
64
65         dir = g_dir_open(PLUGINDIR, 0, NULL);
66         if (dir != NULL) {
67                 while ((file = g_dir_read_name(dir)) != NULL) {
68                         void *handle;
69                         struct connman_plugin_desc *desc;
70
71                         if (g_str_has_prefix(file, "lib") == TRUE ||
72                                         g_str_has_suffix(file, ".so") == FALSE)
73                                 continue;
74
75                         filename = g_build_filename(PLUGINDIR, file, NULL);
76
77                         handle = dlopen(filename, RTLD_NOW);
78                         if (handle == NULL) {
79                                 g_warning("Can't load %s: %s", filename,
80                                                                 dlerror());
81                                 g_free(filename);
82                                 continue;
83                         }
84
85                         g_free(filename);
86
87                         desc = dlsym(handle, "connman_plugin_desc");
88                         if (desc == NULL) {
89                                 g_warning("Can't load symbol: %s", dlerror());
90                                 dlclose(handle);
91                                 continue;
92                         }
93
94                         if (desc->init == NULL) {
95                                 dlclose(handle);
96                                 continue;
97                         }
98
99                         if (add_plugin(handle, desc) == FALSE)
100                                 dlclose(handle);
101                 }
102
103                 g_dir_close(dir);
104         }
105
106         return 0;
107 }
108
109 void __connman_plugin_cleanup(void)
110 {
111         GSList *list;
112
113         DBG("");
114
115         for (list = plugins; list; list = list->next) {
116                 struct connman_plugin *plugin = list->data;
117
118                 if (plugin->desc->exit)
119                         plugin->desc->exit();
120
121                 dlclose(plugin->handle);
122
123                 g_free(plugin);
124         }
125
126         g_slist_free(plugins);
127 }