Do not print filename twice if plugin loading fails, dlerror() contains the filename
[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 #ifdef MONOLITHIC
54 /**
55 * load a single plugin in monolithic mode
56 */
57 static plugin_t* load_plugin(private_plugin_loader_t *this,
58 char *path, char *name)
59 {
60 char create[128];
61 plugin_t *plugin;
62 plugin_constructor_t constructor;
63
64 if (snprintf(create, sizeof(create), "%s_plugin_create",
65 name) >= sizeof(create))
66 {
67 return NULL;
68 }
69 translate(create, "-", "_");
70 constructor = dlsym(RTLD_DEFAULT, create);
71 if (constructor == NULL)
72 {
73 DBG1(DBG_LIB, "plugin '%s': failed to load - %s not found", name,
74 create);
75 return NULL;
76 }
77 plugin = constructor();
78 if (plugin == NULL)
79 {
80 DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
81 create);
82 return NULL;
83 }
84 DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
85
86 return plugin;
87 }
88 #else
89 /**
90 * load a single plugin
91 */
92 static plugin_t* load_plugin(private_plugin_loader_t *this,
93 char *path, char *name)
94 {
95 char create[128];
96 char file[PATH_MAX];
97 void *handle;
98 plugin_t *plugin;
99 plugin_constructor_t constructor;
100
101 if (snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path,
102 name) >= sizeof(file) ||
103 snprintf(create, sizeof(create), "%s_plugin_create",
104 name) >= sizeof(create))
105 {
106 return NULL;
107 }
108 translate(create, "-", "_");
109 if (lib->integrity)
110 {
111 if (!lib->integrity->check_file(lib->integrity, name, file))
112 {
113 DBG1(DBG_LIB, "plugin '%s': failed file integrity test of '%s'",
114 name, file);
115 return NULL;
116 }
117 }
118 handle = dlopen(file, RTLD_LAZY);
119 if (handle == NULL)
120 {
121 DBG1(DBG_LIB, "plugin '%s' failed to load: %s", name, dlerror());
122 return NULL;
123 }
124 constructor = dlsym(handle, create);
125 if (constructor == NULL)
126 {
127 DBG1(DBG_LIB, "plugin '%s': failed to load - %s not found", name,
128 create);
129 dlclose(handle);
130 return NULL;
131 }
132 if (lib->integrity)
133 {
134 if (!lib->integrity->check_segment(lib->integrity, name, constructor))
135 {
136 DBG1(DBG_LIB, "plugin '%s': failed segment integrity test", name);
137 dlclose(handle);
138 return NULL;
139 }
140 DBG1(DBG_LIB, "plugin '%s': passed file and segment integrity tests",
141 name);
142 }
143 plugin = constructor();
144 if (plugin == NULL)
145 {
146 DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
147 create);
148 dlclose(handle);
149 return NULL;
150 }
151 DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
152
153 /* we do not store or free dlopen() handles, leak_detective requires
154 * the modules to keep loaded until leak report */
155 return plugin;
156 }
157 #endif
158
159 /**
160 * Implementation of plugin_loader_t.load_plugins.
161 */
162 static bool load(private_plugin_loader_t *this, char *path, char *list)
163 {
164 enumerator_t *enumerator;
165 char *token;
166 bool critical_failed = FALSE;
167
168 #ifndef MONOLITHIC
169 if (path == NULL)
170 {
171 path = PLUGINDIR;
172 }
173 #endif
174
175 enumerator = enumerator_create_token(list, " ", " ");
176 while (!critical_failed && enumerator->enumerate(enumerator, &token))
177 {
178 plugin_t *plugin;
179 bool critical = FALSE;
180 int len;
181
182 token = strdup(token);
183 len = strlen(token);
184 if (token[len-1] == '!')
185 {
186 critical = TRUE;
187 token[len-1] = '\0';
188 }
189 plugin = load_plugin(this, path, token);
190 if (plugin)
191 {
192 /* insert in front to destroy them in reverse order */
193 this->plugins->insert_last(this->plugins, plugin);
194 this->names->insert_last(this->names, token);
195 }
196 else
197 {
198 if (critical)
199 {
200 critical_failed = TRUE;
201 DBG1(DBG_LIB, "loading critical plugin '%s' failed", token);
202 }
203 free(token);
204 }
205 }
206 enumerator->destroy(enumerator);
207 return !critical_failed;
208 }
209
210 /**
211 * Implementation of plugin_loader_t.unload
212 */
213 static void unload(private_plugin_loader_t *this)
214 {
215 plugin_t *plugin;
216 char *name;
217
218 while (this->plugins->remove_first(this->plugins,
219 (void**)&plugin) == SUCCESS)
220 {
221 plugin->destroy(plugin);
222 }
223 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
224 {
225 free(name);
226 }
227 }
228
229 /**
230 * Implementation of plugin_loader_t.create_plugin_enumerator
231 */
232 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
233 {
234 return this->names->create_enumerator(this->names);
235 }
236
237 /**
238 * Implementation of plugin_loader_t.destroy
239 */
240 static void destroy(private_plugin_loader_t *this)
241 {
242 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
243 this->names->destroy_function(this->names, free);
244 free(this);
245 }
246
247 /*
248 * see header file
249 */
250 plugin_loader_t *plugin_loader_create()
251 {
252 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
253
254 this->public.load = (bool(*)(plugin_loader_t*, char *path, char *prefix))load;
255 this->public.unload = (void(*)(plugin_loader_t*))unload;
256 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
257 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
258
259 this->plugins = linked_list_create();
260 this->names = linked_list_create();
261
262 return &this->public;
263 }
264