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"
23 #include <utils/linked_list.h>
24 #include <plugins/plugin.h>
26 typedef struct private_plugin_loader_t private_plugin_loader_t
;
29 * private data of plugin_loader
31 struct private_plugin_loader_t
{
36 plugin_loader_t
public;
39 * list of loaded plugins
41 linked_list_t
*plugins
;
45 * Implementation of plugin_loader_t.load_plugins.
47 static int load(private_plugin_loader_t
*this, char *path
, char *prefix
)
49 enumerator_t
*enumerator
;
50 char *file
, *ending
, *rel
;
54 enumerator
= enumerator_create_directory(path
);
57 DBG1("opening plugin directory %s failed", path
);
60 DBG1("loading plugins from %s", path
);
61 while (enumerator
->enumerate(enumerator
, &rel
, &file
, NULL
))
64 plugin_constructor_t constructor
;
66 ending
= file
+ strlen(file
) - 3;
67 if (ending
<= file
|| !streq(ending
, ".so"))
68 { /* only process .so libraries */
71 if (!strneq(prefix
, rel
, strlen(prefix
)))
75 handle
= dlopen(file
, RTLD_LAZY
);
78 DBG1("loading plugin %s failed: %s", rel
, dlerror());
81 constructor
= dlsym(handle
, "plugin_create");
82 if (constructor
== NULL
)
84 DBG1("plugin %s has no plugin_create() function, skipped", rel
);
88 plugin
= constructor();
91 DBG1("plugin %s constructor failed, skipping", rel
);
95 DBG1("plugin %s loaded successfully", rel
);
96 /* insert in front to destroy them in reverse order */
97 this->plugins
->insert_last(this->plugins
, plugin
);
98 /* we do not store or free dlopen() handles, leak_detective requires
99 * the modules to keep loaded until leak report */
102 enumerator
->destroy(enumerator
);
107 * Implementation of plugin_loader_t.destroy
109 static void destroy(private_plugin_loader_t
*this)
111 this->plugins
->destroy_offset(this->plugins
, offsetof(plugin_t
, destroy
));
118 plugin_loader_t
*plugin_loader_create()
120 private_plugin_loader_t
*this = malloc_thing(private_plugin_loader_t
);
122 this->public.load
= (int(*)(plugin_loader_t
*, char *path
, char *prefix
))load
;
123 this->public.destroy
= (void(*)(plugin_loader_t
*))destroy
;
125 this->plugins
= linked_list_create();
127 return &this->public;