using token enumerator to parser plugin list
[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 enumerator_t *enumerator;
100 char *token;
101 int count = 0;
102
103 enumerator = enumerator_create_token(list, " ", " ");
104 while (enumerator->enumerate(enumerator, &token))
105 {
106 plugin = load_plugin(this, path, token);
107 if (plugin)
108 { /* insert in front to destroy them in reverse order */
109 this->plugins->insert_last(this->plugins, plugin);
110 this->names->insert_last(this->names, strdup(token));
111 count++;
112 }
113 }
114 enumerator->destroy(enumerator);
115 return count;
116 }
117
118 /**
119 * Implementation of plugin_loader_t.unload
120 */
121 static void unload(private_plugin_loader_t *this)
122 {
123 plugin_t *plugin;
124 char *name;
125
126 while (this->plugins->remove_first(this->plugins,
127 (void**)&plugin) == SUCCESS)
128 {
129 plugin->destroy(plugin);
130 }
131 while (this->names->remove_first(this->names, (void**)&name) == SUCCESS)
132 {
133 free(name);
134 }
135 }
136
137 /**
138 * Implementation of plugin_loader_t.create_plugin_enumerator
139 */
140 static enumerator_t* create_plugin_enumerator(private_plugin_loader_t *this)
141 {
142 return this->names->create_enumerator(this->names);
143 }
144
145 /**
146 * Implementation of plugin_loader_t.destroy
147 */
148 static void destroy(private_plugin_loader_t *this)
149 {
150 this->plugins->destroy_offset(this->plugins, offsetof(plugin_t, destroy));
151 this->names->destroy_function(this->names, free);
152 free(this);
153 }
154
155 /*
156 * see header file
157 */
158 plugin_loader_t *plugin_loader_create()
159 {
160 private_plugin_loader_t *this = malloc_thing(private_plugin_loader_t);
161
162 this->public.load = (int(*)(plugin_loader_t*, char *path, char *prefix))load;
163 this->public.unload = (void(*)(plugin_loader_t*))unload;
164 this->public.create_plugin_enumerator = (enumerator_t*(*)(plugin_loader_t*))create_plugin_enumerator;
165 this->public.destroy = (void(*)(plugin_loader_t*))destroy;
166
167 this->plugins = linked_list_create();
168 this->names = linked_list_create();
169
170 return &this->public;
171 }
172