Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / lib / plugin / plugin.c
index 9518b8eaa4e3578e7e9cdc79a887a3ee92ba8078..eb935905c5cdb516c101a21849b11e22a0cd0e03 100644 (file)
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler-internal.h>
-#include <babeltrace/ref.h>
+#include <babeltrace/object.h>
 #include <babeltrace/common-internal.h>
 #include <babeltrace/plugin/plugin-internal.h>
 #include <babeltrace/plugin/plugin-so-internal.h>
 #include <babeltrace/graph/component-class.h>
 #include <babeltrace/graph/component-class-internal.h>
 #include <babeltrace/types.h>
+#include <babeltrace/assert-internal.h>
 #include <glib.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <sys/stat.h>
-#include <dirent.h>
+#include <ftw.h>
+#include <pthread.h>
 
 #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)
 
+#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX        8
+
 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
 #include <babeltrace/plugin/python-plugin-provider-internal.h>
+
 static
 struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
        bt_plugin_python_create_all_from_file;
+
+static
+void init_python_plugin_provider(void) {}
 #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);
 
-__attribute__((constructor)) static
+static
 void init_python_plugin_provider(void) {
+       if (bt_plugin_python_create_all_from_file_sym != NULL) {
+               return;
+       }
+
        BT_LOGD_STR("Loading Python plugin provider module.");
        python_plugin_provider_module =
-               g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
-                       G_MODULE_BIND_LOCAL);
+               g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
        if (!python_plugin_provider_module) {
-               BT_LOGI("Cannot find `%s`: continuing without Python plugin support.",
-                       PYTHON_PLUGIN_PROVIDER_FILENAME);
+               BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
+                       PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
                return;
        }
 
@@ -136,7 +147,7 @@ struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
                goto end;
        }
 
-       plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index));
+       plugin = bt_object_get_ref(g_ptr_array_index(plugin_set->plugins, index));
 
 end:
        return plugin;
@@ -166,6 +177,7 @@ struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
        }
 
        /* Try Python plugins if support is available */
+       init_python_plugin_provider();
        if (bt_plugin_python_create_all_from_file_sym) {
                plugin_set = bt_plugin_python_create_all_from_file_sym(path);
                if (plugin_set) {
@@ -267,7 +279,17 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
        for (i = 0; i < dirs->len; i++) {
                GString *dir = g_ptr_array_index(dirs, i);
 
-               BT_PUT(plugin_set);
+               BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
+
+               /*
+                * Skip this if the directory does not exist because
+                * bt_plugin_create_all_from_dir() would log a warning.
+                */
+               if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
+                       BT_LOGV("Skipping nonexistent directory path: "
+                               "path=\"%s\"", dir->str);
+                       continue;
+               }
 
                /* bt_plugin_create_all_from_dir() logs details/errors */
                plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
@@ -285,7 +307,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
                                        plugin_name) == 0) {
                                BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
                                        plugin_name, dir->str);
-                               plugin = bt_get(candidate_plugin);
+                               plugin = bt_object_get_ref(candidate_plugin);
                                goto end;
                        }
                }
@@ -294,7 +316,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
                        plugin_name, dir->str);
        }
 
-       bt_put(plugin_set);
+       bt_object_put_ref(plugin_set);
        plugin_set = bt_plugin_create_all_from_static();
        if (plugin_set) {
                for (j = 0; j < plugin_set->plugins->len; j++) {
@@ -305,7 +327,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
                                        plugin_name) == 0) {
                                BT_LOGD("Plugin found in built-in plugins: "
                                        "name=\"%s\"", plugin_name);
-                               plugin = bt_get(candidate_plugin);
+                               plugin = bt_object_get_ref(candidate_plugin);
                                goto end;
                        }
                }
@@ -313,7 +335,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
 
 end:
        free(home_plugin_dir);
-       bt_put(plugin_set);
+       bt_object_put_ref(plugin_set);
 
        if (dirs) {
                g_ptr_array_free(dirs, TRUE);
@@ -366,25 +388,72 @@ struct bt_component_class *bt_plugin_find_component_class(
        }
 
 end:
-       bt_put(plugin);
+       bt_object_put_ref(plugin);
        return comp_cls;
 }
 
-/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
+static struct {
+       pthread_mutex_t lock;
+       struct bt_plugin_set *plugin_set;
+       bt_bool recurse;
+} append_all_from_dir_info = {
+       .lock = PTHREAD_MUTEX_INITIALIZER
+};
+
 static
-struct dirent *alloc_dirent(const char *path)
+int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag,
+               struct FTW *s)
 {
-       size_t len;
-       long name_max;
-       struct dirent *entry;
+       int ret = 0;
+       const char *name = file + s->base;
+
+       /* Check for recursion */
+       if (!append_all_from_dir_info.recurse && s->level > 1) {
+               goto end;
+       }
 
-       name_max = pathconf(path, _PC_NAME_MAX);
-       if (name_max == -1) {
-               name_max = PATH_MAX;
+       switch (flag) {
+       case FTW_F:
+       {
+               struct bt_plugin_set *plugins_from_file;
+
+               if (name[0] == '.') {
+                       /* Skip hidden files */
+                       BT_LOGV("Skipping hidden file: path=\"%s\"", file);
+                       goto end;
+               }
+
+               plugins_from_file = bt_plugin_create_all_from_file(file);
+
+               if (plugins_from_file) {
+                       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);
+
+                               BT_LOGD("Adding plugin to plugin set: "
+                                       "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
+                                       file, plugin, bt_plugin_get_name(plugin));
+                               bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin);
+                       }
+
+                       bt_object_put_ref(plugins_from_file);
+               }
+               break;
        }
-       len = offsetof(struct dirent, d_name) + name_max + 1;
-       entry = zmalloc(len);
-       return entry;
+       case FTW_DNR:
+               /* Continue to next file / directory. */
+               BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file);
+               break;
+       case FTW_NS:
+               /* Continue to next file / directory. */
+               BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file);
+               break;
+       }
+
+end:
+       return ret;
 }
 
 static
@@ -392,9 +461,7 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                struct bt_plugin_set *plugin_set, const char *path,
                bt_bool recurse)
 {
-       DIR *directory = NULL;
-       struct dirent *entry = NULL, *result = NULL;
-       char *file_path = NULL;
+       int nftw_flags = FTW_PHYS;
        size_t path_len;
        enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
 
@@ -412,128 +479,22 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                goto end;
        }
 
-       entry = alloc_dirent(path);
-       if (!entry) {
-               BT_LOGE_STR("Failed to allocate a directory entry.");
-               ret = BT_PLUGIN_STATUS_ERROR;
-               goto end;
-       }
-
-       file_path = zmalloc(PATH_MAX);
-       if (!file_path) {
-               BT_LOGE("Failed to allocate %zu bytes.", (size_t) PATH_MAX);
-               ret = BT_PLUGIN_STATUS_NOMEM;
-               goto end;
-       }
+       pthread_mutex_lock(&append_all_from_dir_info.lock);
 
-       strncpy(file_path, path, path_len);
-       /* Append a trailing '/' to the path */
-       if (file_path[path_len - 1] != '/') {
-               file_path[path_len++] = '/';
-       }
+       append_all_from_dir_info.plugin_set = plugin_set;
+       append_all_from_dir_info.recurse = recurse;
+       ret = nftw(path, nftw_append_all_from_dir,
+               APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
 
-       directory = opendir(file_path);
-       if (!directory) {
-               if (errno == EACCES) {
-                       BT_LOGD("Cannot open directory: %s: continuing: "
-                               "path=\"%s\", errno=%d",
-                               strerror(errno), file_path, errno);
-                       goto end;
-               }
+       pthread_mutex_unlock(&append_all_from_dir_info.lock);
 
-               BT_LOGW("Cannot open directory: %s: "
-                       "path=\"%s\", errno=%d",
-                       strerror(errno), file_path, errno);
+       if (ret != 0) {
+               BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
+                       strerror(errno), path, errno);
                ret = BT_PLUGIN_STATUS_ERROR;
-               goto end;
-       }
-
-       /* Recursively walk directory */
-       while (!readdir_r(directory, entry, &result) && result) {
-               struct stat st;
-               int stat_ret;
-               size_t file_name_len;
-
-               if (strcmp(result->d_name, ".") == 0 ||
-                               strcmp(result->d_name, "..") == 0) {
-                       continue;
-               }
-
-               if (result->d_name[0] == '.') {
-                       /* Skip hidden files, . and .. */
-                       BT_LOGV("Skipping hidden file: path=\"%s/%s\"",
-                               path, result->d_name);
-                       continue;
-               }
-
-               file_name_len = strlen(result->d_name);
-
-               if (path_len + file_name_len >= PATH_MAX) {
-                       BT_LOGD("Skipping file because its path length is too large: continuing: "
-                               "path=\"%s/%s\", length=%zu",
-                               path, result->d_name,
-                               (size_t) (path_len + file_name_len));
-                       continue;
-               }
-
-               strncpy(file_path + path_len, result->d_name, file_name_len);
-               file_path[path_len + file_name_len] = '\0';
-               stat_ret = stat(file_path, &st);
-               if (stat_ret < 0) {
-                       /* Continue to next file / directory. */
-                       BT_LOGD("Cannot get file information: %s: continuing: "
-                               "path=\"%s\", errno=%d",
-                               strerror(errno), file_path, errno);
-                       continue;
-               }
-
-               if (S_ISDIR(st.st_mode) && recurse) {
-                       ret = bt_plugin_create_append_all_from_dir(plugin_set,
-                               file_path, BT_TRUE);
-                       if (ret < 0) {
-                               BT_LOGW("Cannot recurse into directory to find plugins: "
-                                       "path=\"%s\", ret=%d", file_path, ret);
-                               goto end;
-                       }
-               } else if (S_ISREG(st.st_mode)) {
-                       struct bt_plugin_set *plugins_from_file =
-                               bt_plugin_create_all_from_file(file_path);
-
-                       if (plugins_from_file) {
-                               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);
-
-                                       BT_LOGD("Adding plugin to plugin set: "
-                                               "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
-                                               file_path, plugin,
-                                               bt_plugin_get_name(plugin));
-                                       bt_plugin_set_add_plugin(plugin_set,
-                                               plugin);
-                               }
-
-                               bt_put(plugins_from_file);
-                       }
-               }
        }
 
 end:
-       if (directory) {
-               if (closedir(directory)) {
-                       /*
-                        * We don't want to override the error since there is
-                        * nothing could do.
-                        */
-                       BT_LOGE("Cannot close directory entry: %s: "
-                               "path=\"%s\", errno=%d",
-                               strerror(errno), path, errno);
-               }
-       }
-
-       free(entry);
-       free(file_path);
        return ret;
 }
 
@@ -566,7 +527,7 @@ struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
        goto end;
 
 error:
-       BT_PUT(plugin_set);
+       BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
 
 end:
        return plugin_set;
@@ -718,11 +679,11 @@ struct bt_component_class *bt_plugin_get_component_class_by_index(
        }
 
        comp_class = g_ptr_array_index(plugin->comp_classes, index);
-       bt_get(comp_class);
+       bt_object_get_ref(comp_class);
        goto end;
 
 error:
-       BT_PUT(comp_class);
+       BT_OBJECT_PUT_REF_AND_RESET(comp_class);
 
 end:
        return comp_class;
@@ -753,12 +714,12 @@ struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
                enum bt_component_class_type comp_class_cand_type =
                        bt_component_class_get_type(comp_class_candidate);
 
-               assert(comp_class_cand_name);
-               assert(comp_class_cand_type >= 0);
+               BT_ASSERT(comp_class_cand_name);
+               BT_ASSERT(comp_class_cand_type >= 0);
 
                if (strcmp(name, comp_class_cand_name) == 0 &&
                                comp_class_cand_type == type) {
-                       comp_class = bt_get(comp_class_candidate);
+                       comp_class = bt_object_get_ref(comp_class_candidate);
                        break;
                }
        }
@@ -766,7 +727,7 @@ struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
        goto end;
 
 error:
-       BT_PUT(comp_class);
+       BT_OBJECT_PUT_REF_AND_RESET(comp_class);
 
 end:
        return comp_class;
@@ -814,7 +775,7 @@ enum bt_plugin_status bt_plugin_add_component_class(
 
        /* Add new component class */
        comp_class_index = plugin->comp_classes->len;
-       g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
+       g_ptr_array_add(plugin->comp_classes, bt_object_get_ref(comp_class));
 
        /* Special case for a shared object plugin */
        if (plugin->type == BT_PLUGIN_TYPE_SO) {
@@ -842,6 +803,6 @@ error:
        status = BT_PLUGIN_STATUS_ERROR;
 
 end:
-       bt_put(comp_class_dup);
+       bt_object_put_ref(comp_class_dup);
        return status;
 }
This page took 0.029059 seconds and 4 git commands to generate.