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