47c244be3403e2943687751352de8c24f80af350
[strongswan.git] / src / libstrongswan / plugins / plugin_loader.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include "plugin_loader.h"
18
19 #include <string.h>
20 #include <dlfcn.h>
21 #include <limits.h>
22 #include <stdio.h>
23
24 #include <debug.h>
25 #include <integrity_checker.h>
26 #include <utils/linked_list.h>
27 #include <plugins/plugin.h>
28
29 typedef struct private_plugin_loader_t private_plugin_loader_t;
30
31 /**
32 * private data of plugin_loader
33 */
34 struct private_plugin_loader_t {
35
36 /**
37 * public functions
38 */
39 plugin_loader_t public;
40
41 /**
42 * list of loaded plugins
43 */
44 linked_list_t *plugins;
45
46 /**
47 * names of loaded plugins
48 */
49 linked_list_t *names;
50 };
51
52 /**
53 * load a single plugin
54 */
55 static plugin_t* load_plugin(private_plugin_loader_t *this,
56 char *path, char *name)
57 {
58 char file[PATH_MAX];
59 void *handle;
60 plugin_t *plugin;
61 plugin_constructor_t constructor;
62
63 snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path, name);
64
65 if (lib->integrity &&
66 !lib->integrity->check_file(lib->integrity, name, file))
67 {
68 DBG1("file integrity test of plugin '%s' failed", name);
69 return NULL;
70 }
71 handle = dlopen(file, RTLD_LAZY);
72 if (handle == NULL)
73 {
74 DBG1("loading plugin '%s' failed: %s", name, dlerror());
75 return NULL;
76 }
77 constructor = dlsym(handle, "plugin_create");
78 if (constructor == NULL)
79 {
80 DBG1("loading plugin '%s' failed: no plugin_create() function", name);
81 dlclose(handle);
82 return NULL;
83 }
84 if (lib->integrity &&
85 !lib->integrity->check_segment(lib->integrity, name, constructor))
86 {
87 DBG1("segment integrity test of plugin '%s' failed", name);
88 dlclose(handle);
89 return NULL;
90 }
91 plugin = constructor();
92 if (plugin == NULL)
93 {
94 DBG1("loading plugin '%s' failed: plugin_create() returned NULL", name);
95 dlclose(handle);
96 return NULL;
97 }
98 DBG2("plugin '%s' loaded successfully", name);
99
100 /* we do not store or free dlopen() handles, leak_detective requires
101 * the modules to keep loaded until leak report */
102 return plugin;
103 }
104
105 /**
106 * Implementation of plugin_loader_t.load_plugins.
107 */
108 static int load(private_plugin_loader_t *this, char *path, char *list)
109 {
110 plugin_t *plugin;
111 enumerator_t *enumerator;
112 char *token;
113 int count = 0;
114
115 enumerator = enumerator_create_token(list, " ", " ");
116 while (enumerator->enumerate(enumerator, &token))
117 {
118 plugin = load_plugin(this, path, token);
119 if (plugin)
120 { /* insert in front to destroy them in reverse order */
121 this->plugins->insert_last(this->plugins, plugin);
122 this->names->insert_last(this->names, strdup(token));
123 count++;
124 }
125 }
126 enumerator->destroy(enumerator);
127 return count;
128 }
129
130 /**
131 * Implementation of plugin_loader_t.unload
132 */
133 static void unload(private_plugin_loader_t *this)
134 {
135 plugin_t *plugin;
136 char *name;
137
138 while (this->plugins->remove_first(this->plugins,
139 (void**)&plugin) == SUCCESS)
140 {
141 plugin->destroy(plugin);
142 }
143 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
144 {
145 free(name);
146 }
147 }
148
149 /**
150 * Implementation of plugin_loader_t.create_plugin_enumerator
151 */
152 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
153 {
154 return this->names->create_enumerator(this->names);
155 }
156
157 /**
158 * Implementation of plugin_loader_t.destroy
159 */
160 static void destroy(private_plugin_loader_t *this)
161 {
162 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
163 this->names->destroy_function(this->names, free);
164 free(this);
165 }
166
167 /*
168 * see header file
169 */
170 plugin_loader_t *plugin_loader_create()
171 {
172 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
173
174 this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
175 this->public.unload = (void(*)(plugin_loader_t*))unload;
176 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
177 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
178
179 this->plugins = linked_list_create();
180 this->names = linked_list_create();
181
182 return &this->public;
183 }
184