lib: assign a unique ID to each pre/postcond. and report it on failure
[babeltrace.git] / src / lib / plugin / plugin.c
CommitLineData
33b34c43 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
33b34c43 5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
33b34c43
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/PLUGIN"
c2d9d9cf 9#include "lib/logging.h"
8c1a3187 10
578e048b 11#include "common/assert.h"
d98421f2 12#include "lib/assert-cond.h"
91d81473 13#include "common/macros.h"
578e048b
MJ
14#include "compat/compiler.h"
15#include "common/common.h"
43c59509
PP
16#include <babeltrace2/plugin/plugin-loading.h>
17#include <babeltrace2/graph/component-class.h>
18#include <babeltrace2/error-reporting.h>
578e048b 19#include "lib/graph/component-class.h"
3fadfbc0 20#include <babeltrace2/types.h>
33b34c43 21#include <glib.h>
33b34c43 22#include <unistd.h>
c4f23e30 23#include <stdbool.h>
33b34c43 24#include <stdlib.h>
9ac68eb1 25#include <stdint.h>
8c1a3187 26#include <inttypes.h>
33b34c43 27#include <sys/stat.h>
e1f4c4f7
MJ
28#include <ftw.h>
29#include <pthread.h>
33b34c43 30
578e048b
MJ
31#include "plugin.h"
32#include "plugin-so.h"
d24d5663 33#include "lib/func-status.h"
578e048b 34
b14c7bf1
MJ
35#define PYTHON_PLUGIN_PROVIDER_FILENAME "babeltrace2-python-plugin-provider." G_MODULE_SUFFIX
36#define PYTHON_PLUGIN_PROVIDER_DIR BABELTRACE_PLUGIN_PROVIDERS_DIR
6fbd4105 37#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
91d81473 38#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 39
e1f4c4f7
MJ
40#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
41
7c7301d5
SM
42/* Declare here to make sure definition in both ifdef branches are in sync. */
43static
44int init_python_plugin_provider(void);
45typedef 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
6fbd4105 50#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
578e048b 51#include <plugin/python-plugin-provider.h>
e1f4c4f7 52
33b34c43 53static
7c7301d5
SM
54create_all_from_file_sym_type
55 bt_plugin_python_create_all_from_file_sym =
56 bt_plugin_python_create_all_from_file;
95ef44ce
MJ
57
58static
7c7301d5 59int init_python_plugin_provider(void)
9736d991
PP
60{
61}
6fbd4105
PP
62#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
63static GModule *python_plugin_provider_module;
9736d991 64
6fbd4105 65static
7c7301d5 66create_all_from_file_sym_type bt_plugin_python_create_all_from_file_sym;
33b34c43 67
95ef44ce 68static
870631a2
PP
69int init_python_plugin_provider(void) {
70 int status = BT_FUNC_STATUS_OK;
b14c7bf1
MJ
71 const char *provider_dir_envvar;
72 static const char * const provider_dir_envvar_name = "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR";
73 char *provider_path = NULL;
870631a2 74
5084732e 75 if (bt_plugin_python_create_all_from_file_sym) {
870631a2 76 goto end;
95ef44ce
MJ
77 }
78
3f7d4d90 79 BT_LOGI_STR("Loading Python plugin provider module.");
b14c7bf1
MJ
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);
6fbd4105 97 if (!python_plugin_provider_module) {
9736d991
PP
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 */
a77aed14 103 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
b14c7bf1 104 provider_path, g_module_error());
870631a2 105 goto end;
33b34c43
PP
106 }
107
6fbd4105
PP
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)) {
9736d991
PP
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 */
870631a2
PP
116 BT_LIB_LOGE_APPEND_CAUSE(
117 "Cannot find the Python plugin provider loading symbol: "
9736d991 118 "%s: continuing without Python plugin support: "
8c1a3187 119 "file=\"%s\", symbol=\"%s\"",
9736d991 120 g_module_error(),
b14c7bf1 121 provider_path,
8c1a3187 122 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
870631a2
PP
123 status = BT_FUNC_STATUS_ERROR;
124 goto end;
33b34c43 125 }
8c1a3187 126
9e0bf9b0 127 BT_LOGI("Loaded Python plugin provider module: addr=%p",
8c1a3187 128 python_plugin_provider_module);
870631a2
PP
129
130end:
b14c7bf1
MJ
131 g_free(provider_path);
132
870631a2 133 return status;
33b34c43
PP
134}
135
6fbd4105
PP
136__attribute__((destructor)) static
137void fini_python_plugin_provider(void) {
138 if (python_plugin_provider_module) {
3f7d4d90 139 BT_LOGI("Unloading Python plugin provider module.");
8c1a3187
PP
140
141 if (!g_module_close(python_plugin_provider_module)) {
870631a2
PP
142 /*
143 * This occurs when the library is finalized: do
144 * NOT append an error cause.
145 */
8c1a3187
PP
146 BT_LOGE("Failed to close the Python plugin provider module: %s.",
147 g_module_error());
148 }
149
6fbd4105 150 python_plugin_provider_module = NULL;
6ba0b073 151 }
6ba0b073 152}
6fbd4105 153#endif
6ba0b073 154
e58aeae2 155uint64_t bt_plugin_set_get_plugin_count(const struct bt_plugin_set *plugin_set)
a8ff38ef 156{
d5b13b9b 157 BT_ASSERT_PRE_DEV_PLUGIN_SET_NON_NULL(plugin_set);
d94d92ac 158 return (uint64_t) plugin_set->plugins->len;
a8ff38ef
PP
159}
160
92fed4e1
PP
161const struct bt_plugin *bt_plugin_set_borrow_plugin_by_index_const(
162 const struct bt_plugin_set *plugin_set, uint64_t index)
a8ff38ef 163{
d5b13b9b 164 BT_ASSERT_PRE_DEV_PLUGIN_SET_NON_NULL(plugin_set);
bdb288b3 165 BT_ASSERT_PRE_DEV_VALID_INDEX(index, plugin_set->plugins->len);
d94d92ac 166 return g_ptr_array_index(plugin_set->plugins, index);
a8ff38ef
PP
167}
168
d24d5663 169enum bt_plugin_find_all_from_static_status bt_plugin_find_all_from_static(
9736d991
PP
170 bt_bool fail_on_load_error,
171 const struct bt_plugin_set **plugin_set_out)
6ba0b073 172{
17f3083a
SM
173 BT_ASSERT_PRE_NO_ERROR();
174
8c1a3187 175 /* bt_plugin_so_create_all_from_static() logs errors */
9736d991
PP
176 return bt_plugin_so_create_all_from_static(fail_on_load_error,
177 (void *) plugin_set_out);
6ba0b073
PP
178}
179
d24d5663
PP
180enum bt_plugin_find_all_from_file_status bt_plugin_find_all_from_file(
181 const char *path, bt_bool fail_on_load_error,
9736d991 182 const struct bt_plugin_set **plugin_set_out)
33b34c43 183{
d24d5663 184 enum bt_plugin_find_all_from_file_status status;
33b34c43 185
17f3083a 186 BT_ASSERT_PRE_NO_ERROR();
1778c2a4 187 BT_ASSERT_PRE_NON_NULL("path", path, "Path");
d5b13b9b 188 BT_ASSERT_PRE_PLUGIN_SET_OUT_NON_NULL(path);
3f7d4d90 189 BT_LOGI("Creating plugins from file: path=\"%s\"", path);
6ba0b073 190
55bb57e0 191 /* Try shared object plugins */
9736d991
PP
192 status = bt_plugin_so_create_all_from_file(path, fail_on_load_error,
193 (void *) plugin_set_out);
d24d5663 194 if (status == BT_FUNC_STATUS_OK) {
9736d991
PP
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);
6ba0b073
PP
200 goto end;
201 }
202
d24d5663 203 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991
PP
204 BT_ASSERT(!*plugin_set_out);
205
6fbd4105 206 /* Try Python plugins if support is available */
870631a2
PP
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
6fbd4105 216 if (bt_plugin_python_create_all_from_file_sym) {
870631a2 217 /* Python plugin provider exists */
9736d991
PP
218 status = bt_plugin_python_create_all_from_file_sym(path,
219 fail_on_load_error, (void *) plugin_set_out);
d24d5663 220 if (status == BT_FUNC_STATUS_OK) {
9736d991
PP
221 BT_ASSERT(*plugin_set_out);
222 BT_ASSERT((*plugin_set_out)->plugins->len > 0);
223 goto end;
224 } else if (status < 0) {
870631a2
PP
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 */
9736d991 230 BT_ASSERT(!*plugin_set_out);
6fbd4105
PP
231 goto end;
232 }
9736d991 233
d24d5663 234 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991 235 BT_ASSERT(!*plugin_set_out);
6ba0b073
PP
236 }
237
33b34c43 238end:
d24d5663 239 if (status == BT_FUNC_STATUS_OK) {
3f7d4d90 240 BT_LOGI("Created %u plugins from file: "
8c1a3187 241 "path=\"%s\", count=%u, plugin-set-addr=%p",
9736d991
PP
242 (*plugin_set_out)->plugins->len, path,
243 (*plugin_set_out)->plugins->len,
244 *plugin_set_out);
d24d5663 245 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 246 BT_LOGI("Found no plugins in file: path=\"%s\"", path);
8c1a3187
PP
247 }
248
9736d991 249 return status;
33b34c43
PP
250}
251
577fa92f
PP
252static
253void destroy_gstring(void *data)
1670bffd
PP
254{
255 g_string_free(data, TRUE);
256}
257
577fa92f
PP
258enum 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)
1670bffd 262{
1670bffd 263 char *home_plugin_dir = NULL;
92fed4e1 264 const struct bt_plugin_set *plugin_set = NULL;
1670bffd
PP
265 GPtrArray *dirs = NULL;
266 int ret;
d24d5663 267 int status = BT_FUNC_STATUS_OK;
577fa92f
PP
268 uint64_t dir_i, plugin_i;
269
17f3083a 270 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 271 BT_ASSERT_PRE_PLUGIN_SET_OUT_NON_NULL(plugin_set_out);
577fa92f
PP
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);
1670bffd
PP
277 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
278 if (!dirs) {
870631a2 279 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
d24d5663 280 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
281 goto end;
282 }
283
577fa92f
PP
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
1670bffd
PP
291 /*
292 * Search order is:
293 *
577fa92f 294 * 1. `BABELTRACE_PLUGIN_PATH` environment variable
1670bffd 295 * (colon-separated list of directories)
577fa92f 296 * 2. `~/.local/lib/babeltrace2/plugins`
1670bffd 297 * 3. Default system directory for Babeltrace plugins, usually
577fa92f
PP
298 * `/usr/lib/babeltrace2/plugins` or
299 * `/usr/local/lib/babeltrace2/plugins` if installed locally
1670bffd
PP
300 * 4. Built-in plugins (static)
301 *
302 * Directories are searched non-recursively.
303 */
577fa92f
PP
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 }
1670bffd
PP
315 }
316 }
317
577fa92f
PP
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);
1670bffd 324
577fa92f
PP
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 }
1670bffd 330
577fa92f
PP
331 g_ptr_array_add(dirs, home_plugin_dir_str);
332 }
1670bffd
PP
333 }
334
577fa92f
PP
335 if (find_in_sys_dir) {
336 const char *system_plugin_dir =
337 bt_common_get_system_plugin_path();
1670bffd 338
577fa92f
PP
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 }
1670bffd 348
577fa92f
PP
349 g_ptr_array_add(dirs, system_plugin_dir_str);
350 }
1670bffd
PP
351 }
352
577fa92f
PP
353 for (dir_i = 0; dir_i < dirs->len; dir_i++) {
354 GString *dir = dirs->pdata[dir_i];
1670bffd 355
65300d60 356 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
8c1a3187 357
50ad9320
PP
358 /*
359 * Skip this if the directory does not exist because
c8db3219 360 * bt_plugin_find_all_from_dir() would log a warning.
50ad9320
PP
361 */
362 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
3f7d4d90 363 BT_LOGI("Skipping nonexistent directory path: "
50ad9320
PP
364 "path=\"%s\"", dir->str);
365 continue;
366 }
367
c8db3219 368 /* bt_plugin_find_all_from_dir() logs details/errors */
9736d991
PP
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;
d24d5663 374 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991 375 BT_ASSERT(!plugin_set);
3f7d4d90 376 BT_LOGI("No plugins found in directory: path=\"%s\"",
8c1a3187 377 dir->str);
1670bffd
PP
378 continue;
379 }
380
d24d5663 381 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991 382 BT_ASSERT(plugin_set);
577fa92f
PP
383 BT_LOGI("Found plugins in directory: path=\"%s\", count=%u",
384 dir->str, plugin_set->plugins->len);
9736d991 385
577fa92f
PP
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 }
1670bffd 392
577fa92f
PP
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;
1670bffd 404 }
8c1a3187 405
577fa92f
PP
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
418end:
419 free(home_plugin_dir);
420 bt_object_put_ref(plugin_set);
421
422 if (dirs) {
423 g_ptr_array_free(dirs, TRUE);
1670bffd
PP
424 }
425
9736d991 426 if (status < 0) {
577fa92f
PP
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
445enum 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
17f3083a 454 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
455 BT_ASSERT_PRE_NAME_NON_NULL(plugin_name);
456 BT_ASSERT_PRE_PLUGIN_OUT_NON_NULL(plugin_out);
577fa92f
PP
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);
7aa2e7df 462 status = (enum bt_plugin_find_status) bt_plugin_find_all(find_in_std_env_var, find_in_user_dir,
577fa92f
PP
463 find_in_sys_dir, find_in_static, fail_on_load_error,
464 &plugin_set);
465 if (status != BT_FUNC_STATUS_OK) {
f600f713 466 BT_ASSERT(!plugin_set);
9736d991
PP
467 goto end;
468 }
469
9736d991
PP
470 BT_ASSERT(plugin_set);
471
577fa92f
PP
472 for (i = 0; i < plugin_set->plugins->len; i++) {
473 const struct bt_plugin *plugin = plugin_set->plugins->pdata[i];
9736d991 474
577fa92f
PP
475 if (strcmp(plugin->info.name->str, plugin_name) == 0) {
476 *plugin_out = plugin;
6871026b 477 bt_object_get_ref_no_null_check(*plugin_out);
9736d991 478 goto end;
1670bffd 479 }
1670bffd
PP
480 }
481
d24d5663 482 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991 483
1670bffd 484end:
d24d5663 485 if (status == BT_FUNC_STATUS_OK) {
577fa92f 486 BT_ASSERT(*plugin_out);
3f7d4d90 487 BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: "
577fa92f
PP
488 "%!+l", *plugin_out);
489 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 490 BT_LOGI("No plugin found in standard directories and built-in plugins: "
8c1a3187
PP
491 "name=\"%s\"", plugin_name);
492 }
493
f600f713
FD
494 bt_plugin_set_put_ref(plugin_set);
495
9736d991 496 return status;
1670bffd
PP
497}
498
e1f4c4f7
MJ
499static struct {
500 pthread_mutex_t lock;
501 struct bt_plugin_set *plugin_set;
d94d92ac 502 bool recurse;
9736d991 503 bool fail_on_load_error;
d24d5663 504 int status;
e1f4c4f7
MJ
505} append_all_from_dir_info = {
506 .lock = PTHREAD_MUTEX_INITIALIZER
507};
508
33b34c43 509static
9736d991
PP
510int nftw_append_all_from_dir(const char *file,
511 const struct stat *sb, int flag, struct FTW *s)
33b34c43 512{
e1f4c4f7
MJ
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:
52238017 523 {
9736d991 524 const struct bt_plugin_set *plugins_from_file = NULL;
52238017 525
e1f4c4f7
MJ
526 if (name[0] == '.') {
527 /* Skip hidden files */
3f7d4d90 528 BT_LOGI("Skipping hidden file: path=\"%s\"", file);
e1f4c4f7
MJ
529 goto end;
530 }
52238017 531
9736d991
PP
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);
d24d5663 536 if (append_all_from_dir_info.status == BT_FUNC_STATUS_OK) {
e1f4c4f7
MJ
537 size_t j;
538
9736d991
PP
539 BT_ASSERT(plugins_from_file);
540
e1f4c4f7
MJ
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
3f7d4d90 545 BT_LIB_LOGI("Adding plugin to plugin set: "
d94d92ac
PP
546 "plugin-path=\"%s\", %![plugin-]+l",
547 file, plugin);
92fed4e1
PP
548 bt_plugin_set_add_plugin(
549 append_all_from_dir_info.plugin_set,
550 plugin);
e1f4c4f7 551 }
33b34c43 552
65300d60 553 bt_object_put_ref(plugins_from_file);
9736d991
PP
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;
e1f4c4f7 560 }
9736d991
PP
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 ==
d24d5663 568 BT_FUNC_STATUS_NOT_FOUND);
e1f4c4f7 569 break;
52238017 570 }
e1f4c4f7
MJ
571 case FTW_DNR:
572 /* Continue to next file / directory. */
9736d991 573 BT_LOGI("Cannot enter directory: continuing: path=\"%s\"", file);
e1f4c4f7
MJ
574 break;
575 case FTW_NS:
576 /* Continue to next file / directory. */
3f7d4d90 577 BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file);
e1f4c4f7 578 break;
33b34c43 579 }
e1f4c4f7
MJ
580
581end:
582 return ret;
33b34c43
PP
583}
584
585static
d24d5663
PP
586int 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)
33b34c43 588{
e1f4c4f7 589 int nftw_flags = FTW_PHYS;
9736d991 590 int ret;
d24d5663 591 int status;
037cbd1a 592 struct stat sb;
33b34c43 593
d94d92ac
PP
594 BT_ASSERT(plugin_set);
595 BT_ASSERT(path);
596 BT_ASSERT(strlen(path) < PATH_MAX);
037cbd1a
JR
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);
870631a2 610 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
d5becf10 611 BT_LIB_LOG_LIBBABELTRACE2_NAME,
870631a2
PP
612 "Cannot open directory: path=\"%s\", recurse=%d",
613 path, recurse);
d24d5663 614 status = BT_FUNC_STATUS_ERROR;
037cbd1a
JR
615 goto end;
616 }
617
e1f4c4f7 618 pthread_mutex_lock(&append_all_from_dir_info.lock);
e1f4c4f7
MJ
619 append_all_from_dir_info.plugin_set = plugin_set;
620 append_all_from_dir_info.recurse = recurse;
d24d5663 621 append_all_from_dir_info.status = BT_FUNC_STATUS_OK;
9736d991 622 append_all_from_dir_info.fail_on_load_error = fail_on_load_error;
e1f4c4f7
MJ
623 ret = nftw(path, nftw_append_all_from_dir,
624 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
9736d991
PP
625 append_all_from_dir_info.plugin_set = NULL;
626 status = append_all_from_dir_info.status;
e1f4c4f7 627 pthread_mutex_unlock(&append_all_from_dir_info.lock);
9736d991 628 if (ret) {
870631a2 629 BT_LIB_LOGW_APPEND_CAUSE("Failed to walk directory",
9736d991
PP
630 ": path=\"%s\", recurse=%d",
631 path, recurse);
d24d5663 632 status = BT_FUNC_STATUS_ERROR;
9736d991
PP
633 goto end;
634 }
635
d24d5663 636 if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991
PP
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 */
d24d5663 642 status = BT_FUNC_STATUS_OK;
33b34c43 643 }
8c1a3187 644
9736d991 645end:
060cf347 646 return status;
33b34c43
PP
647}
648
d24d5663
PP
649enum 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,
9736d991 651 const struct bt_plugin_set **plugin_set_out)
33b34c43 652{
d24d5663
PP
653 enum bt_plugin_find_all_from_dir_status status =
654 BT_FUNC_STATUS_OK;
33b34c43 655
17f3083a 656 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 657 BT_ASSERT_PRE_PLUGIN_SET_OUT_NON_NULL(plugin_set_out);
3f7d4d90 658 BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d",
8c1a3187 659 path, recurse);
9736d991
PP
660 *plugin_set_out = bt_plugin_set_create();
661 if (!*plugin_set_out) {
870631a2 662 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
d24d5663 663 status = BT_FUNC_STATUS_MEMORY_ERROR;
a8ff38ef
PP
664 goto error;
665 }
666
9736d991
PP
667 /*
668 * Append found plugins to array (never returns
d24d5663 669 * `BT_FUNC_STATUS_NOT_FOUND`)
9736d991
PP
670 */
671 status = bt_plugin_create_append_all_from_dir((void *) *plugin_set_out,
672 path, recurse, fail_on_load_error);
33b34c43 673 if (status < 0) {
9736d991
PP
674 /*
675 * bt_plugin_create_append_all_from_dir() handles
676 * `fail_on_load_error`, so this is a "real" error.
677 */
870631a2
PP
678 BT_LIB_LOGE_APPEND_CAUSE(
679 "Cannot append plugins found in directory: "
dbd7f7e9 680 "path=\"%s\", status=%s",
d24d5663 681 path, bt_common_func_status_string(status));
33b34c43
PP
682 goto error;
683 }
684
d24d5663 685 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
PP
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);
d24d5663 690 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991
PP
691 goto error;
692 }
693
3f7d4d90 694 BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"",
9736d991
PP
695 (*plugin_set_out)->plugins->len,
696 (*plugin_set_out)->plugins->len, path);
33b34c43
PP
697 goto end;
698
699error:
d24d5663 700 BT_ASSERT(status != BT_FUNC_STATUS_OK);
9736d991 701 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
33b34c43
PP
702
703end:
9736d991 704 return status;
33b34c43
PP
705}
706
92fed4e1 707const char *bt_plugin_get_name(const struct bt_plugin *plugin)
33b34c43 708{
d5b13b9b 709 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac 710 return plugin->info.name_set ? plugin->info.name->str : NULL;
33b34c43
PP
711}
712
92fed4e1 713const char *bt_plugin_get_author(const struct bt_plugin *plugin)
33b34c43 714{
d5b13b9b 715 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac 716 return plugin->info.author_set ? plugin->info.author->str : NULL;
33b34c43
PP
717}
718
92fed4e1 719const char *bt_plugin_get_license(const struct bt_plugin *plugin)
33b34c43 720{
d5b13b9b 721 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac 722 return plugin->info.license_set ? plugin->info.license->str : NULL;
33b34c43
PP
723}
724
92fed4e1 725const char *bt_plugin_get_path(const struct bt_plugin *plugin)
33b34c43 726{
d5b13b9b 727 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac 728 return plugin->info.path_set ? plugin->info.path->str : NULL;
33b34c43
PP
729}
730
92fed4e1 731const char *bt_plugin_get_description(const struct bt_plugin *plugin)
33b34c43 732{
d5b13b9b 733 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac
PP
734 return plugin->info.description_set ?
735 plugin->info.description->str : NULL;
33b34c43
PP
736}
737
92fed4e1 738enum bt_property_availability bt_plugin_get_version(const struct bt_plugin *plugin,
b6de043b
PP
739 unsigned int *major, unsigned int *minor, unsigned int *patch,
740 const char **extra)
741{
d94d92ac
PP
742 enum bt_property_availability avail =
743 BT_PROPERTY_AVAILABILITY_AVAILABLE;
b6de043b 744
d5b13b9b 745 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
8c1a3187
PP
746
747 if (!plugin->info.version_set) {
3f7d4d90 748 BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin);
d94d92ac 749 avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
b6de043b
PP
750 goto end;
751 }
752
753 if (major) {
55bb57e0 754 *major = plugin->info.version.major;
b6de043b
PP
755 }
756
757 if (minor) {
55bb57e0 758 *minor = plugin->info.version.minor;
b6de043b
PP
759 }
760
761 if (patch) {
55bb57e0 762 *patch = plugin->info.version.patch;
b6de043b
PP
763 }
764
765 if (extra) {
55bb57e0 766 *extra = plugin->info.version.extra->str;
b6de043b
PP
767 }
768
769end:
d94d92ac 770 return avail;
b6de043b
PP
771}
772
92fed4e1 773uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin)
33b34c43 774{
d5b13b9b 775 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac 776 return (uint64_t) plugin->src_comp_classes->len;
33b34c43
PP
777}
778
92fed4e1 779uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin)
33b34c43 780{
d5b13b9b 781 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac
PP
782 return (uint64_t) plugin->flt_comp_classes->len;
783}
33b34c43 784
92fed4e1 785uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin)
d94d92ac 786{
d5b13b9b 787 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
d94d92ac
PP
788 return (uint64_t) plugin->sink_comp_classes->len;
789}
8c1a3187 790
d94d92ac
PP
791static inline
792struct bt_component_class *borrow_component_class_by_index(
92fed4e1 793 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac
PP
794 uint64_t index)
795{
d5b13b9b 796 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
bdb288b3 797 BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp_classes->len);
d94d92ac
PP
798 return g_ptr_array_index(comp_classes, index);
799}
33b34c43 800
0d72b8c3 801const struct bt_component_class_source *
3f79b5cf 802bt_plugin_borrow_source_component_class_by_index_const(
92fed4e1 803 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 804{
0d72b8c3 805 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
806 plugin->src_comp_classes, index);
807}
33b34c43 808
0d72b8c3 809const struct bt_component_class_filter *
92fed4e1
PP
810bt_plugin_borrow_filter_component_class_by_index_const(
811 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 812{
0d72b8c3 813 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
814 plugin->flt_comp_classes, index);
815}
816
0d72b8c3 817const struct bt_component_class_sink *
92fed4e1
PP
818bt_plugin_borrow_sink_component_class_by_index_const(
819 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 820{
0d72b8c3 821 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac 822 plugin->sink_comp_classes, index);
33b34c43
PP
823}
824
d94d92ac
PP
825static inline
826struct bt_component_class *borrow_component_class_by_name(
92fed4e1 827 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac 828 const char *name)
33b34c43
PP
829{
830 struct bt_component_class *comp_class = NULL;
831 size_t i;
832
d5b13b9b
PP
833 BT_ASSERT_PRE_DEV_PLUGIN_NON_NULL(plugin);
834 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name);
33b34c43 835
d94d92ac 836 for (i = 0; i < comp_classes->len; i++) {
33b34c43 837 struct bt_component_class *comp_class_candidate =
d94d92ac 838 g_ptr_array_index(comp_classes, i);
33b34c43
PP
839 const char *comp_class_cand_name =
840 bt_component_class_get_name(comp_class_candidate);
33b34c43 841
98b15851 842 BT_ASSERT_DBG(comp_class_cand_name);
33b34c43 843
d94d92ac
PP
844 if (strcmp(name, comp_class_cand_name) == 0) {
845 comp_class = comp_class_candidate;
33b34c43
PP
846 break;
847 }
848 }
849
33b34c43
PP
850 return comp_class;
851}
852
0d72b8c3 853const struct bt_component_class_source *
92fed4e1
PP
854bt_plugin_borrow_source_component_class_by_name_const(
855 const struct bt_plugin *plugin, const char *name)
33b34c43 856{
0d72b8c3 857 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
858 plugin->src_comp_classes, name);
859}
33b34c43 860
0d72b8c3 861const struct bt_component_class_filter *
92fed4e1
PP
862bt_plugin_borrow_filter_component_class_by_name_const(
863 const struct bt_plugin *plugin, const char *name)
d94d92ac 864{
0d72b8c3 865 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
866 plugin->flt_comp_classes, name);
867}
33b34c43 868
0d72b8c3 869const struct bt_component_class_sink *
92fed4e1
PP
870bt_plugin_borrow_sink_component_class_by_name_const(
871 const struct bt_plugin *plugin, const char *name)
d94d92ac 872{
0d72b8c3 873 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac 874 plugin->sink_comp_classes, name);
33b34c43 875}
c5b9b441
PP
876
877void bt_plugin_get_ref(const struct bt_plugin *plugin)
878{
879 bt_object_get_ref(plugin);
880}
881
882void bt_plugin_put_ref(const struct bt_plugin *plugin)
883{
884 bt_object_put_ref(plugin);
885}
886
887void bt_plugin_set_get_ref(const struct bt_plugin_set *plugin_set)
888{
889 bt_object_get_ref(plugin_set);
890}
891
892void 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.126304 seconds and 4 git commands to generate.