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