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