| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #define BT_LOG_TAG "LIB/PLUGIN" |
| 27 | #include "lib/logging.h" |
| 28 | |
| 29 | #include "common/assert.h" |
| 30 | #include "lib/assert-pre.h" |
| 31 | #include "common/macros.h" |
| 32 | #include "compat/compiler.h" |
| 33 | #include "common/common.h" |
| 34 | #include <babeltrace2/plugin/plugin-const.h> |
| 35 | #include <babeltrace2/graph/component-class-const.h> |
| 36 | #include "lib/graph/component-class.h" |
| 37 | #include <babeltrace2/types.h> |
| 38 | #include <glib.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <stdint.h> |
| 42 | #include <inttypes.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <ftw.h> |
| 45 | #include <pthread.h> |
| 46 | |
| 47 | #include "plugin.h" |
| 48 | #include "plugin-so.h" |
| 49 | |
| 50 | #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace2-python-plugin-provider." G_MODULE_SUFFIX |
| 51 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file |
| 52 | #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME) |
| 53 | |
| 54 | #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8 |
| 55 | |
| 56 | #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT |
| 57 | #include <plugin/python-plugin-provider.h> |
| 58 | |
| 59 | static |
| 60 | enum bt_plugin_status (*bt_plugin_python_create_all_from_file_sym)( |
| 61 | const char *path, bool fail_on_load_error, |
| 62 | struct bt_plugin_set **plugin_set_out) = |
| 63 | bt_plugin_python_create_all_from_file; |
| 64 | |
| 65 | static |
| 66 | void init_python_plugin_provider(void) |
| 67 | { |
| 68 | } |
| 69 | #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */ |
| 70 | static GModule *python_plugin_provider_module; |
| 71 | |
| 72 | static |
| 73 | enum bt_plugin_status (*bt_plugin_python_create_all_from_file_sym)( |
| 74 | const char *path, bool fail_on_load_error, |
| 75 | struct bt_plugin_set **plugin_set_out); |
| 76 | |
| 77 | static |
| 78 | void init_python_plugin_provider(void) { |
| 79 | if (bt_plugin_python_create_all_from_file_sym != NULL) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | BT_LOGI_STR("Loading Python plugin provider module."); |
| 84 | python_plugin_provider_module = |
| 85 | g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0); |
| 86 | if (!python_plugin_provider_module) { |
| 87 | /* |
| 88 | * This is not an error. The whole point of having an |
| 89 | * external Python plugin provider is that it can be |
| 90 | * missing and the Babeltrace library still works. |
| 91 | */ |
| 92 | BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.", |
| 93 | PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (!g_module_symbol(python_plugin_provider_module, |
| 98 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR, |
| 99 | (gpointer) &bt_plugin_python_create_all_from_file_sym)) { |
| 100 | /* |
| 101 | * This is an error because, since we found the Python |
| 102 | * plugin provider shared object, we expect this symbol |
| 103 | * to exist. |
| 104 | */ |
| 105 | BT_LOGE("Cannot find the Python plugin provider loading symbol: " |
| 106 | "%s: continuing without Python plugin support: " |
| 107 | "file=\"%s\", symbol=\"%s\"", |
| 108 | g_module_error(), |
| 109 | PYTHON_PLUGIN_PROVIDER_FILENAME, |
| 110 | PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | BT_LOGI("Loaded Python plugin provider module: addr=%p", |
| 115 | python_plugin_provider_module); |
| 116 | } |
| 117 | |
| 118 | __attribute__((destructor)) static |
| 119 | void fini_python_plugin_provider(void) { |
| 120 | if (python_plugin_provider_module) { |
| 121 | BT_LOGI("Unloading Python plugin provider module."); |
| 122 | |
| 123 | if (!g_module_close(python_plugin_provider_module)) { |
| 124 | BT_LOGE("Failed to close the Python plugin provider module: %s.", |
| 125 | g_module_error()); |
| 126 | } |
| 127 | |
| 128 | python_plugin_provider_module = NULL; |
| 129 | } |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set) |
| 134 | { |
| 135 | BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set"); |
| 136 | return (uint64_t) plugin_set->plugins->len; |
| 137 | } |
| 138 | |
| 139 | const struct bt_plugin *bt_plugin_set_borrow_plugin_by_index_const( |
| 140 | const struct bt_plugin_set *plugin_set, uint64_t index) |
| 141 | { |
| 142 | BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set"); |
| 143 | BT_ASSERT_PRE_VALID_INDEX(index, plugin_set->plugins->len); |
| 144 | return g_ptr_array_index(plugin_set->plugins, index); |
| 145 | } |
| 146 | |
| 147 | enum bt_plugin_status bt_plugin_find_all_from_static( |
| 148 | bt_bool fail_on_load_error, |
| 149 | const struct bt_plugin_set **plugin_set_out) |
| 150 | { |
| 151 | /* bt_plugin_so_create_all_from_static() logs errors */ |
| 152 | return bt_plugin_so_create_all_from_static(fail_on_load_error, |
| 153 | (void *) plugin_set_out); |
| 154 | } |
| 155 | |
| 156 | enum bt_plugin_status bt_plugin_find_all_from_file(const char *path, |
| 157 | bt_bool fail_on_load_error, |
| 158 | const struct bt_plugin_set **plugin_set_out) |
| 159 | { |
| 160 | enum bt_plugin_status status; |
| 161 | |
| 162 | BT_ASSERT_PRE_NON_NULL(path, "Path"); |
| 163 | BT_ASSERT_PRE_NON_NULL(path, "Plugin set (output)"); |
| 164 | BT_LOGI("Creating plugins from file: path=\"%s\"", path); |
| 165 | |
| 166 | /* Try shared object plugins */ |
| 167 | status = bt_plugin_so_create_all_from_file(path, fail_on_load_error, |
| 168 | (void *) plugin_set_out); |
| 169 | if (status == BT_PLUGIN_STATUS_OK) { |
| 170 | BT_ASSERT(*plugin_set_out); |
| 171 | BT_ASSERT((*plugin_set_out)->plugins->len > 0); |
| 172 | goto end; |
| 173 | } else if (status < 0) { |
| 174 | BT_ASSERT(!*plugin_set_out); |
| 175 | goto end; |
| 176 | } |
| 177 | |
| 178 | BT_ASSERT(status == BT_PLUGIN_STATUS_NOT_FOUND); |
| 179 | BT_ASSERT(!*plugin_set_out); |
| 180 | |
| 181 | /* Try Python plugins if support is available */ |
| 182 | init_python_plugin_provider(); |
| 183 | if (bt_plugin_python_create_all_from_file_sym) { |
| 184 | status = bt_plugin_python_create_all_from_file_sym(path, |
| 185 | fail_on_load_error, (void *) plugin_set_out); |
| 186 | if (status == BT_PLUGIN_STATUS_OK) { |
| 187 | BT_ASSERT(*plugin_set_out); |
| 188 | BT_ASSERT((*plugin_set_out)->plugins->len > 0); |
| 189 | goto end; |
| 190 | } else if (status < 0) { |
| 191 | BT_ASSERT(!*plugin_set_out); |
| 192 | goto end; |
| 193 | } |
| 194 | |
| 195 | BT_ASSERT(status == BT_PLUGIN_STATUS_NOT_FOUND); |
| 196 | BT_ASSERT(!*plugin_set_out); |
| 197 | } |
| 198 | |
| 199 | end: |
| 200 | if (status == BT_PLUGIN_STATUS_OK) { |
| 201 | BT_LOGI("Created %u plugins from file: " |
| 202 | "path=\"%s\", count=%u, plugin-set-addr=%p", |
| 203 | (*plugin_set_out)->plugins->len, path, |
| 204 | (*plugin_set_out)->plugins->len, |
| 205 | *plugin_set_out); |
| 206 | } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) { |
| 207 | BT_LOGI("Found no plugins in file: path=\"%s\"", path); |
| 208 | } |
| 209 | |
| 210 | return status; |
| 211 | } |
| 212 | |
| 213 | static void destroy_gstring(void *data) |
| 214 | { |
| 215 | g_string_free(data, TRUE); |
| 216 | } |
| 217 | |
| 218 | enum bt_plugin_status bt_plugin_find(const char *plugin_name, |
| 219 | bt_bool fail_on_load_error, const struct bt_plugin **plugin_out) |
| 220 | { |
| 221 | const char *system_plugin_dir; |
| 222 | char *home_plugin_dir = NULL; |
| 223 | const char *envvar; |
| 224 | const struct bt_plugin *plugin = NULL; |
| 225 | const struct bt_plugin_set *plugin_set = NULL; |
| 226 | GPtrArray *dirs = NULL; |
| 227 | int ret; |
| 228 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
| 229 | size_t i, j; |
| 230 | |
| 231 | BT_ASSERT_PRE_NON_NULL(plugin_name, "Name"); |
| 232 | BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)"); |
| 233 | BT_LOGI("Finding named plugin in standard directories and built-in plugins: " |
| 234 | "name=\"%s\"", plugin_name); |
| 235 | dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring); |
| 236 | if (!dirs) { |
| 237 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 238 | status = BT_PLUGIN_STATUS_NOMEM; |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Search order is: |
| 244 | * |
| 245 | * 1. BABELTRACE_PLUGIN_PATH environment variable |
| 246 | * (colon-separated list of directories) |
| 247 | * 2. ~/.local/lib/babeltrace2/plugins |
| 248 | * 3. Default system directory for Babeltrace plugins, usually |
| 249 | * /usr/lib/babeltrace2/plugins or |
| 250 | * /usr/local/lib/babeltrace2/plugins if installed |
| 251 | * locally |
| 252 | * 4. Built-in plugins (static) |
| 253 | * |
| 254 | * Directories are searched non-recursively. |
| 255 | */ |
| 256 | envvar = getenv("BABELTRACE_PLUGIN_PATH"); |
| 257 | if (envvar) { |
| 258 | ret = bt_common_append_plugin_path_dirs(envvar, dirs); |
| 259 | if (ret) { |
| 260 | BT_LOGE_STR("Failed to append plugin path to array of directories."); |
| 261 | status = BT_PLUGIN_STATUS_NOMEM; |
| 262 | goto end; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | home_plugin_dir = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL); |
| 267 | if (home_plugin_dir) { |
| 268 | GString *home_plugin_dir_str = g_string_new(home_plugin_dir); |
| 269 | |
| 270 | if (!home_plugin_dir_str) { |
| 271 | BT_LOGE_STR("Failed to allocate a GString."); |
| 272 | status = BT_PLUGIN_STATUS_NOMEM; |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | g_ptr_array_add(dirs, home_plugin_dir_str); |
| 277 | } |
| 278 | |
| 279 | system_plugin_dir = bt_common_get_system_plugin_path(); |
| 280 | if (system_plugin_dir) { |
| 281 | GString *system_plugin_dir_str = |
| 282 | g_string_new(system_plugin_dir); |
| 283 | |
| 284 | if (!system_plugin_dir_str) { |
| 285 | BT_LOGE_STR("Failed to allocate a GString."); |
| 286 | status = BT_PLUGIN_STATUS_NOMEM; |
| 287 | goto end; |
| 288 | } |
| 289 | |
| 290 | g_ptr_array_add(dirs, system_plugin_dir_str); |
| 291 | } |
| 292 | |
| 293 | for (i = 0; i < dirs->len; i++) { |
| 294 | GString *dir = g_ptr_array_index(dirs, i); |
| 295 | |
| 296 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
| 297 | |
| 298 | /* |
| 299 | * Skip this if the directory does not exist because |
| 300 | * bt_plugin_find_all_from_dir() would log a warning. |
| 301 | */ |
| 302 | if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) { |
| 303 | BT_LOGI("Skipping nonexistent directory path: " |
| 304 | "path=\"%s\"", dir->str); |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | /* bt_plugin_find_all_from_dir() logs details/errors */ |
| 309 | status = bt_plugin_find_all_from_dir(dir->str, BT_FALSE, |
| 310 | fail_on_load_error, &plugin_set); |
| 311 | if (status < 0) { |
| 312 | BT_ASSERT(!plugin_set); |
| 313 | goto end; |
| 314 | } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) { |
| 315 | BT_ASSERT(!plugin_set); |
| 316 | BT_LOGI("No plugins found in directory: path=\"%s\"", |
| 317 | dir->str); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | BT_ASSERT(status == BT_PLUGIN_STATUS_OK); |
| 322 | BT_ASSERT(plugin_set); |
| 323 | |
| 324 | for (j = 0; j < plugin_set->plugins->len; j++) { |
| 325 | const struct bt_plugin *candidate_plugin = |
| 326 | g_ptr_array_index(plugin_set->plugins, j); |
| 327 | |
| 328 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
| 329 | plugin_name) == 0) { |
| 330 | BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"", |
| 331 | plugin_name, dir->str); |
| 332 | *plugin_out = candidate_plugin; |
| 333 | bt_object_get_no_null_check(*plugin_out); |
| 334 | goto end; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"", |
| 339 | plugin_name, dir->str); |
| 340 | } |
| 341 | |
| 342 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
| 343 | status = bt_plugin_find_all_from_static(fail_on_load_error, |
| 344 | &plugin_set); |
| 345 | if (status < 0) { |
| 346 | BT_ASSERT(!plugin_set); |
| 347 | goto end; |
| 348 | } |
| 349 | |
| 350 | if (status == BT_PLUGIN_STATUS_NOT_FOUND) { |
| 351 | BT_ASSERT(!plugin_set); |
| 352 | BT_LOGI_STR("No plugins found in built-in plugins."); |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | BT_ASSERT(status == BT_PLUGIN_STATUS_OK); |
| 357 | BT_ASSERT(plugin_set); |
| 358 | |
| 359 | for (j = 0; j < plugin_set->plugins->len; j++) { |
| 360 | const struct bt_plugin *candidate_plugin = |
| 361 | g_ptr_array_index(plugin_set->plugins, j); |
| 362 | |
| 363 | if (strcmp(bt_plugin_get_name(candidate_plugin), |
| 364 | plugin_name) == 0) { |
| 365 | BT_LOGI("Plugin found in built-in plugins: " |
| 366 | "name=\"%s\"", plugin_name); |
| 367 | *plugin_out = candidate_plugin; |
| 368 | bt_object_get_no_null_check(*plugin_out); |
| 369 | goto end; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | status = BT_PLUGIN_STATUS_NOT_FOUND; |
| 374 | |
| 375 | end: |
| 376 | free(home_plugin_dir); |
| 377 | bt_object_put_ref(plugin_set); |
| 378 | |
| 379 | if (dirs) { |
| 380 | g_ptr_array_free(dirs, TRUE); |
| 381 | } |
| 382 | |
| 383 | if (status == BT_PLUGIN_STATUS_OK) { |
| 384 | BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: " |
| 385 | "%!+l", plugin); |
| 386 | } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) { |
| 387 | BT_LOGI("No plugin found in standard directories and built-in plugins: " |
| 388 | "name=\"%s\"", plugin_name); |
| 389 | } |
| 390 | |
| 391 | return status; |
| 392 | } |
| 393 | |
| 394 | static struct { |
| 395 | pthread_mutex_t lock; |
| 396 | struct bt_plugin_set *plugin_set; |
| 397 | bool recurse; |
| 398 | bool fail_on_load_error; |
| 399 | enum bt_plugin_status status; |
| 400 | } append_all_from_dir_info = { |
| 401 | .lock = PTHREAD_MUTEX_INITIALIZER |
| 402 | }; |
| 403 | |
| 404 | static |
| 405 | int nftw_append_all_from_dir(const char *file, |
| 406 | const struct stat *sb, int flag, struct FTW *s) |
| 407 | { |
| 408 | int ret = 0; |
| 409 | const char *name = file + s->base; |
| 410 | |
| 411 | /* Check for recursion */ |
| 412 | if (!append_all_from_dir_info.recurse && s->level > 1) { |
| 413 | goto end; |
| 414 | } |
| 415 | |
| 416 | switch (flag) { |
| 417 | case FTW_F: |
| 418 | { |
| 419 | const struct bt_plugin_set *plugins_from_file = NULL; |
| 420 | |
| 421 | if (name[0] == '.') { |
| 422 | /* Skip hidden files */ |
| 423 | BT_LOGI("Skipping hidden file: path=\"%s\"", file); |
| 424 | goto end; |
| 425 | } |
| 426 | |
| 427 | append_all_from_dir_info.status = |
| 428 | bt_plugin_find_all_from_file(file, |
| 429 | append_all_from_dir_info.fail_on_load_error, |
| 430 | &plugins_from_file); |
| 431 | if (append_all_from_dir_info.status == BT_PLUGIN_STATUS_OK) { |
| 432 | size_t j; |
| 433 | |
| 434 | BT_ASSERT(plugins_from_file); |
| 435 | |
| 436 | for (j = 0; j < plugins_from_file->plugins->len; j++) { |
| 437 | struct bt_plugin *plugin = |
| 438 | g_ptr_array_index(plugins_from_file->plugins, j); |
| 439 | |
| 440 | BT_LIB_LOGI("Adding plugin to plugin set: " |
| 441 | "plugin-path=\"%s\", %![plugin-]+l", |
| 442 | file, plugin); |
| 443 | bt_plugin_set_add_plugin( |
| 444 | append_all_from_dir_info.plugin_set, |
| 445 | plugin); |
| 446 | } |
| 447 | |
| 448 | bt_object_put_ref(plugins_from_file); |
| 449 | goto end; |
| 450 | } else if (append_all_from_dir_info.status < 0) { |
| 451 | /* bt_plugin_find_all_from_file() logs errors */ |
| 452 | BT_ASSERT(!plugins_from_file); |
| 453 | ret = -1; |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Not found in this file: this is no an error; continue |
| 459 | * walking the directories. |
| 460 | */ |
| 461 | BT_ASSERT(!plugins_from_file); |
| 462 | BT_ASSERT(append_all_from_dir_info.status == |
| 463 | BT_PLUGIN_STATUS_NOT_FOUND); |
| 464 | break; |
| 465 | } |
| 466 | case FTW_DNR: |
| 467 | /* Continue to next file / directory. */ |
| 468 | BT_LOGI("Cannot enter directory: continuing: path=\"%s\"", file); |
| 469 | break; |
| 470 | case FTW_NS: |
| 471 | /* Continue to next file / directory. */ |
| 472 | BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file); |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | end: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static |
| 481 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( |
| 482 | struct bt_plugin_set *plugin_set, const char *path, |
| 483 | bt_bool recurse, bt_bool fail_on_load_error) |
| 484 | { |
| 485 | int nftw_flags = FTW_PHYS; |
| 486 | int ret; |
| 487 | enum bt_plugin_status status; |
| 488 | struct stat sb; |
| 489 | |
| 490 | BT_ASSERT(plugin_set); |
| 491 | BT_ASSERT(path); |
| 492 | BT_ASSERT(strlen(path) < PATH_MAX); |
| 493 | |
| 494 | /* |
| 495 | * Make sure that path exists and is accessible. |
| 496 | * This is necessary since Cygwin implementation of nftw() is not POSIX |
| 497 | * compliant. Cygwin nftw() implementation does not fail on non-existent |
| 498 | * path with ENOENT. Instead, it flags the directory as FTW_NS. FTW_NS during |
| 499 | * nftw_append_all_from_dir is not treated as an error since we are |
| 500 | * traversing the tree for plugin discovery. |
| 501 | */ |
| 502 | if (stat(path, &sb)) { |
| 503 | BT_LOGW_ERRNO("Cannot open directory", |
| 504 | ": path=\"%s\", recurse=%d", |
| 505 | path, recurse); |
| 506 | status = BT_PLUGIN_STATUS_ERROR; |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | pthread_mutex_lock(&append_all_from_dir_info.lock); |
| 511 | append_all_from_dir_info.plugin_set = plugin_set; |
| 512 | append_all_from_dir_info.recurse = recurse; |
| 513 | append_all_from_dir_info.status = BT_PLUGIN_STATUS_OK; |
| 514 | append_all_from_dir_info.fail_on_load_error = fail_on_load_error; |
| 515 | ret = nftw(path, nftw_append_all_from_dir, |
| 516 | APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags); |
| 517 | append_all_from_dir_info.plugin_set = NULL; |
| 518 | status = append_all_from_dir_info.status; |
| 519 | pthread_mutex_unlock(&append_all_from_dir_info.lock); |
| 520 | if (ret) { |
| 521 | BT_LOGW_ERRNO("Failed to walk directory", |
| 522 | ": path=\"%s\", recurse=%d", |
| 523 | path, recurse); |
| 524 | status = BT_PLUGIN_STATUS_ERROR; |
| 525 | goto end; |
| 526 | } |
| 527 | |
| 528 | if (status == BT_PLUGIN_STATUS_NOT_FOUND) { |
| 529 | /* |
| 530 | * We're just appending in this function; even if |
| 531 | * nothing was found, it's still okay from the caller's |
| 532 | * perspective. |
| 533 | */ |
| 534 | status = BT_PLUGIN_STATUS_OK; |
| 535 | } |
| 536 | |
| 537 | end: |
| 538 | return status; |
| 539 | } |
| 540 | |
| 541 | enum bt_plugin_status bt_plugin_find_all_from_dir(const char *path, |
| 542 | bt_bool recurse, bt_bool fail_on_load_error, |
| 543 | const struct bt_plugin_set **plugin_set_out) |
| 544 | { |
| 545 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
| 546 | |
| 547 | BT_ASSERT_PRE_NON_NULL(plugin_set_out, "Plugin set (output)"); |
| 548 | BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d", |
| 549 | path, recurse); |
| 550 | *plugin_set_out = bt_plugin_set_create(); |
| 551 | if (!*plugin_set_out) { |
| 552 | BT_LOGE_STR("Cannot create empty plugin set."); |
| 553 | status = BT_PLUGIN_STATUS_NOMEM; |
| 554 | goto error; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Append found plugins to array (never returns |
| 559 | * `BT_PLUGIN_STATUS_NOT_FOUND`) |
| 560 | */ |
| 561 | status = bt_plugin_create_append_all_from_dir((void *) *plugin_set_out, |
| 562 | path, recurse, fail_on_load_error); |
| 563 | if (status < 0) { |
| 564 | /* |
| 565 | * bt_plugin_create_append_all_from_dir() handles |
| 566 | * `fail_on_load_error`, so this is a "real" error. |
| 567 | */ |
| 568 | BT_LOGW("Cannot append plugins found in directory: " |
| 569 | "path=\"%s\", status=%s", |
| 570 | path, bt_plugin_status_string(status)); |
| 571 | goto error; |
| 572 | } |
| 573 | |
| 574 | BT_ASSERT(status == BT_PLUGIN_STATUS_OK); |
| 575 | |
| 576 | if ((*plugin_set_out)->plugins->len == 0) { |
| 577 | /* Nothing was appended: not found */ |
| 578 | BT_LOGI("No plugins found in directory: path=\"%s\"", path); |
| 579 | status = BT_PLUGIN_STATUS_NOT_FOUND; |
| 580 | goto error; |
| 581 | } |
| 582 | |
| 583 | BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"", |
| 584 | (*plugin_set_out)->plugins->len, |
| 585 | (*plugin_set_out)->plugins->len, path); |
| 586 | goto end; |
| 587 | |
| 588 | error: |
| 589 | BT_ASSERT(status != BT_PLUGIN_STATUS_OK); |
| 590 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
| 591 | |
| 592 | end: |
| 593 | return status; |
| 594 | } |
| 595 | |
| 596 | const char *bt_plugin_get_name(const struct bt_plugin *plugin) |
| 597 | { |
| 598 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 599 | return plugin->info.name_set ? plugin->info.name->str : NULL; |
| 600 | } |
| 601 | |
| 602 | const char *bt_plugin_get_author(const struct bt_plugin *plugin) |
| 603 | { |
| 604 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 605 | return plugin->info.author_set ? plugin->info.author->str : NULL; |
| 606 | } |
| 607 | |
| 608 | const char *bt_plugin_get_license(const struct bt_plugin *plugin) |
| 609 | { |
| 610 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 611 | return plugin->info.license_set ? plugin->info.license->str : NULL; |
| 612 | } |
| 613 | |
| 614 | const char *bt_plugin_get_path(const struct bt_plugin *plugin) |
| 615 | { |
| 616 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 617 | return plugin->info.path_set ? plugin->info.path->str : NULL; |
| 618 | } |
| 619 | |
| 620 | const char *bt_plugin_get_description(const struct bt_plugin *plugin) |
| 621 | { |
| 622 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 623 | return plugin->info.description_set ? |
| 624 | plugin->info.description->str : NULL; |
| 625 | } |
| 626 | |
| 627 | enum bt_property_availability bt_plugin_get_version(const struct bt_plugin *plugin, |
| 628 | unsigned int *major, unsigned int *minor, unsigned int *patch, |
| 629 | const char **extra) |
| 630 | { |
| 631 | enum bt_property_availability avail = |
| 632 | BT_PROPERTY_AVAILABILITY_AVAILABLE; |
| 633 | |
| 634 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 635 | |
| 636 | if (!plugin->info.version_set) { |
| 637 | BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin); |
| 638 | avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | if (major) { |
| 643 | *major = plugin->info.version.major; |
| 644 | } |
| 645 | |
| 646 | if (minor) { |
| 647 | *minor = plugin->info.version.minor; |
| 648 | } |
| 649 | |
| 650 | if (patch) { |
| 651 | *patch = plugin->info.version.patch; |
| 652 | } |
| 653 | |
| 654 | if (extra) { |
| 655 | *extra = plugin->info.version.extra->str; |
| 656 | } |
| 657 | |
| 658 | end: |
| 659 | return avail; |
| 660 | } |
| 661 | |
| 662 | uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin) |
| 663 | { |
| 664 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 665 | return (uint64_t) plugin->src_comp_classes->len; |
| 666 | } |
| 667 | |
| 668 | uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin) |
| 669 | { |
| 670 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 671 | return (uint64_t) plugin->flt_comp_classes->len; |
| 672 | } |
| 673 | |
| 674 | uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin) |
| 675 | { |
| 676 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 677 | return (uint64_t) plugin->sink_comp_classes->len; |
| 678 | } |
| 679 | |
| 680 | static inline |
| 681 | struct bt_component_class *borrow_component_class_by_index( |
| 682 | const struct bt_plugin *plugin, GPtrArray *comp_classes, |
| 683 | uint64_t index) |
| 684 | { |
| 685 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 686 | BT_ASSERT_PRE_VALID_INDEX(index, comp_classes->len); |
| 687 | return g_ptr_array_index(comp_classes, index); |
| 688 | } |
| 689 | |
| 690 | const struct bt_component_class_source * |
| 691 | bt_plugin_borrow_source_component_class_by_index_const( |
| 692 | const struct bt_plugin *plugin, uint64_t index) |
| 693 | { |
| 694 | return (const void *) borrow_component_class_by_index(plugin, |
| 695 | plugin->src_comp_classes, index); |
| 696 | } |
| 697 | |
| 698 | const struct bt_component_class_filter * |
| 699 | bt_plugin_borrow_filter_component_class_by_index_const( |
| 700 | const struct bt_plugin *plugin, uint64_t index) |
| 701 | { |
| 702 | return (const void *) borrow_component_class_by_index(plugin, |
| 703 | plugin->flt_comp_classes, index); |
| 704 | } |
| 705 | |
| 706 | const struct bt_component_class_sink * |
| 707 | bt_plugin_borrow_sink_component_class_by_index_const( |
| 708 | const struct bt_plugin *plugin, uint64_t index) |
| 709 | { |
| 710 | return (const void *) borrow_component_class_by_index(plugin, |
| 711 | plugin->sink_comp_classes, index); |
| 712 | } |
| 713 | |
| 714 | static inline |
| 715 | struct bt_component_class *borrow_component_class_by_name( |
| 716 | const struct bt_plugin *plugin, GPtrArray *comp_classes, |
| 717 | const char *name) |
| 718 | { |
| 719 | struct bt_component_class *comp_class = NULL; |
| 720 | size_t i; |
| 721 | |
| 722 | BT_ASSERT_PRE_NON_NULL(plugin, "Plugin"); |
| 723 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 724 | |
| 725 | for (i = 0; i < comp_classes->len; i++) { |
| 726 | struct bt_component_class *comp_class_candidate = |
| 727 | g_ptr_array_index(comp_classes, i); |
| 728 | const char *comp_class_cand_name = |
| 729 | bt_component_class_get_name(comp_class_candidate); |
| 730 | |
| 731 | BT_ASSERT(comp_class_cand_name); |
| 732 | |
| 733 | if (strcmp(name, comp_class_cand_name) == 0) { |
| 734 | comp_class = comp_class_candidate; |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return comp_class; |
| 740 | } |
| 741 | |
| 742 | const struct bt_component_class_source * |
| 743 | bt_plugin_borrow_source_component_class_by_name_const( |
| 744 | const struct bt_plugin *plugin, const char *name) |
| 745 | { |
| 746 | return (const void *) borrow_component_class_by_name(plugin, |
| 747 | plugin->src_comp_classes, name); |
| 748 | } |
| 749 | |
| 750 | const struct bt_component_class_filter * |
| 751 | bt_plugin_borrow_filter_component_class_by_name_const( |
| 752 | const struct bt_plugin *plugin, const char *name) |
| 753 | { |
| 754 | return (const void *) borrow_component_class_by_name(plugin, |
| 755 | plugin->flt_comp_classes, name); |
| 756 | } |
| 757 | |
| 758 | const struct bt_component_class_sink * |
| 759 | bt_plugin_borrow_sink_component_class_by_name_const( |
| 760 | const struct bt_plugin *plugin, const char *name) |
| 761 | { |
| 762 | return (const void *) borrow_component_class_by_name(plugin, |
| 763 | plugin->sink_comp_classes, name); |
| 764 | } |
| 765 | |
| 766 | void bt_plugin_get_ref(const struct bt_plugin *plugin) |
| 767 | { |
| 768 | bt_object_get_ref(plugin); |
| 769 | } |
| 770 | |
| 771 | void bt_plugin_put_ref(const struct bt_plugin *plugin) |
| 772 | { |
| 773 | bt_object_put_ref(plugin); |
| 774 | } |
| 775 | |
| 776 | void bt_plugin_set_get_ref(const struct bt_plugin_set *plugin_set) |
| 777 | { |
| 778 | bt_object_get_ref(plugin_set); |
| 779 | } |
| 780 | |
| 781 | void bt_plugin_set_put_ref(const struct bt_plugin_set *plugin_set) |
| 782 | { |
| 783 | bt_object_put_ref(plugin_set); |
| 784 | } |