47b49cf993caf2856a19531a6189f37a0cd94b72
[babeltrace.git] / src / lib / plugin / plugin.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #define BT_LOG_TAG "LIB/PLUGIN"
27 #include "lib/logging.h"
28
29 #include "common/assert.h"
30 #include "lib/assert-pre.h"
31 #include "common/macros.h"
32 #include "compat/compiler.h"
33 #include "common/common.h"
34 #include <babeltrace2/plugin/plugin-const.h>
35 #include <babeltrace2/graph/component-class-const.h>
36 #include "lib/graph/component-class.h"
37 #include <babeltrace2/types.h>
38 #include <glib.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <stdint.h>
42 #include <inttypes.h>
43 #include <sys/stat.h>
44 #include <ftw.h>
45 #include <pthread.h>
46
47 #include "plugin.h"
48 #include "plugin-so.h"
49
50 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace2-python-plugin-provider." G_MODULE_SUFFIX
51 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
52 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
53
54 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
55
56 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
57 #include <plugin/python-plugin-provider.h>
58
59 static
60 enum bt_plugin_status (*bt_plugin_python_create_all_from_file_sym)(
61 const char *path, bool fail_on_load_error,
62 struct bt_plugin_set **plugin_set_out) =
63 bt_plugin_python_create_all_from_file;
64
65 static
66 void init_python_plugin_provider(void)
67 {
68 }
69 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
70 static GModule *python_plugin_provider_module;
71
72 static
73 enum bt_plugin_status (*bt_plugin_python_create_all_from_file_sym)(
74 const char *path, bool fail_on_load_error,
75 struct bt_plugin_set **plugin_set_out);
76
77 static
78 void init_python_plugin_provider(void) {
79 if (bt_plugin_python_create_all_from_file_sym != NULL) {
80 return;
81 }
82
83 BT_LOGI_STR("Loading Python plugin provider module.");
84 python_plugin_provider_module =
85 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
86 if (!python_plugin_provider_module) {
87 /*
88 * This is not an error. The whole point of having an
89 * external Python plugin provider is that it can be
90 * missing and the Babeltrace library still works.
91 */
92 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
93 PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
94 return;
95 }
96
97 if (!g_module_symbol(python_plugin_provider_module,
98 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
99 (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
100 /*
101 * This is an error because, since we found the Python
102 * plugin provider shared object, we expect this symbol
103 * to exist.
104 */
105 BT_LOGE("Cannot find the Python plugin provider loading symbol: "
106 "%s: continuing without Python plugin support: "
107 "file=\"%s\", symbol=\"%s\"",
108 g_module_error(),
109 PYTHON_PLUGIN_PROVIDER_FILENAME,
110 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
111 return;
112 }
113
114 BT_LOGI("Loaded Python plugin provider module: addr=%p",
115 python_plugin_provider_module);
116 }
117
118 __attribute__((destructor)) static
119 void fini_python_plugin_provider(void) {
120 if (python_plugin_provider_module) {
121 BT_LOGI("Unloading Python plugin provider module.");
122
123 if (!g_module_close(python_plugin_provider_module)) {
124 BT_LOGE("Failed to close the Python plugin provider module: %s.",
125 g_module_error());
126 }
127
128 python_plugin_provider_module = NULL;
129 }
130 }
131 #endif
132
133 uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
134 {
135 BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set");
136 return (uint64_t) plugin_set->plugins->len;
137 }
138
139 const struct bt_plugin *bt_plugin_set_borrow_plugin_by_index_const(
140 const struct bt_plugin_set *plugin_set, uint64_t index)
141 {
142 BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set");
143 BT_ASSERT_PRE_VALID_INDEX(index, plugin_set->plugins->len);
144 return g_ptr_array_index(plugin_set->plugins, index);
145 }
146
147 enum bt_plugin_status bt_plugin_find_all_from_static(
148 bt_bool fail_on_load_error,
149 const struct bt_plugin_set **plugin_set_out)
150 {
151 /* bt_plugin_so_create_all_from_static() logs errors */
152 return bt_plugin_so_create_all_from_static(fail_on_load_error,
153 (void *) plugin_set_out);
154 }
155
156 enum bt_plugin_status bt_plugin_find_all_from_file(const char *path,
157 bt_bool fail_on_load_error,
158 const struct bt_plugin_set **plugin_set_out)
159 {
160 enum bt_plugin_status status;
161
162 BT_ASSERT_PRE_NON_NULL(path, "Path");
163 BT_ASSERT_PRE_NON_NULL(path, "Plugin set (output)");
164 BT_LOGI("Creating plugins from file: path=\"%s\"", path);
165
166 /* Try shared object plugins */
167 status = bt_plugin_so_create_all_from_file(path, fail_on_load_error,
168 (void *) plugin_set_out);
169 if (status == BT_PLUGIN_STATUS_OK) {
170 BT_ASSERT(*plugin_set_out);
171 BT_ASSERT((*plugin_set_out)->plugins->len > 0);
172 goto end;
173 } else if (status < 0) {
174 BT_ASSERT(!*plugin_set_out);
175 goto end;
176 }
177
178 BT_ASSERT(status == BT_PLUGIN_STATUS_NOT_FOUND);
179 BT_ASSERT(!*plugin_set_out);
180
181 /* Try Python plugins if support is available */
182 init_python_plugin_provider();
183 if (bt_plugin_python_create_all_from_file_sym) {
184 status = bt_plugin_python_create_all_from_file_sym(path,
185 fail_on_load_error, (void *) plugin_set_out);
186 if (status == BT_PLUGIN_STATUS_OK) {
187 BT_ASSERT(*plugin_set_out);
188 BT_ASSERT((*plugin_set_out)->plugins->len > 0);
189 goto end;
190 } else if (status < 0) {
191 BT_ASSERT(!*plugin_set_out);
192 goto end;
193 }
194
195 BT_ASSERT(status == BT_PLUGIN_STATUS_NOT_FOUND);
196 BT_ASSERT(!*plugin_set_out);
197 }
198
199 end:
200 if (status == BT_PLUGIN_STATUS_OK) {
201 BT_LOGI("Created %u plugins from file: "
202 "path=\"%s\", count=%u, plugin-set-addr=%p",
203 (*plugin_set_out)->plugins->len, path,
204 (*plugin_set_out)->plugins->len,
205 *plugin_set_out);
206 } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) {
207 BT_LOGI("Found no plugins in file: path=\"%s\"", path);
208 }
209
210 return status;
211 }
212
213 static void destroy_gstring(void *data)
214 {
215 g_string_free(data, TRUE);
216 }
217
218 enum bt_plugin_status bt_plugin_find(const char *plugin_name,
219 bt_bool fail_on_load_error, const struct bt_plugin **plugin_out)
220 {
221 const char *system_plugin_dir;
222 char *home_plugin_dir = NULL;
223 const char *envvar;
224 const struct bt_plugin *plugin = NULL;
225 const struct bt_plugin_set *plugin_set = NULL;
226 GPtrArray *dirs = NULL;
227 int ret;
228 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
229 size_t i, j;
230
231 BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
232 BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)");
233 BT_LOGI("Finding named plugin in standard directories and built-in plugins: "
234 "name=\"%s\"", plugin_name);
235 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
236 if (!dirs) {
237 BT_LOGE_STR("Failed to allocate a GPtrArray.");
238 status = BT_PLUGIN_STATUS_NOMEM;
239 goto end;
240 }
241
242 /*
243 * Search order is:
244 *
245 * 1. BABELTRACE_PLUGIN_PATH environment variable
246 * (colon-separated list of directories)
247 * 2. ~/.local/lib/babeltrace2/plugins
248 * 3. Default system directory for Babeltrace plugins, usually
249 * /usr/lib/babeltrace2/plugins or
250 * /usr/local/lib/babeltrace2/plugins if installed
251 * locally
252 * 4. Built-in plugins (static)
253 *
254 * Directories are searched non-recursively.
255 */
256 envvar = getenv("BABELTRACE_PLUGIN_PATH");
257 if (envvar) {
258 ret = bt_common_append_plugin_path_dirs(envvar, dirs);
259 if (ret) {
260 BT_LOGE_STR("Failed to append plugin path to array of directories.");
261 status = BT_PLUGIN_STATUS_NOMEM;
262 goto end;
263 }
264 }
265
266 home_plugin_dir = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
267 if (home_plugin_dir) {
268 GString *home_plugin_dir_str = g_string_new(home_plugin_dir);
269
270 if (!home_plugin_dir_str) {
271 BT_LOGE_STR("Failed to allocate a GString.");
272 status = BT_PLUGIN_STATUS_NOMEM;
273 goto end;
274 }
275
276 g_ptr_array_add(dirs, home_plugin_dir_str);
277 }
278
279 system_plugin_dir = bt_common_get_system_plugin_path();
280 if (system_plugin_dir) {
281 GString *system_plugin_dir_str =
282 g_string_new(system_plugin_dir);
283
284 if (!system_plugin_dir_str) {
285 BT_LOGE_STR("Failed to allocate a GString.");
286 status = BT_PLUGIN_STATUS_NOMEM;
287 goto end;
288 }
289
290 g_ptr_array_add(dirs, system_plugin_dir_str);
291 }
292
293 for (i = 0; i < dirs->len; i++) {
294 GString *dir = g_ptr_array_index(dirs, i);
295
296 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
297
298 /*
299 * Skip this if the directory does not exist because
300 * bt_plugin_find_all_from_dir() would log a warning.
301 */
302 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
303 BT_LOGI("Skipping nonexistent directory path: "
304 "path=\"%s\"", dir->str);
305 continue;
306 }
307
308 /* bt_plugin_find_all_from_dir() logs details/errors */
309 status = bt_plugin_find_all_from_dir(dir->str, BT_FALSE,
310 fail_on_load_error, &plugin_set);
311 if (status < 0) {
312 BT_ASSERT(!plugin_set);
313 goto end;
314 } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) {
315 BT_ASSERT(!plugin_set);
316 BT_LOGI("No plugins found in directory: path=\"%s\"",
317 dir->str);
318 continue;
319 }
320
321 BT_ASSERT(status == BT_PLUGIN_STATUS_OK);
322 BT_ASSERT(plugin_set);
323
324 for (j = 0; j < plugin_set->plugins->len; j++) {
325 const struct bt_plugin *candidate_plugin =
326 g_ptr_array_index(plugin_set->plugins, j);
327
328 if (strcmp(bt_plugin_get_name(candidate_plugin),
329 plugin_name) == 0) {
330 BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"",
331 plugin_name, dir->str);
332 *plugin_out = candidate_plugin;
333 bt_object_get_no_null_check(*plugin_out);
334 goto end;
335 }
336 }
337
338 BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"",
339 plugin_name, dir->str);
340 }
341
342 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
343 status = bt_plugin_find_all_from_static(fail_on_load_error,
344 &plugin_set);
345 if (status < 0) {
346 BT_ASSERT(!plugin_set);
347 goto end;
348 }
349
350 if (status == BT_PLUGIN_STATUS_NOT_FOUND) {
351 BT_ASSERT(!plugin_set);
352 BT_LOGI_STR("No plugins found in built-in plugins.");
353 goto end;
354 }
355
356 BT_ASSERT(status == BT_PLUGIN_STATUS_OK);
357 BT_ASSERT(plugin_set);
358
359 for (j = 0; j < plugin_set->plugins->len; j++) {
360 const struct bt_plugin *candidate_plugin =
361 g_ptr_array_index(plugin_set->plugins, j);
362
363 if (strcmp(bt_plugin_get_name(candidate_plugin),
364 plugin_name) == 0) {
365 BT_LOGI("Plugin found in built-in plugins: "
366 "name=\"%s\"", plugin_name);
367 *plugin_out = candidate_plugin;
368 bt_object_get_no_null_check(*plugin_out);
369 goto end;
370 }
371 }
372
373 status = BT_PLUGIN_STATUS_NOT_FOUND;
374
375 end:
376 free(home_plugin_dir);
377 bt_object_put_ref(plugin_set);
378
379 if (dirs) {
380 g_ptr_array_free(dirs, TRUE);
381 }
382
383 if (status == BT_PLUGIN_STATUS_OK) {
384 BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: "
385 "%!+l", plugin);
386 } else if (status == BT_PLUGIN_STATUS_NOT_FOUND) {
387 BT_LOGI("No plugin found in standard directories and built-in plugins: "
388 "name=\"%s\"", plugin_name);
389 }
390
391 return status;
392 }
393
394 static struct {
395 pthread_mutex_t lock;
396 struct bt_plugin_set *plugin_set;
397 bool recurse;
398 bool fail_on_load_error;
399 enum bt_plugin_status status;
400 } append_all_from_dir_info = {
401 .lock = PTHREAD_MUTEX_INITIALIZER
402 };
403
404 static
405 int nftw_append_all_from_dir(const char *file,
406 const struct stat *sb, int flag, struct FTW *s)
407 {
408 int ret = 0;
409 const char *name = file + s->base;
410
411 /* Check for recursion */
412 if (!append_all_from_dir_info.recurse && s->level > 1) {
413 goto end;
414 }
415
416 switch (flag) {
417 case FTW_F:
418 {
419 const struct bt_plugin_set *plugins_from_file = NULL;
420
421 if (name[0] == '.') {
422 /* Skip hidden files */
423 BT_LOGI("Skipping hidden file: path=\"%s\"", file);
424 goto end;
425 }
426
427 append_all_from_dir_info.status =
428 bt_plugin_find_all_from_file(file,
429 append_all_from_dir_info.fail_on_load_error,
430 &plugins_from_file);
431 if (append_all_from_dir_info.status == BT_PLUGIN_STATUS_OK) {
432 size_t j;
433
434 BT_ASSERT(plugins_from_file);
435
436 for (j = 0; j < plugins_from_file->plugins->len; j++) {
437 struct bt_plugin *plugin =
438 g_ptr_array_index(plugins_from_file->plugins, j);
439
440 BT_LIB_LOGI("Adding plugin to plugin set: "
441 "plugin-path=\"%s\", %![plugin-]+l",
442 file, plugin);
443 bt_plugin_set_add_plugin(
444 append_all_from_dir_info.plugin_set,
445 plugin);
446 }
447
448 bt_object_put_ref(plugins_from_file);
449 goto end;
450 } else if (append_all_from_dir_info.status < 0) {
451 /* bt_plugin_find_all_from_file() logs errors */
452 BT_ASSERT(!plugins_from_file);
453 ret = -1;
454 goto end;
455 }
456
457 /*
458 * Not found in this file: this is no an error; continue
459 * walking the directories.
460 */
461 BT_ASSERT(!plugins_from_file);
462 BT_ASSERT(append_all_from_dir_info.status ==
463 BT_PLUGIN_STATUS_NOT_FOUND);
464 break;
465 }
466 case FTW_DNR:
467 /* Continue to next file / directory. */
468 BT_LOGI("Cannot enter directory: continuing: path=\"%s\"", file);
469 break;
470 case FTW_NS:
471 /* Continue to next file / directory. */
472 BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file);
473 break;
474 }
475
476 end:
477 return ret;
478 }
479
480 static
481 enum bt_plugin_status bt_plugin_create_append_all_from_dir(
482 struct bt_plugin_set *plugin_set, const char *path,
483 bt_bool recurse, bt_bool fail_on_load_error)
484 {
485 int nftw_flags = FTW_PHYS;
486 int ret;
487 enum bt_plugin_status status;
488
489 BT_ASSERT(plugin_set);
490 BT_ASSERT(path);
491 BT_ASSERT(strlen(path) < PATH_MAX);
492 pthread_mutex_lock(&append_all_from_dir_info.lock);
493 append_all_from_dir_info.plugin_set = plugin_set;
494 append_all_from_dir_info.recurse = recurse;
495 append_all_from_dir_info.status = BT_PLUGIN_STATUS_OK;
496 append_all_from_dir_info.fail_on_load_error = fail_on_load_error;
497 ret = nftw(path, nftw_append_all_from_dir,
498 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
499 append_all_from_dir_info.plugin_set = NULL;
500 status = append_all_from_dir_info.status;
501 pthread_mutex_unlock(&append_all_from_dir_info.lock);
502 if (ret) {
503 BT_LOGW_ERRNO("Failed to walk directory",
504 ": path=\"%s\", recurse=%d",
505 path, recurse);
506 status = BT_PLUGIN_STATUS_ERROR;
507 goto end;
508 }
509
510 if (status == BT_PLUGIN_STATUS_NOT_FOUND) {
511 /*
512 * We're just appending in this function; even if
513 * nothing was found, it's still okay from the caller's
514 * perspective.
515 */
516 status = BT_PLUGIN_STATUS_OK;
517 }
518
519 end:
520 return ret;
521 }
522
523 enum bt_plugin_status bt_plugin_find_all_from_dir(const char *path,
524 bt_bool recurse, bt_bool fail_on_load_error,
525 const struct bt_plugin_set **plugin_set_out)
526 {
527 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
528
529 BT_ASSERT_PRE_NON_NULL(plugin_set_out, "Plugin set (output)");
530 BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d",
531 path, recurse);
532 *plugin_set_out = bt_plugin_set_create();
533 if (!*plugin_set_out) {
534 BT_LOGE_STR("Cannot create empty plugin set.");
535 status = BT_PLUGIN_STATUS_NOMEM;
536 goto error;
537 }
538
539 /*
540 * Append found plugins to array (never returns
541 * `BT_PLUGIN_STATUS_NOT_FOUND`)
542 */
543 status = bt_plugin_create_append_all_from_dir((void *) *plugin_set_out,
544 path, recurse, fail_on_load_error);
545 if (status < 0) {
546 /*
547 * bt_plugin_create_append_all_from_dir() handles
548 * `fail_on_load_error`, so this is a "real" error.
549 */
550 BT_LOGW("Cannot append plugins found in directory: "
551 "path=\"%s\", status=%s",
552 path, bt_plugin_status_string(status));
553 goto error;
554 }
555
556 BT_ASSERT(status == BT_PLUGIN_STATUS_OK);
557
558 if ((*plugin_set_out)->plugins->len == 0) {
559 /* Nothing was appended: not found */
560 BT_LOGI("No plugins found in directory: path=\"%s\"", path);
561 status = BT_PLUGIN_STATUS_NOT_FOUND;
562 goto error;
563 }
564
565 BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"",
566 (*plugin_set_out)->plugins->len,
567 (*plugin_set_out)->plugins->len, path);
568 goto end;
569
570 error:
571 BT_ASSERT(status != BT_PLUGIN_STATUS_OK);
572 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
573
574 end:
575 return status;
576 }
577
578 const char *bt_plugin_get_name(const struct bt_plugin *plugin)
579 {
580 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
581 return plugin->info.name_set ? plugin->info.name->str : NULL;
582 }
583
584 const char *bt_plugin_get_author(const struct bt_plugin *plugin)
585 {
586 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
587 return plugin->info.author_set ? plugin->info.author->str : NULL;
588 }
589
590 const char *bt_plugin_get_license(const struct bt_plugin *plugin)
591 {
592 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
593 return plugin->info.license_set ? plugin->info.license->str : NULL;
594 }
595
596 const char *bt_plugin_get_path(const struct bt_plugin *plugin)
597 {
598 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
599 return plugin->info.path_set ? plugin->info.path->str : NULL;
600 }
601
602 const char *bt_plugin_get_description(const struct bt_plugin *plugin)
603 {
604 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
605 return plugin->info.description_set ?
606 plugin->info.description->str : NULL;
607 }
608
609 enum bt_property_availability bt_plugin_get_version(const struct bt_plugin *plugin,
610 unsigned int *major, unsigned int *minor, unsigned int *patch,
611 const char **extra)
612 {
613 enum bt_property_availability avail =
614 BT_PROPERTY_AVAILABILITY_AVAILABLE;
615
616 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
617
618 if (!plugin->info.version_set) {
619 BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin);
620 avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
621 goto end;
622 }
623
624 if (major) {
625 *major = plugin->info.version.major;
626 }
627
628 if (minor) {
629 *minor = plugin->info.version.minor;
630 }
631
632 if (patch) {
633 *patch = plugin->info.version.patch;
634 }
635
636 if (extra) {
637 *extra = plugin->info.version.extra->str;
638 }
639
640 end:
641 return avail;
642 }
643
644 uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin)
645 {
646 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
647 return (uint64_t) plugin->src_comp_classes->len;
648 }
649
650 uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin)
651 {
652 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
653 return (uint64_t) plugin->flt_comp_classes->len;
654 }
655
656 uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin)
657 {
658 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
659 return (uint64_t) plugin->sink_comp_classes->len;
660 }
661
662 static inline
663 struct bt_component_class *borrow_component_class_by_index(
664 const struct bt_plugin *plugin, GPtrArray *comp_classes,
665 uint64_t index)
666 {
667 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
668 BT_ASSERT_PRE_VALID_INDEX(index, comp_classes->len);
669 return g_ptr_array_index(comp_classes, index);
670 }
671
672 const struct bt_component_class_source *
673 bt_plugin_borrow_source_component_class_by_index_const(
674 const struct bt_plugin *plugin, uint64_t index)
675 {
676 return (const void *) borrow_component_class_by_index(plugin,
677 plugin->src_comp_classes, index);
678 }
679
680 const struct bt_component_class_filter *
681 bt_plugin_borrow_filter_component_class_by_index_const(
682 const struct bt_plugin *plugin, uint64_t index)
683 {
684 return (const void *) borrow_component_class_by_index(plugin,
685 plugin->flt_comp_classes, index);
686 }
687
688 const struct bt_component_class_sink *
689 bt_plugin_borrow_sink_component_class_by_index_const(
690 const struct bt_plugin *plugin, uint64_t index)
691 {
692 return (const void *) borrow_component_class_by_index(plugin,
693 plugin->sink_comp_classes, index);
694 }
695
696 static inline
697 struct bt_component_class *borrow_component_class_by_name(
698 const struct bt_plugin *plugin, GPtrArray *comp_classes,
699 const char *name)
700 {
701 struct bt_component_class *comp_class = NULL;
702 size_t i;
703
704 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
705 BT_ASSERT_PRE_NON_NULL(name, "Name");
706
707 for (i = 0; i < comp_classes->len; i++) {
708 struct bt_component_class *comp_class_candidate =
709 g_ptr_array_index(comp_classes, i);
710 const char *comp_class_cand_name =
711 bt_component_class_get_name(comp_class_candidate);
712
713 BT_ASSERT(comp_class_cand_name);
714
715 if (strcmp(name, comp_class_cand_name) == 0) {
716 comp_class = comp_class_candidate;
717 break;
718 }
719 }
720
721 return comp_class;
722 }
723
724 const struct bt_component_class_source *
725 bt_plugin_borrow_source_component_class_by_name_const(
726 const struct bt_plugin *plugin, const char *name)
727 {
728 return (const void *) borrow_component_class_by_name(plugin,
729 plugin->src_comp_classes, name);
730 }
731
732 const struct bt_component_class_filter *
733 bt_plugin_borrow_filter_component_class_by_name_const(
734 const struct bt_plugin *plugin, const char *name)
735 {
736 return (const void *) borrow_component_class_by_name(plugin,
737 plugin->flt_comp_classes, name);
738 }
739
740 const struct bt_component_class_sink *
741 bt_plugin_borrow_sink_component_class_by_name_const(
742 const struct bt_plugin *plugin, const char *name)
743 {
744 return (const void *) borrow_component_class_by_name(plugin,
745 plugin->sink_comp_classes, name);
746 }
747
748 void bt_plugin_get_ref(const struct bt_plugin *plugin)
749 {
750 bt_object_get_ref(plugin);
751 }
752
753 void bt_plugin_put_ref(const struct bt_plugin *plugin)
754 {
755 bt_object_put_ref(plugin);
756 }
757
758 void bt_plugin_set_get_ref(const struct bt_plugin_set *plugin_set)
759 {
760 bt_object_get_ref(plugin_set);
761 }
762
763 void bt_plugin_set_put_ref(const struct bt_plugin_set *plugin_set)
764 {
765 bt_object_put_ref(plugin_set);
766 }
This page took 0.042388 seconds and 3 git commands to generate.