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