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