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