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