| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/PLUGIN-SO" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "common/assert.h" |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include "compat/compiler.h" |
| 14 | #include <babeltrace2/plugin/plugin-dev.h> |
| 15 | #include "lib/graph/component-class.h" |
| 16 | #include <babeltrace2/graph/component-class.h> |
| 17 | #include <babeltrace2/types.h> |
| 18 | #include "common/list.h" |
| 19 | #include <string.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <glib.h> |
| 23 | #include <gmodule.h> |
| 24 | |
| 25 | #include "plugin.h" |
| 26 | #include "plugin-so.h" |
| 27 | #include "lib/func-status.h" |
| 28 | #include "common/common.h" |
| 29 | |
| 30 | #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX |
| 31 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) |
| 32 | #define LIBTOOL_PLUGIN_SUFFIX ".la" |
| 33 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) |
| 34 | |
| 35 | #define PLUGIN_SUFFIX_LEN bt_max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ |
| 36 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) |
| 37 | |
| 38 | BT_PLUGIN_MODULE(); |
| 39 | |
| 40 | /* |
| 41 | * This list, global to the library, keeps all component classes that |
| 42 | * have a reference to their shared library handles. It allows iteration |
| 43 | * on all component classes still present when the destructor executes |
| 44 | * to release the shared library handle references they might still have. |
| 45 | * |
| 46 | * The list items are the component classes created with |
| 47 | * bt_plugin_add_component_class(). They keep the shared library handle |
| 48 | * object created by their plugin alive so that the plugin's code is |
| 49 | * not discarded when it could still be in use by living components |
| 50 | * created from those component classes: |
| 51 | * |
| 52 | * [component] --ref-> [component class]-> [shlib handle] |
| 53 | * |
| 54 | * It allows this use-case: |
| 55 | * |
| 56 | * my_plugins = bt_plugin_find_all_from_file("/path/to/my-plugin.so"); |
| 57 | * // instantiate components from a plugin's component classes |
| 58 | * // put plugins and free my_plugins here |
| 59 | * // user code of instantiated components still exists |
| 60 | * |
| 61 | * An entry is removed from this list when a component class is |
| 62 | * destroyed thanks to a custom destroy listener. When the entry is |
| 63 | * removed, the entry is removed from the list, and we release the |
| 64 | * reference on the shlib handle. Assuming the original plugin object |
| 65 | * which contained some component classes is put first, when the last |
| 66 | * component class is removed from this list, the shared library handle |
| 67 | * object's reference count falls to zero and the shared library is |
| 68 | * finally closed. |
| 69 | * |
| 70 | * We're not using a GLib linked list here because this destructor is |
| 71 | * called after GLib's thread-specific data is destroyed, which contains |
| 72 | * the allocated memory for GLib data structures (what's used by |
| 73 | * g_slice_alloc()). |
| 74 | */ |
| 75 | |
| 76 | static |
| 77 | BT_LIST_HEAD(component_class_list); |
| 78 | |
| 79 | __attribute__((destructor)) static |
| 80 | void fini_comp_class_list(void) |
| 81 | { |
| 82 | struct bt_component_class *comp_class, *tmp; |
| 83 | |
| 84 | bt_list_for_each_entry_safe(comp_class, tmp, &component_class_list, node) { |
| 85 | bt_list_del(&comp_class->node); |
| 86 | BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle); |
| 87 | } |
| 88 | |
| 89 | BT_LOGD_STR("Released references from all component classes to shared library handles."); |
| 90 | } |
| 91 | |
| 92 | static |
| 93 | void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj) |
| 94 | { |
| 95 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle; |
| 96 | |
| 97 | BT_ASSERT(obj); |
| 98 | shared_lib_handle = container_of(obj, |
| 99 | struct bt_plugin_so_shared_lib_handle, base); |
| 100 | const char *path = shared_lib_handle->path ? |
| 101 | shared_lib_handle->path->str : NULL; |
| 102 | |
| 103 | BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"", |
| 104 | shared_lib_handle, path); |
| 105 | |
| 106 | if (shared_lib_handle->init_called && shared_lib_handle->exit) { |
| 107 | BT_LOGD_STR("Calling user's plugin exit function."); |
| 108 | shared_lib_handle->exit(); |
| 109 | BT_LOGD_STR("User function returned."); |
| 110 | } |
| 111 | |
| 112 | if (shared_lib_handle->module) { |
| 113 | #ifdef BT_DEBUG_MODE |
| 114 | /* |
| 115 | * Valgrind shows incomplete stack traces when |
| 116 | * dynamically loaded libraries are closed before it |
| 117 | * finishes. Use the LIBBABELTRACE2_NO_DLCLOSE in a debug |
| 118 | * build to avoid this. |
| 119 | */ |
| 120 | const char *var = getenv("LIBBABELTRACE2_NO_DLCLOSE"); |
| 121 | |
| 122 | if (!var || strcmp(var, "1") != 0) { |
| 123 | #endif |
| 124 | BT_LOGI("Closing GModule: path=\"%s\"", path); |
| 125 | |
| 126 | if (!g_module_close(shared_lib_handle->module)) { |
| 127 | /* |
| 128 | * Just log here: we're in a destructor, |
| 129 | * so we cannot append an error cause |
| 130 | * (there's no returned status). |
| 131 | */ |
| 132 | BT_LOGE("Cannot close GModule: %s: path=\"%s\"", |
| 133 | g_module_error(), path); |
| 134 | } |
| 135 | |
| 136 | shared_lib_handle->module = NULL; |
| 137 | #ifdef BT_DEBUG_MODE |
| 138 | } else { |
| 139 | BT_LOGI("Not closing GModule because `LIBBABELTRACE2_NO_DLCLOSE=1`: " |
| 140 | "path=\"%s\"", path); |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | if (shared_lib_handle->path) { |
| 146 | g_string_free(shared_lib_handle->path, TRUE); |
| 147 | shared_lib_handle->path = NULL; |
| 148 | } |
| 149 | |
| 150 | g_free(shared_lib_handle); |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | int bt_plugin_so_shared_lib_handle_create( |
| 155 | const char *path, |
| 156 | struct bt_plugin_so_shared_lib_handle **shared_lib_handle) |
| 157 | { |
| 158 | int status = BT_FUNC_STATUS_OK; |
| 159 | |
| 160 | BT_ASSERT(shared_lib_handle); |
| 161 | BT_LOGI("Creating shared library handle: path=\"%s\"", path ? path : "(null)"); |
| 162 | *shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1); |
| 163 | if (!*shared_lib_handle) { |
| 164 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one shared library handle."); |
| 165 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 166 | goto end; |
| 167 | } |
| 168 | |
| 169 | bt_object_init_shared(&(*shared_lib_handle)->base, |
| 170 | bt_plugin_so_shared_lib_handle_destroy); |
| 171 | |
| 172 | if (!path) { |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | (*shared_lib_handle)->path = g_string_new(path); |
| 177 | if (!(*shared_lib_handle)->path) { |
| 178 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 179 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 180 | goto end; |
| 181 | } |
| 182 | |
| 183 | (*shared_lib_handle)->module = g_module_open(path, G_MODULE_BIND_LOCAL); |
| 184 | if (!(*shared_lib_handle)->module) { |
| 185 | /* |
| 186 | * INFO-level logging because we're only _trying_ to |
| 187 | * open this file as a Babeltrace plugin: if it's not, |
| 188 | * it's not an error. And because this can be tried |
| 189 | * during bt_plugin_find_all_from_dir(), it's not even a |
| 190 | * warning. |
| 191 | */ |
| 192 | BT_LOGI("Cannot open GModule: %s: path=\"%s\"", |
| 193 | g_module_error(), path); |
| 194 | BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle); |
| 195 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 196 | goto end; |
| 197 | } |
| 198 | |
| 199 | goto end; |
| 200 | |
| 201 | end: |
| 202 | BT_ASSERT(*shared_lib_handle || status != BT_FUNC_STATUS_OK); |
| 203 | if (*shared_lib_handle) { |
| 204 | BT_LOGI("Created shared library handle: path=\"%s\", addr=%p", |
| 205 | path ? path : "(null)", *shared_lib_handle); |
| 206 | } |
| 207 | |
| 208 | return status; |
| 209 | } |
| 210 | |
| 211 | static |
| 212 | void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin) |
| 213 | { |
| 214 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; |
| 215 | |
| 216 | if (!plugin->spec_data) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO); |
| 221 | BT_ASSERT(spec); |
| 222 | BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle); |
| 223 | g_free(plugin->spec_data); |
| 224 | plugin->spec_data = NULL; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * This function does the following: |
| 229 | * |
| 230 | * 1. Iterate on the plugin descriptor attributes section and set the |
| 231 | * plugin's attributes depending on the attribute types. This |
| 232 | * includes the name of the plugin, its description, and its |
| 233 | * initialization function, for example. |
| 234 | * |
| 235 | * 2. Iterate on the component class descriptors section and create one |
| 236 | * "full descriptor" (temporary structure) for each one that is found |
| 237 | * and attached to our plugin descriptor. |
| 238 | * |
| 239 | * 3. Iterate on the component class descriptor attributes section and |
| 240 | * set the corresponding full descriptor's attributes depending on |
| 241 | * the attribute types. This includes the description of the |
| 242 | * component class, as well as its initialization and destroy |
| 243 | * methods. |
| 244 | * |
| 245 | * 4. Call the user's plugin initialization function, if any is |
| 246 | * defined. |
| 247 | * |
| 248 | * 5. For each full component class descriptor, create a component class |
| 249 | * object, set its optional attributes, and add it to the plugin |
| 250 | * object. |
| 251 | * |
| 252 | * 6. Freeze the plugin object. |
| 253 | */ |
| 254 | static |
| 255 | int bt_plugin_so_init(struct bt_plugin *plugin, |
| 256 | bool fail_on_load_error, |
| 257 | const struct __bt_plugin_descriptor *descriptor, |
| 258 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, |
| 259 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, |
| 260 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, |
| 261 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, |
| 262 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, |
| 263 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) |
| 264 | { |
| 265 | /* |
| 266 | * This structure's members point to the plugin's memory |
| 267 | * (do NOT free). |
| 268 | */ |
| 269 | struct comp_class_full_descriptor { |
| 270 | const struct __bt_plugin_component_class_descriptor *descriptor; |
| 271 | const char *description; |
| 272 | const char *help; |
| 273 | |
| 274 | union { |
| 275 | struct { |
| 276 | bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions; |
| 277 | bt_component_class_source_initialize_method init; |
| 278 | bt_component_class_source_finalize_method finalize; |
| 279 | bt_component_class_source_query_method query; |
| 280 | bt_component_class_source_output_port_connected_method output_port_connected; |
| 281 | bt_message_iterator_class_initialize_method msg_iter_initialize; |
| 282 | bt_message_iterator_class_finalize_method msg_iter_finalize; |
| 283 | bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin; |
| 284 | bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning; |
| 285 | bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin; |
| 286 | bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning; |
| 287 | } source; |
| 288 | |
| 289 | struct { |
| 290 | bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions; |
| 291 | bt_component_class_filter_initialize_method init; |
| 292 | bt_component_class_filter_finalize_method finalize; |
| 293 | bt_component_class_filter_query_method query; |
| 294 | bt_component_class_filter_input_port_connected_method input_port_connected; |
| 295 | bt_component_class_filter_output_port_connected_method output_port_connected; |
| 296 | bt_message_iterator_class_initialize_method msg_iter_initialize; |
| 297 | bt_message_iterator_class_finalize_method msg_iter_finalize; |
| 298 | bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin; |
| 299 | bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning; |
| 300 | bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin; |
| 301 | bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning; |
| 302 | } filter; |
| 303 | |
| 304 | struct { |
| 305 | bt_component_class_sink_get_supported_mip_versions_method get_supported_mip_versions; |
| 306 | bt_component_class_sink_initialize_method init; |
| 307 | bt_component_class_sink_finalize_method finalize; |
| 308 | bt_component_class_sink_query_method query; |
| 309 | bt_component_class_sink_input_port_connected_method input_port_connected; |
| 310 | bt_component_class_sink_graph_is_configured_method graph_is_configured; |
| 311 | } sink; |
| 312 | } methods; |
| 313 | }; |
| 314 | |
| 315 | int status = BT_FUNC_STATUS_OK; |
| 316 | struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr; |
| 317 | struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr; |
| 318 | struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr; |
| 319 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; |
| 320 | GArray *comp_class_full_descriptors; |
| 321 | size_t i; |
| 322 | int ret; |
| 323 | struct bt_message_iterator_class *msg_iter_class = NULL; |
| 324 | |
| 325 | BT_LOGI("Initializing plugin object from descriptors found in sections: " |
| 326 | "plugin-addr=%p, plugin-path=\"%s\", " |
| 327 | "attrs-begin-addr=%p, attrs-end-addr=%p, " |
| 328 | "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " |
| 329 | "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p", |
| 330 | plugin, |
| 331 | spec->shared_lib_handle->path ? |
| 332 | spec->shared_lib_handle->path->str : NULL, |
| 333 | attrs_begin, attrs_end, |
| 334 | cc_descriptors_begin, cc_descriptors_end, |
| 335 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 336 | comp_class_full_descriptors = g_array_new(FALSE, TRUE, |
| 337 | sizeof(struct comp_class_full_descriptor)); |
| 338 | if (!comp_class_full_descriptors) { |
| 339 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
| 340 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 341 | goto end; |
| 342 | } |
| 343 | |
| 344 | /* Set mandatory attributes */ |
| 345 | spec->descriptor = descriptor; |
| 346 | bt_plugin_set_name(plugin, descriptor->name); |
| 347 | |
| 348 | /* |
| 349 | * Find and set optional attributes attached to this plugin |
| 350 | * descriptor. |
| 351 | */ |
| 352 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { |
| 353 | const struct __bt_plugin_descriptor_attribute *cur_attr = |
| 354 | *cur_attr_ptr; |
| 355 | |
| 356 | if (!cur_attr) { |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if (cur_attr->plugin_descriptor != descriptor) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | switch (cur_attr->type) { |
| 365 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: |
| 366 | spec->init = cur_attr->value.init; |
| 367 | break; |
| 368 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: |
| 369 | spec->shared_lib_handle->exit = cur_attr->value.exit; |
| 370 | break; |
| 371 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: |
| 372 | bt_plugin_set_author(plugin, cur_attr->value.author); |
| 373 | break; |
| 374 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: |
| 375 | bt_plugin_set_license(plugin, cur_attr->value.license); |
| 376 | break; |
| 377 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: |
| 378 | bt_plugin_set_description(plugin, cur_attr->value.description); |
| 379 | break; |
| 380 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION: |
| 381 | bt_plugin_set_version(plugin, |
| 382 | (unsigned int) cur_attr->value.version.major, |
| 383 | (unsigned int) cur_attr->value.version.minor, |
| 384 | (unsigned int) cur_attr->value.version.patch, |
| 385 | cur_attr->value.version.extra); |
| 386 | break; |
| 387 | default: |
| 388 | if (fail_on_load_error) { |
| 389 | BT_LIB_LOGW_APPEND_CAUSE( |
| 390 | "Unknown plugin descriptor attribute: " |
| 391 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
| 392 | "attr-type-name=\"%s\", attr-type-id=%d", |
| 393 | spec->shared_lib_handle->path ? |
| 394 | spec->shared_lib_handle->path->str : |
| 395 | NULL, |
| 396 | descriptor->name, cur_attr->type_name, |
| 397 | cur_attr->type); |
| 398 | status = BT_FUNC_STATUS_ERROR; |
| 399 | goto end; |
| 400 | } else { |
| 401 | BT_LIB_LOGW( |
| 402 | "Ignoring unknown plugin descriptor attribute: " |
| 403 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
| 404 | "attr-type-name=\"%s\", attr-type-id=%d", |
| 405 | spec->shared_lib_handle->path ? |
| 406 | spec->shared_lib_handle->path->str : |
| 407 | NULL, |
| 408 | descriptor->name, cur_attr->type_name, |
| 409 | cur_attr->type); |
| 410 | } |
| 411 | |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Find component class descriptors attached to this plugin |
| 418 | * descriptor and initialize corresponding full component class |
| 419 | * descriptors in the array. |
| 420 | */ |
| 421 | for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) { |
| 422 | const struct __bt_plugin_component_class_descriptor *cur_cc_descr = |
| 423 | *cur_cc_descr_ptr; |
| 424 | struct comp_class_full_descriptor full_descriptor = {0}; |
| 425 | |
| 426 | if (!cur_cc_descr) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | if (cur_cc_descr->plugin_descriptor != descriptor) { |
| 431 | continue; |
| 432 | } |
| 433 | |
| 434 | full_descriptor.descriptor = cur_cc_descr; |
| 435 | g_array_append_val(comp_class_full_descriptors, |
| 436 | full_descriptor); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Find component class descriptor attributes attached to this |
| 441 | * plugin descriptor and update corresponding full component |
| 442 | * class descriptors in the array. |
| 443 | */ |
| 444 | for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) { |
| 445 | const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr = |
| 446 | *cur_cc_descr_attr_ptr; |
| 447 | enum bt_component_class_type cc_type; |
| 448 | |
| 449 | if (!cur_cc_descr_attr) { |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != |
| 454 | descriptor) { |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | cc_type = cur_cc_descr_attr->comp_class_descriptor->type; |
| 459 | |
| 460 | /* Find the corresponding component class descriptor entry */ |
| 461 | for (i = 0; i < comp_class_full_descriptors->len; i++) { |
| 462 | struct comp_class_full_descriptor *cc_full_descr = |
| 463 | &g_array_index(comp_class_full_descriptors, |
| 464 | struct comp_class_full_descriptor, i); |
| 465 | |
| 466 | if (cur_cc_descr_attr->comp_class_descriptor != |
| 467 | cc_full_descr->descriptor) { |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | switch (cur_cc_descr_attr->type) { |
| 472 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: |
| 473 | cc_full_descr->description = |
| 474 | cur_cc_descr_attr->value.description; |
| 475 | break; |
| 476 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP: |
| 477 | cc_full_descr->help = |
| 478 | cur_cc_descr_attr->value.help; |
| 479 | break; |
| 480 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GET_SUPPORTED_MIP_VERSIONS_METHOD: |
| 481 | switch (cc_type) { |
| 482 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 483 | cc_full_descr->methods.source.get_supported_mip_versions = |
| 484 | cur_cc_descr_attr->value.source_get_supported_mip_versions_method; |
| 485 | break; |
| 486 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 487 | cc_full_descr->methods.filter.get_supported_mip_versions = |
| 488 | cur_cc_descr_attr->value.filter_get_supported_mip_versions_method; |
| 489 | break; |
| 490 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 491 | cc_full_descr->methods.sink.get_supported_mip_versions = |
| 492 | cur_cc_descr_attr->value.sink_get_supported_mip_versions_method; |
| 493 | break; |
| 494 | default: |
| 495 | bt_common_abort(); |
| 496 | } |
| 497 | break; |
| 498 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INITIALIZE_METHOD: |
| 499 | switch (cc_type) { |
| 500 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 501 | cc_full_descr->methods.source.init = |
| 502 | cur_cc_descr_attr->value.source_initialize_method; |
| 503 | break; |
| 504 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 505 | cc_full_descr->methods.filter.init = |
| 506 | cur_cc_descr_attr->value.filter_initialize_method; |
| 507 | break; |
| 508 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 509 | cc_full_descr->methods.sink.init = |
| 510 | cur_cc_descr_attr->value.sink_initialize_method; |
| 511 | break; |
| 512 | default: |
| 513 | bt_common_abort(); |
| 514 | } |
| 515 | break; |
| 516 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD: |
| 517 | switch (cc_type) { |
| 518 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 519 | cc_full_descr->methods.source.finalize = |
| 520 | cur_cc_descr_attr->value.source_finalize_method; |
| 521 | break; |
| 522 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 523 | cc_full_descr->methods.filter.finalize = |
| 524 | cur_cc_descr_attr->value.filter_finalize_method; |
| 525 | break; |
| 526 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 527 | cc_full_descr->methods.sink.finalize = |
| 528 | cur_cc_descr_attr->value.sink_finalize_method; |
| 529 | break; |
| 530 | default: |
| 531 | bt_common_abort(); |
| 532 | } |
| 533 | break; |
| 534 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD: |
| 535 | switch (cc_type) { |
| 536 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 537 | cc_full_descr->methods.source.query = |
| 538 | cur_cc_descr_attr->value.source_query_method; |
| 539 | break; |
| 540 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 541 | cc_full_descr->methods.filter.query = |
| 542 | cur_cc_descr_attr->value.filter_query_method; |
| 543 | break; |
| 544 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 545 | cc_full_descr->methods.sink.query = |
| 546 | cur_cc_descr_attr->value.sink_query_method; |
| 547 | break; |
| 548 | default: |
| 549 | bt_common_abort(); |
| 550 | } |
| 551 | break; |
| 552 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD: |
| 553 | switch (cc_type) { |
| 554 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 555 | cc_full_descr->methods.filter.input_port_connected = |
| 556 | cur_cc_descr_attr->value.filter_input_port_connected_method; |
| 557 | break; |
| 558 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 559 | cc_full_descr->methods.sink.input_port_connected = |
| 560 | cur_cc_descr_attr->value.sink_input_port_connected_method; |
| 561 | break; |
| 562 | default: |
| 563 | bt_common_abort(); |
| 564 | } |
| 565 | break; |
| 566 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD: |
| 567 | switch (cc_type) { |
| 568 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 569 | cc_full_descr->methods.source.output_port_connected = |
| 570 | cur_cc_descr_attr->value.source_output_port_connected_method; |
| 571 | break; |
| 572 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 573 | cc_full_descr->methods.filter.output_port_connected = |
| 574 | cur_cc_descr_attr->value.filter_output_port_connected_method; |
| 575 | break; |
| 576 | default: |
| 577 | bt_common_abort(); |
| 578 | } |
| 579 | break; |
| 580 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD: |
| 581 | switch (cc_type) { |
| 582 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 583 | cc_full_descr->methods.sink.graph_is_configured = |
| 584 | cur_cc_descr_attr->value.sink_graph_is_configured_method; |
| 585 | break; |
| 586 | default: |
| 587 | bt_common_abort(); |
| 588 | } |
| 589 | break; |
| 590 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INITIALIZE_METHOD: |
| 591 | switch (cc_type) { |
| 592 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 593 | cc_full_descr->methods.source.msg_iter_initialize = |
| 594 | cur_cc_descr_attr->value.msg_iter_initialize_method; |
| 595 | break; |
| 596 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 597 | cc_full_descr->methods.filter.msg_iter_initialize = |
| 598 | cur_cc_descr_attr->value.msg_iter_initialize_method; |
| 599 | break; |
| 600 | default: |
| 601 | bt_common_abort(); |
| 602 | } |
| 603 | break; |
| 604 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD: |
| 605 | switch (cc_type) { |
| 606 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 607 | cc_full_descr->methods.source.msg_iter_finalize = |
| 608 | cur_cc_descr_attr->value.msg_iter_finalize_method; |
| 609 | break; |
| 610 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 611 | cc_full_descr->methods.filter.msg_iter_finalize = |
| 612 | cur_cc_descr_attr->value.msg_iter_finalize_method; |
| 613 | break; |
| 614 | default: |
| 615 | bt_common_abort(); |
| 616 | } |
| 617 | break; |
| 618 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD: |
| 619 | switch (cc_type) { |
| 620 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 621 | cc_full_descr->methods.source.msg_iter_seek_ns_from_origin = |
| 622 | cur_cc_descr_attr->value.msg_iter_seek_ns_from_origin_method; |
| 623 | break; |
| 624 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 625 | cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin = |
| 626 | cur_cc_descr_attr->value.msg_iter_seek_ns_from_origin_method; |
| 627 | break; |
| 628 | default: |
| 629 | bt_common_abort(); |
| 630 | } |
| 631 | break; |
| 632 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD: |
| 633 | switch (cc_type) { |
| 634 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 635 | cc_full_descr->methods.source.msg_iter_seek_beginning = |
| 636 | cur_cc_descr_attr->value.msg_iter_seek_beginning_method; |
| 637 | break; |
| 638 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 639 | cc_full_descr->methods.filter.msg_iter_seek_beginning = |
| 640 | cur_cc_descr_attr->value.msg_iter_seek_beginning_method; |
| 641 | break; |
| 642 | default: |
| 643 | bt_common_abort(); |
| 644 | } |
| 645 | break; |
| 646 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD: |
| 647 | switch (cc_type) { |
| 648 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 649 | cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin = |
| 650 | cur_cc_descr_attr->value.msg_iter_can_seek_ns_from_origin_method; |
| 651 | break; |
| 652 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 653 | cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin = |
| 654 | cur_cc_descr_attr->value.msg_iter_can_seek_ns_from_origin_method; |
| 655 | break; |
| 656 | default: |
| 657 | bt_common_abort(); |
| 658 | } |
| 659 | break; |
| 660 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD: |
| 661 | switch (cc_type) { |
| 662 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 663 | cc_full_descr->methods.source.msg_iter_can_seek_beginning = |
| 664 | cur_cc_descr_attr->value.msg_iter_can_seek_beginning_method; |
| 665 | break; |
| 666 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 667 | cc_full_descr->methods.filter.msg_iter_can_seek_beginning = |
| 668 | cur_cc_descr_attr->value.msg_iter_can_seek_beginning_method; |
| 669 | break; |
| 670 | default: |
| 671 | bt_common_abort(); |
| 672 | } |
| 673 | break; |
| 674 | default: |
| 675 | if (fail_on_load_error) { |
| 676 | BT_LIB_LOGW_APPEND_CAUSE( |
| 677 | "Unknown component class descriptor attribute: " |
| 678 | "plugin-path=\"%s\", " |
| 679 | "plugin-name=\"%s\", " |
| 680 | "comp-class-name=\"%s\", " |
| 681 | "comp-class-type=%s, " |
| 682 | "attr-type-name=\"%s\", " |
| 683 | "attr-type-id=%d", |
| 684 | spec->shared_lib_handle->path ? |
| 685 | spec->shared_lib_handle->path->str : |
| 686 | NULL, |
| 687 | descriptor->name, |
| 688 | cur_cc_descr_attr->comp_class_descriptor->name, |
| 689 | bt_common_component_class_type_string( |
| 690 | cur_cc_descr_attr->comp_class_descriptor->type), |
| 691 | cur_cc_descr_attr->type_name, |
| 692 | cur_cc_descr_attr->type); |
| 693 | status = BT_FUNC_STATUS_ERROR; |
| 694 | goto end; |
| 695 | } else { |
| 696 | BT_LIB_LOGW( |
| 697 | "Ignoring unknown component class descriptor attribute: " |
| 698 | "plugin-path=\"%s\", " |
| 699 | "plugin-name=\"%s\", " |
| 700 | "comp-class-name=\"%s\", " |
| 701 | "comp-class-type=%s, " |
| 702 | "attr-type-name=\"%s\", " |
| 703 | "attr-type-id=%d", |
| 704 | spec->shared_lib_handle->path ? |
| 705 | spec->shared_lib_handle->path->str : |
| 706 | NULL, |
| 707 | descriptor->name, |
| 708 | cur_cc_descr_attr->comp_class_descriptor->name, |
| 709 | bt_common_component_class_type_string( |
| 710 | cur_cc_descr_attr->comp_class_descriptor->type), |
| 711 | cur_cc_descr_attr->type_name, |
| 712 | cur_cc_descr_attr->type); |
| 713 | } |
| 714 | |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /* Initialize plugin */ |
| 721 | if (spec->init) { |
| 722 | enum bt_plugin_initialize_func_status init_status; |
| 723 | |
| 724 | BT_LOGD_STR("Calling user's plugin initialization function."); |
| 725 | init_status = spec->init((void *) plugin); |
| 726 | BT_LOGD("User function returned: status=%s", |
| 727 | bt_common_func_status_string(init_status)); |
| 728 | |
| 729 | if (init_status < 0) { |
| 730 | if (fail_on_load_error) { |
| 731 | BT_LIB_LOGW_APPEND_CAUSE( |
| 732 | "User's plugin initialization function failed: " |
| 733 | "status=%s", |
| 734 | bt_common_func_status_string(init_status)); |
| 735 | status = init_status; |
| 736 | goto end; |
| 737 | } else { |
| 738 | BT_LIB_LOGW( |
| 739 | "User's plugin initialization function failed: " |
| 740 | "status=%s", |
| 741 | bt_common_func_status_string(init_status)); |
| 742 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 743 | } |
| 744 | |
| 745 | goto end; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | spec->shared_lib_handle->init_called = BT_TRUE; |
| 750 | |
| 751 | /* Add described component classes to plugin */ |
| 752 | for (i = 0; i < comp_class_full_descriptors->len; i++) { |
| 753 | struct comp_class_full_descriptor *cc_full_descr = |
| 754 | &g_array_index(comp_class_full_descriptors, |
| 755 | struct comp_class_full_descriptor, i); |
| 756 | struct bt_component_class *comp_class = NULL; |
| 757 | struct bt_component_class_source *src_comp_class = NULL; |
| 758 | struct bt_component_class_filter *flt_comp_class = NULL; |
| 759 | struct bt_component_class_sink *sink_comp_class = NULL; |
| 760 | |
| 761 | BT_LOGI("Creating and setting properties of plugin's component class: " |
| 762 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
| 763 | "comp-class-name=\"%s\", comp-class-type=%s", |
| 764 | spec->shared_lib_handle->path ? |
| 765 | spec->shared_lib_handle->path->str : |
| 766 | NULL, |
| 767 | descriptor->name, |
| 768 | cc_full_descr->descriptor->name, |
| 769 | bt_common_component_class_type_string( |
| 770 | cc_full_descr->descriptor->type)); |
| 771 | |
| 772 | if (cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 773 | cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_FILTER) { |
| 774 | bt_message_iterator_class_next_method next_method; |
| 775 | bt_message_iterator_class_initialize_method init_method; |
| 776 | bt_message_iterator_class_finalize_method fini_method; |
| 777 | bt_message_iterator_class_seek_ns_from_origin_method seek_ns_from_origin_method; |
| 778 | bt_message_iterator_class_seek_beginning_method seek_beginning_method; |
| 779 | bt_message_iterator_class_can_seek_ns_from_origin_method can_seek_ns_from_origin_method; |
| 780 | bt_message_iterator_class_can_seek_beginning_method can_seek_beginning_method; |
| 781 | |
| 782 | if (cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_SOURCE) { |
| 783 | next_method = cc_full_descr->descriptor->methods.source.msg_iter_next; |
| 784 | init_method = cc_full_descr->methods.source.msg_iter_initialize; |
| 785 | fini_method = cc_full_descr->methods.source.msg_iter_finalize; |
| 786 | seek_ns_from_origin_method = cc_full_descr->methods.source.msg_iter_seek_ns_from_origin; |
| 787 | can_seek_ns_from_origin_method = cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin; |
| 788 | seek_beginning_method = cc_full_descr->methods.source.msg_iter_seek_beginning; |
| 789 | can_seek_beginning_method = cc_full_descr->methods.source.msg_iter_can_seek_beginning; |
| 790 | } else { |
| 791 | next_method = cc_full_descr->descriptor->methods.filter.msg_iter_next; |
| 792 | init_method = cc_full_descr->methods.filter.msg_iter_initialize; |
| 793 | fini_method = cc_full_descr->methods.filter.msg_iter_finalize; |
| 794 | seek_ns_from_origin_method = cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin; |
| 795 | can_seek_ns_from_origin_method = cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin; |
| 796 | seek_beginning_method = cc_full_descr->methods.filter.msg_iter_seek_beginning; |
| 797 | can_seek_beginning_method = cc_full_descr->methods.filter.msg_iter_can_seek_beginning; |
| 798 | } |
| 799 | |
| 800 | msg_iter_class = bt_message_iterator_class_create(next_method); |
| 801 | if (!msg_iter_class) { |
| 802 | BT_LIB_LOGE_APPEND_CAUSE( |
| 803 | "Cannot create message iterator class."); |
| 804 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 805 | goto end; |
| 806 | } |
| 807 | |
| 808 | if (init_method) { |
| 809 | ret = bt_message_iterator_class_set_initialize_method( |
| 810 | msg_iter_class, init_method); |
| 811 | if (ret) { |
| 812 | BT_LIB_LOGE_APPEND_CAUSE( |
| 813 | "Cannot set message iterator initialization method."); |
| 814 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 815 | goto end; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (fini_method) { |
| 820 | ret = bt_message_iterator_class_set_finalize_method( |
| 821 | msg_iter_class, fini_method); |
| 822 | if (ret) { |
| 823 | BT_LIB_LOGE_APPEND_CAUSE( |
| 824 | "Cannot set message iterator finalization method."); |
| 825 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 826 | goto end; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (seek_ns_from_origin_method) { |
| 831 | ret = bt_message_iterator_class_set_seek_ns_from_origin_methods( |
| 832 | msg_iter_class, |
| 833 | seek_ns_from_origin_method, |
| 834 | can_seek_ns_from_origin_method); |
| 835 | if (ret) { |
| 836 | BT_LIB_LOGE_APPEND_CAUSE( |
| 837 | "Cannot set message iterator \"seek nanoseconds from origin\" methods."); |
| 838 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 839 | goto end; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (seek_beginning_method) { |
| 844 | ret = bt_message_iterator_class_set_seek_beginning_methods( |
| 845 | msg_iter_class, |
| 846 | seek_beginning_method, |
| 847 | can_seek_beginning_method); |
| 848 | if (ret) { |
| 849 | BT_LIB_LOGE_APPEND_CAUSE( |
| 850 | "Cannot set message iterator \"seek beginning\" methods."); |
| 851 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 852 | goto end; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | switch (cc_full_descr->descriptor->type) { |
| 858 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 859 | BT_ASSERT(msg_iter_class); |
| 860 | |
| 861 | src_comp_class = bt_component_class_source_create( |
| 862 | cc_full_descr->descriptor->name, msg_iter_class); |
| 863 | comp_class = bt_component_class_source_as_component_class( |
| 864 | src_comp_class); |
| 865 | break; |
| 866 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 867 | BT_ASSERT(msg_iter_class); |
| 868 | |
| 869 | flt_comp_class = bt_component_class_filter_create( |
| 870 | cc_full_descr->descriptor->name, msg_iter_class); |
| 871 | comp_class = bt_component_class_filter_as_component_class( |
| 872 | flt_comp_class); |
| 873 | break; |
| 874 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 875 | BT_ASSERT(!msg_iter_class); |
| 876 | |
| 877 | sink_comp_class = bt_component_class_sink_create( |
| 878 | cc_full_descr->descriptor->name, |
| 879 | cc_full_descr->descriptor->methods.sink.consume); |
| 880 | comp_class = bt_component_class_sink_as_component_class( |
| 881 | sink_comp_class); |
| 882 | break; |
| 883 | default: |
| 884 | if (fail_on_load_error) { |
| 885 | BT_LIB_LOGW_APPEND_CAUSE( |
| 886 | "Unknown component class type: " |
| 887 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
| 888 | "comp-class-name=\"%s\", comp-class-type=%d", |
| 889 | spec->shared_lib_handle->path->str ? |
| 890 | spec->shared_lib_handle->path->str : |
| 891 | NULL, |
| 892 | descriptor->name, |
| 893 | cc_full_descr->descriptor->name, |
| 894 | cc_full_descr->descriptor->type); |
| 895 | status = BT_FUNC_STATUS_ERROR; |
| 896 | goto end; |
| 897 | } else { |
| 898 | BT_LIB_LOGW( |
| 899 | "Ignoring unknown component class type: " |
| 900 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
| 901 | "comp-class-name=\"%s\", comp-class-type=%d", |
| 902 | spec->shared_lib_handle->path->str ? |
| 903 | spec->shared_lib_handle->path->str : |
| 904 | NULL, |
| 905 | descriptor->name, |
| 906 | cc_full_descr->descriptor->name, |
| 907 | cc_full_descr->descriptor->type); |
| 908 | continue; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (!comp_class) { |
| 913 | BT_LIB_LOGE_APPEND_CAUSE( |
| 914 | "Cannot create component class."); |
| 915 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 916 | goto end; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * The component class has taken a reference on the message |
| 921 | * iterator class, so we can drop ours. The message iterator |
| 922 | * class will get destroyed at the same time as the component |
| 923 | * class. |
| 924 | */ |
| 925 | bt_message_iterator_class_put_ref(msg_iter_class); |
| 926 | msg_iter_class = NULL; |
| 927 | |
| 928 | if (cc_full_descr->description) { |
| 929 | ret = bt_component_class_set_description( |
| 930 | comp_class, cc_full_descr->description); |
| 931 | if (ret) { |
| 932 | BT_LIB_LOGE_APPEND_CAUSE( |
| 933 | "Cannot set component class's description."); |
| 934 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 935 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
| 936 | goto end; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if (cc_full_descr->help) { |
| 941 | ret = bt_component_class_set_help(comp_class, |
| 942 | cc_full_descr->help); |
| 943 | if (ret) { |
| 944 | BT_LIB_LOGE_APPEND_CAUSE( |
| 945 | "Cannot set component class's help string."); |
| 946 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 947 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
| 948 | goto end; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | switch (cc_full_descr->descriptor->type) { |
| 953 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 954 | if (cc_full_descr->methods.source.get_supported_mip_versions) { |
| 955 | ret = bt_component_class_source_set_get_supported_mip_versions_method( |
| 956 | src_comp_class, |
| 957 | cc_full_descr->methods.source.get_supported_mip_versions); |
| 958 | if (ret) { |
| 959 | BT_LIB_LOGE_APPEND_CAUSE( |
| 960 | "Cannot set source component class's \"get supported MIP versions\" method."); |
| 961 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 962 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
| 963 | goto end; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (cc_full_descr->methods.source.init) { |
| 968 | ret = bt_component_class_source_set_initialize_method( |
| 969 | src_comp_class, |
| 970 | cc_full_descr->methods.source.init); |
| 971 | if (ret) { |
| 972 | BT_LIB_LOGE_APPEND_CAUSE( |
| 973 | "Cannot set source component class's initialization method."); |
| 974 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 975 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
| 976 | goto end; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (cc_full_descr->methods.source.finalize) { |
| 981 | ret = bt_component_class_source_set_finalize_method( |
| 982 | src_comp_class, |
| 983 | cc_full_descr->methods.source.finalize); |
| 984 | if (ret) { |
| 985 | BT_LIB_LOGE_APPEND_CAUSE( |
| 986 | "Cannot set source component class's finalization method."); |
| 987 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 988 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
| 989 | goto end; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | if (cc_full_descr->methods.source.query) { |
| 994 | ret = bt_component_class_source_set_query_method( |
| 995 | src_comp_class, |
| 996 | cc_full_descr->methods.source.query); |
| 997 | if (ret) { |
| 998 | BT_LIB_LOGE_APPEND_CAUSE( |
| 999 | "Cannot set source component class's query method."); |
| 1000 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1001 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
| 1002 | goto end; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (cc_full_descr->methods.source.output_port_connected) { |
| 1007 | ret = bt_component_class_source_set_output_port_connected_method( |
| 1008 | src_comp_class, |
| 1009 | cc_full_descr->methods.source.output_port_connected); |
| 1010 | if (ret) { |
| 1011 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1012 | "Cannot set source component class's \"output port connected\" method."); |
| 1013 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1014 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
| 1015 | goto end; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | break; |
| 1020 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1021 | if (cc_full_descr->methods.filter.get_supported_mip_versions) { |
| 1022 | ret = bt_component_class_filter_set_get_supported_mip_versions_method( |
| 1023 | flt_comp_class, |
| 1024 | cc_full_descr->methods.filter.get_supported_mip_versions); |
| 1025 | if (ret) { |
| 1026 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1027 | "Cannot set filter component class's \"get supported MIP versions\" method."); |
| 1028 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1029 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1030 | goto end; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (cc_full_descr->methods.filter.init) { |
| 1035 | ret = bt_component_class_filter_set_initialize_method( |
| 1036 | flt_comp_class, |
| 1037 | cc_full_descr->methods.filter.init); |
| 1038 | if (ret) { |
| 1039 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1040 | "Cannot set filter component class's initialization method."); |
| 1041 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1042 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1043 | goto end; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if (cc_full_descr->methods.filter.finalize) { |
| 1048 | ret = bt_component_class_filter_set_finalize_method( |
| 1049 | flt_comp_class, |
| 1050 | cc_full_descr->methods.filter.finalize); |
| 1051 | if (ret) { |
| 1052 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1053 | "Cannot set filter component class's finalization method."); |
| 1054 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1055 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1056 | goto end; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | if (cc_full_descr->methods.filter.query) { |
| 1061 | ret = bt_component_class_filter_set_query_method( |
| 1062 | flt_comp_class, |
| 1063 | cc_full_descr->methods.filter.query); |
| 1064 | if (ret) { |
| 1065 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1066 | "Cannot set filter component class's query method."); |
| 1067 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1068 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1069 | goto end; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | if (cc_full_descr->methods.filter.input_port_connected) { |
| 1074 | ret = bt_component_class_filter_set_input_port_connected_method( |
| 1075 | flt_comp_class, |
| 1076 | cc_full_descr->methods.filter.input_port_connected); |
| 1077 | if (ret) { |
| 1078 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1079 | "Cannot set filter component class's \"input port connected\" method."); |
| 1080 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1081 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1082 | goto end; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | if (cc_full_descr->methods.filter.output_port_connected) { |
| 1087 | ret = bt_component_class_filter_set_output_port_connected_method( |
| 1088 | flt_comp_class, |
| 1089 | cc_full_descr->methods.filter.output_port_connected); |
| 1090 | if (ret) { |
| 1091 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1092 | "Cannot set filter component class's \"output port connected\" method."); |
| 1093 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1094 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
| 1095 | goto end; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | break; |
| 1100 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 1101 | if (cc_full_descr->methods.sink.get_supported_mip_versions) { |
| 1102 | ret = bt_component_class_sink_set_get_supported_mip_versions_method( |
| 1103 | sink_comp_class, |
| 1104 | cc_full_descr->methods.sink.get_supported_mip_versions); |
| 1105 | if (ret) { |
| 1106 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1107 | "Cannot set sink component class's \"get supported MIP versions\" method."); |
| 1108 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1109 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1110 | goto end; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | if (cc_full_descr->methods.sink.init) { |
| 1115 | ret = bt_component_class_sink_set_initialize_method( |
| 1116 | sink_comp_class, |
| 1117 | cc_full_descr->methods.sink.init); |
| 1118 | if (ret) { |
| 1119 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1120 | "Cannot set sink component class's initialization method."); |
| 1121 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1122 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1123 | goto end; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (cc_full_descr->methods.sink.finalize) { |
| 1128 | ret = bt_component_class_sink_set_finalize_method( |
| 1129 | sink_comp_class, |
| 1130 | cc_full_descr->methods.sink.finalize); |
| 1131 | if (ret) { |
| 1132 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1133 | "Cannot set sink component class's finalization method."); |
| 1134 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1135 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1136 | goto end; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | if (cc_full_descr->methods.sink.query) { |
| 1141 | ret = bt_component_class_sink_set_query_method( |
| 1142 | sink_comp_class, |
| 1143 | cc_full_descr->methods.sink.query); |
| 1144 | if (ret) { |
| 1145 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1146 | "Cannot set sink component class's query method."); |
| 1147 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1148 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1149 | goto end; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | if (cc_full_descr->methods.sink.input_port_connected) { |
| 1154 | ret = bt_component_class_sink_set_input_port_connected_method( |
| 1155 | sink_comp_class, |
| 1156 | cc_full_descr->methods.sink.input_port_connected); |
| 1157 | if (ret) { |
| 1158 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1159 | "Cannot set sink component class's \"input port connected\" method."); |
| 1160 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1161 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1162 | goto end; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | if (cc_full_descr->methods.sink.graph_is_configured) { |
| 1167 | ret = bt_component_class_sink_set_graph_is_configured_method( |
| 1168 | sink_comp_class, |
| 1169 | cc_full_descr->methods.sink.graph_is_configured); |
| 1170 | if (ret) { |
| 1171 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1172 | "Cannot set sink component class's \"graph is configured\" method."); |
| 1173 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1174 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
| 1175 | goto end; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | break; |
| 1180 | default: |
| 1181 | bt_common_abort(); |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * Add component class to the plugin object. |
| 1186 | * |
| 1187 | * This will call back |
| 1188 | * bt_plugin_so_on_add_component_class() so that we can |
| 1189 | * add a mapping in the component class list when we |
| 1190 | * know the component class is successfully added. |
| 1191 | */ |
| 1192 | status = bt_plugin_add_component_class(plugin, |
| 1193 | (void *) comp_class); |
| 1194 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
| 1195 | if (status < 0) { |
| 1196 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1197 | "Cannot add component class to plugin."); |
| 1198 | goto end; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | end: |
| 1203 | bt_message_iterator_class_put_ref(msg_iter_class); |
| 1204 | g_array_free(comp_class_full_descriptors, TRUE); |
| 1205 | return status; |
| 1206 | } |
| 1207 | |
| 1208 | static |
| 1209 | struct bt_plugin *bt_plugin_so_create_empty( |
| 1210 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle) |
| 1211 | { |
| 1212 | struct bt_plugin *plugin; |
| 1213 | struct bt_plugin_so_spec_data *spec; |
| 1214 | |
| 1215 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO); |
| 1216 | if (!plugin) { |
| 1217 | goto error; |
| 1218 | } |
| 1219 | |
| 1220 | plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data; |
| 1221 | plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); |
| 1222 | if (!plugin->spec_data) { |
| 1223 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1224 | "Failed to allocate one SO plugin specific data structure."); |
| 1225 | goto error; |
| 1226 | } |
| 1227 | |
| 1228 | spec = plugin->spec_data; |
| 1229 | spec->shared_lib_handle = shared_lib_handle; |
| 1230 | bt_object_get_ref_no_null_check(spec->shared_lib_handle); |
| 1231 | goto end; |
| 1232 | |
| 1233 | error: |
| 1234 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
| 1235 | |
| 1236 | end: |
| 1237 | return plugin; |
| 1238 | } |
| 1239 | |
| 1240 | static |
| 1241 | size_t count_non_null_items_in_section(const void *begin, const void *end) |
| 1242 | { |
| 1243 | size_t count = 0; |
| 1244 | const int * const *begin_int = (const int * const *) begin; |
| 1245 | const int * const *end_int = (const int * const *) end; |
| 1246 | const int * const *iter; |
| 1247 | |
| 1248 | for (iter = begin_int; iter != end_int; iter++) { |
| 1249 | if (*iter) { |
| 1250 | count++; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return count; |
| 1255 | } |
| 1256 | |
| 1257 | static |
| 1258 | int bt_plugin_so_create_all_from_sections( |
| 1259 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle, |
| 1260 | bool fail_on_load_error, |
| 1261 | struct __bt_plugin_descriptor const * const *descriptors_begin, |
| 1262 | struct __bt_plugin_descriptor const * const *descriptors_end, |
| 1263 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, |
| 1264 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, |
| 1265 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, |
| 1266 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, |
| 1267 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, |
| 1268 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end, |
| 1269 | struct bt_plugin_set **plugin_set_out) |
| 1270 | { |
| 1271 | int status = BT_FUNC_STATUS_OK; |
| 1272 | size_t descriptor_count; |
| 1273 | size_t attrs_count; |
| 1274 | size_t cc_descriptors_count; |
| 1275 | size_t cc_descr_attrs_count; |
| 1276 | size_t i; |
| 1277 | |
| 1278 | BT_ASSERT(shared_lib_handle); |
| 1279 | BT_ASSERT(plugin_set_out); |
| 1280 | *plugin_set_out = NULL; |
| 1281 | descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end); |
| 1282 | attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end); |
| 1283 | cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end); |
| 1284 | cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end); |
| 1285 | BT_LOGI("Creating all SO plugins from sections: " |
| 1286 | "plugin-path=\"%s\", " |
| 1287 | "descr-begin-addr=%p, descr-end-addr=%p, " |
| 1288 | "attrs-begin-addr=%p, attrs-end-addr=%p, " |
| 1289 | "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " |
| 1290 | "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, " |
| 1291 | "descr-count=%zu, attrs-count=%zu, " |
| 1292 | "cc-descr-count=%zu, cc-descr-attrs-count=%zu", |
| 1293 | shared_lib_handle->path ? shared_lib_handle->path->str : NULL, |
| 1294 | descriptors_begin, descriptors_end, |
| 1295 | attrs_begin, attrs_end, |
| 1296 | cc_descriptors_begin, cc_descriptors_end, |
| 1297 | cc_descr_attrs_begin, cc_descr_attrs_end, |
| 1298 | descriptor_count, attrs_count, |
| 1299 | cc_descriptors_count, cc_descr_attrs_count); |
| 1300 | *plugin_set_out = bt_plugin_set_create(); |
| 1301 | if (!*plugin_set_out) { |
| 1302 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set."); |
| 1303 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1304 | goto error; |
| 1305 | } |
| 1306 | |
| 1307 | for (i = 0; i < descriptors_end - descriptors_begin; i++) { |
| 1308 | const struct __bt_plugin_descriptor *descriptor = |
| 1309 | descriptors_begin[i]; |
| 1310 | struct bt_plugin *plugin; |
| 1311 | |
| 1312 | if (!descriptor) { |
| 1313 | continue; |
| 1314 | } |
| 1315 | |
| 1316 | BT_LOGI("Creating plugin object for plugin: name=\"%s\"", |
| 1317 | descriptor->name); |
| 1318 | plugin = bt_plugin_so_create_empty(shared_lib_handle); |
| 1319 | if (!plugin) { |
| 1320 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1321 | "Cannot create empty shared library handle."); |
| 1322 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1323 | goto error; |
| 1324 | } |
| 1325 | |
| 1326 | if (shared_lib_handle->path) { |
| 1327 | bt_plugin_set_path(plugin, |
| 1328 | shared_lib_handle->path->str); |
| 1329 | } |
| 1330 | |
| 1331 | status = bt_plugin_so_init(plugin, fail_on_load_error, |
| 1332 | descriptor, attrs_begin, attrs_end, |
| 1333 | cc_descriptors_begin, cc_descriptors_end, |
| 1334 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 1335 | if (status == BT_FUNC_STATUS_OK) { |
| 1336 | /* Add to plugin set */ |
| 1337 | bt_plugin_set_add_plugin(*plugin_set_out, plugin); |
| 1338 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
| 1339 | } else if (status < 0) { |
| 1340 | /* |
| 1341 | * bt_plugin_so_init() handles |
| 1342 | * `fail_on_load_error`, so this is a "real" |
| 1343 | * error. |
| 1344 | */ |
| 1345 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1346 | "Cannot initialize SO plugin object from sections."); |
| 1347 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
| 1348 | goto error; |
| 1349 | } |
| 1350 | |
| 1351 | BT_ASSERT(!plugin); |
| 1352 | } |
| 1353 | |
| 1354 | BT_ASSERT(*plugin_set_out); |
| 1355 | |
| 1356 | if ((*plugin_set_out)->plugins->len == 0) { |
| 1357 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
| 1358 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1359 | } |
| 1360 | |
| 1361 | goto end; |
| 1362 | |
| 1363 | error: |
| 1364 | BT_ASSERT(status < 0); |
| 1365 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
| 1366 | |
| 1367 | end: |
| 1368 | return status; |
| 1369 | } |
| 1370 | |
| 1371 | BT_HIDDEN |
| 1372 | int bt_plugin_so_create_all_from_static(bool fail_on_load_error, |
| 1373 | struct bt_plugin_set **plugin_set_out) |
| 1374 | { |
| 1375 | int status; |
| 1376 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; |
| 1377 | |
| 1378 | BT_ASSERT(plugin_set_out); |
| 1379 | *plugin_set_out = NULL; |
| 1380 | status = bt_plugin_so_shared_lib_handle_create(NULL, |
| 1381 | &shared_lib_handle); |
| 1382 | if (status != BT_FUNC_STATUS_OK) { |
| 1383 | BT_ASSERT(!shared_lib_handle); |
| 1384 | goto end; |
| 1385 | } |
| 1386 | |
| 1387 | BT_ASSERT(shared_lib_handle); |
| 1388 | BT_LOGD_STR("Creating all SO plugins from built-in plugins."); |
| 1389 | status = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
| 1390 | fail_on_load_error, |
| 1391 | __bt_get_begin_section_plugin_descriptors(), |
| 1392 | __bt_get_end_section_plugin_descriptors(), |
| 1393 | __bt_get_begin_section_plugin_descriptor_attributes(), |
| 1394 | __bt_get_end_section_plugin_descriptor_attributes(), |
| 1395 | __bt_get_begin_section_component_class_descriptors(), |
| 1396 | __bt_get_end_section_component_class_descriptors(), |
| 1397 | __bt_get_begin_section_component_class_descriptor_attributes(), |
| 1398 | __bt_get_end_section_component_class_descriptor_attributes(), |
| 1399 | plugin_set_out); |
| 1400 | BT_ASSERT((status == BT_FUNC_STATUS_OK && *plugin_set_out && |
| 1401 | (*plugin_set_out)->plugins->len > 0) || !*plugin_set_out); |
| 1402 | |
| 1403 | end: |
| 1404 | BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle); |
| 1405 | return status; |
| 1406 | } |
| 1407 | |
| 1408 | BT_HIDDEN |
| 1409 | int bt_plugin_so_create_all_from_file(const char *path, |
| 1410 | bool fail_on_load_error, struct bt_plugin_set **plugin_set_out) |
| 1411 | { |
| 1412 | size_t path_len; |
| 1413 | int status; |
| 1414 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; |
| 1415 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; |
| 1416 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; |
| 1417 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; |
| 1418 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; |
| 1419 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; |
| 1420 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; |
| 1421 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; |
| 1422 | struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void); |
| 1423 | struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void); |
| 1424 | struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void); |
| 1425 | struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void); |
| 1426 | struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void); |
| 1427 | struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void); |
| 1428 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void); |
| 1429 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void); |
| 1430 | bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE; |
| 1431 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; |
| 1432 | |
| 1433 | BT_ASSERT(path); |
| 1434 | BT_ASSERT(plugin_set_out); |
| 1435 | *plugin_set_out = NULL; |
| 1436 | path_len = strlen(path); |
| 1437 | |
| 1438 | /* |
| 1439 | * An SO plugin file must have a known plugin file suffix. So the file |
| 1440 | * path must be longer than the suffix length. |
| 1441 | */ |
| 1442 | if (path_len <= PLUGIN_SUFFIX_LEN) { |
| 1443 | BT_LOGI("Path is too short to be an `.so` or `.la` plugin file:" |
| 1444 | "path=%s, path-length=%zu, min-length=%zu", |
| 1445 | path, path_len, PLUGIN_SUFFIX_LEN); |
| 1446 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1447 | goto end; |
| 1448 | } |
| 1449 | |
| 1450 | BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path); |
| 1451 | path_len++; |
| 1452 | |
| 1453 | /* |
| 1454 | * Check if the file ends with a known plugin file type suffix |
| 1455 | * (i.e. .so or .la on Linux). |
| 1456 | */ |
| 1457 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, |
| 1458 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, |
| 1459 | LIBTOOL_PLUGIN_SUFFIX_LEN); |
| 1460 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, |
| 1461 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, |
| 1462 | NATIVE_PLUGIN_SUFFIX_LEN); |
| 1463 | if (!is_shared_object && !is_libtool_wrapper) { |
| 1464 | /* Name indicates this is not a plugin file; not an error */ |
| 1465 | BT_LOGI("File is not an SO plugin file: path=\"%s\"", path); |
| 1466 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1467 | goto end; |
| 1468 | } |
| 1469 | |
| 1470 | status = bt_plugin_so_shared_lib_handle_create(path, |
| 1471 | &shared_lib_handle); |
| 1472 | if (status != BT_FUNC_STATUS_OK) { |
| 1473 | /* bt_plugin_so_shared_lib_handle_create() logs more details */ |
| 1474 | BT_ASSERT(!shared_lib_handle); |
| 1475 | goto end; |
| 1476 | } |
| 1477 | |
| 1478 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors", |
| 1479 | (gpointer *) &get_begin_section_plugin_descriptors)) { |
| 1480 | descriptors_begin = get_begin_section_plugin_descriptors(); |
| 1481 | } else { |
| 1482 | /* |
| 1483 | * Use this first symbol to know whether or not this |
| 1484 | * shared object _looks like_ a Babeltrace plugin. Since |
| 1485 | * g_module_symbol() failed, assume that this is not a |
| 1486 | * Babeltrace plugin, so it's not an error. |
| 1487 | */ |
| 1488 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1489 | "symbol=\"%s\"", path, |
| 1490 | "__bt_get_begin_section_plugin_descriptors"); |
| 1491 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1492 | goto end; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * If g_module_symbol() fails for any of the other symbols, fail |
| 1497 | * if `fail_on_load_error` is true. |
| 1498 | */ |
| 1499 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors", |
| 1500 | (gpointer *) &get_end_section_plugin_descriptors)) { |
| 1501 | descriptors_end = get_end_section_plugin_descriptors(); |
| 1502 | } else { |
| 1503 | if (fail_on_load_error) { |
| 1504 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1505 | "Cannot resolve plugin symbol: path=\"%s\", " |
| 1506 | "symbol=\"%s\"", path, |
| 1507 | "__bt_get_end_section_plugin_descriptors"); |
| 1508 | status = BT_FUNC_STATUS_ERROR; |
| 1509 | } else { |
| 1510 | BT_LIB_LOGW( |
| 1511 | "Cannot resolve plugin symbol: path=\"%s\", " |
| 1512 | "symbol=\"%s\"", path, |
| 1513 | "__bt_get_end_section_plugin_descriptors"); |
| 1514 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1515 | } |
| 1516 | |
| 1517 | goto end; |
| 1518 | } |
| 1519 | |
| 1520 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes", |
| 1521 | (gpointer *) &get_begin_section_plugin_descriptor_attributes)) { |
| 1522 | attrs_begin = get_begin_section_plugin_descriptor_attributes(); |
| 1523 | } else { |
| 1524 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1525 | "symbol=\"%s\"", path, |
| 1526 | "__bt_get_begin_section_plugin_descriptor_attributes"); |
| 1527 | } |
| 1528 | |
| 1529 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes", |
| 1530 | (gpointer *) &get_end_section_plugin_descriptor_attributes)) { |
| 1531 | attrs_end = get_end_section_plugin_descriptor_attributes(); |
| 1532 | } else { |
| 1533 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1534 | "symbol=\"%s\"", path, |
| 1535 | "__bt_get_end_section_plugin_descriptor_attributes"); |
| 1536 | } |
| 1537 | |
| 1538 | if ((!!attrs_begin - !!attrs_end) != 0) { |
| 1539 | if (fail_on_load_error) { |
| 1540 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1541 | "Found section start or end symbol, but not both: " |
| 1542 | "path=\"%s\", symbol-start=\"%s\", " |
| 1543 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1544 | "symbol-end-addr=%p", |
| 1545 | path, "__bt_get_begin_section_plugin_descriptor_attributes", |
| 1546 | "__bt_get_end_section_plugin_descriptor_attributes", |
| 1547 | attrs_begin, attrs_end); |
| 1548 | status = BT_FUNC_STATUS_ERROR; |
| 1549 | } else { |
| 1550 | BT_LIB_LOGW( |
| 1551 | "Found section start or end symbol, but not both: " |
| 1552 | "path=\"%s\", symbol-start=\"%s\", " |
| 1553 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1554 | "symbol-end-addr=%p", |
| 1555 | path, "__bt_get_begin_section_plugin_descriptor_attributes", |
| 1556 | "__bt_get_end_section_plugin_descriptor_attributes", |
| 1557 | attrs_begin, attrs_end); |
| 1558 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1559 | } |
| 1560 | |
| 1561 | goto end; |
| 1562 | } |
| 1563 | |
| 1564 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors", |
| 1565 | (gpointer *) &get_begin_section_component_class_descriptors)) { |
| 1566 | cc_descriptors_begin = get_begin_section_component_class_descriptors(); |
| 1567 | } else { |
| 1568 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1569 | "symbol=\"%s\"", path, |
| 1570 | "__bt_get_begin_section_component_class_descriptors"); |
| 1571 | } |
| 1572 | |
| 1573 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors", |
| 1574 | (gpointer *) &get_end_section_component_class_descriptors)) { |
| 1575 | cc_descriptors_end = get_end_section_component_class_descriptors(); |
| 1576 | } else { |
| 1577 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1578 | "symbol=\"%s\"", path, |
| 1579 | "__bt_get_end_section_component_class_descriptors"); |
| 1580 | } |
| 1581 | |
| 1582 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { |
| 1583 | if (fail_on_load_error) { |
| 1584 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1585 | "Found section start or end symbol, but not both: " |
| 1586 | "path=\"%s\", symbol-start=\"%s\", " |
| 1587 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1588 | "symbol-end-addr=%p", |
| 1589 | path, "__bt_get_begin_section_component_class_descriptors", |
| 1590 | "__bt_get_end_section_component_class_descriptors", |
| 1591 | cc_descriptors_begin, cc_descriptors_end); |
| 1592 | status = BT_FUNC_STATUS_ERROR; |
| 1593 | } else { |
| 1594 | BT_LIB_LOGW( |
| 1595 | "Found section start or end symbol, but not both: " |
| 1596 | "path=\"%s\", symbol-start=\"%s\", " |
| 1597 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1598 | "symbol-end-addr=%p", |
| 1599 | path, "__bt_get_begin_section_component_class_descriptors", |
| 1600 | "__bt_get_end_section_component_class_descriptors", |
| 1601 | cc_descriptors_begin, cc_descriptors_end); |
| 1602 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1603 | } |
| 1604 | |
| 1605 | goto end; |
| 1606 | } |
| 1607 | |
| 1608 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes", |
| 1609 | (gpointer *) &get_begin_section_component_class_descriptor_attributes)) { |
| 1610 | cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes(); |
| 1611 | } else { |
| 1612 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1613 | "symbol=\"%s\"", path, |
| 1614 | "__bt_get_begin_section_component_class_descriptor_attributes"); |
| 1615 | } |
| 1616 | |
| 1617 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes", |
| 1618 | (gpointer *) &get_end_section_component_class_descriptor_attributes)) { |
| 1619 | cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes(); |
| 1620 | } else { |
| 1621 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
| 1622 | "symbol=\"%s\"", path, |
| 1623 | "__bt_get_end_section_component_class_descriptor_attributes"); |
| 1624 | } |
| 1625 | |
| 1626 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { |
| 1627 | if (fail_on_load_error) { |
| 1628 | BT_LIB_LOGW_APPEND_CAUSE( |
| 1629 | "Found section start or end symbol, but not both: " |
| 1630 | "path=\"%s\", symbol-start=\"%s\", " |
| 1631 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1632 | "symbol-end-addr=%p", |
| 1633 | path, "__bt_get_begin_section_component_class_descriptor_attributes", |
| 1634 | "__bt_get_end_section_component_class_descriptor_attributes", |
| 1635 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 1636 | status = BT_FUNC_STATUS_ERROR; |
| 1637 | } else { |
| 1638 | BT_LIB_LOGW( |
| 1639 | "Found section start or end symbol, but not both: " |
| 1640 | "path=\"%s\", symbol-start=\"%s\", " |
| 1641 | "symbol-end=\"%s\", symbol-start-addr=%p, " |
| 1642 | "symbol-end-addr=%p", |
| 1643 | path, "__bt_get_begin_section_component_class_descriptor_attributes", |
| 1644 | "__bt_get_end_section_component_class_descriptor_attributes", |
| 1645 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 1646 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 1647 | } |
| 1648 | |
| 1649 | goto end; |
| 1650 | } |
| 1651 | |
| 1652 | /* Initialize plugin */ |
| 1653 | BT_LOGD_STR("Initializing plugin object."); |
| 1654 | status = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
| 1655 | fail_on_load_error, |
| 1656 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, |
| 1657 | cc_descriptors_begin, cc_descriptors_end, |
| 1658 | cc_descr_attrs_begin, cc_descr_attrs_end, plugin_set_out); |
| 1659 | |
| 1660 | end: |
| 1661 | BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle); |
| 1662 | return status; |
| 1663 | } |
| 1664 | |
| 1665 | static |
| 1666 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, |
| 1667 | void *data) |
| 1668 | { |
| 1669 | bt_list_del(&comp_class->node); |
| 1670 | BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle); |
| 1671 | BT_LOGD("Component class destroyed: removed entry from list: " |
| 1672 | "comp-cls-addr=%p", comp_class); |
| 1673 | } |
| 1674 | |
| 1675 | void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, |
| 1676 | struct bt_component_class *comp_class) |
| 1677 | { |
| 1678 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; |
| 1679 | |
| 1680 | BT_ASSERT(plugin->spec_data); |
| 1681 | BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO); |
| 1682 | |
| 1683 | bt_list_add(&comp_class->node, &component_class_list); |
| 1684 | comp_class->so_handle = spec->shared_lib_handle; |
| 1685 | bt_object_get_ref_no_null_check(comp_class->so_handle); |
| 1686 | |
| 1687 | /* Add our custom destroy listener */ |
| 1688 | bt_component_class_add_destroy_listener(comp_class, |
| 1689 | plugin_comp_class_destroy_listener, NULL); |
| 1690 | } |