Values API: standardize function names
[babeltrace.git] / lib / plugin / plugin.c
CommitLineData
33b34c43
PP
1/*
2 * plugin.c
3 *
55bb57e0 4 * Babeltrace Plugin (generic)
33b34c43
PP
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
8c1a3187
PP
30#define BT_LOG_TAG "PLUGIN"
31#include <babeltrace/lib-logging-internal.h>
32
6fbd4105 33#include <babeltrace/babeltrace-internal.h>
3d9990ac 34#include <babeltrace/compiler-internal.h>
33b34c43 35#include <babeltrace/ref.h>
1670bffd 36#include <babeltrace/common-internal.h>
33b34c43 37#include <babeltrace/plugin/plugin-internal.h>
55bb57e0 38#include <babeltrace/plugin/plugin-so-internal.h>
8c1a3187
PP
39#include <babeltrace/graph/component-class.h>
40#include <babeltrace/graph/component-class-internal.h>
c55a9f58 41#include <babeltrace/types.h>
f6ccaed9 42#include <babeltrace/assert-internal.h>
33b34c43 43#include <glib.h>
33b34c43
PP
44#include <unistd.h>
45#include <stdlib.h>
9ac68eb1 46#include <stdint.h>
8c1a3187 47#include <inttypes.h>
33b34c43 48#include <sys/stat.h>
e1f4c4f7
MJ
49#include <ftw.h>
50#include <pthread.h>
33b34c43 51
6fbd4105
PP
52#define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
53#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
54#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
33b34c43 55
e1f4c4f7
MJ
56#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
57
6fbd4105
PP
58#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
59#include <babeltrace/plugin/python-plugin-provider-internal.h>
e1f4c4f7 60
33b34c43 61static
a8ff38ef 62struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
6fbd4105 63 bt_plugin_python_create_all_from_file;
95ef44ce
MJ
64
65static
66void init_python_plugin_provider(void) {}
6fbd4105
PP
67#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
68static GModule *python_plugin_provider_module;
69static
a8ff38ef 70struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
33b34c43 71
95ef44ce 72static
6fbd4105 73void init_python_plugin_provider(void) {
95ef44ce
MJ
74 if (bt_plugin_python_create_all_from_file_sym != NULL) {
75 return;
76 }
77
8c1a3187 78 BT_LOGD_STR("Loading Python plugin provider module.");
6fbd4105 79 python_plugin_provider_module =
c92fb666 80 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
6fbd4105 81 if (!python_plugin_provider_module) {
a77aed14
PP
82 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
83 PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
6fbd4105 84 return;
33b34c43
PP
85 }
86
6fbd4105
PP
87 if (!g_module_symbol(python_plugin_provider_module,
88 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
89 (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
8c1a3187
PP
90 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
91 "file=\"%s\", symbol=\"%s\"",
92 PYTHON_PLUGIN_PROVIDER_FILENAME,
93 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
94 return;
33b34c43 95 }
8c1a3187 96
9e0bf9b0 97 BT_LOGI("Loaded Python plugin provider module: addr=%p",
8c1a3187 98 python_plugin_provider_module);
33b34c43
PP
99}
100
6fbd4105
PP
101__attribute__((destructor)) static
102void fini_python_plugin_provider(void) {
103 if (python_plugin_provider_module) {
8c1a3187
PP
104 BT_LOGD("Unloading Python plugin provider module.");
105
106 if (!g_module_close(python_plugin_provider_module)) {
107 BT_LOGE("Failed to close the Python plugin provider module: %s.",
108 g_module_error());
109 }
110
6fbd4105 111 python_plugin_provider_module = NULL;
6ba0b073 112 }
6ba0b073 113}
6fbd4105 114#endif
6ba0b073 115
a8ff38ef 116extern
544d0515 117int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
a8ff38ef 118{
544d0515 119 int64_t count = -1;
a8ff38ef
PP
120
121 if (!plugin_set) {
8c1a3187 122 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
a8ff38ef
PP
123 goto end;
124 }
125
9ac68eb1 126 count = (int64_t) plugin_set->plugins->len;
a8ff38ef
PP
127
128end:
129 return count;
130}
131
132extern
133struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
9ac68eb1 134 uint64_t index)
a8ff38ef
PP
135{
136 struct bt_plugin *plugin = NULL;
137
8c1a3187
PP
138 if (!plugin_set) {
139 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
140 goto end;
141 }
142
143 if (index >= plugin_set->plugins->len) {
144 BT_LOGW("Invalid parameter: index is out of bounds: "
145 "addr=%p, index=%" PRIu64 ", count=%u",
146 plugin_set, index, plugin_set->plugins->len);
a8ff38ef
PP
147 goto end;
148 }
149
150 plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index));
151
152end:
153 return plugin;
154}
155
156struct bt_plugin_set *bt_plugin_create_all_from_static(void)
6ba0b073 157{
8c1a3187 158 /* bt_plugin_so_create_all_from_static() logs errors */
55bb57e0 159 return bt_plugin_so_create_all_from_static();
6ba0b073
PP
160}
161
a8ff38ef 162struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
33b34c43 163{
a8ff38ef 164 struct bt_plugin_set *plugin_set = NULL;
33b34c43
PP
165
166 if (!path) {
8c1a3187 167 BT_LOGW_STR("Invalid parameter: path is NULL.");
6ba0b073 168 goto end;
33b34c43
PP
169 }
170
8c1a3187 171 BT_LOGD("Creating plugins from file: path=\"%s\"", path);
6ba0b073 172
55bb57e0 173 /* Try shared object plugins */
a8ff38ef
PP
174 plugin_set = bt_plugin_so_create_all_from_file(path);
175 if (plugin_set) {
6ba0b073
PP
176 goto end;
177 }
178
6fbd4105 179 /* Try Python plugins if support is available */
95ef44ce 180 init_python_plugin_provider();
6fbd4105 181 if (bt_plugin_python_create_all_from_file_sym) {
a8ff38ef
PP
182 plugin_set = bt_plugin_python_create_all_from_file_sym(path);
183 if (plugin_set) {
6fbd4105
PP
184 goto end;
185 }
6ba0b073
PP
186 }
187
33b34c43 188end:
8c1a3187
PP
189 if (plugin_set) {
190 BT_LOGD("Created %u plugins from file: "
191 "path=\"%s\", count=%u, plugin-set-addr=%p",
192 plugin_set->plugins->len, path,
193 plugin_set->plugins->len, plugin_set);
194 } else {
195 BT_LOGD("Found no plugins in file: path=\"%s\"", path);
196 }
197
a8ff38ef 198 return plugin_set;
33b34c43
PP
199}
200
1670bffd
PP
201static void destroy_gstring(void *data)
202{
203 g_string_free(data, TRUE);
204}
205
2b43acf9 206struct bt_plugin *bt_plugin_find(const char *plugin_name)
1670bffd
PP
207{
208 const char *system_plugin_dir;
209 char *home_plugin_dir = NULL;
210 const char *envvar;
211 struct bt_plugin *plugin = NULL;
a8ff38ef 212 struct bt_plugin_set *plugin_set = NULL;
1670bffd
PP
213 GPtrArray *dirs = NULL;
214 int ret;
a8ff38ef 215 size_t i, j;
1670bffd
PP
216
217 if (!plugin_name) {
8c1a3187 218 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
1670bffd
PP
219 goto end;
220 }
221
8c1a3187
PP
222 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
223 "name=\"%s\"", plugin_name);
1670bffd
PP
224 dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
225 if (!dirs) {
8c1a3187 226 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1670bffd
PP
227 goto end;
228 }
229
230 /*
231 * Search order is:
232 *
233 * 1. BABELTRACE_PLUGIN_PATH environment variable
234 * (colon-separated list of directories)
235 * 2. ~/.local/lib/babeltrace/plugins
236 * 3. Default system directory for Babeltrace plugins, usually
237 * /usr/lib/babeltrace/plugins or
238 * /usr/local/lib/babeltrace/plugins if installed
239 * locally
240 * 4. Built-in plugins (static)
241 *
242 * Directories are searched non-recursively.
243 */
244 envvar = getenv("BABELTRACE_PLUGIN_PATH");
245 if (envvar) {
246 ret = bt_common_append_plugin_path_dirs(envvar, dirs);
247 if (ret) {
8c1a3187 248 BT_LOGE_STR("Failed to append plugin path to array of directories.");
1670bffd
PP
249 goto end;
250 }
251 }
252
253 home_plugin_dir = bt_common_get_home_plugin_path();
254 if (home_plugin_dir) {
255 GString *home_plugin_dir_str =
256 g_string_new(home_plugin_dir);
257
258 if (!home_plugin_dir_str) {
8c1a3187 259 BT_LOGE_STR("Failed to allocate a GString.");
1670bffd
PP
260 goto end;
261 }
262
263 g_ptr_array_add(dirs, home_plugin_dir_str);
264 }
265
266 system_plugin_dir = bt_common_get_system_plugin_path();
267 if (system_plugin_dir) {
268 GString *system_plugin_dir_str =
269 g_string_new(system_plugin_dir);
270
271 if (!system_plugin_dir_str) {
8c1a3187 272 BT_LOGE_STR("Failed to allocate a GString.");
1670bffd
PP
273 goto end;
274 }
275
276 g_ptr_array_add(dirs, system_plugin_dir_str);
277 }
278
279 for (i = 0; i < dirs->len; i++) {
280 GString *dir = g_ptr_array_index(dirs, i);
281
a8ff38ef 282 BT_PUT(plugin_set);
8c1a3187 283
50ad9320
PP
284 /*
285 * Skip this if the directory does not exist because
286 * bt_plugin_create_all_from_dir() would log a warning.
287 */
288 if (!g_file_test(dir->str, G_FILE_TEST_IS_DIR)) {
289 BT_LOGV("Skipping nonexistent directory path: "
290 "path=\"%s\"", dir->str);
291 continue;
292 }
293
8c1a3187 294 /* bt_plugin_create_all_from_dir() logs details/errors */
c55a9f58 295 plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
a8ff38ef 296 if (!plugin_set) {
8c1a3187
PP
297 BT_LOGD("No plugins found in directory: path=\"%s\"",
298 dir->str);
1670bffd
PP
299 continue;
300 }
301
a8ff38ef
PP
302 for (j = 0; j < plugin_set->plugins->len; j++) {
303 struct bt_plugin *candidate_plugin =
304 g_ptr_array_index(plugin_set->plugins, j);
1670bffd 305
a8ff38ef
PP
306 if (strcmp(bt_plugin_get_name(candidate_plugin),
307 plugin_name) == 0) {
8c1a3187
PP
308 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
309 plugin_name, dir->str);
a8ff38ef 310 plugin = bt_get(candidate_plugin);
1670bffd
PP
311 goto end;
312 }
1670bffd 313 }
8c1a3187
PP
314
315 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
316 plugin_name, dir->str);
1670bffd
PP
317 }
318
a8ff38ef
PP
319 bt_put(plugin_set);
320 plugin_set = bt_plugin_create_all_from_static();
8c1a3187
PP
321 if (plugin_set) {
322 for (j = 0; j < plugin_set->plugins->len; j++) {
323 struct bt_plugin *candidate_plugin =
324 g_ptr_array_index(plugin_set->plugins, j);
a8ff38ef 325
8c1a3187
PP
326 if (strcmp(bt_plugin_get_name(candidate_plugin),
327 plugin_name) == 0) {
328 BT_LOGD("Plugin found in built-in plugins: "
329 "name=\"%s\"", plugin_name);
330 plugin = bt_get(candidate_plugin);
331 goto end;
332 }
1670bffd 333 }
1670bffd
PP
334 }
335
336end:
337 free(home_plugin_dir);
a8ff38ef 338 bt_put(plugin_set);
1670bffd
PP
339
340 if (dirs) {
341 g_ptr_array_free(dirs, TRUE);
342 }
343
8c1a3187
PP
344 if (plugin) {
345 BT_LOGD("Found plugin in standard directories and built-in plugins: "
346 "addr=%p, name=\"%s\", path=\"%s\"",
347 plugin, plugin_name, bt_plugin_get_path(plugin));
348 } else {
349 BT_LOGD("No plugin found in standard directories and built-in plugins: "
350 "name=\"%s\"", plugin_name);
351 }
352
1670bffd
PP
353 return plugin;
354}
355
2b43acf9
PP
356struct bt_component_class *bt_plugin_find_component_class(
357 const char *plugin_name, const char *comp_cls_name,
358 enum bt_component_class_type comp_cls_type)
359{
360 struct bt_plugin *plugin = NULL;
361 struct bt_component_class *comp_cls = NULL;
362
8c1a3187
PP
363 if (!plugin_name) {
364 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
2b43acf9
PP
365 goto end;
366 }
367
8c1a3187
PP
368 if (!comp_cls_name) {
369 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
370 goto end;
371 }
372
373 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
374 "plugin-name=\"%s\", comp-class-name=\"%s\"",
375 plugin_name, comp_cls_name);
2b43acf9
PP
376 plugin = bt_plugin_find(plugin_name);
377 if (!plugin) {
8c1a3187 378 BT_LOGD_STR("Plugin not found.");
2b43acf9
PP
379 goto end;
380 }
381
382 comp_cls = bt_plugin_get_component_class_by_name_and_type(
383 plugin, comp_cls_name, comp_cls_type);
8c1a3187
PP
384 if (!comp_cls) {
385 BT_LOGD("Component class not found in plugin: "
386 "plugin-addr=%p, plugin-path=\"%s\"",
387 plugin, bt_plugin_get_path(plugin));
388 }
2b43acf9
PP
389
390end:
391 bt_put(plugin);
392 return comp_cls;
393}
394
e1f4c4f7
MJ
395static struct {
396 pthread_mutex_t lock;
397 struct bt_plugin_set *plugin_set;
398 bt_bool recurse;
399} append_all_from_dir_info = {
400 .lock = PTHREAD_MUTEX_INITIALIZER
401};
402
33b34c43 403static
e1f4c4f7
MJ
404int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag,
405 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
MJ
417 {
418 struct bt_plugin_set *plugins_from_file;
419
e1f4c4f7
MJ
420 if (name[0] == '.') {
421 /* Skip hidden files */
422 BT_LOGV("Skipping hidden file: path=\"%s\"", file);
423 goto end;
424 }
52238017
MJ
425
426 plugins_from_file = bt_plugin_create_all_from_file(file);
e1f4c4f7
MJ
427
428 if (plugins_from_file) {
429 size_t j;
430
431 for (j = 0; j < plugins_from_file->plugins->len; j++) {
432 struct bt_plugin *plugin =
433 g_ptr_array_index(plugins_from_file->plugins, j);
434
435 BT_LOGD("Adding plugin to plugin set: "
436 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
437 file, plugin, bt_plugin_get_name(plugin));
438 bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin);
439 }
33b34c43 440
e1f4c4f7
MJ
441 bt_put(plugins_from_file);
442 }
443 break;
52238017 444 }
e1f4c4f7
MJ
445 case FTW_DNR:
446 /* Continue to next file / directory. */
447 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file);
448 break;
449 case FTW_NS:
450 /* Continue to next file / directory. */
451 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file);
452 break;
33b34c43 453 }
e1f4c4f7
MJ
454
455end:
456 return ret;
33b34c43
PP
457}
458
459static
460enum bt_plugin_status bt_plugin_create_append_all_from_dir(
a8ff38ef 461 struct bt_plugin_set *plugin_set, const char *path,
c55a9f58 462 bt_bool recurse)
33b34c43 463{
e1f4c4f7 464 int nftw_flags = FTW_PHYS;
6ba0b073 465 size_t path_len;
33b34c43
PP
466 enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
467
6ba0b073 468 if (!path) {
8c1a3187 469 BT_LOGW_STR("Invalid parameter: path is NULL.");
6ba0b073
PP
470 ret = BT_PLUGIN_STATUS_ERROR;
471 goto end;
472 }
473
474 path_len = strlen(path);
33b34c43 475 if (path_len >= PATH_MAX) {
8c1a3187
PP
476 BT_LOGW("Invalid parameter: path length is too large: "
477 "path-length=%zu", path_len);
33b34c43
PP
478 ret = BT_PLUGIN_STATUS_ERROR;
479 goto end;
480 }
481
e1f4c4f7 482 pthread_mutex_lock(&append_all_from_dir_info.lock);
33b34c43 483
e1f4c4f7
MJ
484 append_all_from_dir_info.plugin_set = plugin_set;
485 append_all_from_dir_info.recurse = recurse;
486 ret = nftw(path, nftw_append_all_from_dir,
487 APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags);
33b34c43 488
e1f4c4f7 489 pthread_mutex_unlock(&append_all_from_dir_info.lock);
33b34c43 490
e1f4c4f7
MJ
491 if (ret != 0) {
492 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
493 strerror(errno), path, errno);
33b34c43 494 ret = BT_PLUGIN_STATUS_ERROR;
33b34c43 495 }
8c1a3187 496
33b34c43 497end:
33b34c43
PP
498 return ret;
499}
500
a8ff38ef 501struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
c55a9f58 502 bt_bool recurse)
33b34c43 503{
a8ff38ef 504 struct bt_plugin_set *plugin_set;
33b34c43
PP
505 enum bt_plugin_status status;
506
8c1a3187
PP
507 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
508 path, recurse);
a8ff38ef
PP
509 plugin_set = bt_plugin_set_create();
510 if (!plugin_set) {
8c1a3187 511 BT_LOGE_STR("Cannot create empty plugin set.");
a8ff38ef
PP
512 goto error;
513 }
514
33b34c43 515 /* Append found plugins to array */
a8ff38ef 516 status = bt_plugin_create_append_all_from_dir(plugin_set, path,
33b34c43
PP
517 recurse);
518 if (status < 0) {
8c1a3187 519 BT_LOGW("Cannot append plugins found in directory: "
dbd7f7e9
PP
520 "path=\"%s\", status=%s",
521 path, bt_plugin_status_string(status));
33b34c43
PP
522 goto error;
523 }
524
8c1a3187
PP
525 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
526 plugin_set->plugins->len, plugin_set->plugins->len, path);
33b34c43
PP
527 goto end;
528
529error:
a8ff38ef 530 BT_PUT(plugin_set);
33b34c43
PP
531
532end:
a8ff38ef 533 return plugin_set;
33b34c43
PP
534}
535
33b34c43
PP
536const char *bt_plugin_get_name(struct bt_plugin *plugin)
537{
8c1a3187
PP
538 const char *val = NULL;
539
540 if (!plugin) {
541 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
542 goto end;
543 }
544
545 if (plugin->info.name_set) {
546 val = plugin->info.name->str;
547 }
548
549end:
550 return val;
33b34c43
PP
551}
552
553const char *bt_plugin_get_author(struct bt_plugin *plugin)
554{
8c1a3187
PP
555 const char *val = NULL;
556
557 if (!plugin) {
558 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
559 goto end;
560 }
561
562 if (plugin->info.author_set) {
563 val = plugin->info.author->str;
564 }
565
566end:
567 return val;
33b34c43
PP
568}
569
570const char *bt_plugin_get_license(struct bt_plugin *plugin)
571{
8c1a3187
PP
572 const char *val = NULL;
573
574 if (!plugin) {
575 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
576 goto end;
577 }
578
579 if (plugin->info.license_set) {
580 val = plugin->info.license->str;
581 }
582
583end:
584 return val;
33b34c43
PP
585}
586
587const char *bt_plugin_get_path(struct bt_plugin *plugin)
588{
8c1a3187
PP
589 const char *val = NULL;
590
591 if (!plugin) {
592 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
593 goto end;
594 }
595
596 if (plugin->info.path_set) {
597 val = plugin->info.path->str;
598 }
599
600end:
601 return val;
33b34c43
PP
602}
603
604const char *bt_plugin_get_description(struct bt_plugin *plugin)
605{
8c1a3187
PP
606 const char *val = NULL;
607
608 if (!plugin) {
609 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
610 goto end;
611 }
612
613 if (plugin->info.description_set) {
614 val = plugin->info.description->str;
615 }
616
617end:
618 return val;
33b34c43
PP
619}
620
b6de043b
PP
621enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
622 unsigned int *major, unsigned int *minor, unsigned int *patch,
623 const char **extra)
624{
625 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
626
8c1a3187
PP
627 if (!plugin) {
628 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
629 status = BT_PLUGIN_STATUS_ERROR;
630 goto end;
631 }
632
633 if (!plugin->info.version_set) {
634 BT_LOGV("Plugin's version is not set: addr=%p", plugin);
b6de043b
PP
635 status = BT_PLUGIN_STATUS_ERROR;
636 goto end;
637 }
638
639 if (major) {
55bb57e0 640 *major = plugin->info.version.major;
b6de043b
PP
641 }
642
643 if (minor) {
55bb57e0 644 *minor = plugin->info.version.minor;
b6de043b
PP
645 }
646
647 if (patch) {
55bb57e0 648 *patch = plugin->info.version.patch;
b6de043b
PP
649 }
650
651 if (extra) {
55bb57e0 652 *extra = plugin->info.version.extra->str;
b6de043b
PP
653 }
654
655end:
656 return status;
657}
658
544d0515 659int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin)
33b34c43 660{
9ac68eb1 661 return plugin ? plugin->comp_classes->len : (int64_t) -1;
33b34c43
PP
662}
663
9ac68eb1
PP
664struct bt_component_class *bt_plugin_get_component_class_by_index(
665 struct bt_plugin *plugin, uint64_t index)
33b34c43
PP
666{
667 struct bt_component_class *comp_class = NULL;
668
8c1a3187
PP
669 if (!plugin) {
670 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
671 goto error;
672 }
673
674 if (index >= plugin->comp_classes->len) {
675 BT_LOGW("Invalid parameter: index is out of bounds: "
676 "addr=%p, index=%" PRIu64 ", count=%u",
677 plugin, index, plugin->comp_classes->len);
33b34c43
PP
678 goto error;
679 }
680
681 comp_class = g_ptr_array_index(plugin->comp_classes, index);
682 bt_get(comp_class);
683 goto end;
684
685error:
686 BT_PUT(comp_class);
687
688end:
689 return comp_class;
690}
691
692struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
693 struct bt_plugin *plugin, const char *name,
d3e4dcd8 694 enum bt_component_class_type type)
33b34c43
PP
695{
696 struct bt_component_class *comp_class = NULL;
697 size_t i;
698
8c1a3187
PP
699 if (!plugin) {
700 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
701 goto error;
702 }
703
704 if (!name) {
705 BT_LOGW_STR("Invalid parameter: name is NULL.");
33b34c43
PP
706 goto error;
707 }
708
709 for (i = 0; i < plugin->comp_classes->len; i++) {
710 struct bt_component_class *comp_class_candidate =
711 g_ptr_array_index(plugin->comp_classes, i);
712 const char *comp_class_cand_name =
713 bt_component_class_get_name(comp_class_candidate);
d3e4dcd8 714 enum bt_component_class_type comp_class_cand_type =
33b34c43
PP
715 bt_component_class_get_type(comp_class_candidate);
716
f6ccaed9
PP
717 BT_ASSERT(comp_class_cand_name);
718 BT_ASSERT(comp_class_cand_type >= 0);
33b34c43
PP
719
720 if (strcmp(name, comp_class_cand_name) == 0 &&
721 comp_class_cand_type == type) {
722 comp_class = bt_get(comp_class_candidate);
723 break;
724 }
725 }
726
727 goto end;
728
729error:
730 BT_PUT(comp_class);
731
732end:
733 return comp_class;
734}
735
33b34c43
PP
736enum bt_plugin_status bt_plugin_add_component_class(
737 struct bt_plugin *plugin, struct bt_component_class *comp_class)
738{
739 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
740 struct bt_component_class *comp_class_dup = NULL;
33b34c43
PP
741 int comp_class_index = -1;
742
8c1a3187
PP
743 if (!plugin) {
744 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
745 goto error;
746 }
747
748 if (!comp_class) {
749 BT_LOGW_STR("Invalid parameter: component class is NULL.");
750 goto error;
751 }
752
753 if (plugin->frozen) {
754 BT_LOGW("Invalid parameter: plugin is frozen: "
755 "addr=%p, name=\"%s\"", plugin,
756 bt_plugin_get_name(plugin));
33b34c43
PP
757 goto error;
758 }
759
760 /* Check for duplicate */
761 comp_class_dup = bt_plugin_get_component_class_by_name_and_type(plugin,
762 bt_component_class_get_name(comp_class),
763 bt_component_class_get_type(comp_class));
764 if (comp_class_dup) {
8c1a3187
PP
765 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
766 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
767 "comp-class-name=\"%s\", comp-class-type=%s",
768 plugin, bt_plugin_get_name(plugin),
769 bt_plugin_get_path(plugin),
33b34c43 770 bt_component_class_get_name(comp_class),
8c1a3187
PP
771 bt_component_class_type_string(
772 bt_component_class_get_type(comp_class)));
33b34c43
PP
773 goto error;
774 }
775
776 /* Add new component class */
777 comp_class_index = plugin->comp_classes->len;
778 g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
779
55bb57e0
PP
780 /* Special case for a shared object plugin */
781 if (plugin->type == BT_PLUGIN_TYPE_SO) {
3230ee6b 782 bt_plugin_so_on_add_component_class(plugin, comp_class);
33b34c43
PP
783 }
784
8c1a3187
PP
785 BT_LOGD("Added component class to plugin: "
786 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
787 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
788 plugin, bt_plugin_get_name(plugin),
789 bt_plugin_get_path(plugin),
790 comp_class,
791 bt_component_class_get_name(comp_class),
792 bt_component_class_type_string(
793 bt_component_class_get_type(comp_class)));
33b34c43
PP
794 goto end;
795
796error:
33b34c43
PP
797 /* Remove entry from plugin's component classes (if added) */
798 if (comp_class_index >= 0) {
799 g_ptr_array_remove_index(plugin->comp_classes,
800 comp_class_index);
801 }
802
803 status = BT_PLUGIN_STATUS_ERROR;
804
805end:
806 bt_put(comp_class_dup);
807 return status;
808}
This page took 0.076408 seconds and 4 git commands to generate.