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