prevent multiple debug outputs in case of non-monolithic plugins
[strongswan.git] / src / libstrongswan / plugins / plugin_loader.c
1 /*
2 * Copyright (C) 2010 Tobias Brunner
3 * Copyright (C) 2007 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include "plugin_loader.h"
19
20 #include <string.h>
21 #include <dlfcn.h>
22 #include <limits.h>
23 #include <stdio.h>
24
25 #include <debug.h>
26 #include <integrity_checker.h>
27 #include <utils/linked_list.h>
28 #include <plugins/plugin.h>
29
30 typedef struct private_plugin_loader_t private_plugin_loader_t;
31
32 /**
33 * private data of plugin_loader
34 */
35 struct private_plugin_loader_t {
36
37 /**
38 * public functions
39 */
40 plugin_loader_t public;
41
42 /**
43 * list of loaded plugins
44 */
45 linked_list_t *plugins;
46
47 /**
48 * names of loaded plugins
49 */
50 linked_list_t *names;
51 };
52
53 /**
54 * create a plugin
55 * returns: NOT_FOUND, if the constructor was not found
56 * FAILED, if the plugin could not be constructed
57 */
58 static status_t create_plugin(private_plugin_loader_t *this, void *handle,
59 char *name, bool integrity, plugin_t **plugin)
60 {
61 char create[128];
62 plugin_constructor_t constructor;
63
64 if (snprintf(create, sizeof(create), "%s_plugin_create",
65 name) >= sizeof(create))
66 {
67 return FAILED;
68 }
69 translate(create, "-", "_");
70 constructor = dlsym(handle, create);
71 if (constructor == NULL)
72 {
73 return NOT_FOUND;
74 }
75 if (integrity && lib->integrity)
76 {
77 if (!lib->integrity->check_segment(lib->integrity, name, constructor))
78 {
79 DBG1(DBG_LIB, "plugin '%s': failed segment integrity test", name);
80 return FAILED;
81 }
82 DBG1(DBG_LIB, "plugin '%s': passed file and segment integrity tests",
83 name);
84 }
85 *plugin = constructor();
86 if (*plugin == NULL)
87 {
88 DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
89 create);
90 return FAILED;
91 }
92 DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
93 return SUCCESS;
94 }
95
96 /**
97 * load a single plugin
98 */
99 static plugin_t* load_plugin(private_plugin_loader_t *this,
100 char *path, char *name)
101 {
102 char file[PATH_MAX];
103 void *handle;
104 plugin_t *plugin;
105
106 switch (create_plugin(this, RTLD_DEFAULT, name, FALSE, &plugin))
107 {
108 case SUCCESS:
109 return plugin;
110 case NOT_FOUND:
111 /* try to load the plugin from a file */
112 break;
113 default:
114 return NULL;
115 }
116
117 if (snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path,
118 name) >= sizeof(file))
119 {
120 return NULL;
121 }
122 if (lib->integrity)
123 {
124 if (!lib->integrity->check_file(lib->integrity, name, file))
125 {
126 DBG1(DBG_LIB, "plugin '%s': failed file integrity test of '%s'",
127 name, file);
128 return NULL;
129 }
130 }
131 handle = dlopen(file, RTLD_LAZY);
132 if (handle == NULL)
133 {
134 DBG1(DBG_LIB, "plugin '%s' failed to load: %s", name, dlerror());
135 return NULL;
136 }
137 if (create_plugin(this, handle, name, TRUE, &plugin) != SUCCESS)
138 {
139 dlclose(handle);
140 return NULL;
141 }
142 /* we do not store or free dlopen() handles, leak_detective requires
143 * the modules to keep loaded until leak report */
144 return plugin;
145 }
146
147 /**
148 * Check if a plugin is already loaded
149 */
150 static bool plugin_loaded(private_plugin_loader_t *this, char *name)
151 {
152 enumerator_t *enumerator;
153 bool found = FALSE;
154 char *current;
155
156 enumerator = this->names->create_enumerator(this->names);
157 while (enumerator->enumerate(enumerator, &current))
158 {
159 if (streq(name, current))
160 {
161 found = TRUE;
162 break;
163 }
164 }
165 enumerator->destroy(enumerator);
166 return found;
167 }
168
169 /**
170 * Implementation of plugin_loader_t.load_plugins.
171 */
172 static bool load(private_plugin_loader_t *this, char *path, char *list)
173 {
174 enumerator_t *enumerator;
175 char *token;
176 bool critical_failed = FALSE;
177
178 if (path == NULL)
179 {
180 path = PLUGINDIR;
181 }
182
183 enumerator = enumerator_create_token(list, " ", " ");
184 while (!critical_failed && enumerator->enumerate(enumerator, &token))
185 {
186 plugin_t *plugin;
187 bool critical = FALSE;
188 int len;
189
190 token = strdup(token);
191 len = strlen(token);
192 if (token[len-1] == '!')
193 {
194 critical = TRUE;
195 token[len-1] = '\0';
196 }
197 if (plugin_loaded(this, token))
198 {
199 free(token);
200 continue;
201 }
202 plugin = load_plugin(this, path, token);
203 if (plugin)
204 {
205 this->plugins->insert_last(this->plugins, plugin);
206 this->names->insert_last(this->names, token);
207 }
208 else
209 {
210 if (critical)
211 {
212 critical_failed = TRUE;
213 DBG1(DBG_LIB, "loading critical plugin '%s' failed", token);
214 }
215 free(token);
216 }
217 }
218 enumerator->destroy(enumerator);
219 return !critical_failed;
220 }
221
222 /**
223 * Implementation of plugin_loader_t.unload
224 */
225 static void unload(private_plugin_loader_t *this)
226 {
227 plugin_t *plugin;
228 char *name;
229
230 /* unload plugins in reverse order */
231 while (this->plugins->remove_last(this->plugins,
232 (void**)&plugin) == SUCCESS)
233 {
234 plugin->destroy(plugin);
235 }
236 while (this->names->remove_last(this->names, (void**)&name) == SUCCESS)
237 {
238 free(name);
239 }
240 }
241
242 /**
243 * Implementation of plugin_loader_t.create_plugin_enumerator
244 */
245 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
246 {
247 return this->names->create_enumerator(this->names);
248 }
249
250 /**
251 * Implementation of plugin_loader_t.destroy
252 */
253 static void destroy(private_plugin_loader_t *this)
254 {
255 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
256 this->names->destroy_function(this->names, free);
257 free(this);
258 }
259
260 /*
261 * see header file
262 */
263 plugin_loader_t *plugin_loader_create()
264 {
265 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
266
267 this->public.load = (bool(*)(plugin_loader_t*, char *path, char *prefix))load;
268 this->public.unload = (void(*)(plugin_loader_t*))unload;
269 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
270 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
271
272 this->plugins = linked_list_create();
273 this->names = linked_list_create();
274
275 return &this->public;
276 }
277