Introduce bt_plugin and bt_component_class
[babeltrace.git] / plugins / component-factory.c
index b4ebe3f2ef9f0ac57abae257bdfac92a9f30c63a..a91b63a01a6099d76e729138547b78950fb5f03c 100644 (file)
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/stat.h>
+#include <gmodule.h>
 
 #define NATIVE_PLUGIN_SUFFIX ".so"
 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
 #define LIBTOOL_PLUGIN_SUFFIX ".la"
 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
-#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX),\
-                               sizeof(LIBTOOL_PLUGIN_SUFFIX))
-
-static
-void module_close(gpointer data)
-{
-       if (g_module_close((GModule *) data)) {
-               printf_error("Failed to close plugin");
-       }
-}
-
-static
-void factory_destroy(gpointer data)
-{
-       bt_component_factory_destroy((struct bt_component_factory *)data);
-}
+#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
+               sizeof(LIBTOOL_PLUGIN_SUFFIX))
 
 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
 static
@@ -78,8 +65,10 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
                const char *path)
 {
        enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
+       enum bt_component_status component_status;
        size_t path_len;
        GModule *module;
+       struct bt_plugin *plugin;
 
        if (!factory || !path) {
                ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
@@ -88,6 +77,7 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
 
        path_len = strlen(path);
        if (path_len <= PLUGIN_SUFFIX_LEN) {
+               ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
                goto end;
        }
 
@@ -102,17 +92,45 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
                strncmp(LIBTOOL_PLUGIN_SUFFIX,
                path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
                LIBTOOL_PLUGIN_SUFFIX_LEN)) {
-               /* Not a plugin file. */
+               /* Name indicates that this is not a plugin file. */
+               ret = BT_COMPONENT_FACTORY_STATUS_INVAL;
                goto end;
        }
 
        module = g_module_open(path, 0);
        if (!module) {
                printf_error("Module open error: %s", g_module_error());
+               ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
+               goto end;
+       }
+
+       /* Load plugin and make sure it defines the required entry points */
+       plugin = bt_plugin_create(module);
+       if (!plugin) {
+               ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
+               if (!g_module_close(module)) {
+                       printf_error("Module close error: %s",
+                               g_module_error());
+               }
                goto end;
        }
 
-       /* Check if the module defines the appropriate entry points */
+       component_status = bt_plugin_register_component_classes(plugin,
+               factory);
+       if (component_status != BT_COMPONENT_STATUS_OK) {
+               switch (component_status) {
+               case BT_COMPONENT_STATUS_NOMEM:
+                       ret = BT_COMPONENT_FACTORY_STATUS_NOMEM;
+                       break;
+               default:
+                       ret = BT_COMPONENT_FACTORY_STATUS_ERROR;
+                       break;
+               }
+               bt_plugin_put(plugin);
+               plugin = NULL;
+               goto end;
+       }
+       g_ptr_array_add(factory->plugins, plugin);
 end:
        return ret;
 }
@@ -178,11 +196,7 @@ bt_component_factory_load_dir_recursive(struct bt_component_factory *factory,
                                goto end;
                        }
                } else if (S_ISREG(st.st_mode)) {
-                       ret = bt_component_factory_load_file(factory,
-                               file_path);
-                       if (ret != BT_COMPONENT_FACTORY_STATUS_OK) {
-                               goto end;
-                       }
+                       bt_component_factory_load_file(factory, file_path);
                }
        }
 end:
@@ -197,6 +211,12 @@ void bt_component_factory_destroy(struct bt_component_factory *factory)
                return;
        }
 
+       if (factory->plugins) {
+               g_ptr_array_free(factory->plugins, TRUE);
+       }
+       if (factory->components) {
+               g_ptr_array_free(factory->components, TRUE);
+       }
        g_free(factory);
 }
 
@@ -210,13 +230,14 @@ bt_component_factory_create(void)
                goto end;
        }
 
-       factory->modules = g_ptr_array_new_with_free_func(module_close);
-       if (!factory->modules) {
+       factory->plugins = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_plugin_put);
+       if (!factory->plugins) {
                goto error;
        }
-
-       factory->components = g_ptr_array_new_with_free_func(factory_destroy);
-       if (factory->components) {
+       factory->components = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_component_put);
+       if (!factory->components) {
                goto error;
        }
 end:
This page took 0.026323 seconds and 4 git commands to generate.