lib: strictly type function return status enumerations
[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>
578e048b 36#include "lib/graph/component-class.h"
3fadfbc0 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>
e1f4c4f7
MJ
44#include <ftw.h>
45#include <pthread.h>
33b34c43 46
578e048b
MJ
47#include "plugin.h"
48#include "plugin-so.h"
d24d5663 49#include "lib/func-status.h"
578e048b 50
a12f3d62 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
91d81473 53#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 54
e1f4c4f7
MJ
55#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
56
6fbd4105 57#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
578e048b 58#include <plugin/python-plugin-provider.h>
e1f4c4f7 59
33b34c43 60static
d24d5663 61int (*bt_plugin_python_create_all_from_file_sym)(
9736d991
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;
95ef44ce
MJ
65
66static
9736d991
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;
9736d991 72
6fbd4105 73static
d24d5663
PP
74int (*bt_plugin_python_create_all_from_file_sym)(
75 const char *path, bool fail_on_load_error,
9736d991 76 struct bt_plugin_set **plugin_set_out);
33b34c43 77
95ef44ce 78static
6fbd4105 79void init_python_plugin_provider(void) {
95ef44ce
MJ
80 if (bt_plugin_python_create_all_from_file_sym != NULL) {
81 return;
82 }
83
3f7d4d90 84 BT_LOGI_STR("Loading Python plugin provider module.");
6fbd4105 85 python_plugin_provider_module =
c92fb666 86 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
6fbd4105 87 if (!python_plugin_provider_module) {
9736d991
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 */
a77aed14
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)) {
9736d991
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\"",
9736d991 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) {
3f7d4d90 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
d94d92ac 134uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
a8ff38ef 135{
d94d92ac
PP
136 BT_ASSERT_PRE_NON_NULL(plugin_set, "Plugin set");
137 return (uint64_t) plugin_set->plugins->len;
a8ff38ef
PP
138}
139
92fed4e1
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{
d94d92ac
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
d24d5663 148enum bt_plugin_find_all_from_static_status bt_plugin_find_all_from_static(
9736d991
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 */
9736d991
PP
153 return bt_plugin_so_create_all_from_static(fail_on_load_error,
154 (void *) plugin_set_out);
6ba0b073
PP
155}
156
d24d5663
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,
9736d991 159 const struct bt_plugin_set **plugin_set_out)
33b34c43 160{
d24d5663 161 enum bt_plugin_find_all_from_file_status status;
33b34c43 162
d94d92ac 163 BT_ASSERT_PRE_NON_NULL(path, "Path");
9736d991 164 BT_ASSERT_PRE_NON_NULL(path, "Plugin set (output)");
3f7d4d90 165 BT_LOGI("Creating plugins from file: path=\"%s\"", path);
6ba0b073 166
55bb57e0 167 /* Try shared object plugins */
9736d991
PP
168 status = bt_plugin_so_create_all_from_file(path, fail_on_load_error,
169 (void *) plugin_set_out);
d24d5663 170 if (status == BT_FUNC_STATUS_OK) {
9736d991
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
d24d5663 179 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991
PP
180 BT_ASSERT(!*plugin_set_out);
181
6fbd4105 182 /* Try Python plugins if support is available */
95ef44ce 183 init_python_plugin_provider();
6fbd4105 184 if (bt_plugin_python_create_all_from_file_sym) {
9736d991
PP
185 status = bt_plugin_python_create_all_from_file_sym(path,
186 fail_on_load_error, (void *) plugin_set_out);
d24d5663 187 if (status == BT_FUNC_STATUS_OK) {
9736d991
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 }
9736d991 195
d24d5663 196 BT_ASSERT(status == BT_FUNC_STATUS_NOT_FOUND);
9736d991 197 BT_ASSERT(!*plugin_set_out);
6ba0b073
PP
198 }
199
33b34c43 200end:
d24d5663 201 if (status == BT_FUNC_STATUS_OK) {
3f7d4d90 202 BT_LOGI("Created %u plugins from file: "
8c1a3187 203 "path=\"%s\", count=%u, plugin-set-addr=%p",
9736d991
PP
204 (*plugin_set_out)->plugins->len, path,
205 (*plugin_set_out)->plugins->len,
206 *plugin_set_out);
d24d5663 207 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 208 BT_LOGI("Found no plugins in file: path=\"%s\"", path);
8c1a3187
PP
209 }
210
9736d991 211 return status;
33b34c43
PP
212}
213
1670bffd
PP
214static void destroy_gstring(void *data)
215{
216 g_string_free(data, TRUE);
217}
218
d24d5663 219enum bt_plugin_find_status bt_plugin_find(const char *plugin_name,
9736d991 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;
92fed4e1
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;
d24d5663 229 int status = BT_FUNC_STATUS_OK;
a8ff38ef 230 size_t i, j;
1670bffd 231
d94d92ac 232 BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
9736d991 233 BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)");
3f7d4d90 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.");
d24d5663 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)
d9676d8c 248 * 2. ~/.local/lib/babeltrace2/plugins
1670bffd 249 * 3. Default system directory for Babeltrace plugins, usually
d9676d8c
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.");
d24d5663 262 status = BT_FUNC_STATUS_MEMORY_ERROR;
1670bffd
PP
263 goto end;
264 }
265 }
266
86d8b7b8 267 home_plugin_dir = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
1670bffd 268 if (home_plugin_dir) {
9736d991 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.");
d24d5663 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.");
d24d5663 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
65300d60 297 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
8c1a3187 298
50ad9320
PP
299 /*
300 * Skip this if the directory does not exist because
c8db3219 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)) {
3f7d4d90 304 BT_LOGI("Skipping nonexistent directory path: "
50ad9320
PP
305 "path=\"%s\"", dir->str);
306 continue;
307 }
308
c8db3219 309 /* bt_plugin_find_all_from_dir() logs details/errors */
9736d991
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;
d24d5663 315 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991 316 BT_ASSERT(!plugin_set);
3f7d4d90 317 BT_LOGI("No plugins found in directory: path=\"%s\"",
8c1a3187 318 dir->str);
1670bffd
PP
319 continue;
320 }
321
d24d5663 322 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
PP
323 BT_ASSERT(plugin_set);
324
a8ff38ef 325 for (j = 0; j < plugin_set->plugins->len; j++) {
92fed4e1 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) {
3f7d4d90 331 BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"",
8c1a3187 332 plugin_name, dir->str);
9736d991
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
3f7d4d90 339 BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"",
8c1a3187 340 plugin_name, dir->str);
1670bffd
PP
341 }
342
9736d991
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;
d24d5663 349 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991
PP
350 BT_ASSERT(!plugin_set);
351 BT_LOGI_STR("No plugins found in built-in plugins.");
352 goto end;
353 }
354
d24d5663 355 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
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
d24d5663 372 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991 373
1670bffd
PP
374end:
375 free(home_plugin_dir);
65300d60 376 bt_object_put_ref(plugin_set);
1670bffd
PP
377
378 if (dirs) {
379 g_ptr_array_free(dirs, TRUE);
380 }
381
d24d5663 382 if (status == BT_FUNC_STATUS_OK) {
3f7d4d90 383 BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: "
d94d92ac 384 "%!+l", plugin);
d24d5663 385 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
3f7d4d90 386 BT_LOGI("No plugin found in standard directories and built-in plugins: "
8c1a3187
PP
387 "name=\"%s\"", plugin_name);
388 }
389
9736d991 390 return status;
1670bffd
PP
391}
392
e1f4c4f7
MJ
393static struct {
394 pthread_mutex_t lock;
395 struct bt_plugin_set *plugin_set;
d94d92ac 396 bool recurse;
9736d991 397 bool fail_on_load_error;
d24d5663 398 int status;
e1f4c4f7
MJ
399} append_all_from_dir_info = {
400 .lock = PTHREAD_MUTEX_INITIALIZER
401};
402
33b34c43 403static
9736d991
PP
404int nftw_append_all_from_dir(const char *file,
405 const struct stat *sb, int flag, struct FTW *s)
33b34c43 406{
e1f4c4f7
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:
52238017 417 {
9736d991 418 const struct bt_plugin_set *plugins_from_file = NULL;
52238017 419
e1f4c4f7
MJ
420 if (name[0] == '.') {
421 /* Skip hidden files */
3f7d4d90 422 BT_LOGI("Skipping hidden file: path=\"%s\"", file);
e1f4c4f7
MJ
423 goto end;
424 }
52238017 425
9736d991
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);
d24d5663 430 if (append_all_from_dir_info.status == BT_FUNC_STATUS_OK) {
e1f4c4f7
MJ
431 size_t j;
432
9736d991
PP
433 BT_ASSERT(plugins_from_file);
434
e1f4c4f7
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
3f7d4d90 439 BT_LIB_LOGI("Adding plugin to plugin set: "
d94d92ac
PP
440 "plugin-path=\"%s\", %![plugin-]+l",
441 file, plugin);
92fed4e1
PP
442 bt_plugin_set_add_plugin(
443 append_all_from_dir_info.plugin_set,
444 plugin);
e1f4c4f7 445 }
33b34c43 446
65300d60 447 bt_object_put_ref(plugins_from_file);
9736d991
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;
e1f4c4f7 454 }
9736d991
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 ==
d24d5663 462 BT_FUNC_STATUS_NOT_FOUND);
e1f4c4f7 463 break;
52238017 464 }
e1f4c4f7
MJ
465 case FTW_DNR:
466 /* Continue to next file / directory. */
9736d991 467 BT_LOGI("Cannot enter directory: continuing: path=\"%s\"", file);
e1f4c4f7
MJ
468 break;
469 case FTW_NS:
470 /* Continue to next file / directory. */
3f7d4d90 471 BT_LOGI("Cannot get file information: continuing: path=\"%s\"", file);
e1f4c4f7 472 break;
33b34c43 473 }
e1f4c4f7
MJ
474
475end:
476 return ret;
33b34c43
PP
477}
478
479static
d24d5663
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{
e1f4c4f7 483 int nftw_flags = FTW_PHYS;
9736d991 484 int ret;
d24d5663 485 int status;
037cbd1a 486 struct stat sb;
33b34c43 487
d94d92ac
PP
488 BT_ASSERT(plugin_set);
489 BT_ASSERT(path);
490 BT_ASSERT(strlen(path) < PATH_MAX);
037cbd1a
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);
d24d5663 504 status = BT_FUNC_STATUS_ERROR;
037cbd1a
JR
505 goto end;
506 }
507
e1f4c4f7 508 pthread_mutex_lock(&append_all_from_dir_info.lock);
e1f4c4f7
MJ
509 append_all_from_dir_info.plugin_set = plugin_set;
510 append_all_from_dir_info.recurse = recurse;
d24d5663 511 append_all_from_dir_info.status = BT_FUNC_STATUS_OK;
9736d991 512 append_all_from_dir_info.fail_on_load_error = fail_on_load_error;
e1f4c4f7
MJ
513 ret = nftw(path, nftw_append_all_from_dir,
514 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
9736d991
PP
515 append_all_from_dir_info.plugin_set = NULL;
516 status = append_all_from_dir_info.status;
e1f4c4f7 517 pthread_mutex_unlock(&append_all_from_dir_info.lock);
9736d991
PP
518 if (ret) {
519 BT_LOGW_ERRNO("Failed to walk directory",
520 ": path=\"%s\", recurse=%d",
521 path, recurse);
d24d5663 522 status = BT_FUNC_STATUS_ERROR;
9736d991
PP
523 goto end;
524 }
525
d24d5663 526 if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991
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 */
d24d5663 532 status = BT_FUNC_STATUS_OK;
33b34c43 533 }
8c1a3187 534
9736d991 535end:
060cf347 536 return status;
33b34c43
PP
537}
538
d24d5663
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,
9736d991 541 const struct bt_plugin_set **plugin_set_out)
33b34c43 542{
d24d5663
PP
543 enum bt_plugin_find_all_from_dir_status status =
544 BT_FUNC_STATUS_OK;
33b34c43 545
9736d991 546 BT_ASSERT_PRE_NON_NULL(plugin_set_out, "Plugin set (output)");
3f7d4d90 547 BT_LOGI("Creating all plugins in directory: path=\"%s\", recurse=%d",
8c1a3187 548 path, recurse);
9736d991
PP
549 *plugin_set_out = bt_plugin_set_create();
550 if (!*plugin_set_out) {
8c1a3187 551 BT_LOGE_STR("Cannot create empty plugin set.");
d24d5663 552 status = BT_FUNC_STATUS_MEMORY_ERROR;
a8ff38ef
PP
553 goto error;
554 }
555
9736d991
PP
556 /*
557 * Append found plugins to array (never returns
d24d5663 558 * `BT_FUNC_STATUS_NOT_FOUND`)
9736d991
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) {
9736d991
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",
d24d5663 569 path, bt_common_func_status_string(status));
33b34c43
PP
570 goto error;
571 }
572
d24d5663 573 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991
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);
d24d5663 578 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991
PP
579 goto error;
580 }
581
3f7d4d90 582 BT_LOGI("Created %u plugins from directory: count=%u, path=\"%s\"",
9736d991
PP
583 (*plugin_set_out)->plugins->len,
584 (*plugin_set_out)->plugins->len, path);
33b34c43
PP
585 goto end;
586
587error:
d24d5663 588 BT_ASSERT(status != BT_FUNC_STATUS_OK);
9736d991 589 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
33b34c43
PP
590
591end:
9736d991 592 return status;
33b34c43
PP
593}
594
92fed4e1 595const char *bt_plugin_get_name(const struct bt_plugin *plugin)
33b34c43 596{
d94d92ac
PP
597 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
598 return plugin->info.name_set ? plugin->info.name->str : NULL;
33b34c43
PP
599}
600
92fed4e1 601const char *bt_plugin_get_author(const struct bt_plugin *plugin)
33b34c43 602{
d94d92ac
PP
603 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
604 return plugin->info.author_set ? plugin->info.author->str : NULL;
33b34c43
PP
605}
606
92fed4e1 607const char *bt_plugin_get_license(const struct bt_plugin *plugin)
33b34c43 608{
d94d92ac
PP
609 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
610 return plugin->info.license_set ? plugin->info.license->str : NULL;
33b34c43
PP
611}
612
92fed4e1 613const char *bt_plugin_get_path(const struct bt_plugin *plugin)
33b34c43 614{
d94d92ac
PP
615 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
616 return plugin->info.path_set ? plugin->info.path->str : NULL;
33b34c43
PP
617}
618
92fed4e1 619const char *bt_plugin_get_description(const struct bt_plugin *plugin)
33b34c43 620{
d94d92ac
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
92fed4e1 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{
d94d92ac
PP
630 enum bt_property_availability avail =
631 BT_PROPERTY_AVAILABILITY_AVAILABLE;
b6de043b 632
d94d92ac 633 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
8c1a3187
PP
634
635 if (!plugin->info.version_set) {
3f7d4d90 636 BT_LIB_LOGD("Plugin's version is not set: %!+l", plugin);
d94d92ac 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:
d94d92ac 658 return avail;
b6de043b
PP
659}
660
92fed4e1 661uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin *plugin)
33b34c43 662{
d94d92ac
PP
663 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
664 return (uint64_t) plugin->src_comp_classes->len;
33b34c43
PP
665}
666
92fed4e1 667uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin *plugin)
33b34c43 668{
d94d92ac
PP
669 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
670 return (uint64_t) plugin->flt_comp_classes->len;
671}
33b34c43 672
92fed4e1 673uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin *plugin)
d94d92ac
PP
674{
675 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
676 return (uint64_t) plugin->sink_comp_classes->len;
677}
8c1a3187 678
d94d92ac
PP
679static inline
680struct bt_component_class *borrow_component_class_by_index(
92fed4e1 681 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac
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
0d72b8c3 689const struct bt_component_class_source *
3f79b5cf 690bt_plugin_borrow_source_component_class_by_index_const(
92fed4e1 691 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 692{
0d72b8c3 693 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
694 plugin->src_comp_classes, index);
695}
33b34c43 696
0d72b8c3 697const struct bt_component_class_filter *
92fed4e1
PP
698bt_plugin_borrow_filter_component_class_by_index_const(
699 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 700{
0d72b8c3 701 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac
PP
702 plugin->flt_comp_classes, index);
703}
704
0d72b8c3 705const struct bt_component_class_sink *
92fed4e1
PP
706bt_plugin_borrow_sink_component_class_by_index_const(
707 const struct bt_plugin *plugin, uint64_t index)
d94d92ac 708{
0d72b8c3 709 return (const void *) borrow_component_class_by_index(plugin,
d94d92ac 710 plugin->sink_comp_classes, index);
33b34c43
PP
711}
712
d94d92ac
PP
713static inline
714struct bt_component_class *borrow_component_class_by_name(
92fed4e1 715 const struct bt_plugin *plugin, GPtrArray *comp_classes,
d94d92ac 716 const char *name)
33b34c43
PP
717{
718 struct bt_component_class *comp_class = NULL;
719 size_t i;
720
d94d92ac
PP
721 BT_ASSERT_PRE_NON_NULL(plugin, "Plugin");
722 BT_ASSERT_PRE_NON_NULL(name, "Name");
33b34c43 723
d94d92ac 724 for (i = 0; i < comp_classes->len; i++) {
33b34c43 725 struct bt_component_class *comp_class_candidate =
d94d92ac 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
f6ccaed9 730 BT_ASSERT(comp_class_cand_name);
33b34c43 731
d94d92ac
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
0d72b8c3 741const struct bt_component_class_source *
92fed4e1
PP
742bt_plugin_borrow_source_component_class_by_name_const(
743 const struct bt_plugin *plugin, const char *name)
33b34c43 744{
0d72b8c3 745 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
746 plugin->src_comp_classes, name);
747}
33b34c43 748
0d72b8c3 749const struct bt_component_class_filter *
92fed4e1
PP
750bt_plugin_borrow_filter_component_class_by_name_const(
751 const struct bt_plugin *plugin, const char *name)
d94d92ac 752{
0d72b8c3 753 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac
PP
754 plugin->flt_comp_classes, name);
755}
33b34c43 756
0d72b8c3 757const struct bt_component_class_sink *
92fed4e1
PP
758bt_plugin_borrow_sink_component_class_by_name_const(
759 const struct bt_plugin *plugin, const char *name)
d94d92ac 760{
0d72b8c3 761 return (const void *) borrow_component_class_by_name(plugin,
d94d92ac 762 plugin->sink_comp_classes, name);
33b34c43 763}
c5b9b441
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.094287 seconds and 4 git commands to generate.