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