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