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