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