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