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