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