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