6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
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>
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)
49 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
50 sizeof(LIBTOOL_PLUGIN_SUFFIX))
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))
56 #define DECLARE_SECTION(_type, _name) \
57 extern _type __start_##_name __attribute((weak)); \
58 extern _type __stop_##_name __attribute((weak))
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
);
66 * This hash table, global to the library, maps component class pointers
67 * to shared library handles.
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.
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:
78 * [component] --ref-> [component class] --through this HT-> [shlib handle]
80 * This hash table exists for two reasons:
82 * 1. To allow this application:
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
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
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.
105 GHashTable
*comp_classes_to_shlib_handles
;
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
);
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
);
122 void bt_plugin_shared_lib_handle_destroy(struct bt_object
*obj
)
124 struct bt_plugin_shared_lib_handle
*shared_lib_handle
;
127 shared_lib_handle
= container_of(obj
,
128 struct bt_plugin_shared_lib_handle
, base
);
130 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
131 enum bt_plugin_status status
= shared_lib_handle
->exit();
134 const char *path
= shared_lib_handle
->path
?
135 shared_lib_handle
->path
->str
: "[built-in]";
137 printf_verbose("Plugin in module `%s` exited with error %d\n",
142 if (shared_lib_handle
->module
) {
143 if (!g_module_close(shared_lib_handle
->module
)) {
144 printf_error("Module close error: %s\n",
149 if (shared_lib_handle
->path
) {
150 g_string_free(shared_lib_handle
->path
, TRUE
);
153 g_free(shared_lib_handle
);
157 struct bt_plugin_shared_lib_handle
*bt_plugin_shared_lib_handle_create(
160 struct bt_plugin_shared_lib_handle
*shared_lib_handle
= NULL
;
162 shared_lib_handle
= g_new0(struct bt_plugin_shared_lib_handle
, 1);
163 if (!shared_lib_handle
) {
167 bt_object_init(shared_lib_handle
, bt_plugin_shared_lib_handle_destroy
);
173 shared_lib_handle
->path
= g_string_new(path
);
174 if (!shared_lib_handle
->path
) {
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());
187 BT_PUT(shared_lib_handle
);
190 return shared_lib_handle
;
194 void bt_plugin_destroy(struct bt_object
*obj
)
196 struct bt_plugin
*plugin
;
199 plugin
= container_of(obj
, struct bt_plugin
, base
);
201 BT_PUT(plugin
->shared_lib_handle
);
203 if (plugin
->comp_classes
) {
204 g_ptr_array_free(plugin
->comp_classes
, TRUE
);
211 struct bt_plugin
*bt_plugin_create_empty(
212 struct bt_plugin_shared_lib_handle
*shared_lib_handle
)
214 struct bt_plugin
*plugin
= NULL
;
216 plugin
= g_new0(struct bt_plugin
, 1);
221 bt_object_init(plugin
, bt_plugin_destroy
);
222 plugin
->shared_lib_handle
= bt_get(shared_lib_handle
);
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
) {
241 enum bt_plugin_status
bt_plugin_init(
242 struct bt_plugin
*plugin
,
243 const struct __bt_plugin_descriptor
*descriptor
,
244 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
245 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
246 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
247 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
248 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
249 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
252 * This structure's members point to the plugin's memory
255 struct comp_class_full_descriptor
{
256 const struct __bt_plugin_component_class_descriptor
*descriptor
;
257 const char *description
;
260 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
261 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
262 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
263 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
264 GArray
*comp_class_full_descriptors
;
267 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
268 sizeof(struct comp_class_full_descriptor
));
269 if (!comp_class_full_descriptors
) {
270 status
= BT_PLUGIN_STATUS_ERROR
;
274 /* Set mandatory attributes */
275 plugin
->descriptor
= descriptor
;
276 plugin
->name
= descriptor
->name
;
279 * Find and set optional attributes attached to this plugin
282 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
283 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
286 if (cur_attr
->plugin_descriptor
!= descriptor
) {
290 switch (cur_attr
->type
) {
291 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
292 plugin
->init
= cur_attr
->value
.init
;
294 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
295 plugin
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
297 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
298 plugin
->author
= cur_attr
->value
.author
;
300 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
301 plugin
->license
= cur_attr
->value
.license
;
303 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
304 plugin
->description
= cur_attr
->value
.description
;
307 printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n",
308 cur_attr
->type_name
, cur_attr
->type
,
315 * Find component class descriptors attached to this plugin
316 * descriptor and initialize corresponding full component class
317 * descriptors in the array.
319 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
320 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
322 struct comp_class_full_descriptor full_descriptor
;
324 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
328 full_descriptor
.descriptor
= cur_cc_descr
;
329 full_descriptor
.description
= NULL
;
330 g_array_append_val(comp_class_full_descriptors
,
335 * Find component class descriptor attributes attached to this
336 * plugin descriptor and update corresponding full component
337 * class descriptors in the array.
339 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
340 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
341 *cur_cc_descr_attr_ptr
;
343 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
348 /* Find the corresponding component class descriptor entry */
349 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
350 struct comp_class_full_descriptor
*cc_full_descr
=
351 &g_array_index(comp_class_full_descriptors
,
352 struct comp_class_full_descriptor
, i
);
354 if (cur_cc_descr_attr
->comp_class_descriptor
==
355 cc_full_descr
->descriptor
) {
356 switch (cur_cc_descr_attr
->type
) {
357 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
358 cc_full_descr
->description
=
359 cur_cc_descr_attr
->value
.description
;
362 printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n",
363 cur_cc_descr_attr
->type_name
,
364 cur_cc_descr_attr
->type
,
365 cur_cc_descr_attr
->comp_class_descriptor
->name
,
366 cur_cc_descr_attr
->comp_class_descriptor
->type
,
374 /* Initialize plugin */
376 status
= plugin
->init(plugin
);
378 printf_verbose("Plugin `%s` initialization error: %d\n",
379 plugin
->name
, status
);
384 plugin
->shared_lib_handle
->init_called
= true;
386 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
387 struct comp_class_full_descriptor
*cc_full_descr
=
388 &g_array_index(comp_class_full_descriptors
,
389 struct comp_class_full_descriptor
, i
);
390 struct bt_component_class
*comp_class
;
392 comp_class
= bt_component_class_create(
393 cc_full_descr
->descriptor
->type
,
394 cc_full_descr
->descriptor
->name
,
395 cc_full_descr
->description
,
396 cc_full_descr
->descriptor
->init_cb
);
398 status
= BT_PLUGIN_STATUS_ERROR
;
402 status
= bt_plugin_add_component_class(plugin
,
406 printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n",
407 cc_full_descr
->descriptor
->name
,
408 cc_full_descr
->descriptor
->type
,
409 plugin
->name
, status
);
415 * All the plugin's component classes should be added at this
416 * point. We freeze the plugin so that it's not possible to add
417 * component classes to this plugin object after this stage
418 * (plugin object becomes immutable).
420 plugin
->frozen
= true;
423 g_array_free(comp_class_full_descriptors
, TRUE
);
428 struct bt_plugin
**bt_plugin_create_all_from_sections(
429 struct bt_plugin_shared_lib_handle
*shared_lib_handle
,
430 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
431 struct __bt_plugin_descriptor
const * const *descriptors_end
,
432 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
433 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
434 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
435 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
436 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
437 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
439 size_t descriptor_count
;
441 size_t cc_descriptors_count
;
442 size_t cc_descr_attrs_count
;
444 struct bt_plugin
**plugins
= NULL
;
446 descriptor_count
= descriptors_end
- descriptors_begin
;
447 attrs_count
= attrs_end
- attrs_begin
;
448 cc_descriptors_count
= cc_descriptors_end
- cc_descriptors_begin
;
449 cc_descr_attrs_count
= cc_descr_attrs_end
- cc_descr_attrs_begin
;
450 printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n",
451 descriptors_begin
, descriptors_end
, descriptor_count
);
452 printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n",
453 attrs_begin
, attrs_end
, attrs_count
);
454 printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n",
455 cc_descriptors_begin
, cc_descriptors_end
, cc_descriptors_count
);
456 printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n",
457 cc_descr_attrs_begin
, cc_descr_attrs_end
, attrs_count
);
458 plugins
= calloc(descriptor_count
+ 1, sizeof(*plugins
));
463 for (i
= 0; i
< descriptor_count
; i
++) {
464 enum bt_plugin_status status
;
465 const struct __bt_plugin_descriptor
*descriptor
=
466 descriptors_begin
[i
];
467 struct bt_plugin
*plugin
;
469 printf_verbose("Loading plugin %s (%d.%d)\n", descriptor
->name
,
470 descriptor
->major
, descriptor
->minor
);
472 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
473 printf_error("Unknown plugin's major version: %d\n",
478 plugin
= bt_plugin_create_empty(shared_lib_handle
);
480 printf_error("Cannot allocate plugin object for plugin %s\n",
485 status
= bt_plugin_init(plugin
, descriptor
, attrs_begin
,
486 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
487 cc_descr_attrs_begin
, cc_descr_attrs_end
);
489 printf_error("Cannot initialize plugin object %s\n",
495 /* Transfer ownership to the array */
509 struct bt_plugin
**bt_plugin_create_all_from_static(void)
511 struct bt_plugin
**plugins
= NULL
;
512 struct bt_plugin_shared_lib_handle
*shared_lib_handle
=
513 bt_plugin_shared_lib_handle_create(NULL
);
515 if (!shared_lib_handle
) {
519 plugins
= bt_plugin_create_all_from_sections(shared_lib_handle
,
520 SECTION_BEGIN(__bt_plugin_descriptors
),
521 SECTION_END(__bt_plugin_descriptors
),
522 SECTION_BEGIN(__bt_plugin_descriptor_attributes
),
523 SECTION_END(__bt_plugin_descriptor_attributes
),
524 SECTION_BEGIN(__bt_plugin_component_class_descriptors
),
525 SECTION_END(__bt_plugin_component_class_descriptors
),
526 SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes
),
527 SECTION_END(__bt_plugin_component_class_descriptor_attributes
));
530 BT_PUT(shared_lib_handle
);
535 struct bt_plugin
**bt_plugin_create_all_from_file(const char *path
)
538 struct bt_plugin
**plugins
= NULL
;
539 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
540 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
541 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
542 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
543 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
544 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
545 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
546 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
547 bool is_libtool_wrapper
= false, is_shared_object
= false;
548 struct bt_plugin_shared_lib_handle
*shared_lib_handle
= NULL
;
554 path_len
= strlen(path
);
555 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
561 * Check if the file ends with a known plugin file type suffix (i.e. .so
564 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
565 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
566 LIBTOOL_PLUGIN_SUFFIX_LEN
);
567 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
568 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
569 NATIVE_PLUGIN_SUFFIX_LEN
);
570 if (!is_shared_object
&& !is_libtool_wrapper
) {
571 /* Name indicates that this is not a plugin file. */
575 shared_lib_handle
= bt_plugin_shared_lib_handle_create(path
);
576 if (!shared_lib_handle
) {
580 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptors",
581 (gpointer
*) &descriptors_begin
)) {
582 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
583 "__start___bt_plugin_descriptors",
584 g_module_name(shared_lib_handle
->module
));
588 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptors",
589 (gpointer
*) &descriptors_end
)) {
590 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
591 "__stop___bt_plugin_descriptors",
592 g_module_name(shared_lib_handle
->module
));
596 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptor_attributes",
597 (gpointer
*) &attrs_begin
)) {
598 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
599 "__start___bt_plugin_descriptor_attributes",
600 g_module_name(shared_lib_handle
->module
));
603 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptor_attributes",
604 (gpointer
*) &attrs_end
)) {
605 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
606 "__stop___bt_plugin_descriptor_attributes",
607 g_module_name(shared_lib_handle
->module
));
610 if ((!!attrs_begin
- !!attrs_end
) != 0) {
611 printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n",
612 g_module_name(shared_lib_handle
->module
));
616 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptors",
617 (gpointer
*) &cc_descriptors_begin
)) {
618 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
619 "__start___bt_plugin_component_class_descriptors",
620 g_module_name(shared_lib_handle
->module
));
623 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptors",
624 (gpointer
*) &cc_descriptors_end
)) {
625 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
626 "__stop___bt_plugin_component_class_descriptors",
627 g_module_name(shared_lib_handle
->module
));
630 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
631 printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n",
632 g_module_name(shared_lib_handle
->module
));
636 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptor_attributes",
637 (gpointer
*) &cc_descr_attrs_begin
)) {
638 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
639 "__start___bt_plugin_component_class_descriptor_attributes",
640 g_module_name(shared_lib_handle
->module
));
643 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptor_attributes",
644 (gpointer
*) &cc_descr_attrs_end
)) {
645 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
646 "__stop___bt_plugin_component_class_descriptor_attributes",
647 g_module_name(shared_lib_handle
->module
));
650 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
651 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",
652 g_module_name(shared_lib_handle
->module
));
656 /* Initialize plugin */
657 plugins
= bt_plugin_create_all_from_sections(shared_lib_handle
,
658 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
659 cc_descriptors_begin
, cc_descriptors_end
,
660 cc_descr_attrs_begin
, cc_descr_attrs_end
);
663 BT_PUT(shared_lib_handle
);
667 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
669 struct dirent
*alloc_dirent(const char *path
)
673 struct dirent
*entry
;
675 name_max
= pathconf(path
, _PC_NAME_MAX
);
676 if (name_max
== -1) {
679 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
680 entry
= zmalloc(len
);
685 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
686 GPtrArray
*plugins
, const char *path
, bool recurse
)
688 DIR *directory
= NULL
;
689 struct dirent
*entry
= NULL
, *result
= NULL
;
690 char *file_path
= NULL
;
692 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
695 ret
= BT_PLUGIN_STATUS_ERROR
;
699 path_len
= strlen(path
);
701 if (path_len
>= PATH_MAX
) {
702 ret
= BT_PLUGIN_STATUS_ERROR
;
706 entry
= alloc_dirent(path
);
708 ret
= BT_PLUGIN_STATUS_ERROR
;
712 file_path
= zmalloc(PATH_MAX
);
714 ret
= BT_PLUGIN_STATUS_NOMEM
;
718 strncpy(file_path
, path
, path_len
);
719 /* Append a trailing '/' to the path */
720 if (file_path
[path_len
- 1] != '/') {
721 file_path
[path_len
++] = '/';
724 directory
= opendir(file_path
);
726 perror("Failed to open plug-in directory");
727 ret
= BT_PLUGIN_STATUS_ERROR
;
731 /* Recursively walk directory */
732 while (!readdir_r(directory
, entry
, &result
) && result
) {
735 size_t file_name_len
;
737 if (result
->d_name
[0] == '.') {
738 /* Skip hidden files, . and .. */
742 file_name_len
= strlen(result
->d_name
);
744 if (path_len
+ file_name_len
>= PATH_MAX
) {
748 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
749 file_path
[path_len
+ file_name_len
] = '\0';
751 stat_ret
= stat(file_path
, &st
);
753 /* Continue to next file / directory. */
754 printf_perror("Failed to stat() plugin file\n");
758 if (S_ISDIR(st
.st_mode
) && recurse
) {
759 ret
= bt_plugin_create_append_all_from_dir(plugins
,
764 } else if (S_ISREG(st
.st_mode
)) {
765 struct bt_plugin
**plugins_from_file
=
766 bt_plugin_create_all_from_file(file_path
);
768 if (plugins_from_file
) {
769 struct bt_plugin
**plugin
;
771 for (plugin
= plugins_from_file
; *plugin
; plugin
++) {
772 /* Transfer ownership to array */
773 g_ptr_array_add(plugins
, *plugin
);
776 free(plugins_from_file
);
782 if (closedir(directory
)) {
784 * We don't want to override the error since there is
787 perror("Failed to close plug-in directory");
795 struct bt_plugin
**bt_plugin_create_all_from_dir(const char *path
,
798 GPtrArray
*plugins_array
= g_ptr_array_new();
799 struct bt_plugin
**plugins
= NULL
;
800 enum bt_plugin_status status
;
802 /* Append found plugins to array */
803 status
= bt_plugin_create_append_all_from_dir(plugins_array
, path
,
809 /* Add sentinel to array */
810 g_ptr_array_add(plugins_array
, NULL
);
811 plugins
= (struct bt_plugin
**) plugins_array
->pdata
;
816 g_ptr_array_free(plugins_array
, TRUE
);
817 plugins_array
= NULL
;
822 g_ptr_array_free(plugins_array
, FALSE
);
828 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
830 return plugin
? plugin
->name
: NULL
;
833 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
835 return plugin
? plugin
->author
: NULL
;
838 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
840 return plugin
? plugin
->license
: NULL
;
843 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
845 return (plugin
&& plugin
->shared_lib_handle
->path
) ?
846 plugin
->shared_lib_handle
->path
->str
: NULL
;
849 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
851 return plugin
? plugin
->description
: NULL
;
854 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
856 return plugin
? plugin
->comp_classes
->len
: -1;
859 struct bt_component_class
*bt_plugin_get_component_class(
860 struct bt_plugin
*plugin
, size_t index
)
862 struct bt_component_class
*comp_class
= NULL
;
864 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
868 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
879 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
880 struct bt_plugin
*plugin
, const char *name
,
881 enum bt_component_type type
)
883 struct bt_component_class
*comp_class
= NULL
;
886 if (!plugin
|| !name
) {
890 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
891 struct bt_component_class
*comp_class_candidate
=
892 g_ptr_array_index(plugin
->comp_classes
, i
);
893 const char *comp_class_cand_name
=
894 bt_component_class_get_name(comp_class_candidate
);
895 enum bt_component_type comp_class_cand_type
=
896 bt_component_class_get_type(comp_class_candidate
);
898 assert(comp_class_cand_name
);
899 assert(comp_class_cand_type
>= 0);
901 if (strcmp(name
, comp_class_cand_name
) == 0 &&
902 comp_class_cand_type
== type
) {
903 comp_class
= bt_get(comp_class_candidate
);
918 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
921 gboolean exists
= g_hash_table_remove(comp_classes_to_shlib_handles
,
926 enum bt_plugin_status
bt_plugin_add_component_class(
927 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
929 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
930 struct bt_component_class
*comp_class_dup
= NULL
;
932 int comp_class_index
= -1;
934 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
938 /* Check for duplicate */
939 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
940 bt_component_class_get_name(comp_class
),
941 bt_component_class_get_type(comp_class
));
942 if (comp_class_dup
) {
943 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
945 bt_component_class_get_name(comp_class
),
946 bt_component_class_get_type(comp_class
));
950 /* Add new component class */
951 comp_class_index
= plugin
->comp_classes
->len
;
952 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
954 /* Map component class pointer to shared lib handle in global HT */
955 g_hash_table_insert(comp_classes_to_shlib_handles
, comp_class
,
956 bt_get(plugin
->shared_lib_handle
));
958 /* Add our custom destroy listener */
959 ret
= bt_component_class_add_destroy_listener(comp_class
,
960 plugin_comp_class_destroy_listener
, NULL
);
968 /* Remove entry from global hash table (if exists) */
969 g_hash_table_remove(comp_classes_to_shlib_handles
,
972 /* Remove entry from plugin's component classes (if added) */
973 if (comp_class_index
>= 0) {
974 g_ptr_array_remove_index(plugin
->comp_classes
,
978 status
= BT_PLUGIN_STATUS_ERROR
;
981 bt_put(comp_class_dup
);