Do not use `bool` type; use new `bt_bool` instead
[babeltrace.git] / lib / plugin / plugin.c
index ce3a866f18935d0e4f803980a06dbdc249adf020..d219fe893bfe5762afa2823a3aafa6b8395fb69e 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/compiler.h>
+#include <babeltrace/babeltrace-internal.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 WITH_PYTHON_PLUGINS
-# include <babeltrace/plugin/plugin-python-enabled-internal.h>
-#else
-# include <babeltrace/plugin/plugin-python-disabled-internal.h>
-#endif
+#define PYTHON_PLUGIN_PROVIDER_FILENAME        "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
+#define PYTHON_PLUGIN_PROVIDER_SYM_NAME        bt_plugin_python_create_all_from_file
+#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR    TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
 
+#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
+#include <babeltrace/plugin/python-plugin-provider-internal.h>
 static
-void bt_plugin_destroy(struct bt_object *obj)
-{
-       struct bt_plugin *plugin;
-
-       assert(obj);
-       plugin = container_of(obj, struct bt_plugin, base);
+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_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
 
-       if (plugin->type == BT_PLUGIN_TYPE_SO) {
-               bt_plugin_so_destroy_spec_data(plugin);
-       } else if (plugin->type == BT_PLUGIN_TYPE_PYTHON) {
-               bt_plugin_python_destroy_spec_data(plugin);
-       } else {
-               assert(false);
+__attribute__((constructor)) static
+void init_python_plugin_provider(void) {
+       python_plugin_provider_module =
+               g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
+                       G_MODULE_BIND_LOCAL);
+       if (!python_plugin_provider_module) {
+               printf_verbose("Cannot find `%s`: Python plugin support is disabled\n",
+                       PYTHON_PLUGIN_PROVIDER_FILENAME);
+               return;
        }
 
-       if (plugin->comp_classes) {
-               g_ptr_array_free(plugin->comp_classes, TRUE);
+       if (!g_module_symbol(python_plugin_provider_module,
+                       PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
+                       (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
+               printf_verbose("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
        }
+}
 
-       if (plugin->info.name) {
-               g_string_free(plugin->info.name, TRUE);
+__attribute__((destructor)) static
+void fini_python_plugin_provider(void) {
+       if (python_plugin_provider_module) {
+               (void) g_module_close(python_plugin_provider_module);
+               python_plugin_provider_module = NULL;
        }
+}
+#endif
+
+extern
+int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
+{
+       int64_t count = -1;
 
-       if (plugin->info.path) {
-               g_string_free(plugin->info.path, TRUE);
+       if (!plugin_set) {
+               goto end;
        }
 
-       if (plugin->info.description) {
-               g_string_free(plugin->info.description, TRUE);
+       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;
        }
 
-       if (plugin->info.author) {
-               g_string_free(plugin->info.author, TRUE);
+       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_set *bt_plugin_create_all_from_file(const char *path)
+{
+       struct bt_plugin_set *plugin_set = NULL;
+
+       if (!path) {
+               goto end;
        }
 
-       if (plugin->info.license) {
-               g_string_free(plugin->info.license, TRUE);
+       printf_verbose("Trying to load plugins from `%s`\n", path);
+
+       /* Try shared object plugins */
+       plugin_set = bt_plugin_so_create_all_from_file(path);
+       if (plugin_set) {
+               goto end;
        }
 
-       if (plugin->info.version.extra) {
-               g_string_free(plugin->info.version.extra, TRUE);
+       /* Try Python plugins if support is available */
+       if (bt_plugin_python_create_all_from_file_sym) {
+               plugin_set = bt_plugin_python_create_all_from_file_sym(path);
+               if (plugin_set) {
+                       goto end;
+               }
        }
 
-       g_free(plugin);
+end:
+       return plugin_set;
 }
 
-BT_HIDDEN
-struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
+static void destroy_gstring(void *data)
 {
+       g_string_free(data, TRUE);
+}
+
+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_set *plugin_set = NULL;
+       GPtrArray *dirs = NULL;
+       int ret;
+       size_t i, j;
 
-       plugin = g_new0(struct bt_plugin, 1);
-       if (!plugin) {
-               goto error;
+       if (!plugin_name) {
+               goto end;
        }
 
-       bt_object_init(plugin, bt_plugin_destroy);
-       plugin->type = type;
-
-       /* Create empty array of component classes */
-       plugin->comp_classes =
-               g_ptr_array_new_with_free_func((GDestroyNotify) bt_put);
-       if (!plugin->comp_classes) {
-               goto error;
+       dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
+       if (!dirs) {
+               goto end;
        }
 
-       /* Create empty info */
-       plugin->info.name = g_string_new(NULL);
-       if (!plugin->info.name) {
-               goto error;
+       /*
+        * Search order is:
+        *
+        * 1. BABELTRACE_PLUGIN_PATH environment variable
+        *    (colon-separated list of directories)
+        * 2. ~/.local/lib/babeltrace/plugins
+        * 3. Default system directory for Babeltrace plugins, usually
+        *    /usr/lib/babeltrace/plugins or
+        *    /usr/local/lib/babeltrace/plugins if installed
+        *    locally
+        * 4. Built-in plugins (static)
+        *
+        * Directories are searched non-recursively.
+        */
+       envvar = getenv("BABELTRACE_PLUGIN_PATH");
+       if (envvar) {
+               ret = bt_common_append_plugin_path_dirs(envvar, dirs);
+               if (ret) {
+                       goto end;
+               }
        }
 
-       plugin->info.path = g_string_new(NULL);
-       if (!plugin->info.path) {
-               goto error;
-       }
+       home_plugin_dir = bt_common_get_home_plugin_path();
+       if (home_plugin_dir) {
+               GString *home_plugin_dir_str =
+                       g_string_new(home_plugin_dir);
 
-       plugin->info.description = g_string_new(NULL);
-       if (!plugin->info.description) {
-               goto error;
-       }
+               if (!home_plugin_dir_str) {
+                       goto end;
+               }
 
-       plugin->info.author = g_string_new(NULL);
-       if (!plugin->info.author) {
-               goto error;
+               g_ptr_array_add(dirs, home_plugin_dir_str);
        }
 
-       plugin->info.license = g_string_new(NULL);
-       if (!plugin->info.license) {
-               goto error;
+       system_plugin_dir = bt_common_get_system_plugin_path();
+       if (system_plugin_dir) {
+               GString *system_plugin_dir_str =
+                       g_string_new(system_plugin_dir);
+
+               if (!system_plugin_dir_str) {
+                       goto end;
+               }
+
+               g_ptr_array_add(dirs, system_plugin_dir_str);
        }
 
-       plugin->info.version.extra = g_string_new(NULL);
-       if (!plugin->info.version.extra) {
-               goto error;
+       for (i = 0; i < dirs->len; i++) {
+               GString *dir = g_ptr_array_index(dirs, i);
+
+               printf_verbose("Trying to load plugins from directory `%s`\n",
+                       dir->str);
+               BT_PUT(plugin_set);
+               plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
+               if (!plugin_set) {
+                       continue;
+               }
+
+               for (j = 0; j < plugin_set->plugins->len; j++) {
+                       struct bt_plugin *candidate_plugin =
+                               g_ptr_array_index(plugin_set->plugins, j);
+
+                       if (strcmp(bt_plugin_get_name(candidate_plugin),
+                                       plugin_name) == 0) {
+                               plugin = bt_get(candidate_plugin);
+                               goto end;
+                       }
+               }
        }
 
-       goto end;
+       bt_put(plugin_set);
+       plugin_set = bt_plugin_create_all_from_static();
 
-error:
-       BT_PUT(plugin);
+       for (j = 0; j < plugin_set->plugins->len; j++) {
+               struct bt_plugin *candidate_plugin =
+                       g_ptr_array_index(plugin_set->plugins, j);
+
+               if (strcmp(bt_plugin_get_name(candidate_plugin),
+                               plugin_name) == 0) {
+                       plugin = bt_get(candidate_plugin);
+                       goto end;
+               }
+       }
 
 end:
-       return plugin;
-}
+       free(home_plugin_dir);
+       bt_put(plugin_set);
 
-struct bt_plugin **bt_plugin_create_all_from_static(void)
-{
-       return bt_plugin_so_create_all_from_static();
+       if (dirs) {
+               g_ptr_array_free(dirs, TRUE);
+       }
+
+       return plugin;
 }
 
-struct bt_plugin **bt_plugin_create_all_from_file(const char *path)
+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 **plugins = NULL;
+       struct bt_plugin *plugin = NULL;
+       struct bt_component_class *comp_cls = NULL;
 
-       if (!path) {
+       if (!plugin_name || !comp_cls_name) {
                goto end;
        }
 
-       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 = bt_plugin_find(plugin_name);
+       if (!plugin) {
                goto end;
        }
 
-       plugins = bt_plugin_python_create_all_from_file(path);
-       if (plugins) {
-               goto end;
-       }
+       comp_cls = bt_plugin_get_component_class_by_name_and_type(
+               plugin, comp_cls_name, comp_cls_type);
 
 end:
-       return plugins;
+       bt_put(plugin);
+       return comp_cls;
 }
 
 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
@@ -199,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;
@@ -239,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;
        }
@@ -271,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);
                        }
                }
        }
@@ -307,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)
@@ -396,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;
 
@@ -464,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) {
@@ -489,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.030443 seconds and 4 git commands to generate.