Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | /* |
2 | * plugin.c | |
3 | * | |
4 | * Babeltrace Plugin | |
5 | * | |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
30 | #include <babeltrace/compiler.h> | |
31 | #include <babeltrace/ref.h> | |
32 | #include <babeltrace/plugin/plugin-dev.h> | |
33 | #include <babeltrace/plugin/plugin-internal.h> | |
34 | #include <babeltrace/component/component-class-internal.h> | |
35 | #include <string.h> | |
36 | #include <stdbool.h> | |
37 | #include <glib.h> | |
38 | #include <gmodule.h> | |
39 | #include <unistd.h> | |
40 | #include <stdlib.h> | |
41 | #include <sys/stat.h> | |
42 | #include <dirent.h> | |
43 | ||
33b34c43 PP |
44 | #define NATIVE_PLUGIN_SUFFIX ".so" |
45 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) | |
46 | #define LIBTOOL_PLUGIN_SUFFIX ".la" | |
47 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) | |
48 | ||
49 | #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ | |
50 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) | |
51 | ||
6ba0b073 PP |
52 | #define SECTION_BEGIN(_name) (&(__start_##_name)) |
53 | #define SECTION_END(_name) (&(__stop_##_name)) | |
33b34c43 PP |
54 | #define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name)) |
55 | ||
56 | #define DECLARE_SECTION(_type, _name) \ | |
6ba0b073 PP |
57 | extern _type __start_##_name __attribute((weak)); \ |
58 | extern _type __stop_##_name __attribute((weak)) | |
59 | ||
60 | DECLARE_SECTION(struct __bt_plugin_descriptor const *, __bt_plugin_descriptors); | |
61 | DECLARE_SECTION(struct __bt_plugin_descriptor_attribute const *, __bt_plugin_descriptor_attributes); | |
62 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor const *, __bt_plugin_component_class_descriptors); | |
63 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute const *, __bt_plugin_component_class_descriptor_attributes); | |
33b34c43 PP |
64 | |
65 | /* | |
66 | * This hash table, global to the library, maps component class pointers | |
67 | * to shared library handles. | |
68 | * | |
69 | * The keys (component classes) are NOT owned by this hash table, whereas | |
70 | * the values (shared library handles) are owned by this hash table. | |
71 | * | |
72 | * The keys are the component classes created with | |
73 | * bt_plugin_add_component_class(). They keep the shared library handle | |
74 | * object created by their plugin alive so that the plugin's code is | |
75 | * not discarded when it could still be in use by living components | |
76 | * created from those component classes: | |
77 | * | |
78 | * [component] --ref-> [component class] --through this HT-> [shlib handle] | |
79 | * | |
80 | * This hash table exists for two reasons: | |
81 | * | |
82 | * 1. To allow this application: | |
83 | * | |
6ba0b073 PP |
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 | |
33b34c43 PP |
87 | * // user code of instantiated components still exists |
88 | * | |
89 | * 2. To decouple the plugin subsystem from the component subsystem: | |
90 | * while plugins objects need to know component class objects, the | |
91 | * opposite is not necessary, thus it makes no sense for a component | |
92 | * class to keep a reference to the plugin object from which it was | |
93 | * created. | |
94 | * | |
95 | * An entry is removed from this HT when a component class is destroyed | |
96 | * thanks to a custom destroy listener. When the entry is removed, the | |
97 | * GLib function calls the value destroy notifier of the HT, which is | |
98 | * bt_put(). This decreases the reference count of the mapped shared | |
99 | * library handle. Assuming the original plugin object which contained | |
100 | * some component classes is put first, when the last component class is | |
101 | * removed from this HT, the shared library handle object's reference | |
102 | * count falls to zero and the shared library is finally closed. | |
103 | */ | |
104 | static | |
105 | GHashTable *comp_classes_to_shlib_handles; | |
106 | ||
107 | __attribute__((constructor)) static | |
108 | void init_comp_classes_to_shlib_handles(void) { | |
109 | comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash, | |
110 | g_direct_equal, NULL, bt_put); | |
111 | assert(comp_classes_to_shlib_handles); | |
112 | } | |
113 | ||
114 | __attribute__((destructor)) static | |
115 | void fini_comp_classes_to_shlib_handles(void) { | |
116 | if (comp_classes_to_shlib_handles) { | |
117 | g_hash_table_destroy(comp_classes_to_shlib_handles); | |
118 | } | |
119 | } | |
120 | ||
121 | static | |
122 | void bt_plugin_shared_lib_handle_destroy(struct bt_object *obj) | |
123 | { | |
124 | struct bt_plugin_shared_lib_handle *shared_lib_handle; | |
125 | ||
126 | assert(obj); | |
127 | shared_lib_handle = container_of(obj, | |
128 | struct bt_plugin_shared_lib_handle, base); | |
129 | ||
130 | if (shared_lib_handle->init_called && shared_lib_handle->exit) { | |
131 | enum bt_plugin_status status = shared_lib_handle->exit(); | |
132 | ||
133 | if (status < 0) { | |
6ba0b073 PP |
134 | const char *path = shared_lib_handle->path ? |
135 | shared_lib_handle->path->str : "[built-in]"; | |
136 | ||
137 | printf_verbose("Plugin in module `%s` exited with error %d\n", | |
138 | path, status); | |
33b34c43 PP |
139 | } |
140 | } | |
141 | ||
142 | if (shared_lib_handle->module) { | |
143 | if (!g_module_close(shared_lib_handle->module)) { | |
144 | printf_error("Module close error: %s\n", | |
145 | g_module_error()); | |
146 | } | |
147 | } | |
148 | ||
149 | if (shared_lib_handle->path) { | |
150 | g_string_free(shared_lib_handle->path, TRUE); | |
151 | } | |
152 | ||
153 | g_free(shared_lib_handle); | |
154 | } | |
155 | ||
156 | static | |
157 | struct bt_plugin_shared_lib_handle *bt_plugin_shared_lib_handle_create( | |
158 | const char *path) | |
159 | { | |
160 | struct bt_plugin_shared_lib_handle *shared_lib_handle = NULL; | |
33b34c43 PP |
161 | |
162 | shared_lib_handle = g_new0(struct bt_plugin_shared_lib_handle, 1); | |
163 | if (!shared_lib_handle) { | |
164 | goto error; | |
165 | } | |
166 | ||
167 | bt_object_init(shared_lib_handle, bt_plugin_shared_lib_handle_destroy); | |
168 | ||
169 | if (!path) { | |
170 | goto end; | |
171 | } | |
172 | ||
173 | shared_lib_handle->path = g_string_new(path); | |
174 | if (!shared_lib_handle->path) { | |
175 | goto error; | |
176 | } | |
177 | ||
178 | shared_lib_handle->module = g_module_open(path, 0); | |
179 | if (!shared_lib_handle->module) { | |
180 | printf_verbose("Module open error: %s\n", g_module_error()); | |
181 | goto error; | |
182 | } | |
183 | ||
33b34c43 PP |
184 | goto end; |
185 | ||
186 | error: | |
187 | BT_PUT(shared_lib_handle); | |
188 | ||
189 | end: | |
190 | return shared_lib_handle; | |
191 | } | |
192 | ||
193 | static | |
194 | void bt_plugin_destroy(struct bt_object *obj) | |
195 | { | |
196 | struct bt_plugin *plugin; | |
197 | ||
198 | assert(obj); | |
199 | plugin = container_of(obj, struct bt_plugin, base); | |
200 | ||
201 | BT_PUT(plugin->shared_lib_handle); | |
202 | ||
203 | if (plugin->comp_classes) { | |
204 | g_ptr_array_free(plugin->comp_classes, TRUE); | |
205 | } | |
206 | ||
207 | g_free(plugin); | |
208 | } | |
209 | ||
210 | static | |
6ba0b073 PP |
211 | struct bt_plugin *bt_plugin_create_empty( |
212 | struct bt_plugin_shared_lib_handle *shared_lib_handle) | |
213 | { | |
214 | struct bt_plugin *plugin = NULL; | |
215 | ||
216 | plugin = g_new0(struct bt_plugin, 1); | |
217 | if (!plugin) { | |
218 | goto error; | |
219 | } | |
220 | ||
221 | bt_object_init(plugin, bt_plugin_destroy); | |
222 | plugin->shared_lib_handle = bt_get(shared_lib_handle); | |
223 | ||
224 | /* Create empty array of component classes */ | |
225 | plugin->comp_classes = | |
226 | g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); | |
227 | if (!plugin->comp_classes) { | |
228 | goto error; | |
229 | } | |
230 | ||
231 | goto end; | |
232 | ||
233 | error: | |
234 | BT_PUT(plugin); | |
235 | ||
236 | end: | |
237 | return plugin; | |
238 | } | |
239 | ||
240 | static | |
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) | |
33b34c43 | 250 | { |
6ba0b073 PP |
251 | /* |
252 | * This structure's members point to the plugin's memory | |
253 | * (do NOT free). | |
254 | */ | |
255 | struct comp_class_full_descriptor { | |
256 | const struct __bt_plugin_component_class_descriptor *descriptor; | |
257 | const char *description; | |
258 | }; | |
259 | ||
33b34c43 | 260 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
6ba0b073 PP |
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; | |
265 | size_t i; | |
266 | ||
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; | |
271 | goto end; | |
272 | } | |
33b34c43 | 273 | |
6ba0b073 PP |
274 | /* Set mandatory attributes */ |
275 | plugin->descriptor = descriptor; | |
276 | plugin->name = descriptor->name; | |
33b34c43 | 277 | |
6ba0b073 PP |
278 | /* |
279 | * Find and set optional attributes attached to this plugin | |
280 | * descriptor. | |
281 | */ | |
282 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { | |
283 | const struct __bt_plugin_descriptor_attribute *cur_attr = | |
284 | *cur_attr_ptr; | |
285 | ||
286 | if (cur_attr->plugin_descriptor != descriptor) { | |
287 | continue; | |
288 | } | |
289 | ||
290 | switch (cur_attr->type) { | |
291 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: | |
292 | plugin->init = cur_attr->value.init; | |
293 | break; | |
294 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: | |
295 | plugin->shared_lib_handle->exit = cur_attr->value.exit; | |
296 | break; | |
297 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: | |
298 | plugin->author = cur_attr->value.author; | |
299 | break; | |
300 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: | |
301 | plugin->license = cur_attr->value.license; | |
302 | break; | |
303 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
304 | plugin->description = cur_attr->value.description; | |
305 | break; | |
306 | default: | |
307 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n", | |
308 | cur_attr->type_name, cur_attr->type, | |
309 | descriptor->name); | |
310 | break; | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Find component class descriptors attached to this plugin | |
316 | * descriptor and initialize corresponding full component class | |
317 | * descriptors in the array. | |
318 | */ | |
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 = | |
321 | *cur_cc_descr_ptr; | |
322 | struct comp_class_full_descriptor full_descriptor; | |
323 | ||
324 | if (cur_cc_descr->plugin_descriptor != descriptor) { | |
325 | continue; | |
326 | } | |
327 | ||
328 | full_descriptor.descriptor = cur_cc_descr; | |
329 | full_descriptor.description = NULL; | |
330 | g_array_append_val(comp_class_full_descriptors, | |
331 | full_descriptor); | |
332 | } | |
333 | ||
334 | /* | |
335 | * Find component class descriptor attributes attached to this | |
336 | * plugin descriptor and update corresponding full component | |
337 | * class descriptors in the array. | |
338 | */ | |
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; | |
342 | ||
343 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != | |
344 | descriptor) { | |
345 | continue; | |
346 | } | |
347 | ||
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); | |
353 | ||
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; | |
360 | break; | |
361 | default: | |
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, | |
367 | descriptor->name); | |
368 | break; | |
369 | } | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
374 | /* Initialize plugin */ | |
375 | if (plugin->init) { | |
376 | status = plugin->init(plugin); | |
33b34c43 PP |
377 | if (status < 0) { |
378 | printf_verbose("Plugin `%s` initialization error: %d\n", | |
6ba0b073 | 379 | plugin->name, status); |
33b34c43 PP |
380 | goto end; |
381 | } | |
382 | } | |
383 | ||
384 | plugin->shared_lib_handle->init_called = true; | |
385 | ||
6ba0b073 PP |
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; | |
391 | ||
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); | |
397 | if (!comp_class) { | |
398 | status = BT_PLUGIN_STATUS_ERROR; | |
399 | goto end; | |
400 | } | |
401 | ||
402 | status = bt_plugin_add_component_class(plugin, | |
403 | comp_class); | |
404 | BT_PUT(comp_class); | |
405 | if (status < 0) { | |
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); | |
410 | goto end; | |
411 | } | |
412 | } | |
413 | ||
33b34c43 | 414 | /* |
6ba0b073 PP |
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). | |
33b34c43 PP |
419 | */ |
420 | plugin->frozen = true; | |
421 | ||
422 | end: | |
6ba0b073 | 423 | g_array_free(comp_class_full_descriptors, TRUE); |
33b34c43 PP |
424 | return status; |
425 | } | |
426 | ||
6ba0b073 PP |
427 | static |
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) | |
438 | { | |
439 | size_t descriptor_count; | |
440 | size_t attrs_count; | |
441 | size_t cc_descriptors_count; | |
442 | size_t cc_descr_attrs_count; | |
443 | size_t i; | |
444 | struct bt_plugin **plugins = NULL; | |
445 | ||
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)); | |
459 | if (!plugins) { | |
460 | goto error; | |
461 | } | |
462 | ||
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; | |
468 | ||
469 | printf_verbose("Loading plugin %s (%d.%d)\n", descriptor->name, | |
470 | descriptor->major, descriptor->minor); | |
471 | ||
472 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { | |
473 | printf_error("Unknown plugin's major version: %d\n", | |
474 | descriptor->major); | |
475 | goto error; | |
476 | } | |
477 | ||
478 | plugin = bt_plugin_create_empty(shared_lib_handle); | |
479 | if (!plugin) { | |
480 | printf_error("Cannot allocate plugin object for plugin %s\n", | |
481 | descriptor->name); | |
482 | goto error; | |
483 | } | |
484 | ||
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); | |
488 | if (status < 0) { | |
489 | printf_error("Cannot initialize plugin object %s\n", | |
490 | descriptor->name); | |
491 | BT_PUT(plugin); | |
492 | goto error; | |
493 | } | |
494 | ||
495 | /* Transfer ownership to the array */ | |
496 | plugins[i] = plugin; | |
497 | } | |
498 | ||
499 | goto end; | |
500 | ||
501 | error: | |
502 | g_free(plugins); | |
503 | plugins = NULL; | |
504 | ||
505 | end: | |
506 | return plugins; | |
507 | } | |
508 | ||
509 | struct bt_plugin **bt_plugin_create_all_from_static(void) | |
510 | { | |
511 | struct bt_plugin **plugins = NULL; | |
512 | struct bt_plugin_shared_lib_handle *shared_lib_handle = | |
513 | bt_plugin_shared_lib_handle_create(NULL); | |
514 | ||
515 | if (!shared_lib_handle) { | |
516 | goto end; | |
517 | } | |
518 | ||
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)); | |
528 | ||
529 | end: | |
530 | BT_PUT(shared_lib_handle); | |
531 | ||
532 | return plugins; | |
533 | } | |
534 | ||
535 | struct bt_plugin **bt_plugin_create_all_from_file(const char *path) | |
33b34c43 PP |
536 | { |
537 | size_t path_len; | |
6ba0b073 PP |
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; | |
33b34c43 | 547 | bool is_libtool_wrapper = false, is_shared_object = false; |
6ba0b073 | 548 | struct bt_plugin_shared_lib_handle *shared_lib_handle = NULL; |
33b34c43 PP |
549 | |
550 | if (!path) { | |
6ba0b073 | 551 | goto end; |
33b34c43 PP |
552 | } |
553 | ||
554 | path_len = strlen(path); | |
555 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
6ba0b073 | 556 | goto end; |
33b34c43 PP |
557 | } |
558 | ||
559 | path_len++; | |
560 | /* | |
561 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
562 | * or .la on Linux). | |
563 | */ | |
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. */ | |
6ba0b073 | 572 | goto end; |
33b34c43 PP |
573 | } |
574 | ||
6ba0b073 PP |
575 | shared_lib_handle = bt_plugin_shared_lib_handle_create(path); |
576 | if (!shared_lib_handle) { | |
577 | goto end; | |
33b34c43 PP |
578 | } |
579 | ||
6ba0b073 PP |
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)); | |
585 | goto end; | |
586 | } | |
33b34c43 | 587 | |
6ba0b073 PP |
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)); | |
593 | goto end; | |
33b34c43 PP |
594 | } |
595 | ||
6ba0b073 PP |
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)); | |
33b34c43 PP |
601 | } |
602 | ||
6ba0b073 PP |
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)); | |
33b34c43 PP |
608 | } |
609 | ||
6ba0b073 PP |
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)); | |
613 | goto end; | |
614 | } | |
33b34c43 | 615 | |
6ba0b073 PP |
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)); | |
621 | } | |
622 | ||
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)); | |
628 | } | |
629 | ||
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)); | |
633 | goto end; | |
634 | } | |
635 | ||
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)); | |
641 | } | |
642 | ||
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)); | |
648 | } | |
649 | ||
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)); | |
653 | goto end; | |
654 | } | |
655 | ||
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); | |
33b34c43 PP |
661 | |
662 | end: | |
6ba0b073 PP |
663 | BT_PUT(shared_lib_handle); |
664 | return plugins; | |
33b34c43 PP |
665 | } |
666 | ||
667 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ | |
668 | static | |
669 | struct dirent *alloc_dirent(const char *path) | |
670 | { | |
671 | size_t len; | |
672 | long name_max; | |
673 | struct dirent *entry; | |
674 | ||
675 | name_max = pathconf(path, _PC_NAME_MAX); | |
676 | if (name_max == -1) { | |
677 | name_max = PATH_MAX; | |
678 | } | |
679 | len = offsetof(struct dirent, d_name) + name_max + 1; | |
680 | entry = zmalloc(len); | |
681 | return entry; | |
682 | } | |
683 | ||
684 | static | |
685 | enum bt_plugin_status bt_plugin_create_append_all_from_dir( | |
686 | GPtrArray *plugins, const char *path, bool recurse) | |
687 | { | |
688 | DIR *directory = NULL; | |
689 | struct dirent *entry = NULL, *result = NULL; | |
690 | char *file_path = NULL; | |
6ba0b073 | 691 | size_t path_len; |
33b34c43 PP |
692 | enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; |
693 | ||
6ba0b073 PP |
694 | if (!path) { |
695 | ret = BT_PLUGIN_STATUS_ERROR; | |
696 | goto end; | |
697 | } | |
698 | ||
699 | path_len = strlen(path); | |
700 | ||
33b34c43 PP |
701 | if (path_len >= PATH_MAX) { |
702 | ret = BT_PLUGIN_STATUS_ERROR; | |
703 | goto end; | |
704 | } | |
705 | ||
706 | entry = alloc_dirent(path); | |
707 | if (!entry) { | |
708 | ret = BT_PLUGIN_STATUS_ERROR; | |
709 | goto end; | |
710 | } | |
711 | ||
712 | file_path = zmalloc(PATH_MAX); | |
713 | if (!file_path) { | |
714 | ret = BT_PLUGIN_STATUS_NOMEM; | |
715 | goto end; | |
716 | } | |
717 | ||
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++] = '/'; | |
722 | } | |
723 | ||
724 | directory = opendir(file_path); | |
725 | if (!directory) { | |
726 | perror("Failed to open plug-in directory"); | |
727 | ret = BT_PLUGIN_STATUS_ERROR; | |
728 | goto end; | |
729 | } | |
730 | ||
731 | /* Recursively walk directory */ | |
732 | while (!readdir_r(directory, entry, &result) && result) { | |
733 | struct stat st; | |
734 | int stat_ret; | |
735 | size_t file_name_len; | |
736 | ||
737 | if (result->d_name[0] == '.') { | |
738 | /* Skip hidden files, . and .. */ | |
739 | continue; | |
740 | } | |
741 | ||
742 | file_name_len = strlen(result->d_name); | |
743 | ||
744 | if (path_len + file_name_len >= PATH_MAX) { | |
745 | continue; | |
746 | } | |
747 | ||
748 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
749 | file_path[path_len + file_name_len] = '\0'; | |
750 | ||
751 | stat_ret = stat(file_path, &st); | |
752 | if (stat_ret < 0) { | |
753 | /* Continue to next file / directory. */ | |
754 | printf_perror("Failed to stat() plugin file\n"); | |
755 | continue; | |
756 | } | |
757 | ||
758 | if (S_ISDIR(st.st_mode) && recurse) { | |
759 | ret = bt_plugin_create_append_all_from_dir(plugins, | |
760 | file_path, true); | |
761 | if (ret < 0) { | |
762 | goto end; | |
763 | } | |
764 | } else if (S_ISREG(st.st_mode)) { | |
6ba0b073 PP |
765 | struct bt_plugin **plugins_from_file = |
766 | bt_plugin_create_all_from_file(file_path); | |
33b34c43 | 767 | |
6ba0b073 PP |
768 | if (plugins_from_file) { |
769 | struct bt_plugin **plugin; | |
770 | ||
771 | for (plugin = plugins_from_file; *plugin; plugin++) { | |
772 | /* Transfer ownership to array */ | |
773 | g_ptr_array_add(plugins, *plugin); | |
774 | } | |
775 | ||
776 | free(plugins_from_file); | |
33b34c43 PP |
777 | } |
778 | } | |
779 | } | |
780 | end: | |
781 | if (directory) { | |
782 | if (closedir(directory)) { | |
783 | /* | |
784 | * We don't want to override the error since there is | |
785 | * nothing could do. | |
786 | */ | |
787 | perror("Failed to close plug-in directory"); | |
788 | } | |
789 | } | |
790 | free(entry); | |
791 | free(file_path); | |
792 | return ret; | |
793 | } | |
794 | ||
795 | struct bt_plugin **bt_plugin_create_all_from_dir(const char *path, | |
796 | bool recurse) | |
797 | { | |
6ba0b073 | 798 | GPtrArray *plugins_array = g_ptr_array_new(); |
33b34c43 PP |
799 | struct bt_plugin **plugins = NULL; |
800 | enum bt_plugin_status status; | |
801 | ||
33b34c43 PP |
802 | /* Append found plugins to array */ |
803 | status = bt_plugin_create_append_all_from_dir(plugins_array, path, | |
804 | recurse); | |
805 | if (status < 0) { | |
806 | goto error; | |
807 | } | |
808 | ||
809 | /* Add sentinel to array */ | |
810 | g_ptr_array_add(plugins_array, NULL); | |
811 | plugins = (struct bt_plugin **) plugins_array->pdata; | |
812 | goto end; | |
813 | ||
814 | error: | |
815 | if (plugins_array) { | |
816 | g_ptr_array_free(plugins_array, TRUE); | |
817 | plugins_array = NULL; | |
818 | } | |
819 | ||
820 | end: | |
821 | if (plugins_array) { | |
822 | g_ptr_array_free(plugins_array, FALSE); | |
823 | } | |
824 | ||
825 | return plugins; | |
826 | } | |
827 | ||
33b34c43 PP |
828 | const char *bt_plugin_get_name(struct bt_plugin *plugin) |
829 | { | |
6ba0b073 | 830 | return plugin ? plugin->name : NULL; |
33b34c43 PP |
831 | } |
832 | ||
833 | const char *bt_plugin_get_author(struct bt_plugin *plugin) | |
834 | { | |
6ba0b073 | 835 | return plugin ? plugin->author : NULL; |
33b34c43 PP |
836 | } |
837 | ||
838 | const char *bt_plugin_get_license(struct bt_plugin *plugin) | |
839 | { | |
6ba0b073 | 840 | return plugin ? plugin->license : NULL; |
33b34c43 PP |
841 | } |
842 | ||
843 | const char *bt_plugin_get_path(struct bt_plugin *plugin) | |
844 | { | |
845 | return (plugin && plugin->shared_lib_handle->path) ? | |
846 | plugin->shared_lib_handle->path->str : NULL; | |
847 | } | |
848 | ||
849 | const char *bt_plugin_get_description(struct bt_plugin *plugin) | |
850 | { | |
6ba0b073 | 851 | return plugin ? plugin->description : NULL; |
33b34c43 PP |
852 | } |
853 | ||
854 | int bt_plugin_get_component_class_count(struct bt_plugin *plugin) | |
855 | { | |
856 | return plugin ? plugin->comp_classes->len : -1; | |
857 | } | |
858 | ||
859 | struct bt_component_class *bt_plugin_get_component_class( | |
860 | struct bt_plugin *plugin, size_t index) | |
861 | { | |
862 | struct bt_component_class *comp_class = NULL; | |
863 | ||
864 | if (!plugin || index >= plugin->comp_classes->len) { | |
865 | goto error; | |
866 | } | |
867 | ||
868 | comp_class = g_ptr_array_index(plugin->comp_classes, index); | |
869 | bt_get(comp_class); | |
870 | goto end; | |
871 | ||
872 | error: | |
873 | BT_PUT(comp_class); | |
874 | ||
875 | end: | |
876 | return comp_class; | |
877 | } | |
878 | ||
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) | |
882 | { | |
883 | struct bt_component_class *comp_class = NULL; | |
884 | size_t i; | |
885 | ||
886 | if (!plugin || !name) { | |
887 | goto error; | |
888 | } | |
889 | ||
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); | |
897 | ||
898 | assert(comp_class_cand_name); | |
899 | assert(comp_class_cand_type >= 0); | |
900 | ||
901 | if (strcmp(name, comp_class_cand_name) == 0 && | |
902 | comp_class_cand_type == type) { | |
903 | comp_class = bt_get(comp_class_candidate); | |
904 | break; | |
905 | } | |
906 | } | |
907 | ||
908 | goto end; | |
909 | ||
910 | error: | |
911 | BT_PUT(comp_class); | |
912 | ||
913 | end: | |
914 | return comp_class; | |
915 | } | |
916 | ||
917 | static | |
918 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, | |
919 | void *data) | |
920 | { | |
921 | gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles, | |
922 | comp_class); | |
923 | assert(exists); | |
924 | } | |
925 | ||
926 | enum bt_plugin_status bt_plugin_add_component_class( | |
927 | struct bt_plugin *plugin, struct bt_component_class *comp_class) | |
928 | { | |
929 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
930 | struct bt_component_class *comp_class_dup = NULL; | |
931 | int ret; | |
932 | int comp_class_index = -1; | |
933 | ||
934 | if (!plugin || !comp_class || plugin->frozen) { | |
935 | goto error; | |
936 | } | |
937 | ||
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", | |
6ba0b073 | 944 | plugin->name, |
33b34c43 PP |
945 | bt_component_class_get_name(comp_class), |
946 | bt_component_class_get_type(comp_class)); | |
947 | goto error; | |
948 | } | |
949 | ||
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)); | |
953 | ||
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)); | |
957 | ||
958 | /* Add our custom destroy listener */ | |
959 | ret = bt_component_class_add_destroy_listener(comp_class, | |
960 | plugin_comp_class_destroy_listener, NULL); | |
961 | if (ret) { | |
962 | goto error; | |
963 | } | |
964 | ||
965 | goto end; | |
966 | ||
967 | error: | |
968 | /* Remove entry from global hash table (if exists) */ | |
969 | g_hash_table_remove(comp_classes_to_shlib_handles, | |
970 | comp_class); | |
971 | ||
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, | |
975 | comp_class_index); | |
976 | } | |
977 | ||
978 | status = BT_PLUGIN_STATUS_ERROR; | |
979 | ||
980 | end: | |
981 | bt_put(comp_class_dup); | |
982 | return status; | |
983 | } |