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