* plugin.c
*
* Babeltrace Plugin
- *
+ *
* Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
* Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
* SOFTWARE.
*/
-const char *bt_plugin_get_name(struct bt_plugin *plugin)
+#include <babeltrace/compiler.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/plugin/plugin-internal.h>
+#include <glib.h>
+
+#define PLUGIN_SYMBOL_NAME "__bt_plugin_name"
+#define PLUGIN_SYMBOL_AUTHOR "__bt_plugin_author"
+#define PLUGIN_SYMBOL_LICENSE "__bt_plugin_license"
+#define PLUGIN_SYMBOL_INIT "__bt_plugin_init"
+#define PLUGIN_SYMBOL_EXIT "__bt_plugin_exit"
+#define PLUGIN_SYMBOL_DESCRIPTION "__bt_plugin_description"
+
+static
+void bt_plugin_destroy(struct bt_object *obj)
{
- const char *ret = NULL;
+ struct bt_plugin *plugin;
- if (!plugin) {
- goto end;
+ assert(obj);
+ plugin = container_of(obj, struct bt_plugin, base);
+
+ if (plugin->module) {
+ if (!g_module_close(plugin->module)) {
+ printf_error("Module close error: %s",
+ g_module_error());
+ }
}
- ret = plugin->name->str;
-end:
- return ret;
+ if (plugin->exit) {
+ plugin->exit();
+ }
+ g_string_free(plugin->path, TRUE);
+ g_free(plugin);
}
-enum bt_plugin_status bt_plugin_set_name(struct bt_plugin *plugin,
- const char *name)
+BT_HIDDEN
+struct bt_plugin *bt_plugin_create(GModule *module, const char *path)
{
- enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
+ struct bt_plugin *plugin = NULL;
+ gpointer symbol = NULL;
- if (!plugin || !name || name[0] == '\0') {
- ret = BT_PLUGIN_STATUS_INVAL;
- goto end;
+ if (!module || !path) {
+ goto error;
}
- g_string_assign(plugin->name, name);
-end:
- return ret;
-}
+ plugin = g_new0(struct bt_plugin, 1);
+ if (!plugin) {
+ goto error;
+ }
-enum bt_plugin_type bt_plugin_get_type(struct bt_plugin *plugin)
-{
- enum bt_plugin_type type = BT_PLUGIN_TYPE_UNKNOWN;
+ bt_object_init(plugin, bt_plugin_destroy);
+ plugin->path = g_string_new(path);
+ if (!plugin->path) {
+ goto error;
+ }
- if (!plugin) {
- goto end;
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_NAME,
+ (gpointer *) &plugin->name)) {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_NAME, g_module_name(module));
+ goto error;
+ }
+
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_LICENSE,
+ (gpointer *) &plugin->license)) {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_LICENSE, g_module_name(module));
+ goto error;
+ }
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_INIT, &symbol)) {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_INIT, g_module_name(module));
+ goto error;
+ } else {
+ plugin->init = *((bt_plugin_init_func *) symbol);
+ if (!plugin->init) {
+ printf_error("NULL %s symbol target",
+ PLUGIN_SYMBOL_INIT);
+ goto error;
+ }
}
- type = plugin->type;
-end:
- return type;
+ /* Optional */
+ if (g_module_symbol(module, PLUGIN_SYMBOL_EXIT,
+ (gpointer *) &symbol)) {
+ plugin->exit = *((bt_plugin_exit_func *) symbol);
+ }
+ g_module_symbol(module, PLUGIN_SYMBOL_AUTHOR,
+ (gpointer *) &plugin->author);
+ g_module_symbol(module, PLUGIN_SYMBOL_DESCRIPTION,
+ (gpointer *) &plugin->description);
+
+ return plugin;
+error:
+ BT_PUT(plugin);
+ return plugin;
}
-enum bt_plugin_status bt_plugin_set_error_stream(
- struct bt_plugin *plugin, FILE *stream)
+BT_HIDDEN
+enum bt_component_status bt_plugin_register_component_classes(
+ struct bt_plugin *plugin, struct bt_component_factory *factory)
{
- enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
-
- if (!plugin) {
- ret = BT_PLUGIN_STATUS_INVAL;
- goto end;
- }
+ assert(plugin && factory);
+ return plugin->init(factory);
+}
- plugin->error_stream = stream;
-end:
- return ret;
+const char *bt_plugin_get_name(struct bt_plugin *plugin)
+{
+ return plugin ? plugin->name : NULL;
}
-void bt_plugin_get(struct bt_plugin *plugin)
+const char *bt_plugin_get_author(struct bt_plugin *plugin)
{
- if (!plugin) {
- return;
- }
+ return plugin ? plugin->author : NULL;
+}
- bt_ctf_ref_get(&plugin->ref_count);
+const char *bt_plugin_get_license(struct bt_plugin *plugin)
+{
+ return plugin ? plugin->license : NULL;
}
-void bt_plugin_put(struct bt_plugin *plugin)
+const char *bt_plugin_get_path(struct bt_plugin *plugin)
{
- if (!plugin) {
- return;
- }
+ return plugin ? plugin->path->str : NULL;
+}
- bt_ctf_ref_put(&plugin->ref_count);
+const char *bt_plugin_get_description(struct bt_plugin *plugin)
+{
+ return plugin ? plugin->description : NULL;
}