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