fixed plugin loader destruction
[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 * $Id$
16 */
17
18 #define _GNU_SOURCE
19 #include "plugin_loader.h"
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <limits.h>
24 #include <stdio.h>
25
26 #include <debug.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 * load a single plugin
55 */
56 static plugin_t* load_plugin(private_plugin_loader_t *this,
57 char *path, char *name)
58 {
59 char file[PATH_MAX];
60 void *handle;
61 plugin_t *plugin;
62 plugin_constructor_t constructor;
63
64 snprintf(file, sizeof(file), "%s/libstrongswan-%s.so", path, name);
65
66 handle = dlopen(file, RTLD_LAZY);
67 if (handle == NULL)
68 {
69 DBG1("loading plugin '%s' failed: %s", name, dlerror());
70 return NULL;
71 }
72 constructor = dlsym(handle, "plugin_create");
73 if (constructor == NULL)
74 {
75 DBG1("loading plugin '%s' failed: no plugin_create() function", name);
76 dlclose(handle);
77 return NULL;
78 }
79 plugin = constructor();
80 if (plugin == NULL)
81 {
82 DBG1("loading plugin '%s' failed: plugin_create() returned NULL", name);
83 dlclose(handle);
84 return NULL;
85 }
86 DBG2("plugin '%s' loaded successfully", name);
87
88 /* we do not store or free dlopen() handles, leak_detective requires
89 * the modules to keep loaded until leak report */
90 return plugin;
91 }
92
93 /**
94 * Implementation of plugin_loader_t.load_plugins.
95 */
96 static int load(private_plugin_loader_t *this, char *path, char *list)
97 {
98 plugin_t *plugin;
99 char *pos;
100 int count = 0;
101
102 list = strdupa(list);
103 while (TRUE)
104 {
105 /* eat any whitespace in front */
106 while (*list == ' ')
107 {
108 list++;
109 }
110 /* have we reached the end of the list? */
111 if (!*list)
112 {
113 break;
114 }
115 pos = strchr(list, ' ');
116 if (pos)
117 {
118 *pos++ = '\0';
119 }
120 plugin = load_plugin(this, path, list);
121 if (plugin)
122 { /* insert in front to destroy them in reverse order */
123 this->plugins->insert_last(this->plugins, plugin);
124 this->names->insert_last(this->names, strdup(list));
125 count++;
126 }
127 if (pos)
128 {
129 list = pos;
130 }
131 else
132 {
133 break;
134 }
135 }
136 return count;
137 }
138
139 /**
140 * Implementation of plugin_loader_t.unload
141 */
142 static void unload(private_plugin_loader_t *this)
143 {
144 plugin_t *plugin;
145 char *name;
146
147 while (this->plugins->remove_first(this->plugins,
148 (void**)&plugin) == SUCCESS)
149 {
150 plugin->destroy(plugin);
151 }
152 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
153 {
154 free(name);
155 }
156 }
157
158 /**
159 * Implementation of plugin_loader_t.create_plugin_enumerator
160 */
161 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
162 {
163 return this->names->create_enumerator(this->names);
164 }
165
166 /**
167 * Implementation of plugin_loader_t.destroy
168 */
169 static void destroy(private_plugin_loader_t *this)
170 {
171 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
172 this->names->destroy_function(this->names, free);
173 free(this);
174 }
175
176 /*
177 * see header file
178 */
179 plugin_loader_t *plugin_loader_create()
180 {
181 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
182
183 this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
184 this->public.unload = (void(*)(plugin_loader_t*))unload;
185 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
186 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
187
188 this->plugins = linked_list_create();
189 this->names = linked_list_create();
190
191 return &this->public;
192 }
193