| 1 | /* |
| 2 | * Babeltrace trace converter - CLI tool's configuration |
| 3 | * |
| 4 | * Copyright 2016-2019 EfficiOS Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #define BT_LOG_TAG "CLI/PLUGINS" |
| 26 | #include "logging.h" |
| 27 | |
| 28 | #include "babeltrace2-plugins.h" |
| 29 | |
| 30 | #include <babeltrace2/babeltrace.h> |
| 31 | |
| 32 | /* Array of bt_plugin * */ |
| 33 | static GPtrArray *loaded_plugins; |
| 34 | |
| 35 | void init_loaded_plugins(void) |
| 36 | { |
| 37 | loaded_plugins = g_ptr_array_new_with_free_func( |
| 38 | (GDestroyNotify) bt_plugin_put_ref); |
| 39 | } |
| 40 | |
| 41 | void fini_loaded_plugins(void) |
| 42 | { |
| 43 | g_ptr_array_free(loaded_plugins, TRUE); |
| 44 | } |
| 45 | |
| 46 | const bt_plugin *find_loaded_plugin(const char *name) |
| 47 | { |
| 48 | int i; |
| 49 | const bt_plugin *plugin = NULL; |
| 50 | |
| 51 | BT_ASSERT(name); |
| 52 | BT_LOGI("Finding plugin: name=\"%s\"", name); |
| 53 | |
| 54 | for (i = 0; i < loaded_plugins->len; i++) { |
| 55 | plugin = g_ptr_array_index(loaded_plugins, i); |
| 56 | |
| 57 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | plugin = NULL; |
| 62 | } |
| 63 | |
| 64 | if (plugin) { |
| 65 | BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p", |
| 66 | name, plugin); |
| 67 | } else { |
| 68 | BT_LOGI("Cannot find plugin: name=\"%s\"", name); |
| 69 | } |
| 70 | |
| 71 | bt_plugin_get_ref(plugin); |
| 72 | return plugin; |
| 73 | } |
| 74 | |
| 75 | size_t get_loaded_plugins_count(void) |
| 76 | { |
| 77 | return loaded_plugins->len; |
| 78 | } |
| 79 | |
| 80 | const bt_plugin **borrow_loaded_plugins(void) |
| 81 | { |
| 82 | return (const bt_plugin **) loaded_plugins->pdata; |
| 83 | } |
| 84 | |
| 85 | const bt_plugin *borrow_loaded_plugin(size_t index) |
| 86 | { |
| 87 | BT_ASSERT(index < loaded_plugins->len); |
| 88 | return g_ptr_array_index(loaded_plugins, index); |
| 89 | } |
| 90 | |
| 91 | static |
| 92 | void add_to_loaded_plugins(const bt_plugin_set *plugin_set) |
| 93 | { |
| 94 | int64_t i; |
| 95 | int64_t count; |
| 96 | |
| 97 | count = bt_plugin_set_get_plugin_count(plugin_set); |
| 98 | BT_ASSERT(count >= 0); |
| 99 | |
| 100 | for (i = 0; i < count; i++) { |
| 101 | const bt_plugin *plugin = |
| 102 | bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); |
| 103 | const bt_plugin *loaded_plugin = |
| 104 | find_loaded_plugin(bt_plugin_get_name(plugin)); |
| 105 | |
| 106 | BT_ASSERT(plugin); |
| 107 | |
| 108 | if (loaded_plugin) { |
| 109 | BT_LOGI("Not using plugin: another one already exists with the same name: " |
| 110 | "plugin-name=\"%s\", plugin-path=\"%s\", " |
| 111 | "existing-plugin-path=\"%s\"", |
| 112 | bt_plugin_get_name(plugin), |
| 113 | bt_plugin_get_path(plugin), |
| 114 | bt_plugin_get_path(loaded_plugin)); |
| 115 | bt_plugin_put_ref(loaded_plugin); |
| 116 | } else { |
| 117 | /* Add to global array. */ |
| 118 | BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"", |
| 119 | bt_plugin_get_name(plugin)); |
| 120 | bt_plugin_get_ref(plugin); |
| 121 | g_ptr_array_add(loaded_plugins, (void *) plugin); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static |
| 127 | int load_dynamic_plugins(const bt_value *plugin_paths) |
| 128 | { |
| 129 | int nr_paths, i, ret = 0; |
| 130 | |
| 131 | nr_paths = bt_value_array_get_size(plugin_paths); |
| 132 | if (nr_paths < 0) { |
| 133 | BT_CLI_LOGE_APPEND_CAUSE( |
| 134 | "Cannot load dynamic plugins: no plugin path."); |
| 135 | ret = -1; |
| 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | BT_LOGI_STR("Loading dynamic plugins."); |
| 140 | |
| 141 | for (i = 0; i < nr_paths; i++) { |
| 142 | const bt_value *plugin_path_value = NULL; |
| 143 | const char *plugin_path; |
| 144 | const bt_plugin_set *plugin_set = NULL; |
| 145 | bt_plugin_find_all_from_dir_status status; |
| 146 | |
| 147 | plugin_path_value = |
| 148 | bt_value_array_borrow_element_by_index_const( |
| 149 | plugin_paths, i); |
| 150 | plugin_path = bt_value_string_get(plugin_path_value); |
| 151 | |
| 152 | /* |
| 153 | * Skip this if the directory does not exist because |
| 154 | * bt_plugin_find_all_from_dir() expects an existing |
| 155 | * directory. |
| 156 | */ |
| 157 | if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) { |
| 158 | BT_LOGI("Skipping nonexistent directory path: " |
| 159 | "path=\"%s\"", plugin_path); |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | status = bt_plugin_find_all_from_dir(plugin_path, BT_FALSE, |
| 164 | BT_FALSE, &plugin_set); |
| 165 | if (status < 0) { |
| 166 | BT_LOGE("Unable to load dynamic plugins from directory: " |
| 167 | "path=\"%s\"", plugin_path); |
| 168 | continue; |
| 169 | } else if (status == |
| 170 | BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND) { |
| 171 | BT_LOGI("No plugins found in directory: path=\"%s\"", |
| 172 | plugin_path); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK); |
| 177 | BT_ASSERT(plugin_set); |
| 178 | add_to_loaded_plugins(plugin_set); |
| 179 | bt_plugin_set_put_ref(plugin_set); |
| 180 | } |
| 181 | end: |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static |
| 186 | int load_static_plugins(void) |
| 187 | { |
| 188 | int ret = 0; |
| 189 | const bt_plugin_set *plugin_set; |
| 190 | bt_plugin_find_all_from_static_status status; |
| 191 | |
| 192 | BT_LOGI("Loading static plugins."); |
| 193 | status = bt_plugin_find_all_from_static(BT_FALSE, &plugin_set); |
| 194 | if (status < 0) { |
| 195 | BT_LOGE("Unable to load static plugins."); |
| 196 | ret = -1; |
| 197 | goto end; |
| 198 | } else if (status == |
| 199 | BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND) { |
| 200 | BT_LOGI("No static plugins found."); |
| 201 | goto end; |
| 202 | } |
| 203 | |
| 204 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK); |
| 205 | BT_ASSERT(plugin_set); |
| 206 | add_to_loaded_plugins(plugin_set); |
| 207 | bt_plugin_set_put_ref(plugin_set); |
| 208 | |
| 209 | end: |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | int require_loaded_plugins(const bt_value *plugin_paths) |
| 214 | { |
| 215 | static bool loaded = false; |
| 216 | static int ret = 0; |
| 217 | |
| 218 | if (loaded) { |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | loaded = true; |
| 223 | |
| 224 | if (load_dynamic_plugins(plugin_paths)) { |
| 225 | ret = -1; |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | if (load_static_plugins()) { |
| 230 | ret = -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); |
| 235 | |
| 236 | end: |
| 237 | return ret; |
| 238 | } |