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