| 1 | /* |
| 2 | * plugin.c |
| 3 | * |
| 4 | * Babeltrace Plugin |
| 5 | * |
| 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <babeltrace/compiler.h> |
| 31 | #include <babeltrace/ref.h> |
| 32 | #include <babeltrace/plugin/plugin-dev.h> |
| 33 | #include <babeltrace/plugin/plugin-internal.h> |
| 34 | #include <babeltrace/component/component-class-internal.h> |
| 35 | #include <string.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <glib.h> |
| 38 | #include <gmodule.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <dirent.h> |
| 43 | |
| 44 | #define NATIVE_PLUGIN_SUFFIX ".so" |
| 45 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) |
| 46 | #define LIBTOOL_PLUGIN_SUFFIX ".la" |
| 47 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) |
| 48 | |
| 49 | #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ |
| 50 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) |
| 51 | |
| 52 | #define SECTION_BEGIN(_name) (&(__start_##_name)) |
| 53 | #define SECTION_END(_name) (&(__stop_##_name)) |
| 54 | #define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name)) |
| 55 | |
| 56 | #define DECLARE_SECTION(_type, _name) \ |
| 57 | extern _type __start_##_name __attribute((weak)); \ |
| 58 | extern _type __stop_##_name __attribute((weak)) |
| 59 | |
| 60 | DECLARE_SECTION(struct __bt_plugin_descriptor const *, __bt_plugin_descriptors); |
| 61 | DECLARE_SECTION(struct __bt_plugin_descriptor_attribute const *, __bt_plugin_descriptor_attributes); |
| 62 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor const *, __bt_plugin_component_class_descriptors); |
| 63 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute const *, __bt_plugin_component_class_descriptor_attributes); |
| 64 | |
| 65 | /* |
| 66 | * This hash table, global to the library, maps component class pointers |
| 67 | * to shared library handles. |
| 68 | * |
| 69 | * The keys (component classes) are NOT owned by this hash table, whereas |
| 70 | * the values (shared library handles) are owned by this hash table. |
| 71 | * |
| 72 | * The keys are the component classes created with |
| 73 | * bt_plugin_add_component_class(). They keep the shared library handle |
| 74 | * object created by their plugin alive so that the plugin's code is |
| 75 | * not discarded when it could still be in use by living components |
| 76 | * created from those component classes: |
| 77 | * |
| 78 | * [component] --ref-> [component class] --through this HT-> [shlib handle] |
| 79 | * |
| 80 | * This hash table exists for two reasons: |
| 81 | * |
| 82 | * 1. To allow this application: |
| 83 | * |
| 84 | * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so"); |
| 85 | * // instantiate components from a plugin's component classes |
| 86 | * // put plugins and free my_plugins here |
| 87 | * // user code of instantiated components still exists |
| 88 | * |
| 89 | * 2. To decouple the plugin subsystem from the component subsystem: |
| 90 | * while plugins objects need to know component class objects, the |
| 91 | * opposite is not necessary, thus it makes no sense for a component |
| 92 | * class to keep a reference to the plugin object from which it was |
| 93 | * created. |
| 94 | * |
| 95 | * An entry is removed from this HT when a component class is destroyed |
| 96 | * thanks to a custom destroy listener. When the entry is removed, the |
| 97 | * GLib function calls the value destroy notifier of the HT, which is |
| 98 | * bt_put(). This decreases the reference count of the mapped shared |
| 99 | * library handle. Assuming the original plugin object which contained |
| 100 | * some component classes is put first, when the last component class is |
| 101 | * removed from this HT, the shared library handle object's reference |
| 102 | * count falls to zero and the shared library is finally closed. |
| 103 | */ |
| 104 | static |
| 105 | GHashTable *comp_classes_to_shlib_handles; |
| 106 | |
| 107 | __attribute__((constructor)) static |
| 108 | void init_comp_classes_to_shlib_handles(void) { |
| 109 | comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash, |
| 110 | g_direct_equal, NULL, bt_put); |
| 111 | assert(comp_classes_to_shlib_handles); |
| 112 | } |
| 113 | |
| 114 | __attribute__((destructor)) static |
| 115 | void fini_comp_classes_to_shlib_handles(void) { |
| 116 | if (comp_classes_to_shlib_handles) { |
| 117 | g_hash_table_destroy(comp_classes_to_shlib_handles); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static |
| 122 | void bt_plugin_shared_lib_handle_destroy(struct bt_object *obj) |
| 123 | { |
| 124 | struct bt_plugin_shared_lib_handle *shared_lib_handle; |
| 125 | |
| 126 | assert(obj); |
| 127 | shared_lib_handle = container_of(obj, |
| 128 | struct bt_plugin_shared_lib_handle, base); |
| 129 | |
| 130 | if (shared_lib_handle->init_called && shared_lib_handle->exit) { |
| 131 | enum bt_plugin_status status = shared_lib_handle->exit(); |
| 132 | |
| 133 | if (status < 0) { |
| 134 | const char *path = shared_lib_handle->path ? |
| 135 | shared_lib_handle->path->str : "[built-in]"; |
| 136 | |
| 137 | printf_verbose("Plugin in module `%s` exited with error %d\n", |
| 138 | path, status); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (shared_lib_handle->module) { |
| 143 | if (!g_module_close(shared_lib_handle->module)) { |
| 144 | printf_error("Module close error: %s\n", |
| 145 | g_module_error()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (shared_lib_handle->path) { |
| 150 | g_string_free(shared_lib_handle->path, TRUE); |
| 151 | } |
| 152 | |
| 153 | g_free(shared_lib_handle); |
| 154 | } |
| 155 | |
| 156 | static |
| 157 | struct bt_plugin_shared_lib_handle *bt_plugin_shared_lib_handle_create( |
| 158 | const char *path) |
| 159 | { |
| 160 | struct bt_plugin_shared_lib_handle *shared_lib_handle = NULL; |
| 161 | |
| 162 | shared_lib_handle = g_new0(struct bt_plugin_shared_lib_handle, 1); |
| 163 | if (!shared_lib_handle) { |
| 164 | goto error; |
| 165 | } |
| 166 | |
| 167 | bt_object_init(shared_lib_handle, bt_plugin_shared_lib_handle_destroy); |
| 168 | |
| 169 | if (!path) { |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | shared_lib_handle->path = g_string_new(path); |
| 174 | if (!shared_lib_handle->path) { |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | shared_lib_handle->module = g_module_open(path, 0); |
| 179 | if (!shared_lib_handle->module) { |
| 180 | printf_verbose("Module open error: %s\n", g_module_error()); |
| 181 | goto error; |
| 182 | } |
| 183 | |
| 184 | goto end; |
| 185 | |
| 186 | error: |
| 187 | BT_PUT(shared_lib_handle); |
| 188 | |
| 189 | end: |
| 190 | return shared_lib_handle; |
| 191 | } |
| 192 | |
| 193 | static |
| 194 | void bt_plugin_destroy(struct bt_object *obj) |
| 195 | { |
| 196 | struct bt_plugin *plugin; |
| 197 | |
| 198 | assert(obj); |
| 199 | plugin = container_of(obj, struct bt_plugin, base); |
| 200 | |
| 201 | BT_PUT(plugin->shared_lib_handle); |
| 202 | |
| 203 | if (plugin->comp_classes) { |
| 204 | g_ptr_array_free(plugin->comp_classes, TRUE); |
| 205 | } |
| 206 | |
| 207 | g_free(plugin); |
| 208 | } |
| 209 | |
| 210 | static |
| 211 | struct bt_plugin *bt_plugin_create_empty( |
| 212 | struct bt_plugin_shared_lib_handle *shared_lib_handle) |
| 213 | { |
| 214 | struct bt_plugin *plugin = NULL; |
| 215 | |
| 216 | plugin = g_new0(struct bt_plugin, 1); |
| 217 | if (!plugin) { |
| 218 | goto error; |
| 219 | } |
| 220 | |
| 221 | bt_object_init(plugin, bt_plugin_destroy); |
| 222 | plugin->shared_lib_handle = bt_get(shared_lib_handle); |
| 223 | |
| 224 | /* Create empty array of component classes */ |
| 225 | plugin->comp_classes = |
| 226 | g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); |
| 227 | if (!plugin->comp_classes) { |
| 228 | goto error; |
| 229 | } |
| 230 | |
| 231 | goto end; |
| 232 | |
| 233 | error: |
| 234 | BT_PUT(plugin); |
| 235 | |
| 236 | end: |
| 237 | return plugin; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * This function does the following: |
| 242 | * |
| 243 | * 1. Iterate on the plugin descriptor attributes section and set the |
| 244 | * plugin's attributes depending on the attribute types. This |
| 245 | * includes the name of the plugin, its description, and its |
| 246 | * initialization function, for example. |
| 247 | * |
| 248 | * 2. Iterate on the component class descriptors section and create one |
| 249 | * "full descriptor" (temporary structure) for each one that is found |
| 250 | * and attached to our plugin descriptor. |
| 251 | * |
| 252 | * 3. Iterate on the component class descriptor attributes section and |
| 253 | * set the corresponding full descriptor's attributes depending on |
| 254 | * the attribute types. This includes the description of the |
| 255 | * component class, as well as its initialization and destroy |
| 256 | * methods. |
| 257 | * |
| 258 | * 4. Call the user's plugin initialization function, if any is |
| 259 | * defined. |
| 260 | * |
| 261 | * 5. For each full component class descriptor, create a component class |
| 262 | * object, set its optional attributes, and add it to the plugin |
| 263 | * object. |
| 264 | * |
| 265 | * 6. Freeze the plugin object. |
| 266 | */ |
| 267 | static |
| 268 | enum bt_plugin_status bt_plugin_init( |
| 269 | struct bt_plugin *plugin, |
| 270 | const struct __bt_plugin_descriptor *descriptor, |
| 271 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, |
| 272 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, |
| 273 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, |
| 274 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, |
| 275 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, |
| 276 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) |
| 277 | { |
| 278 | /* |
| 279 | * This structure's members point to the plugin's memory |
| 280 | * (do NOT free). |
| 281 | */ |
| 282 | struct comp_class_full_descriptor { |
| 283 | const struct __bt_plugin_component_class_descriptor *descriptor; |
| 284 | const char *description; |
| 285 | bt_component_class_init_method init_method; |
| 286 | bt_component_class_destroy_method destroy_method; |
| 287 | bt_component_class_filter_add_iterator_method filter_add_iterator_method; |
| 288 | bt_component_class_sink_add_iterator_method sink_add_iterator_method; |
| 289 | struct bt_component_class_iterator_methods iterator_methods; |
| 290 | }; |
| 291 | |
| 292 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
| 293 | struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr; |
| 294 | struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr; |
| 295 | struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr; |
| 296 | GArray *comp_class_full_descriptors; |
| 297 | size_t i; |
| 298 | int ret; |
| 299 | |
| 300 | comp_class_full_descriptors = g_array_new(FALSE, TRUE, |
| 301 | sizeof(struct comp_class_full_descriptor)); |
| 302 | if (!comp_class_full_descriptors) { |
| 303 | status = BT_PLUGIN_STATUS_ERROR; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | /* Set mandatory attributes */ |
| 308 | plugin->descriptor = descriptor; |
| 309 | plugin->name = descriptor->name; |
| 310 | |
| 311 | /* |
| 312 | * Find and set optional attributes attached to this plugin |
| 313 | * descriptor. |
| 314 | */ |
| 315 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { |
| 316 | const struct __bt_plugin_descriptor_attribute *cur_attr = |
| 317 | *cur_attr_ptr; |
| 318 | |
| 319 | if (cur_attr->plugin_descriptor != descriptor) { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | switch (cur_attr->type) { |
| 324 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: |
| 325 | plugin->init = cur_attr->value.init; |
| 326 | break; |
| 327 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: |
| 328 | plugin->shared_lib_handle->exit = cur_attr->value.exit; |
| 329 | break; |
| 330 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: |
| 331 | plugin->author = cur_attr->value.author; |
| 332 | break; |
| 333 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: |
| 334 | plugin->license = cur_attr->value.license; |
| 335 | break; |
| 336 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: |
| 337 | plugin->description = cur_attr->value.description; |
| 338 | break; |
| 339 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION: |
| 340 | plugin->version = &cur_attr->value.version; |
| 341 | break; |
| 342 | default: |
| 343 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n", |
| 344 | cur_attr->type_name, cur_attr->type, |
| 345 | descriptor->name); |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Find component class descriptors attached to this plugin |
| 352 | * descriptor and initialize corresponding full component class |
| 353 | * descriptors in the array. |
| 354 | */ |
| 355 | for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) { |
| 356 | const struct __bt_plugin_component_class_descriptor *cur_cc_descr = |
| 357 | *cur_cc_descr_ptr; |
| 358 | struct comp_class_full_descriptor full_descriptor = {0}; |
| 359 | |
| 360 | if (cur_cc_descr->plugin_descriptor != descriptor) { |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | full_descriptor.descriptor = cur_cc_descr; |
| 365 | g_array_append_val(comp_class_full_descriptors, |
| 366 | full_descriptor); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Find component class descriptor attributes attached to this |
| 371 | * plugin descriptor and update corresponding full component |
| 372 | * class descriptors in the array. |
| 373 | */ |
| 374 | for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) { |
| 375 | const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr = |
| 376 | *cur_cc_descr_attr_ptr; |
| 377 | |
| 378 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != |
| 379 | descriptor) { |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | /* Find the corresponding component class descriptor entry */ |
| 384 | for (i = 0; i < comp_class_full_descriptors->len; i++) { |
| 385 | struct comp_class_full_descriptor *cc_full_descr = |
| 386 | &g_array_index(comp_class_full_descriptors, |
| 387 | struct comp_class_full_descriptor, i); |
| 388 | |
| 389 | if (cur_cc_descr_attr->comp_class_descriptor == |
| 390 | cc_full_descr->descriptor) { |
| 391 | switch (cur_cc_descr_attr->type) { |
| 392 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: |
| 393 | cc_full_descr->description = |
| 394 | cur_cc_descr_attr->value.description; |
| 395 | break; |
| 396 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD: |
| 397 | cc_full_descr->init_method = |
| 398 | cur_cc_descr_attr->value.init_method; |
| 399 | break; |
| 400 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESTROY_METHOD: |
| 401 | cc_full_descr->destroy_method = |
| 402 | cur_cc_descr_attr->value.destroy_method; |
| 403 | break; |
| 404 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FILTER_ADD_ITERATOR_METHOD: |
| 405 | cc_full_descr->filter_add_iterator_method = |
| 406 | cur_cc_descr_attr->value.filter_add_iterator_method; |
| 407 | break; |
| 408 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_SINK_ADD_ITERATOR_METHOD: |
| 409 | cc_full_descr->sink_add_iterator_method = |
| 410 | cur_cc_descr_attr->value.sink_add_iterator_method; |
| 411 | break; |
| 412 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD: |
| 413 | cc_full_descr->iterator_methods.init = |
| 414 | cur_cc_descr_attr->value.notif_iter_init_method; |
| 415 | break; |
| 416 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_DESTROY_METHOD: |
| 417 | cc_full_descr->iterator_methods.destroy = |
| 418 | cur_cc_descr_attr->value.notif_iter_destroy_method; |
| 419 | break; |
| 420 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD: |
| 421 | cc_full_descr->iterator_methods.seek_time = |
| 422 | cur_cc_descr_attr->value.notif_iter_seek_time_method; |
| 423 | break; |
| 424 | default: |
| 425 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n", |
| 426 | cur_cc_descr_attr->type_name, |
| 427 | cur_cc_descr_attr->type, |
| 428 | cur_cc_descr_attr->comp_class_descriptor->name, |
| 429 | cur_cc_descr_attr->comp_class_descriptor->type, |
| 430 | descriptor->name); |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* Initialize plugin */ |
| 438 | if (plugin->init) { |
| 439 | status = plugin->init(plugin); |
| 440 | if (status < 0) { |
| 441 | printf_verbose("Plugin `%s` initialization error: %d\n", |
| 442 | plugin->name, status); |
| 443 | goto end; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | plugin->shared_lib_handle->init_called = true; |
| 448 | |
| 449 | /* Add described component classes to plugin */ |
| 450 | for (i = 0; i < comp_class_full_descriptors->len; i++) { |
| 451 | struct comp_class_full_descriptor *cc_full_descr = |
| 452 | &g_array_index(comp_class_full_descriptors, |
| 453 | struct comp_class_full_descriptor, i); |
| 454 | struct bt_component_class *comp_class; |
| 455 | |
| 456 | switch (cc_full_descr->descriptor->type) { |
| 457 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 458 | comp_class = bt_component_class_source_create( |
| 459 | cc_full_descr->descriptor->name, |
| 460 | cc_full_descr->descriptor->methods.source.notif_iter_get, |
| 461 | cc_full_descr->descriptor->methods.source.notif_iter_next); |
| 462 | break; |
| 463 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 464 | comp_class = bt_component_class_filter_create( |
| 465 | cc_full_descr->descriptor->name, |
| 466 | cc_full_descr->descriptor->methods.source.notif_iter_get, |
| 467 | cc_full_descr->descriptor->methods.source.notif_iter_next); |
| 468 | break; |
| 469 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 470 | comp_class = bt_component_class_sink_create( |
| 471 | cc_full_descr->descriptor->name, |
| 472 | cc_full_descr->descriptor->methods.sink.consume); |
| 473 | break; |
| 474 | default: |
| 475 | printf_verbose("WARNING: Unknown component class type %d for component class %s in plugin %s\n", |
| 476 | cc_full_descr->descriptor->type, |
| 477 | cc_full_descr->descriptor->name, |
| 478 | descriptor->name); |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | if (!comp_class) { |
| 483 | status = BT_PLUGIN_STATUS_ERROR; |
| 484 | goto end; |
| 485 | } |
| 486 | |
| 487 | if (cc_full_descr->description) { |
| 488 | ret = bt_component_class_set_description(comp_class, |
| 489 | cc_full_descr->description); |
| 490 | if (ret) { |
| 491 | status = BT_PLUGIN_STATUS_ERROR; |
| 492 | BT_PUT(comp_class); |
| 493 | goto end; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (cc_full_descr->init_method) { |
| 498 | ret = bt_component_class_set_init_method(comp_class, |
| 499 | cc_full_descr->init_method); |
| 500 | if (ret) { |
| 501 | status = BT_PLUGIN_STATUS_ERROR; |
| 502 | BT_PUT(comp_class); |
| 503 | goto end; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (cc_full_descr->destroy_method) { |
| 508 | ret = bt_component_class_set_destroy_method(comp_class, |
| 509 | cc_full_descr->destroy_method); |
| 510 | if (ret) { |
| 511 | status = BT_PLUGIN_STATUS_ERROR; |
| 512 | BT_PUT(comp_class); |
| 513 | goto end; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | switch (cc_full_descr->descriptor->type) { |
| 518 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 519 | if (cc_full_descr->iterator_methods.init) { |
| 520 | ret = bt_component_class_source_set_notification_iterator_init_method( |
| 521 | comp_class, |
| 522 | cc_full_descr->iterator_methods.init); |
| 523 | if (ret) { |
| 524 | status = BT_PLUGIN_STATUS_ERROR; |
| 525 | BT_PUT(comp_class); |
| 526 | goto end; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if (cc_full_descr->iterator_methods.destroy) { |
| 531 | ret = bt_component_class_source_set_notification_iterator_destroy_method( |
| 532 | comp_class, |
| 533 | cc_full_descr->iterator_methods.destroy); |
| 534 | if (ret) { |
| 535 | status = BT_PLUGIN_STATUS_ERROR; |
| 536 | BT_PUT(comp_class); |
| 537 | goto end; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (cc_full_descr->iterator_methods.seek_time) { |
| 542 | ret = bt_component_class_source_set_notification_iterator_seek_time_method( |
| 543 | comp_class, |
| 544 | cc_full_descr->iterator_methods.seek_time); |
| 545 | if (ret) { |
| 546 | status = BT_PLUGIN_STATUS_ERROR; |
| 547 | BT_PUT(comp_class); |
| 548 | goto end; |
| 549 | } |
| 550 | } |
| 551 | break; |
| 552 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 553 | if (cc_full_descr->filter_add_iterator_method) { |
| 554 | ret = bt_component_class_filter_set_add_iterator_method( |
| 555 | comp_class, |
| 556 | cc_full_descr->filter_add_iterator_method); |
| 557 | if (ret) { |
| 558 | status = BT_PLUGIN_STATUS_ERROR; |
| 559 | BT_PUT(comp_class); |
| 560 | goto end; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (cc_full_descr->iterator_methods.init) { |
| 565 | ret = bt_component_class_filter_set_notification_iterator_init_method( |
| 566 | comp_class, |
| 567 | cc_full_descr->iterator_methods.init); |
| 568 | if (ret) { |
| 569 | status = BT_PLUGIN_STATUS_ERROR; |
| 570 | BT_PUT(comp_class); |
| 571 | goto end; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (cc_full_descr->iterator_methods.destroy) { |
| 576 | ret = bt_component_class_filter_set_notification_iterator_destroy_method( |
| 577 | comp_class, |
| 578 | cc_full_descr->iterator_methods.destroy); |
| 579 | if (ret) { |
| 580 | status = BT_PLUGIN_STATUS_ERROR; |
| 581 | BT_PUT(comp_class); |
| 582 | goto end; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (cc_full_descr->iterator_methods.seek_time) { |
| 587 | ret = bt_component_class_filter_set_notification_iterator_seek_time_method( |
| 588 | comp_class, |
| 589 | cc_full_descr->iterator_methods.seek_time); |
| 590 | if (ret) { |
| 591 | status = BT_PLUGIN_STATUS_ERROR; |
| 592 | BT_PUT(comp_class); |
| 593 | goto end; |
| 594 | } |
| 595 | } |
| 596 | break; |
| 597 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 598 | if (cc_full_descr->sink_add_iterator_method) { |
| 599 | ret = bt_component_class_sink_set_add_iterator_method( |
| 600 | comp_class, |
| 601 | cc_full_descr->sink_add_iterator_method); |
| 602 | if (ret) { |
| 603 | status = BT_PLUGIN_STATUS_ERROR; |
| 604 | BT_PUT(comp_class); |
| 605 | goto end; |
| 606 | } |
| 607 | } |
| 608 | break; |
| 609 | default: |
| 610 | assert(false); |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | /* Add component class to the plugin object */ |
| 615 | status = bt_plugin_add_component_class(plugin, |
| 616 | comp_class); |
| 617 | BT_PUT(comp_class); |
| 618 | if (status < 0) { |
| 619 | printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n", |
| 620 | cc_full_descr->descriptor->name, |
| 621 | cc_full_descr->descriptor->type, |
| 622 | plugin->name, status); |
| 623 | goto end; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * All the plugin's component classes should be added at this |
| 629 | * point. We freeze the plugin so that it's not possible to add |
| 630 | * component classes to this plugin object after this stage |
| 631 | * (plugin object becomes immutable). |
| 632 | */ |
| 633 | plugin->frozen = true; |
| 634 | |
| 635 | end: |
| 636 | g_array_free(comp_class_full_descriptors, TRUE); |
| 637 | return status; |
| 638 | } |
| 639 | |
| 640 | static |
| 641 | struct bt_plugin **bt_plugin_create_all_from_sections( |
| 642 | struct bt_plugin_shared_lib_handle *shared_lib_handle, |
| 643 | struct __bt_plugin_descriptor const * const *descriptors_begin, |
| 644 | struct __bt_plugin_descriptor const * const *descriptors_end, |
| 645 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, |
| 646 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, |
| 647 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, |
| 648 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, |
| 649 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, |
| 650 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) |
| 651 | { |
| 652 | size_t descriptor_count; |
| 653 | size_t attrs_count; |
| 654 | size_t cc_descriptors_count; |
| 655 | size_t cc_descr_attrs_count; |
| 656 | size_t i; |
| 657 | struct bt_plugin **plugins = NULL; |
| 658 | |
| 659 | descriptor_count = descriptors_end - descriptors_begin; |
| 660 | attrs_count = attrs_end - attrs_begin; |
| 661 | cc_descriptors_count = cc_descriptors_end - cc_descriptors_begin; |
| 662 | cc_descr_attrs_count = cc_descr_attrs_end - cc_descr_attrs_begin; |
| 663 | printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n", |
| 664 | descriptors_begin, descriptors_end, descriptor_count); |
| 665 | printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n", |
| 666 | attrs_begin, attrs_end, attrs_count); |
| 667 | printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n", |
| 668 | cc_descriptors_begin, cc_descriptors_end, cc_descriptors_count); |
| 669 | printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n", |
| 670 | cc_descr_attrs_begin, cc_descr_attrs_end, cc_descr_attrs_count); |
| 671 | plugins = calloc(descriptor_count + 1, sizeof(*plugins)); |
| 672 | if (!plugins) { |
| 673 | goto error; |
| 674 | } |
| 675 | |
| 676 | for (i = 0; i < descriptor_count; i++) { |
| 677 | enum bt_plugin_status status; |
| 678 | const struct __bt_plugin_descriptor *descriptor = |
| 679 | descriptors_begin[i]; |
| 680 | struct bt_plugin *plugin; |
| 681 | |
| 682 | printf_verbose("Loading plugin %s (ABI %d.%d)\n", descriptor->name, |
| 683 | descriptor->major, descriptor->minor); |
| 684 | |
| 685 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { |
| 686 | printf_error("Unknown plugin's major version: %d\n", |
| 687 | descriptor->major); |
| 688 | goto error; |
| 689 | } |
| 690 | |
| 691 | plugin = bt_plugin_create_empty(shared_lib_handle); |
| 692 | if (!plugin) { |
| 693 | printf_error("Cannot allocate plugin object for plugin %s\n", |
| 694 | descriptor->name); |
| 695 | goto error; |
| 696 | } |
| 697 | |
| 698 | status = bt_plugin_init(plugin, descriptor, attrs_begin, |
| 699 | attrs_end, cc_descriptors_begin, cc_descriptors_end, |
| 700 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 701 | if (status < 0) { |
| 702 | printf_error("Cannot initialize plugin object %s\n", |
| 703 | descriptor->name); |
| 704 | BT_PUT(plugin); |
| 705 | goto error; |
| 706 | } |
| 707 | |
| 708 | /* Transfer ownership to the array */ |
| 709 | plugins[i] = plugin; |
| 710 | } |
| 711 | |
| 712 | goto end; |
| 713 | |
| 714 | error: |
| 715 | g_free(plugins); |
| 716 | plugins = NULL; |
| 717 | |
| 718 | end: |
| 719 | return plugins; |
| 720 | } |
| 721 | |
| 722 | struct bt_plugin **bt_plugin_create_all_from_static(void) |
| 723 | { |
| 724 | struct bt_plugin **plugins = NULL; |
| 725 | struct bt_plugin_shared_lib_handle *shared_lib_handle = |
| 726 | bt_plugin_shared_lib_handle_create(NULL); |
| 727 | |
| 728 | if (!shared_lib_handle) { |
| 729 | goto end; |
| 730 | } |
| 731 | |
| 732 | plugins = bt_plugin_create_all_from_sections(shared_lib_handle, |
| 733 | SECTION_BEGIN(__bt_plugin_descriptors), |
| 734 | SECTION_END(__bt_plugin_descriptors), |
| 735 | SECTION_BEGIN(__bt_plugin_descriptor_attributes), |
| 736 | SECTION_END(__bt_plugin_descriptor_attributes), |
| 737 | SECTION_BEGIN(__bt_plugin_component_class_descriptors), |
| 738 | SECTION_END(__bt_plugin_component_class_descriptors), |
| 739 | SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes), |
| 740 | SECTION_END(__bt_plugin_component_class_descriptor_attributes)); |
| 741 | |
| 742 | end: |
| 743 | BT_PUT(shared_lib_handle); |
| 744 | |
| 745 | return plugins; |
| 746 | } |
| 747 | |
| 748 | struct bt_plugin **bt_plugin_create_all_from_file(const char *path) |
| 749 | { |
| 750 | size_t path_len; |
| 751 | struct bt_plugin **plugins = NULL; |
| 752 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; |
| 753 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; |
| 754 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; |
| 755 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; |
| 756 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; |
| 757 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; |
| 758 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; |
| 759 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; |
| 760 | bool is_libtool_wrapper = false, is_shared_object = false; |
| 761 | struct bt_plugin_shared_lib_handle *shared_lib_handle = NULL; |
| 762 | |
| 763 | if (!path) { |
| 764 | goto end; |
| 765 | } |
| 766 | |
| 767 | path_len = strlen(path); |
| 768 | if (path_len <= PLUGIN_SUFFIX_LEN) { |
| 769 | goto end; |
| 770 | } |
| 771 | |
| 772 | path_len++; |
| 773 | /* |
| 774 | * Check if the file ends with a known plugin file type suffix (i.e. .so |
| 775 | * or .la on Linux). |
| 776 | */ |
| 777 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, |
| 778 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, |
| 779 | LIBTOOL_PLUGIN_SUFFIX_LEN); |
| 780 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, |
| 781 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, |
| 782 | NATIVE_PLUGIN_SUFFIX_LEN); |
| 783 | if (!is_shared_object && !is_libtool_wrapper) { |
| 784 | /* Name indicates that this is not a plugin file. */ |
| 785 | goto end; |
| 786 | } |
| 787 | |
| 788 | shared_lib_handle = bt_plugin_shared_lib_handle_create(path); |
| 789 | if (!shared_lib_handle) { |
| 790 | goto end; |
| 791 | } |
| 792 | |
| 793 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptors", |
| 794 | (gpointer *) &descriptors_begin)) { |
| 795 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 796 | "__start___bt_plugin_descriptors", |
| 797 | g_module_name(shared_lib_handle->module)); |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptors", |
| 802 | (gpointer *) &descriptors_end)) { |
| 803 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 804 | "__stop___bt_plugin_descriptors", |
| 805 | g_module_name(shared_lib_handle->module)); |
| 806 | goto end; |
| 807 | } |
| 808 | |
| 809 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptor_attributes", |
| 810 | (gpointer *) &attrs_begin)) { |
| 811 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 812 | "__start___bt_plugin_descriptor_attributes", |
| 813 | g_module_name(shared_lib_handle->module)); |
| 814 | } |
| 815 | |
| 816 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptor_attributes", |
| 817 | (gpointer *) &attrs_end)) { |
| 818 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 819 | "__stop___bt_plugin_descriptor_attributes", |
| 820 | g_module_name(shared_lib_handle->module)); |
| 821 | } |
| 822 | |
| 823 | if ((!!attrs_begin - !!attrs_end) != 0) { |
| 824 | printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n", |
| 825 | g_module_name(shared_lib_handle->module)); |
| 826 | goto end; |
| 827 | } |
| 828 | |
| 829 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptors", |
| 830 | (gpointer *) &cc_descriptors_begin)) { |
| 831 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 832 | "__start___bt_plugin_component_class_descriptors", |
| 833 | g_module_name(shared_lib_handle->module)); |
| 834 | } |
| 835 | |
| 836 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptors", |
| 837 | (gpointer *) &cc_descriptors_end)) { |
| 838 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 839 | "__stop___bt_plugin_component_class_descriptors", |
| 840 | g_module_name(shared_lib_handle->module)); |
| 841 | } |
| 842 | |
| 843 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { |
| 844 | printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n", |
| 845 | g_module_name(shared_lib_handle->module)); |
| 846 | goto end; |
| 847 | } |
| 848 | |
| 849 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptor_attributes", |
| 850 | (gpointer *) &cc_descr_attrs_begin)) { |
| 851 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 852 | "__start___bt_plugin_component_class_descriptor_attributes", |
| 853 | g_module_name(shared_lib_handle->module)); |
| 854 | } |
| 855 | |
| 856 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptor_attributes", |
| 857 | (gpointer *) &cc_descr_attrs_end)) { |
| 858 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", |
| 859 | "__stop___bt_plugin_component_class_descriptor_attributes", |
| 860 | g_module_name(shared_lib_handle->module)); |
| 861 | } |
| 862 | |
| 863 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { |
| 864 | printf_verbose("Found __start___bt_plugin_component_class_descriptor_attributes or __stop___bt_plugin_component_class_descriptor_attributes symbol, but not both in %s\n", |
| 865 | g_module_name(shared_lib_handle->module)); |
| 866 | goto end; |
| 867 | } |
| 868 | |
| 869 | /* Initialize plugin */ |
| 870 | plugins = bt_plugin_create_all_from_sections(shared_lib_handle, |
| 871 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, |
| 872 | cc_descriptors_begin, cc_descriptors_end, |
| 873 | cc_descr_attrs_begin, cc_descr_attrs_end); |
| 874 | |
| 875 | end: |
| 876 | BT_PUT(shared_lib_handle); |
| 877 | return plugins; |
| 878 | } |
| 879 | |
| 880 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ |
| 881 | static |
| 882 | struct dirent *alloc_dirent(const char *path) |
| 883 | { |
| 884 | size_t len; |
| 885 | long name_max; |
| 886 | struct dirent *entry; |
| 887 | |
| 888 | name_max = pathconf(path, _PC_NAME_MAX); |
| 889 | if (name_max == -1) { |
| 890 | name_max = PATH_MAX; |
| 891 | } |
| 892 | len = offsetof(struct dirent, d_name) + name_max + 1; |
| 893 | entry = zmalloc(len); |
| 894 | return entry; |
| 895 | } |
| 896 | |
| 897 | static |
| 898 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( |
| 899 | GPtrArray *plugins, const char *path, bool recurse) |
| 900 | { |
| 901 | DIR *directory = NULL; |
| 902 | struct dirent *entry = NULL, *result = NULL; |
| 903 | char *file_path = NULL; |
| 904 | size_t path_len; |
| 905 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
| 906 | |
| 907 | if (!path) { |
| 908 | ret = BT_PLUGIN_STATUS_ERROR; |
| 909 | goto end; |
| 910 | } |
| 911 | |
| 912 | path_len = strlen(path); |
| 913 | |
| 914 | if (path_len >= PATH_MAX) { |
| 915 | ret = BT_PLUGIN_STATUS_ERROR; |
| 916 | goto end; |
| 917 | } |
| 918 | |
| 919 | entry = alloc_dirent(path); |
| 920 | if (!entry) { |
| 921 | ret = BT_PLUGIN_STATUS_ERROR; |
| 922 | goto end; |
| 923 | } |
| 924 | |
| 925 | file_path = zmalloc(PATH_MAX); |
| 926 | if (!file_path) { |
| 927 | ret = BT_PLUGIN_STATUS_NOMEM; |
| 928 | goto end; |
| 929 | } |
| 930 | |
| 931 | strncpy(file_path, path, path_len); |
| 932 | /* Append a trailing '/' to the path */ |
| 933 | if (file_path[path_len - 1] != '/') { |
| 934 | file_path[path_len++] = '/'; |
| 935 | } |
| 936 | |
| 937 | directory = opendir(file_path); |
| 938 | if (!directory) { |
| 939 | perror("Failed to open plug-in directory"); |
| 940 | ret = BT_PLUGIN_STATUS_ERROR; |
| 941 | goto end; |
| 942 | } |
| 943 | |
| 944 | /* Recursively walk directory */ |
| 945 | while (!readdir_r(directory, entry, &result) && result) { |
| 946 | struct stat st; |
| 947 | int stat_ret; |
| 948 | size_t file_name_len; |
| 949 | |
| 950 | if (result->d_name[0] == '.') { |
| 951 | /* Skip hidden files, . and .. */ |
| 952 | continue; |
| 953 | } |
| 954 | |
| 955 | file_name_len = strlen(result->d_name); |
| 956 | |
| 957 | if (path_len + file_name_len >= PATH_MAX) { |
| 958 | continue; |
| 959 | } |
| 960 | |
| 961 | strncpy(file_path + path_len, result->d_name, file_name_len); |
| 962 | file_path[path_len + file_name_len] = '\0'; |
| 963 | |
| 964 | stat_ret = stat(file_path, &st); |
| 965 | if (stat_ret < 0) { |
| 966 | /* Continue to next file / directory. */ |
| 967 | printf_perror("Failed to stat() plugin file\n"); |
| 968 | continue; |
| 969 | } |
| 970 | |
| 971 | if (S_ISDIR(st.st_mode) && recurse) { |
| 972 | ret = bt_plugin_create_append_all_from_dir(plugins, |
| 973 | file_path, true); |
| 974 | if (ret < 0) { |
| 975 | goto end; |
| 976 | } |
| 977 | } else if (S_ISREG(st.st_mode)) { |
| 978 | struct bt_plugin **plugins_from_file = |
| 979 | bt_plugin_create_all_from_file(file_path); |
| 980 | |
| 981 | if (plugins_from_file) { |
| 982 | struct bt_plugin **plugin; |
| 983 | |
| 984 | for (plugin = plugins_from_file; *plugin; plugin++) { |
| 985 | /* Transfer ownership to array */ |
| 986 | g_ptr_array_add(plugins, *plugin); |
| 987 | } |
| 988 | |
| 989 | free(plugins_from_file); |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | end: |
| 994 | if (directory) { |
| 995 | if (closedir(directory)) { |
| 996 | /* |
| 997 | * We don't want to override the error since there is |
| 998 | * nothing could do. |
| 999 | */ |
| 1000 | perror("Failed to close plug-in directory"); |
| 1001 | } |
| 1002 | } |
| 1003 | free(entry); |
| 1004 | free(file_path); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | struct bt_plugin **bt_plugin_create_all_from_dir(const char *path, |
| 1009 | bool recurse) |
| 1010 | { |
| 1011 | GPtrArray *plugins_array = g_ptr_array_new(); |
| 1012 | struct bt_plugin **plugins = NULL; |
| 1013 | enum bt_plugin_status status; |
| 1014 | |
| 1015 | /* Append found plugins to array */ |
| 1016 | status = bt_plugin_create_append_all_from_dir(plugins_array, path, |
| 1017 | recurse); |
| 1018 | if (status < 0) { |
| 1019 | goto error; |
| 1020 | } |
| 1021 | |
| 1022 | /* Add sentinel to array */ |
| 1023 | g_ptr_array_add(plugins_array, NULL); |
| 1024 | plugins = (struct bt_plugin **) plugins_array->pdata; |
| 1025 | goto end; |
| 1026 | |
| 1027 | error: |
| 1028 | if (plugins_array) { |
| 1029 | g_ptr_array_free(plugins_array, TRUE); |
| 1030 | plugins_array = NULL; |
| 1031 | } |
| 1032 | |
| 1033 | end: |
| 1034 | if (plugins_array) { |
| 1035 | g_ptr_array_free(plugins_array, FALSE); |
| 1036 | } |
| 1037 | |
| 1038 | return plugins; |
| 1039 | } |
| 1040 | |
| 1041 | const char *bt_plugin_get_name(struct bt_plugin *plugin) |
| 1042 | { |
| 1043 | return plugin ? plugin->name : NULL; |
| 1044 | } |
| 1045 | |
| 1046 | const char *bt_plugin_get_author(struct bt_plugin *plugin) |
| 1047 | { |
| 1048 | return plugin ? plugin->author : NULL; |
| 1049 | } |
| 1050 | |
| 1051 | const char *bt_plugin_get_license(struct bt_plugin *plugin) |
| 1052 | { |
| 1053 | return plugin ? plugin->license : NULL; |
| 1054 | } |
| 1055 | |
| 1056 | const char *bt_plugin_get_path(struct bt_plugin *plugin) |
| 1057 | { |
| 1058 | return (plugin && plugin->shared_lib_handle->path) ? |
| 1059 | plugin->shared_lib_handle->path->str : NULL; |
| 1060 | } |
| 1061 | |
| 1062 | const char *bt_plugin_get_description(struct bt_plugin *plugin) |
| 1063 | { |
| 1064 | return plugin ? plugin->description : NULL; |
| 1065 | } |
| 1066 | |
| 1067 | enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin, |
| 1068 | unsigned int *major, unsigned int *minor, unsigned int *patch, |
| 1069 | const char **extra) |
| 1070 | { |
| 1071 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
| 1072 | |
| 1073 | if (!plugin || !plugin->version) { |
| 1074 | status = BT_PLUGIN_STATUS_ERROR; |
| 1075 | goto end; |
| 1076 | } |
| 1077 | |
| 1078 | if (major) { |
| 1079 | *major = (unsigned int) plugin->version->major; |
| 1080 | } |
| 1081 | |
| 1082 | if (minor) { |
| 1083 | *minor = (unsigned int) plugin->version->minor; |
| 1084 | } |
| 1085 | |
| 1086 | if (patch) { |
| 1087 | *patch = (unsigned int) plugin->version->patch; |
| 1088 | } |
| 1089 | |
| 1090 | if (extra) { |
| 1091 | *extra = plugin->version->extra; |
| 1092 | } |
| 1093 | |
| 1094 | end: |
| 1095 | return status; |
| 1096 | } |
| 1097 | |
| 1098 | int bt_plugin_get_component_class_count(struct bt_plugin *plugin) |
| 1099 | { |
| 1100 | return plugin ? plugin->comp_classes->len : -1; |
| 1101 | } |
| 1102 | |
| 1103 | struct bt_component_class *bt_plugin_get_component_class( |
| 1104 | struct bt_plugin *plugin, size_t index) |
| 1105 | { |
| 1106 | struct bt_component_class *comp_class = NULL; |
| 1107 | |
| 1108 | if (!plugin || index >= plugin->comp_classes->len) { |
| 1109 | goto error; |
| 1110 | } |
| 1111 | |
| 1112 | comp_class = g_ptr_array_index(plugin->comp_classes, index); |
| 1113 | bt_get(comp_class); |
| 1114 | goto end; |
| 1115 | |
| 1116 | error: |
| 1117 | BT_PUT(comp_class); |
| 1118 | |
| 1119 | end: |
| 1120 | return comp_class; |
| 1121 | } |
| 1122 | |
| 1123 | struct bt_component_class *bt_plugin_get_component_class_by_name_and_type( |
| 1124 | struct bt_plugin *plugin, const char *name, |
| 1125 | enum bt_component_class_type type) |
| 1126 | { |
| 1127 | struct bt_component_class *comp_class = NULL; |
| 1128 | size_t i; |
| 1129 | |
| 1130 | if (!plugin || !name) { |
| 1131 | goto error; |
| 1132 | } |
| 1133 | |
| 1134 | for (i = 0; i < plugin->comp_classes->len; i++) { |
| 1135 | struct bt_component_class *comp_class_candidate = |
| 1136 | g_ptr_array_index(plugin->comp_classes, i); |
| 1137 | const char *comp_class_cand_name = |
| 1138 | bt_component_class_get_name(comp_class_candidate); |
| 1139 | enum bt_component_class_type comp_class_cand_type = |
| 1140 | bt_component_class_get_type(comp_class_candidate); |
| 1141 | |
| 1142 | assert(comp_class_cand_name); |
| 1143 | assert(comp_class_cand_type >= 0); |
| 1144 | |
| 1145 | if (strcmp(name, comp_class_cand_name) == 0 && |
| 1146 | comp_class_cand_type == type) { |
| 1147 | comp_class = bt_get(comp_class_candidate); |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | goto end; |
| 1153 | |
| 1154 | error: |
| 1155 | BT_PUT(comp_class); |
| 1156 | |
| 1157 | end: |
| 1158 | return comp_class; |
| 1159 | } |
| 1160 | |
| 1161 | static |
| 1162 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, |
| 1163 | void *data) |
| 1164 | { |
| 1165 | gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles, |
| 1166 | comp_class); |
| 1167 | assert(exists); |
| 1168 | } |
| 1169 | |
| 1170 | enum bt_plugin_status bt_plugin_add_component_class( |
| 1171 | struct bt_plugin *plugin, struct bt_component_class *comp_class) |
| 1172 | { |
| 1173 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
| 1174 | struct bt_component_class *comp_class_dup = NULL; |
| 1175 | int ret; |
| 1176 | int comp_class_index = -1; |
| 1177 | |
| 1178 | if (!plugin || !comp_class || plugin->frozen) { |
| 1179 | goto error; |
| 1180 | } |
| 1181 | |
| 1182 | /* Check for duplicate */ |
| 1183 | comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin, |
| 1184 | bt_component_class_get_name(comp_class), |
| 1185 | bt_component_class_get_type(comp_class)); |
| 1186 | if (comp_class_dup) { |
| 1187 | printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n", |
| 1188 | plugin->name, |
| 1189 | bt_component_class_get_name(comp_class), |
| 1190 | bt_component_class_get_type(comp_class)); |
| 1191 | goto error; |
| 1192 | } |
| 1193 | |
| 1194 | /* Add new component class */ |
| 1195 | comp_class_index = plugin->comp_classes->len; |
| 1196 | g_ptr_array_add(plugin->comp_classes, bt_get(comp_class)); |
| 1197 | |
| 1198 | /* Map component class pointer to shared lib handle in global HT */ |
| 1199 | g_hash_table_insert(comp_classes_to_shlib_handles, comp_class, |
| 1200 | bt_get(plugin->shared_lib_handle)); |
| 1201 | |
| 1202 | /* Add our custom destroy listener */ |
| 1203 | ret = bt_component_class_add_destroy_listener(comp_class, |
| 1204 | plugin_comp_class_destroy_listener, NULL); |
| 1205 | if (ret) { |
| 1206 | goto error; |
| 1207 | } |
| 1208 | |
| 1209 | goto end; |
| 1210 | |
| 1211 | error: |
| 1212 | /* Remove entry from global hash table (if exists) */ |
| 1213 | g_hash_table_remove(comp_classes_to_shlib_handles, |
| 1214 | comp_class); |
| 1215 | |
| 1216 | /* Remove entry from plugin's component classes (if added) */ |
| 1217 | if (comp_class_index >= 0) { |
| 1218 | g_ptr_array_remove_index(plugin->comp_classes, |
| 1219 | comp_class_index); |
| 1220 | } |
| 1221 | |
| 1222 | status = BT_PLUGIN_STATUS_ERROR; |
| 1223 | |
| 1224 | end: |
| 1225 | bt_put(comp_class_dup); |
| 1226 | return status; |
| 1227 | } |