2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include "plugin_loader.h"
27 #include <utils/linked_list.h>
28 #include <plugins/plugin.h>
30 typedef struct private_plugin_loader_t private_plugin_loader_t
;
33 * private data of plugin_loader
35 struct private_plugin_loader_t
{
40 plugin_loader_t
public;
43 * list of loaded plugins
45 linked_list_t
*plugins
;
49 * load a single plugin
51 static plugin_t
* load_plugin(private_plugin_loader_t
*this,
52 char *path
, char *name
)
57 plugin_constructor_t constructor
;
59 snprintf(file
, sizeof(file
), "%s/libstrongswan-%s.so", path
, name
);
61 handle
= dlopen(file
, RTLD_LAZY
);
64 DBG1("loading plugin '%s' failed: %s", name
, dlerror());
67 constructor
= dlsym(handle
, "plugin_create");
68 if (constructor
== NULL
)
70 DBG1("loading plugin '%s' failed: no plugin_create() function", name
);
74 plugin
= constructor();
77 DBG1("loading plugin '%s' failed: plugin_create() returned NULL", name
);
81 DBG2("plugin '%s' loaded successfully", name
);
83 /* we do not store or free dlopen() handles, leak_detective requires
84 * the modules to keep loaded until leak report */
89 * Implementation of plugin_loader_t.load_plugins.
91 static int load(private_plugin_loader_t
*this, char *path
, char *list
)
100 pos
= strchr(list
, ' ');
105 plugin
= load_plugin(this, path
, list
);
107 { /* insert in front to destroy them in reverse order */
108 this->plugins
->insert_last(this->plugins
, plugin
);
121 * Implementation of plugin_loader_t.unload
123 static void unload(private_plugin_loader_t
*this)
127 while (this->plugins
->remove_first(this->plugins
,
128 (void**)&plugin
) == SUCCESS
)
130 plugin
->destroy(plugin
);
135 * Implementation of plugin_loader_t.destroy
137 static void destroy(private_plugin_loader_t
*this)
139 this->plugins
->destroy_offset(this->plugins
, offsetof(plugin_t
, destroy
));
146 plugin_loader_t
*plugin_loader_create()
148 private_plugin_loader_t
*this = malloc_thing(private_plugin_loader_t
);
150 this->public.load
= (int(*)(plugin_loader_t
*, char *path
, char *prefix
))load
;
151 this->public.unload
= (void(*)(plugin_loader_t
*))unload
;
152 this->public.destroy
= (void(*)(plugin_loader_t
*))destroy
;
154 this->plugins
= linked_list_create();
156 return &this->public;