Update copyright information
[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         plugin = g_try_new0(struct connman_plugin, 1);
47         if (plugin == NULL)
48                 return FALSE;
49
50         plugin->handle = handle;
51         plugin->desc = desc;
52
53         if (desc->init() < 0) {
54                 g_free(plugin);
55                 return FALSE;
56         }
57
58         plugins = g_slist_append(plugins, plugin);
59
60         return TRUE;
61 }
62
63 int __connman_plugin_init(void)
64 {
65         GDir *dir;
66         const gchar *file;
67         gchar *filename;
68
69         DBG("");
70
71         dir = g_dir_open(PLUGINDIR, 0, NULL);
72         if (dir != NULL) {
73                 while ((file = g_dir_read_name(dir)) != NULL) {
74                         void *handle;
75                         struct connman_plugin_desc *desc;
76
77                         if (g_str_has_prefix(file, "lib") == TRUE ||
78                                         g_str_has_suffix(file, ".so") == FALSE)
79                                 continue;
80
81                         filename = g_build_filename(PLUGINDIR, file, NULL);
82
83                         handle = dlopen(filename, RTLD_NOW);
84                         if (handle == NULL) {
85                                 g_warning("Can't load %s: %s", filename,
86                                                                 dlerror());
87                                 g_free(filename);
88                                 continue;
89                         }
90
91                         g_free(filename);
92
93                         desc = dlsym(handle, "connman_plugin_desc");
94                         if (desc == NULL) {
95                                 g_warning("Can't load symbol: %s", dlerror());
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 }