Replace all assert(false) and assert(0) with abort()
[babeltrace.git] / lib / plugin / plugin.c
index 83f628cd8ef79fe387360ba3e55b9fcc1c95c515..d219fe893bfe5762afa2823a3aafa6b8395fb69e 100644 (file)
  */
 
 #include <babeltrace/babeltrace-internal.h>
-#include <babeltrace/compiler.h>
+#include <babeltrace/compiler-internal.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/common-internal.h>
 #include <babeltrace/plugin/plugin-internal.h>
 #include <babeltrace/plugin/plugin-so-internal.h>
+#include <babeltrace/types.h>
 #include <glib.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <sys/stat.h>
 #include <dirent.h>
 
 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
 #include <babeltrace/plugin/python-plugin-provider-internal.h>
 static
-struct bt_plugin **(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
+struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
        bt_plugin_python_create_all_from_file;
 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
 static GModule *python_plugin_provider_module;
 static
-struct bt_plugin **(*bt_plugin_python_create_all_from_file_sym)(const char *path);
+struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
 
 __attribute__((constructor)) static
 void init_python_plugin_provider(void) {
@@ -59,7 +61,7 @@ void init_python_plugin_provider(void) {
                g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
                        G_MODULE_BIND_LOCAL);
        if (!python_plugin_provider_module) {
-               printf_warning("Cannot find `%s`: Python plugin support is disabled\n",
+               printf_verbose("Cannot find `%s`: Python plugin support is disabled\n",
                        PYTHON_PLUGIN_PROVIDER_FILENAME);
                return;
        }
@@ -67,7 +69,7 @@ void init_python_plugin_provider(void) {
        if (!g_module_symbol(python_plugin_provider_module,
                        PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
                        (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
-               printf_warning("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
+               printf_verbose("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
        }
 }
 
@@ -80,14 +82,45 @@ void fini_python_plugin_provider(void) {
 }
 #endif
 
-struct bt_plugin **bt_plugin_create_all_from_static(void)
+extern
+int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
+{
+       int64_t count = -1;
+
+       if (!plugin_set) {
+               goto end;
+       }
+
+       count = (int64_t) plugin_set->plugins->len;
+
+end:
+       return count;
+}
+
+extern
+struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
+               uint64_t index)
+{
+       struct bt_plugin *plugin = NULL;
+
+       if (!plugin_set || index >= plugin_set->plugins->len) {
+               goto end;
+       }
+
+       plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index));
+
+end:
+       return plugin;
+}
+
+struct bt_plugin_set *bt_plugin_create_all_from_static(void)
 {
        return bt_plugin_so_create_all_from_static();
 }
 
-struct bt_plugin **bt_plugin_create_all_from_file(const char *path)
+struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
 {
-       struct bt_plugin **plugins = NULL;
+       struct bt_plugin_set *plugin_set = NULL;
 
        if (!path) {
                goto end;
@@ -96,21 +129,21 @@ struct bt_plugin **bt_plugin_create_all_from_file(const char *path)
        printf_verbose("Trying to load plugins from `%s`\n", path);
 
        /* Try shared object plugins */
-       plugins = bt_plugin_so_create_all_from_file(path);
-       if (plugins) {
+       plugin_set = bt_plugin_so_create_all_from_file(path);
+       if (plugin_set) {
                goto end;
        }
 
        /* Try Python plugins if support is available */
        if (bt_plugin_python_create_all_from_file_sym) {
-               plugins = bt_plugin_python_create_all_from_file_sym(path);
-               if (plugins) {
+               plugin_set = bt_plugin_python_create_all_from_file_sym(path);
+               if (plugin_set) {
                        goto end;
                }
        }
 
 end:
-       return plugins;
+       return plugin_set;
 }
 
 static void destroy_gstring(void *data)
@@ -118,30 +151,16 @@ static void destroy_gstring(void *data)
        g_string_free(data, TRUE);
 }
 
-void free_plugins(struct bt_plugin **plugins) {
-       if (plugins) {
-               struct bt_plugin **cur_plugin = plugins;
-
-               while (*cur_plugin) {
-                       bt_put(*cur_plugin);
-                       cur_plugin++;
-               }
-
-               free(plugins);
-       }
-}
-
-struct bt_plugin *bt_plugin_create_from_name(const char *plugin_name)
+struct bt_plugin *bt_plugin_find(const char *plugin_name)
 {
        const char *system_plugin_dir;
        char *home_plugin_dir = NULL;
        const char *envvar;
        struct bt_plugin *plugin = NULL;
-       struct bt_plugin **plugins = NULL;
-       struct bt_plugin **cur_plugin;
+       struct bt_plugin_set *plugin_set = NULL;
        GPtrArray *dirs = NULL;
        int ret;
-       size_t i;
+       size_t i, j;
 
        if (!plugin_name) {
                goto end;
@@ -203,41 +222,41 @@ struct bt_plugin *bt_plugin_create_from_name(const char *plugin_name)
 
                printf_verbose("Trying to load plugins from directory `%s`\n",
                        dir->str);
-               free_plugins(plugins);
-               plugins = bt_plugin_create_all_from_dir(dir->str, false);
-               if (!plugins) {
+               BT_PUT(plugin_set);
+               plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
+               if (!plugin_set) {
                        continue;
                }
 
-               cur_plugin = plugins;
+               for (j = 0; j < plugin_set->plugins->len; j++) {
+                       struct bt_plugin *candidate_plugin =
+                               g_ptr_array_index(plugin_set->plugins, j);
 
-               while (*cur_plugin) {
-                       if (strcmp(bt_plugin_get_name(*cur_plugin), plugin_name)
-                                       == 0) {
-                               plugin = bt_get(*cur_plugin);
+                       if (strcmp(bt_plugin_get_name(candidate_plugin),
+                                       plugin_name) == 0) {
+                               plugin = bt_get(candidate_plugin);
                                goto end;
                        }
-
-                       cur_plugin++;
                }
        }
 
-       free_plugins(plugins);
-       plugins = bt_plugin_create_all_from_static();
-       cur_plugin = plugins;
+       bt_put(plugin_set);
+       plugin_set = bt_plugin_create_all_from_static();
+
+       for (j = 0; j < plugin_set->plugins->len; j++) {
+               struct bt_plugin *candidate_plugin =
+                       g_ptr_array_index(plugin_set->plugins, j);
 
-       while (*cur_plugin) {
-               if (strcmp(bt_plugin_get_name(*cur_plugin), plugin_name) == 0) {
-                       plugin = bt_get(*cur_plugin);
+               if (strcmp(bt_plugin_get_name(candidate_plugin),
+                               plugin_name) == 0) {
+                       plugin = bt_get(candidate_plugin);
                        goto end;
                }
-
-               cur_plugin++;
        }
 
 end:
        free(home_plugin_dir);
-       free_plugins(plugins);
+       bt_put(plugin_set);
 
        if (dirs) {
                g_ptr_array_free(dirs, TRUE);
@@ -246,6 +265,30 @@ end:
        return plugin;
 }
 
+struct bt_component_class *bt_plugin_find_component_class(
+               const char *plugin_name, const char *comp_cls_name,
+               enum bt_component_class_type comp_cls_type)
+{
+       struct bt_plugin *plugin = NULL;
+       struct bt_component_class *comp_cls = NULL;
+
+       if (!plugin_name || !comp_cls_name) {
+               goto end;
+       }
+
+       plugin = bt_plugin_find(plugin_name);
+       if (!plugin) {
+               goto end;
+       }
+
+       comp_cls = bt_plugin_get_component_class_by_name_and_type(
+               plugin, comp_cls_name, comp_cls_type);
+
+end:
+       bt_put(plugin);
+       return comp_cls;
+}
+
 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
 static
 struct dirent *alloc_dirent(const char *path)
@@ -265,7 +308,8 @@ struct dirent *alloc_dirent(const char *path)
 
 static
 enum bt_plugin_status bt_plugin_create_append_all_from_dir(
-               GPtrArray *plugins, const char *path, bool recurse)
+               struct bt_plugin_set *plugin_set, const char *path,
+               bt_bool recurse)
 {
        DIR *directory = NULL;
        struct dirent *entry = NULL, *result = NULL;
@@ -305,7 +349,8 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
 
        directory = opendir(file_path);
        if (!directory) {
-               perror("Failed to open plug-in directory");
+               printf_verbose("Failed to open plugin directory \"%s\"\n",
+                       file_path);
                ret = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
@@ -337,24 +382,27 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                }
 
                if (S_ISDIR(st.st_mode) && recurse) {
-                       ret = bt_plugin_create_append_all_from_dir(plugins,
-                               file_path, true);
+                       ret = bt_plugin_create_append_all_from_dir(plugin_set,
+                               file_path, BT_TRUE);
                        if (ret < 0) {
                                goto end;
                        }
                } else if (S_ISREG(st.st_mode)) {
-                       struct bt_plugin **plugins_from_file =
+                       struct bt_plugin_set *plugins_from_file =
                                bt_plugin_create_all_from_file(file_path);
 
                        if (plugins_from_file) {
-                               struct bt_plugin **plugin;
+                               size_t j;
+
+                               for (j = 0; j < plugins_from_file->plugins->len; j++) {
+                                       struct bt_plugin *plugin =
+                                               g_ptr_array_index(plugins_from_file->plugins, j);
 
-                               for (plugin = plugins_from_file; *plugin; plugin++) {
-                                       /* Transfer ownership to array */
-                                       g_ptr_array_add(plugins, *plugin);
+                                       bt_plugin_set_add_plugin(plugin_set,
+                                               plugin);
                                }
 
-                               free(plugins_from_file);
+                               bt_put(plugins_from_file);
                        }
                }
        }
@@ -373,37 +421,31 @@ end:
        return ret;
 }
 
-struct bt_plugin **bt_plugin_create_all_from_dir(const char *path,
-               bool recurse)
+struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
+               bt_bool recurse)
 {
-       GPtrArray *plugins_array = g_ptr_array_new();
-       struct bt_plugin **plugins = NULL;
+       struct bt_plugin_set *plugin_set;
        enum bt_plugin_status status;
 
+       plugin_set = bt_plugin_set_create();
+       if (!plugin_set) {
+               goto error;
+       }
+
        /* Append found plugins to array */
-       status = bt_plugin_create_append_all_from_dir(plugins_array, path,
+       status = bt_plugin_create_append_all_from_dir(plugin_set, path,
                recurse);
        if (status < 0) {
                goto error;
        }
 
-       /* Add sentinel to array */
-       g_ptr_array_add(plugins_array, NULL);
-       plugins = (struct bt_plugin **) plugins_array->pdata;
        goto end;
 
 error:
-       if (plugins_array) {
-               g_ptr_array_free(plugins_array, TRUE);
-               plugins_array = NULL;
-       }
+       BT_PUT(plugin_set);
 
 end:
-       if (plugins_array) {
-               g_ptr_array_free(plugins_array, FALSE);
-       }
-
-       return plugins;
+       return plugin_set;
 }
 
 const char *bt_plugin_get_name(struct bt_plugin *plugin)
@@ -462,13 +504,13 @@ end:
        return status;
 }
 
-int bt_plugin_get_component_class_count(struct bt_plugin *plugin)
+int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin)
 {
-       return plugin ? plugin->comp_classes->len : -1;
+       return plugin ? plugin->comp_classes->len : (int64_t) -1;
 }
 
-struct bt_component_class *bt_plugin_get_component_class(
-               struct bt_plugin *plugin, size_t index)
+struct bt_component_class *bt_plugin_get_component_class_by_index(
+               struct bt_plugin *plugin, uint64_t index)
 {
        struct bt_component_class *comp_class = NULL;
 
@@ -530,7 +572,6 @@ enum bt_plugin_status bt_plugin_add_component_class(
 {
        enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
        struct bt_component_class *comp_class_dup = NULL;
-       int ret;
        int comp_class_index = -1;
 
        if (!plugin || !comp_class || plugin->frozen) {
@@ -555,10 +596,7 @@ enum bt_plugin_status bt_plugin_add_component_class(
 
        /* Special case for a shared object plugin */
        if (plugin->type == BT_PLUGIN_TYPE_SO) {
-               ret = bt_plugin_so_on_add_component_class(plugin, comp_class);
-               if (ret) {
-                       goto error;
-               }
+               bt_plugin_so_on_add_component_class(plugin, comp_class);
        }
 
        goto end;
This page took 0.030056 seconds and 4 git commands to generate.