Make the Python plugin provider a libtool module
[babeltrace.git] / src / lib / plugin / plugin.c
CommitLineData
33b34c43 1/*
e2f7325d 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
350ad6c1 26#define BT_LOG_TAG "LIB/PLUGIN"
c2d9d9cf 27#include "lib/logging.h"
8c1a3187 28
578e048b
MJ
29#include "common/assert.h"
30#include "lib/assert-pre.h"
91d81473 31#include "common/macros.h"
578e048b
MJ
32#include "compat/compiler.h"
33#include "common/common.h"
3fadfbc0
MJ
34#include <babeltrace2/plugin/plugin-const.h>
35#include <babeltrace2/graph/component-class-const.h>
870631a2 36#include <babeltrace2/current-thread.h>
578e048b 37#include "lib/graph/component-class.h"
3fadfbc0 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>
e1f4c4f7
MJ
45#include <ftw.h>
46#include <pthread.h>
33b34c43 47
578e048b
MJ
48#include "plugin.h"
49#include "plugin-so.h"
d24d5663 50#include "lib/func-status.h"
578e048b 51
b14c7bf1
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
91d81473 55#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 56
e1f4c4f7
MJ
57#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
58
6fbd4105 59#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
578e048b 60#include <plugin/python-plugin-provider.h>
e1f4c4f7 61
33b34c43 62static
d24d5663 63int (*bt_plugin_python_create_all_from_file_sym)(
9736d991
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;
95ef44ce
MJ
67
68static
870631a2 69enum bt_plugin_status init_python_plugin_provider(void)
9736d991
PP
70{
71}
6fbd4105
PP
72#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
73static GModule *python_plugin_provider_module;
9736d991 74
6fbd4105 75static
d24d5663
PP
76int (*bt_plugin_python_create_all_from_file_sym)(
77 const char *path, bool fail_on_load_error,
9736d991 78 struct bt_plugin_set **plugin_set_out);
33b34c43 79
95ef44ce 80static
870631a2
PP
81int init_python_plugin_provider(void) {
82 int status = BT_FUNC_STATUS_OK;
b14c7bf1
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;
870631a2 86
95ef44ce 87 if (bt_plugin_python_create_all_from_file_sym != NULL) {
870631a2 88 goto end;
95ef44ce
MJ
89 }
90
3f7d4d90 91 BT_LOGI_STR("Loading Python plugin provider module.");
b14c7bf1
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) {
9736d991
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 */
a77aed14 115 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
b14c7bf1 116 provider_path, g_module_error());
870631a2 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)) {
9736d991
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 */
870631a2
PP
128 BT_LIB_LOGE_APPEND_CAUSE(
129 "Cannot find the Python plugin provider loading symbol: "
9736d991 130 "%s: continuing without Python plugin support: "
8c1a3187 131 "file=\"%s\", symbol=\"%s\"",
9736d991 132 g_module_error(),
b14c7bf1 133 provider_path,
8c1a3187 134 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
870631a2
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);
870631a2
PP
141
142end:
b14c7bf1
MJ
143 g_free(provider_path);
144
870631a2 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) {
3f7d4d90 151 BT_LOGI("Unloading Python plugin provider module.");
8c1a3187
PP
152
153 if (!g_module_close(python_plugin_provider_module)) {
870631a2
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
e58aeae2 167uint64_t bt_plugin_set_get_plugin_count(const struct bt_plugin_set *plugin_set)
a8ff38ef 168{
bdb288b3 169 BT_ASSERT_PRE_DEV_NON_NULL(plugin_set, "Plugin set");
d94d92ac 170 return (uint64_t) plugin_set->plugins->len;
a8ff38ef
PP
171}
172
92fed4e1
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{
bdb288b3
PP
176 BT_ASSERT_PRE_DEV_NON_NULL(plugin_set, "Plugin set");
177 BT_ASSERT_PRE_DEV_VALID_INDEX(index, plugin_set->plugins->len);
d94d92ac 178 return g_ptr_array_index(plugin_set->plugins, index);
a8ff38ef
PP
179}
180
d24d5663 181enum bt_plugin_find_all_from_static_status bt_plugin_find_all_from_static(
9736d991
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 */
9736d991
PP
186 return bt_plugin_so_create_all_from_static(fail_on_load_error,
187 (void *) plugin_set_out);
6ba0b073
PP
188}
189
d24d5663
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,
9736d991 192 const struct bt_plugin_set **plugin_set_out)
33b34c43 193{
d24d5663 194 enum bt_plugin_find_all_from_file_status status;
33b34c43 195
d94d92ac 196 BT_ASSERT_PRE_NON_NULL(path, "Path");
9736d991 197 BT_ASSERT_PRE_NON_NULL(path, "Plugin set (output)");
3f7d4d90 198 BT_LOGI("Creating plugins from file: path=\"%s\"", path);
6ba0b073 199
55bb57e0 200 /* Try shared object plugins */
9736d991
PP
201 status = bt_plugin_so_create_all_from_file(path, fail_on_load_error,
202 (void *) plugin_set_out);
d24d5663 203 if (status == BT_FUNC_STATUS_OK) {
9736d991
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
d24d5663 212 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991
PP
213 BT_ASSERT(!*plugin_set_out);
214
6fbd4105 215 /* Try Python plugins if support is available */
870631a2
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) {
870631a2 226 /* Python plugin provider exists */
9736d991
PP
227 status = bt_plugin_python_create_all_from_file_sym(path,
228 fail_on_load_error, (void *) plugin_set_out);
d24d5663 229 if (status == BT_FUNC_STATUS_OK) {
9736d991
PP
230 BT_ASSERT(*plugin_set_out);
231 BT_ASSERT((*plugin_set_out)->plugins->len > 0);
232 goto end;
233 } else if (status < 0) {
870631a2
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 */
9736d991 239 BT_ASSERT(!*plugin_set_out);
6fbd4105
PP
240 goto end;
241 }
9736d991 242
d24d5663 243 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991 244 BT_ASSERT(!*plugin_set_out);
6ba0b073
PP
245 }
246
33b34c43 247end:
d24d5663 248 if (status == BT_FUNC_STATUS_OK) {
3f7d4d90 249 BT_LOGI("Created %u plugins from file: "
8c1a3187 250 "path=\"%s\", count=%u, plugin-set-addr=%p",
9736d991
PP
251 (*plugin_set_out)->plugins->len, path,
252 (*plugin_set_out)->plugins->len,
253 *plugin_set_out);
d24d5663 254 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 255 BT_LOGI("Found no plugins in file: path=\"%s\"", path);
8c1a3187
PP
256 }
257
9736d991 258 return status;
33b34c43
PP
259}
260
1670bffd
PP
261static void destroy_gstring(void *data)
262{
263 g_string_free(data, TRUE);
264}
265
d24d5663 266enum bt_plugin_find_status bt_plugin_find(const char *plugin_name,
9736d991 267 bt_bool fail_on_load_error, const struct bt_plugin **plugin_out)
1670bffd
PP
268{
269 const char *system_plugin_dir;
270 char *home_plugin_dir = NULL;
271 const char *envvar;
92fed4e1
PP
272 const struct bt_plugin *plugin = NULL;
273 const struct bt_plugin_set *plugin_set = NULL;
1670bffd
PP
274 GPtrArray *dirs = NULL;
275 int ret;
d24d5663 276 int status = BT_FUNC_STATUS_OK;
a8ff38ef 277 size_t i, j;
1670bffd 278
d94d92ac 279 BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
9736d991 280 BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)");
3f7d4d90 281 BT_LOGI("Finding named plugin in standard directories and built-in plugins: "
8c1a3187 282 "name=\"%s\"", plugin_name);
1670bffd
PP
283 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
284 if (!dirs) {
870631a2 285 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
d24d5663 286 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
287 goto end;
288 }
289
290 /*
291 * Search order is:
292 *
293 * 1. BABELTRACE_PLUGIN_PATH environment variable
294 * (colon-separated list of directories)
d9676d8c 295 * 2. ~/.local/lib/babeltrace2/plugins
1670bffd 296 * 3. Default system directory for Babeltrace plugins, usually
d9676d8c
PP
297 * /usr/lib/babeltrace2/plugins or
298 * /usr/local/lib/babeltrace2/plugins if installed
1670bffd
PP
299 * locally
300 * 4. Built-in plugins (static)
301 *
302 * Directories are searched non-recursively.
303 */
304 envvar = getenv("BABELTRACE_PLUGIN_PATH");
305 if (envvar) {
306 ret = bt_common_append_plugin_path_dirs(envvar, dirs);
307 if (ret) {
870631a2
PP
308 BT_LIB_LOGE_APPEND_CAUSE(
309 "Failed to append plugin path to array of directories.");
d24d5663 310 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
311 goto end;
312 }
313 }
314
86d8b7b8 315 home_plugin_dir = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
1670bffd 316 if (home_plugin_dir) {
9736d991 317 GString *home_plugin_dir_str = g_string_new(home_plugin_dir);
1670bffd
PP
318
319 if (!home_plugin_dir_str) {
870631a2 320 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
d24d5663 321 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
322 goto end;
323 }
324
325 g_ptr_array_add(dirs, home_plugin_dir_str);
326 }
327
328 system_plugin_dir = bt_common_get_system_plugin_path();
329 if (system_plugin_dir) {
330 GString *system_plugin_dir_str =
331 g_string_new(system_plugin_dir);
332
333 if (!system_plugin_dir_str) {
870631a2 334 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
d24d5663 335 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
336 goto end;
337 }
338
339 g_ptr_array_add(dirs, system_plugin_dir_str);
340 }
341
342 for (i = 0; i < dirs->len; i++) {
343 GString *dir = g_ptr_array_index(dirs, i);
344
65300d60 345 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
8c1a3187 346
50ad9320
PP
347 /*
348 * Skip this if the directory does not exist because
c8db3219 349 * bt_plugin_find_all_from_dir() would log a warning.
50ad9320
PP
350 */
351 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
3f7d4d90 352 BT_LOGI("Skipping nonexistent directory path: "
50ad9320
PP
353 "path=\"%s\"", dir->str);
354 continue;
355 }
356
c8db3219 357 /* bt_plugin_find_all_from_dir() logs details/errors */
9736d991
PP
358 status = bt_plugin_find_all_from_dir(dir->str, BT_FALSE,
359 fail_on_load_error, &plugin_set);
360 if (status < 0) {
361 BT_ASSERT(!plugin_set);
362 goto end;
d24d5663 363 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991 364 BT_ASSERT(!plugin_set);
3f7d4d90 365 BT_LOGI("No plugins found in directory: path=\"%s\"",
8c1a3187 366 dir->str);
1670bffd
PP
367 continue;
368 }
369
d24d5663 370 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
PP
371 BT_ASSERT(plugin_set);
372
a8ff38ef 373 for (j = 0; j < plugin_set->plugins->len; j++) {
92fed4e1 374 const struct bt_plugin *candidate_plugin =
a8ff38ef 375 g_ptr_array_index(plugin_set->plugins, j);
1670bffd 376
a8ff38ef
PP
377 if (strcmp(bt_plugin_get_name(candidate_plugin),
378 plugin_name) == 0) {
3f7d4d90 379 BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"",
8c1a3187 380 plugin_name, dir->str);
9736d991
PP
381 *plugin_out = candidate_plugin;
382 bt_object_get_no_null_check(*plugin_out);
1670bffd
PP
383 goto end;
384 }
1670bffd 385 }
8c1a3187 386
3f7d4d90 387 BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"",
8c1a3187 388 plugin_name, dir->str);
1670bffd
PP
389 }
390
9736d991
PP
391 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
392 status = bt_plugin_find_all_from_static(fail_on_load_error,
393 &plugin_set);
394 if (status < 0) {
395 BT_ASSERT(!plugin_set);
396 goto end;
d24d5663 397 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991
PP
398 BT_ASSERT(!plugin_set);
399 BT_LOGI_STR("No plugins found in built-in plugins.");
400 goto end;
401 }
402
d24d5663 403 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
PP
404 BT_ASSERT(plugin_set);
405
406 for (j = 0; j < plugin_set->plugins->len; j++) {
407 const struct bt_plugin *candidate_plugin =
408 g_ptr_array_index(plugin_set->plugins, j);
409
410 if (strcmp(bt_plugin_get_name(candidate_plugin),
411 plugin_name) == 0) {
412 BT_LOGI("Plugin found in built-in plugins: "
413 "name=\"%s\"", plugin_name);
414 *plugin_out = candidate_plugin;
415 bt_object_get_no_null_check(*plugin_out);
416 goto end;
1670bffd 417 }
1670bffd
PP
418 }
419
d24d5663 420 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991 421
1670bffd
PP
422end:
423 free(home_plugin_dir);
65300d60 424 bt_object_put_ref(plugin_set);
1670bffd
PP
425
426 if (dirs) {
427 g_ptr_array_free(dirs, TRUE);
428 }
429
d24d5663 430 if (status == BT_FUNC_STATUS_OK) {
3f7d4d90 431 BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: "
d94d92ac 432 "%!+l", plugin);
d24d5663 433 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 434 BT_LOGI("No plugin found in standard directories and built-in plugins: "
8c1a3187
PP
435 "name=\"%s\"", plugin_name);
436 }
437
9736d991 438 return status;
1670bffd
PP
439}
440
e1f4c4f7
MJ
441static struct {
442 pthread_mutex_t lock;
443 struct bt_plugin_set *plugin_set;
d94d92ac 444 bool recurse;
9736d991 445 bool fail_on_load_error;
d24d5663 446 int status;
e1f4c4f7
MJ
447} append_all_from_dir_info = {
448 .lock = PTHREAD_MUTEX_INITIALIZER
449};
450
33b34c43 451static
9736d991
PP
452int nftw_append_all_from_dir(const char *file,
453 const struct stat *sb, int flag, struct FTW *s)
33b34c43 454{
e1f4c4f7
MJ
455 int ret = 0;
456 const char *name = file + s->base;
457
458 /* Check for recursion */
459 if (!append_all_from_dir_info.recurse && s->level > 1) {
460 goto end;
461 }
462
463 switch (flag) {
464 case FTW_F:
52238017 465 {
9736d991 466 const struct bt_plugin_set *plugins_from_file = NULL;
52238017 467
e1f4c4f7
MJ
468 if (name[0] == '.') {
469 /* Skip hidden files */
3f7d4d90 470 BT_LOGI("Skipping hidden file: path=\"%s\"", file);
e1f4c4f7
MJ
471 goto end;
472 }
52238017 473
9736d991
PP
474 append_all_from_dir_info.status =
475 bt_plugin_find_all_from_file(file,
476 append_all_from_dir_info.fail_on_load_error,
477 &plugins_from_file);
d24d5663 478 if (append_all_from_dir_info.status == BT_FUNC_STATUS_OK) {
e1f4c4f7
MJ
479 size_t j;
480
9736d991
PP
481 BT_ASSERT(plugins_from_file);
482
e1f4c4f7
MJ
483 for (j = 0; j < plugins_from_file->plugins->len; j++) {
484 struct bt_plugin *plugin =
485 g_ptr_array_index(plugins_from_file->plugins, j);
486
3f7d4d90 487 BT_LIB_LOGI("Adding plugin to plugin set: "
d94d92ac
PP
488 "plugin-path=\"%s\", %![plugin-]+l",
489 file, plugin);
92fed4e1
PP
490 bt_plugin_set_add_plugin(
491 append_all_from_dir_info.plugin_set,
492 plugin);
e1f4c4f7 493 }
33b34c43 494
65300d60 495 bt_object_put_ref(plugins_from_file);
9736d991
PP
496 goto end;
497 } else if (append_all_from_dir_info.status < 0) {
498 /* bt_plugin_find_all_from_file() logs errors */
499 BT_ASSERT(!plugins_from_file);
500 ret = -1;
501 goto end;
e1f4c4f7 502 }
9736d991
PP
503
504 /*
505 * Not found in this file: this is no an error; continue
506 * walking the directories.
507 */
508 BT_ASSERT(!plugins_from_file);
509 BT_ASSERT(append_all_from_dir_info.status ==
d24d5663 510 BT_FUNC_STATUS_NOT_FOUND);
e1f4c4f7 511 break;
52238017 512 }
e1f4c4f7
MJ
513 case FTW_DNR:
514 /* Continue to next file / directory. */
9736d991 515 BT_LOGI("Cannot enter directory: continuing: path=\"%s\"", file);
e1f4c4f7
MJ
516 break;
517 case FTW_NS:
518 /* Continue to next file / directory. */
3f7d4d90 519 BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file);
e1f4c4f7 520 break;
33b34c43 521 }
e1f4c4f7
MJ
522
523end:
524 return ret;
33b34c43
PP
525}
526
527static
d24d5663
PP
528int bt_plugin_create_append_all_from_dir(struct bt_plugin_set *plugin_set,
529 const char *path, bt_bool recurse, bt_bool fail_on_load_error)
33b34c43 530{
e1f4c4f7 531 int nftw_flags = FTW_PHYS;
9736d991 532 int ret;
d24d5663 533 int status;
037cbd1a 534 struct stat sb;
33b34c43 535
d94d92ac
PP
536 BT_ASSERT(plugin_set);
537 BT_ASSERT(path);
538 BT_ASSERT(strlen(path) < PATH_MAX);
037cbd1a
JR
539
540 /*
541 * Make sure that path exists and is accessible.
542 * This is necessary since Cygwin implementation of nftw() is not POSIX
543 * compliant. Cygwin nftw() implementation does not fail on non-existent
544 * path with ENOENT. Instead, it flags the directory as FTW_NS. FTW_NS during
545 * nftw_append_all_from_dir is not treated as an error since we are
546 * traversing the tree for plugin discovery.
547 */
548 if (stat(path, &sb)) {
549 BT_LOGW_ERRNO("Cannot open directory",
550 ": path=\"%s\", recurse=%d",
551 path, recurse);
870631a2
PP
552 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
553 "Babeltrace library",
554 "Cannot open directory: path=\"%s\", recurse=%d",
555 path, recurse);
d24d5663 556 status = BT_FUNC_STATUS_ERROR;
037cbd1a
JR
557 goto end;
558 }
559
e1f4c4f7 560 pthread_mutex_lock(&append_all_from_dir_info.lock);
e1f4c4f7
MJ
561 append_all_from_dir_info.plugin_set = plugin_set;
562 append_all_from_dir_info.recurse = recurse;
d24d5663 563 append_all_from_dir_info.status = BT_FUNC_STATUS_OK;
9736d991 564 append_all_from_dir_info.fail_on_load_error = fail_on_load_error;
e1f4c4f7
MJ
565 ret = nftw(path, nftw_append_all_from_dir,
566 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
9736d991
PP
567 append_all_from_dir_info.plugin_set = NULL;
568 status = append_all_from_dir_info.status;
e1f4c4f7 569 pthread_mutex_unlock(&append_all_from_dir_info.lock);
9736d991 570 if (ret) {
870631a2 571 BT_LIB_LOGW_APPEND_CAUSE("Failed to walk directory",
9736d991
PP
572 ": path=\"%s\", recurse=%d",
573 path, recurse);
d24d5663 574 status = BT_FUNC_STATUS_ERROR;
9736d991
PP
575 goto end;
576 }
577
d24d5663 578 if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991
PP
579 /*
580 * We're just appending in this function; even if
581 * nothing was found, it's still okay from the caller's
582 * perspective.
583 */
d24d5663 584 status = BT_FUNC_STATUS_OK;
33b34c43 585 }
8c1a3187 586
9736d991 587end:
060cf347 588 return status;
33b34c43
PP
589}
590
d24d5663
PP
591enum bt_plugin_find_all_from_dir_status bt_plugin_find_all_from_dir(
592 const char *path, bt_bool recurse, bt_bool fail_on_load_error,
9736d991 593 const struct bt_plugin_set **plugin_set_out)
33b34c43 594{
d24d5663
PP
595 enum bt_plugin_find_all_from_dir_status status =
596 BT_FUNC_STATUS_OK;
33b34c43 597
9736d991 598 BT_ASSERT_PRE_NON_NULL(plugin_set_out, "Plugin set (output)");
3f7d4d90 599 BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d",
8c1a3187 600 path, recurse);
9736d991
PP
601 *plugin_set_out = bt_plugin_set_create();
602 if (!*plugin_set_out) {
870631a2 603 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
d24d5663 604 status = BT_FUNC_STATUS_MEMORY_ERROR;
a8ff38ef
PP
605 goto error;
606 }
607
9736d991
PP
608 /*
609 * Append found plugins to array (never returns
d24d5663 610 * `BT_FUNC_STATUS_NOT_FOUND`)
9736d991
PP
611 */
612 status = bt_plugin_create_append_all_from_dir((void *) *plugin_set_out,
613 path, recurse, fail_on_load_error);
33b34c43 614 if (status < 0) {
9736d991
PP
615 /*
616 * bt_plugin_create_append_all_from_dir() handles
617 * `fail_on_load_error`, so this is a "real" error.
618 */
870631a2
PP
619 BT_LIB_LOGE_APPEND_CAUSE(
620 "Cannot append plugins found in directory: "
dbd7f7e9 621 "path=\"%s\", status=%s",
d24d5663 622 path, bt_common_func_status_string(status));
33b34c43
PP
623 goto error;
624 }
625
d24d5663 626 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
PP
627
628 if ((*plugin_set_out)->plugins->len == 0) {
629 /* Nothing was appended: not found */
630 BT_LOGI("No plugins found in directory: path=\"%s\"", path);
d24d5663 631 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991
PP
632 goto error;
633 }
634
3f7d4d90 635 BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"",
9736d991
PP
636 (*plugin_set_out)->plugins->len,
637 (*plugin_set_out)->plugins->len, path);
33b34c43
PP
638 goto end;
639
640error:
d24d5663 641 BT_ASSERT(status != BT_FUNC_STATUS_OK);
9736d991 642 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
33b34c43
PP
643
644end:
9736d991 645 return status;
33b34c43
PP
646}
647
92fed4e1 648const char *bt_plugin_get_name(const struct bt_plugin *plugin)
33b34c43 649{
bdb288b3 650 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac 651 return plugin->info.name_set ? plugin->info.name->str : NULL;
33b34c43
PP
652}
653
92fed4e1 654const char *bt_plugin_get_author(const struct bt_plugin *plugin)
33b34c43 655{
bdb288b3 656 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac 657 return plugin->info.author_set ? plugin->info.author->str : NULL;
33b34c43
PP
658}
659
92fed4e1 660const char *bt_plugin_get_license(const struct bt_plugin *plugin)
33b34c43 661{
bdb288b3 662 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac 663 return plugin->info.license_set ? plugin->info.license->str : NULL;
33b34c43
PP
664}
665
92fed4e1 666const char *bt_plugin_get_path(const struct bt_plugin *plugin)
33b34c43 667{
bdb288b3 668 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac 669 return plugin->info.path_set ? plugin->info.path->str : NULL;
33b34c43
PP
670}
671
92fed4e1 672const char *bt_plugin_get_description(const struct bt_plugin *plugin)
33b34c43 673{
bdb288b3 674 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac
PP
675 return plugin->info.description_set ?
676 plugin->info.description->str : NULL;
33b34c43
PP
677}
678
92fed4e1 679enum bt_property_availability bt_plugin_get_version(const struct bt_plugin *plugin,
b6de043b
PP
680 unsigned int *major, unsigned int *minor, unsigned int *patch,
681 const char **extra)
682{
d94d92ac
PP
683 enum bt_property_availability avail =
684 BT_PROPERTY_AVAILABILITY_AVAILABLE;
b6de043b 685
bdb288b3 686 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
8c1a3187
PP
687
688 if (!plugin->info.version_set) {
3f7d4d90 689 BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin);
d94d92ac 690 avail = BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
b6de043b
PP
691 goto end;
692 }
693
694 if (major) {
55bb57e0 695 *major = plugin->info.version.major;
b6de043b
PP
696 }
697
698 if (minor) {
55bb57e0 699 *minor = plugin->info.version.minor;
b6de043b
PP
700 }
701
702 if (patch) {
55bb57e0 703 *patch = plugin->info.version.patch;
b6de043b
PP
704 }
705
706 if (extra) {
55bb57e0 707 *extra = plugin->info.version.extra->str;
b6de043b
PP
708 }
709
710end:
d94d92ac 711 return avail;
b6de043b
PP
712}
713
92fed4e1 714uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin)
33b34c43 715{
bdb288b3 716 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac 717 return (uint64_t) plugin->src_comp_classes->len;
33b34c43
PP
718}
719
92fed4e1 720uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin)
33b34c43 721{
bdb288b3 722 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac
PP
723 return (uint64_t) plugin->flt_comp_classes->len;
724}
33b34c43 725
92fed4e1 726uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin)
d94d92ac 727{
bdb288b3 728 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
d94d92ac
PP
729 return (uint64_t) plugin->sink_comp_classes->len;
730}
8c1a3187 731
d94d92ac
PP
732static inline
733struct bt_component_class *borrow_component_class_by_index(
92fed4e1 734 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac
PP
735 uint64_t index)
736{
bdb288b3
PP
737 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
738 BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp_classes->len);
d94d92ac
PP
739 return g_ptr_array_index(comp_classes, index);
740}
33b34c43 741
0d72b8c3 742const struct bt_component_class_source *
3f79b5cf 743bt_plugin_borrow_source_component_class_by_index_const(
92fed4e1 744 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 745{
0d72b8c3 746 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
747 plugin->src_comp_classes, index);
748}
33b34c43 749
0d72b8c3 750const struct bt_component_class_filter *
92fed4e1
PP
751bt_plugin_borrow_filter_component_class_by_index_const(
752 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 753{
0d72b8c3 754 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
755 plugin->flt_comp_classes, index);
756}
757
0d72b8c3 758const struct bt_component_class_sink *
92fed4e1
PP
759bt_plugin_borrow_sink_component_class_by_index_const(
760 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 761{
0d72b8c3 762 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac 763 plugin->sink_comp_classes, index);
33b34c43
PP
764}
765
d94d92ac
PP
766static inline
767struct bt_component_class *borrow_component_class_by_name(
92fed4e1 768 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac 769 const char *name)
33b34c43
PP
770{
771 struct bt_component_class *comp_class = NULL;
772 size_t i;
773
bdb288b3
PP
774 BT_ASSERT_PRE_DEV_NON_NULL(plugin, "Plugin");
775 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
33b34c43 776
d94d92ac 777 for (i = 0; i < comp_classes->len; i++) {
33b34c43 778 struct bt_component_class *comp_class_candidate =
d94d92ac 779 g_ptr_array_index(comp_classes, i);
33b34c43
PP
780 const char *comp_class_cand_name =
781 bt_component_class_get_name(comp_class_candidate);
33b34c43 782
f6ccaed9 783 BT_ASSERT(comp_class_cand_name);
33b34c43 784
d94d92ac
PP
785 if (strcmp(name, comp_class_cand_name) == 0) {
786 comp_class = comp_class_candidate;
33b34c43
PP
787 break;
788 }
789 }
790
33b34c43
PP
791 return comp_class;
792}
793
0d72b8c3 794const struct bt_component_class_source *
92fed4e1
PP
795bt_plugin_borrow_source_component_class_by_name_const(
796 const struct bt_plugin *plugin, const char *name)
33b34c43 797{
0d72b8c3 798 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
799 plugin->src_comp_classes, name);
800}
33b34c43 801
0d72b8c3 802const struct bt_component_class_filter *
92fed4e1
PP
803bt_plugin_borrow_filter_component_class_by_name_const(
804 const struct bt_plugin *plugin, const char *name)
d94d92ac 805{
0d72b8c3 806 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
807 plugin->flt_comp_classes, name);
808}
33b34c43 809
0d72b8c3 810const struct bt_component_class_sink *
92fed4e1
PP
811bt_plugin_borrow_sink_component_class_by_name_const(
812 const struct bt_plugin *plugin, const char *name)
d94d92ac 813{
0d72b8c3 814 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac 815 plugin->sink_comp_classes, name);
33b34c43 816}
c5b9b441
PP
817
818void bt_plugin_get_ref(const struct bt_plugin *plugin)
819{
820 bt_object_get_ref(plugin);
821}
822
823void bt_plugin_put_ref(const struct bt_plugin *plugin)
824{
825 bt_object_put_ref(plugin);
826}
827
828void bt_plugin_set_get_ref(const struct bt_plugin_set *plugin_set)
829{
830 bt_object_get_ref(plugin_set);
831}
832
833void bt_plugin_set_put_ref(const struct bt_plugin_set *plugin_set)
834{
835 bt_object_put_ref(plugin_set);
836}
This page took 0.0998869999999999 seconds and 4 git commands to generate.